Exercises 1
Exercises 1
Review Exercises 1
1. Determine which of the following is a valid identifier. If it is not valid, tell why:
a. r2d2 vaild
b. H20 valid
c. secondCousirOnceRemoved valid
d 2ndBirthday (invalid)invalid because it starts with digit
e. the-United-States-of-America invalid because of using special character
f. -TIME-
g. -12345
h. x(3)
i. cost-in-$
2. What data types would you use to represent the following items?
(a) the number of students in a classintegernum_student=66
(b) the grade (a letter) attained by a student in the classstringgrade= “A”
(c) the average mark in a classfloatavg_mark=45.5
(d) the distance between two pointsfloatdistance=4.5
(e) the population of a cityintegerpopulation=1000
(f) the weight of a postage stampfloatweight=0.01
(g) the registration letter of a carstringreg_letter= “AX00321”
(h) the password of a devicestring password= “diamond7”
3. Write the Python command to make a variable called city with the value "Paris".
city= “Paris”
4 Write the Python command to output the variable age. print(age)
5 Write the Python command to ask the user "How old are you?", get user input, and store the
user input as the variable age. age=int(input(“How old are you?”))
6 Write the Python command to assign the value 9. 99 to a variable called price. price=9.99
7 What is the data type of the price variable? float
8 The variable points store data that the user has input. What is the data type of this variable?
points=input(“Enter something”)string
9 Write a command to convert the points variable to any numerical data type (integer or float).
points=float(input(“Enter something”))float
10. Write a Python program which
► asks the user to input three numbers
► multiplies the three numbers together
► outputs the result.
solution
num1=int(input("Enter first number"))
num2=int(input("Enter second number"))
num3=int(input("Enter third number"))
result=num1*num2*num3
print("The multiplication of the three numbers are:", result)
11. Making a simple calculator
num1=int(input("Enter first number"))
add=num1+num2
sub=num1-num2
multi=num1*num2
div=num1/num2