Professional Documents
Culture Documents
(CLO2)
Learning Outcomes
2
1. Assignment
variable = expression
The = operator assign the expression on the right side to the variable in left side.
a=5 a is 5
x = y = 10 x and y is 10
a, b = 10,5 a is 10, b is 5
s = 10 ** 3 s is 1000
3
amount
Interaction with user
• To read/get a value from the user:
• use the command: input
• Read from the keyboard
4
2. Display statement
print(expression[,expression2, expression3,…])
The print() function prints the given expression on the console screen.
print(5 + 2) 7
5
5
print("5 + 20 = ",5 + 20) 5 + 20 = 25
3. Read user input
variable = input(string)
• Any entry is considered as a string
• Must Convert the entry to the desired data type
Example: ask the user for his name and his age
name = input(“Enter your name:”)
age = input(“Enter your age:”) Returns string
6
Input convertion
x = input(“Enter anything:”)
7
Example 1
Program to read name and age from user and display it.
8
Example 2 – with simple calculation.
Program to read name and age from user and calculate and display his/her age on next year.
9
Example 3
Write a Python program that read price of an item and quantity bought, calculate and display the total.
10
10
Exercise - 1.
11
11
Exercise - 2.
Write a python program that calculate and display
area of the given shape.
height
base
Note: Formula to calculate area of rectangle is
12
12 width
Exercise - 3.
Write a python program that read weight (kg) and
height (m) of a person, calculate and display his BMI
[Body mass index].
Sample output
13
13
13
Exercise - 4.
A basket can hold 5 apples. Write a python program that read total
number of apples, calculate and display the number baskets need and
number of apples that will be without a basket.
(Note: you need to use integer division and modulus operators)
Sample output
14
14
Summary
print() function used to display in standard output [screen]
Input() function used to read input from keyboard and return
as string
= used to assign value / expression on right to a variable in
left.
Program using input → process → output.
15
15