0% found this document useful (0 votes)
32 views

CLO2 - Part 2

This document discusses input, process, output in Python programs. It explains how to use the print() function to display output, the input() function to get user input as a string, and assignment operators. Examples show how to write programs that get input from the user, perform calculations, and display output. Exercises provide practice writing programs to calculate area, BMI, number of baskets for apples, and more using input, processing, and output.

Uploaded by

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

CLO2 - Part 2

This document discusses input, process, output in Python programs. It explains how to use the print() function to display output, the input() function to get user input as a string, and assignment operators. Examples show how to write programs that get input from the user, perform calculations, and display output. Exercises provide practice writing programs to calculate area, BMI, number of baskets for apples, and more using input, processing, and output.

Uploaded by

Hitesh Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy