You are on page 1of 8

Exercise - Variables

Q1
• Identify the datatype for these values/ results of expressions:

a. 4 __________
b. 3.5 __________
c. “hello” __________
d. True __________
e. 3 + 4.5 __________
Q2: Conversion of Data types
a = 2
b = 1.5
c = ’12’
d = ‘alan’

With reference to the codes above, write the result of the following commands:
If the command lead to error, state “error”
If the result is a string value, enclose in quotes

a. float(a) _________
b. str(a) _________
c. int(d) _________
d. int(b) _________
e. float(c) _________
f. str(int(b)) _________
Q3.1: Variables and Data Type
Write the code to perform the following tasks:
a. Create a variable X and assign it a value of 5.
b. Create a variable y and assign it a value of 1.5
c. Create a variable z and assign it a value of “hello world”
d. Create a variable is_raining and assign it with a value of False.
e. Create another variable is_weekday and assign it with a value of True.
f. Concatenate (Join) two values “Happy” and “Birthday” with a space in
between. Assigned the result to a variable greeting.
g. Convert the integer: 42 to a float.
h. Convert the string: “5” to an integer.
i. Convert the number: 5.6 to a string.
Q3.2: Variable and Data Type
Examine each of the python command given. State the name of the variable that is
being assigned a value, the value assigned and the data type of the variable. The
answer for the first statement is given as example:

Variable / value / datatype


a. weight = 65.5 weight / 65.5 / float______
b. gpa = 3 _gpa/ 3/ int_____________
c. gender = “Female” __gender/”Female”/str____
d. Enabled = False __Enabled/False/bool_____
e. height = 180 + 5.0 __height/185.0/float_______
f. w = float(4) + 3 _______7.0_______float____
g. x = 7/2 _________3.5_____float_____
h. y = int(4.5) + 5.0 _________9.0____float_____
i. z = str(“1”) * 4 ________”1111”_str_________
j. total = 42 + 2 ___________44__int_______
k. number = “42” + “2” ________422___str________
Q4.1 : Performing Arithmetic
Write a program perform simple addition of two numbers
• Create the variables: x and y. Assign x with the value 1 and y with the
value 2.
• Create the variable z and store the value of x + y in it.
Q4.2 : Performing Arithmetic
Write a program that compute the balance in a saving account after a
withdrawal
• Assign variable saving with the value 150 and variable withdrawal
with the value 99.
• Create the variable balance and store the value of saving – withdrawal
in it.
Q4.3 : Performing Arithmetic
Write a program that compute the area of a rectangle of length 10.5cm
and breath 7.5cm:
• Assign variable length with the value of 10.5 and assign variable
breadth with value of 7.5.
• Calculate the area of a rectangle with the variables length and
breadth, and store the result in the variable area.

You might also like