0% found this document useful (0 votes)
9 views2 pages

Exercises 1

The document provides exercises on Python programming, focusing on valid identifiers, data types, and basic commands. It includes tasks such as determining valid variable names, declaring variables for different data types, and writing simple Python programs for user input and calculations. Additionally, it covers creating a simple calculator that performs basic arithmetic operations.

Uploaded by

xynxgdpjhm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Exercises 1

The document provides exercises on Python programming, focusing on valid identifiers, data types, and basic commands. It includes tasks such as determining valid variable names, declaring variables for different data types, and writing simple Python programs for user input and calculations. Additionally, it covers creating a simple calculator that performs basic arithmetic operations.

Uploaded by

xynxgdpjhm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to Python

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 classintegernum_student=66
(b) the grade (a letter) attained by a student in the classstringgrade= “A”
(c) the average mark in a classfloatavg_mark=45.5
(d) the distance between two pointsfloatdistance=4.5
(e) the population of a cityintegerpopulation=1000
(f) the weight of a postage stampfloatweight=0.01
(g) the registration letter of a carstringreg_letter= “AX00321”
(h) the password of a devicestring password= “diamond7”

Write suitable declarations for variables in question 2. Be sure to choose meaningful


identifiers.

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"))

num2=int(input("Enter second number"))

add=num1+num2

sub=num1-num2

multi=num1*num2

div=num1/num2

print(“Your first number is” , num1)

print(“Your second number is” , num2)

print(num1,"+", num2, "=", add)

print(num1,"-", num2, "=", sub)

print(num1,"*", num2, "=", multi)

print(num1,"/", num2, "=", div)

#print("the sum of two numbers", add)

You might also like