You are on page 1of 15

Week 5 – Input, Process, Output.

(CLO2)
Learning Outcomes

Use assignment operator and expressions

Display information using print() statement

Read input from the user

Write simple program for real life problems

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

tax = amount * 0.05 tax is 5% of 3

3
amount
Interaction with user
• To read/get a value from the user:
• use the command: input
• Read from the keyboard

• To display a value to the user:


• Use the command: print
• On-screen display

4
2. Display statement
print(expression[,expression2, expression3,…])
The print() function prints the given expression on the console screen.

print("hello world") hello world

print("hello ","world") hello world


print("hello ",2," you") hello 2 you

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

age = int(age) Converts from string to int

6
Input convertion
x = input(“Enter anything:”)

• The data type of x is string

• To convert the x into int:


• x = int(x)

• To Convert the x into float:


• x = float(x)

7
Example 1
Program to read name and age from user and display it.

name = input("Enter a name”) Input


age = input("Enter a age ”)
print("Hello ",name," my age is also ",age) Output

8
Example 2 – with simple calculation.
Program to read name and age from user and calculate and display his/her age on next year.

name = input("Enter a name”)


age = input("Enter a age ”) Input
age = int(age)

ageNextYear = age + 1 Process

print("Hello ",name," you will be ", ageNextYear, "next year" ) Output

9
Example 3
Write a Python program that read price of an item and quantity bought, calculate and display the total.

price = input("Enter the price of item ")


price = float(price) Input
qty = input("Enter the quantity ")
qty = int(qty)
total = price * qty Process

print("Total price ",total," dhs") Output

10

10
Exercise - 1.

Task1: Modify the previous example to read price of


two items, calculate and display the total

Task2: Modify the above example to calculate the VAT


5% and display the total price with VAT.

11

11
Exercise - 2.
Write a python program that calculate and display
area of the given shape.

Note: Formula to calculate area of triangle is

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

You might also like