Variables Code#
Please solve the following questions using Python code.
Question-1#
Create a variable named course
and assign the value math
to it.
Solution
course = 'math'
Question-2#
Create a variable named weight
and assign the value 180.4
to it.
Solution
weight = 180.4
Question-3#
Create a variable named number_of_apples
and assign the value 45
to it.
Solution
number_of_apples = 45
Question-4#
Write the code to convert the integer 1
to the string '1'
Solution
str(1)
Question-5#
Create a variable named
x
and assign the value 7 to it.Create a variable named
y
and assign the value 3 to it.Display the sum, difference, and product of 7 and 3 using the variables
x
andy
.
Solution
x = 7
y = 3
print('Sum :', x+y)
print('Difference :', x-y)
print('Product :', x*y)
Question-6#
Create a variable named
x
and assign the value 7 to it.Create a variable named
y
and assign the value 3 to it.Create a variable named
z
, assign the value ofx * y
to it.Display
z
.
Solution
x = 7
y = 3
z = x*y
print('Product :', z)
Question-7#
Create a variable named
width
and assign the value10
to it.Create a variable named
length
and assign the value50
to it.These variables represent the sides of a rectangle.
Create a variable named
perimeter
and assign the value of the rectangle’s perimeter to it.Create a variable named
area
and assign the value of the rectangle’s area to it.Display the following statements by using the variables
width
,length
,perimeter
andarea
.The width of the garden is 10 ft.
The length of the garden is 50 ft.
The area of the rectangle is 500 square ft, indicating a large size.
The perimeter of the rectangle measures 120 ft, indicating that a considerable amount of fencing is required.
Please avoid using the numbers 10 and 50 in your code, and remember to include appropriate punctuation.
Solution
width = 10
length = 50
perimeter = 2*(width+length)
area = width*length
print('The width of the garden is '+str(width)+' ft.')
print('The length of the garden is '+str(length)+' ft.')
print('The area of the rectangle is '+str(area)+' square ft, indicating a large size.')
print('The perimeter of the rectangle measures '+str(perimeter)+' ft, indicating that a considerable amount of fencing is required.')
Question-8#
Create a variable named
x
and assign the value3
to it. (integer type)Without directly using the number
3
in your code (use variablex
) print3333
using three different codeAssign 5 to x and verify if you obtain
5555
with the same code.
Solution
x = 3
print(str(x)*4)
print(str(x)+str(x)+str(x)+str(x))
print(x*1111)
print('-'*4)
x = 5
print(str(x)*4)
print(str(x)+str(x)+str(x)+str(x))
print(x*1111)