SlideShare a Scribd company logo
1
Introduction to Programming
with Python
Sudharshan Welihinda(weli@iit.ac.lk)
Senior Lecturer, Department of Computing
Informatics Institute of Technology
Lecture 04
Exercise
2
Problem 1 : Write an algorithm to read two numbers and find their sum.
Inputs : First number num1, Second number num2.
Process : Sum = num 1 + num2
Output: Sum (sum of the two numbers.)
Algorithm:
Step1: Start
Step2: Readinput the first num1.
Step3: Readinput the second num2.
Step4: Sum = num1+num2
Step5: Write/Output Sum
Step6: End
Python program
input(‘Enter first number’)
Num2 = int(input(‘Enter second number’))
Sum = Num1 + Num2
print(Sum)
Num1 = int( )
Exercise
3
Problem 2 : Calculate area of a rectangle.
Inputs : Width & Height
Process : Area = Width * Height
Output: Area.
Problem 3 : Find the average of three numbers input by user.
Inputs : Number1 , Number2, Number3
Process : Average = (Number1 + Number2 + Nmber3) / 3
Output: Average
Exercise
4
Problem 2.1 : Write a Python program to enter length and breadth of a rectangle and
find its perimeter.
Inputs : Length & Breadth
Process : Perimeter = (Length + Breadth) * 2
Output: Perimeter.
Problem 2.2 : Write a Python program to enter two angles of a triangle and find the
third angle.
Inputs : Angle1 & Angle2
Process : ThirdAngle = 180 – (Angle1 + Angle2)
Output: ThirdAngle
Exercise
5
Problem 4 : Write a python program to convert temperature Fahrenheit to Celsius
Inputs : Temperature in Fahrenheit
Process : C = 5/9 * (F-32)
Output: Temperature in Celsius .
Problem 5 : Write a python program to Determine the total cost of apples, given the
number of kilos of apples purchased and the cost of apples per kilo.
Inputs : Number of kilos of apples
Cost of apples per kilo
Process : Total cost = Number of kilos of apples * Cost of apples per kilo
Output: Total cost of apples
Exercise
6
Problem 6 :
Write a python program to calculate Body Mass Index(BMI). Body mass index calculator
is based on the standard BMI measurement that takes your weight and height and
basically tells you if you have an unhealthy body weight or not.
The official formula, which is used in this BMI calculator, is
BMI = (weight (Kg) / (height (m) x height (m))
Inputs : Weight, Height
Process : BMI = Weight / (Height x Height)
Output: BMI (Body Mass Index)
Exercise
7
Problem 6.1 :
Write a python program to enter marks of three subjects and calculate total, average and
percentage.
Answer
8
Problem 6 – Answer(without formatting):
Weight = 0.0
Height = 0.0
BMI = 0.0
Weight = float(input('Enter your body weight... '))
Height = float(input('Enter your Height... '))
BMI = (Weight/(Height * Height))
print('Your BMI index is = ‘, BMI)
Problem 6 – Answer(with print formatting):
Weight = 0.0
Height = 0.0
BMI = 0.0
Weight = float(input('Enter your body weight... '))
Height = float(input('Enter your Height... '))
BMI = (Weight/(Height * Height))
print('Your BMI index is = ',format(BMI,'.2f'))
9
Problem 7 :
Write a Python program to read sum of money, Increase it by 8% and print the original
and the increased sum.
Exercise
Problem 8 :
Write a Python program which accepts the radius of a circle from the user and
compute the area.
Assume π = 3.14
Problem 9 :
Write a Python program which accepts the user's first and last name and print them in
reverse order with a space between them
10
Problem 7 :
OriginalSum = 0.0
IncreasedSum = 0.0
Incremental = 0.0
OriginalSum = float(input('Enter original sum... '))
Incremental = (OriginalSum* 0.08)
IncreasedSum = OriginalSum + Incremental
print('Original Sum = ',format(OriginalSum,'.2f'))
print('Increased Sum = ',format(IncreasedSum,'.2f'))
Answer
11
Answer
Problem 8 :
Radius = 0.0
Area = 0.0
Radius = float(input('Enter radius of a circle... '))
Area = Radius * Radius * 3.14
print('Are is = ',format(Area,'.2f'))
12
Answer
Problem 9 :
FirstName = 0.0
LastName = 0.0
FirstName = input('Enter first name : ')
LastName = input('Enter last name : ')
print('Full Name is - ', FirstName, ‘ ', LastName)
# print('Full Name is - ', FirstName, end=‘’)
# print(' ' , LastName)
13
Problem 10 :
Programme is to be written to input the basic salary of an employee and to calculate
and display the net salary. Net salary is to be calculated by deducting 8.5% of EPF
contribution & 1% of society contribution from the basic salary.
a) Define the memory variables needed to hold the data items in this program.
b) Write the statement that will assign the input basic salary to respective memory
variable.
c) Write the statement that will calculate the net salary.
d) Write the statement that will display the net salary.
Exercise
14
Problem 11 :
Write a Python Program to convert days into years, weeks & days.
Exercise

More Related Content

PDF
python lab programs.pdf
PDF
xii cs practicals class 12 computer science.pdf
DOCX
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
PPTX
Python programming workshop session 3
PDF
Xi CBSE Computer Science lab programs
PDF
(2 - 1) CSE1021 - Module 2 Worksheet.pdf
DOCX
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
PDF
analysis of data structure design programs
python lab programs.pdf
xii cs practicals class 12 computer science.pdf
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
Python programming workshop session 3
Xi CBSE Computer Science lab programs
(2 - 1) CSE1021 - Module 2 Worksheet.pdf
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
analysis of data structure design programs

Similar to DOC333-2023-Sep-pythonprogramming-Lec04.pdf (20)

PPTX
Python.pptx
PPT
4 design
PPTX
Revision-of-thehki-basics-of-python.pptx
PDF
The Final Programming Project
PPTX
05A_Java_in yemen adenProgramming_IT.pptx
PPTX
lecture 2.pptx
DOCX
Basic python laboratoty_ PSPP Manual .docx
PDF
python practicals-solution-2019-20-class-xii.pdf
PDF
python.pdf
PDF
c++ exp 1 Suraj...pdf
PPTX
02. Data Types and variables
PDF
C++ Course - Lesson 2
DOCX
C programming Lab 1
PDF
xii cs practicals
PPT
Pseudocode algorithim flowchart
PDF
Python Manuel-R2021.pdf
DOCX
PROGRAMMING QUESTIONS.docx
DOCX
OOP program questions with answers
PDF
Python Training Course in Chandigarh(Mohali)
PDF
Python Training in Chandigarh(Mohali)
Python.pptx
4 design
Revision-of-thehki-basics-of-python.pptx
The Final Programming Project
05A_Java_in yemen adenProgramming_IT.pptx
lecture 2.pptx
Basic python laboratoty_ PSPP Manual .docx
python practicals-solution-2019-20-class-xii.pdf
python.pdf
c++ exp 1 Suraj...pdf
02. Data Types and variables
C++ Course - Lesson 2
C programming Lab 1
xii cs practicals
Pseudocode algorithim flowchart
Python Manuel-R2021.pdf
PROGRAMMING QUESTIONS.docx
OOP program questions with answers
Python Training Course in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
Ad

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Geodesy 1.pptx...............................................
PDF
composite construction of structures.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Welding lecture in detail for understanding
PPT
Mechanical Engineering MATERIALS Selection
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
web development for engineering and engineering
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
DOCX
573137875-Attendance-Management-System-original
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
CH1 Production IntroductoryConcepts.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Geodesy 1.pptx...............................................
composite construction of structures.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Welding lecture in detail for understanding
Mechanical Engineering MATERIALS Selection
Model Code of Practice - Construction Work - 21102022 .pdf
CYBER-CRIMES AND SECURITY A guide to understanding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
web development for engineering and engineering
Internet of Things (IOT) - A guide to understanding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
573137875-Attendance-Management-System-original
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Ad

DOC333-2023-Sep-pythonprogramming-Lec04.pdf

  • 1. 1 Introduction to Programming with Python Sudharshan Welihinda(weli@iit.ac.lk) Senior Lecturer, Department of Computing Informatics Institute of Technology Lecture 04
  • 2. Exercise 2 Problem 1 : Write an algorithm to read two numbers and find their sum. Inputs : First number num1, Second number num2. Process : Sum = num 1 + num2 Output: Sum (sum of the two numbers.) Algorithm: Step1: Start Step2: Readinput the first num1. Step3: Readinput the second num2. Step4: Sum = num1+num2 Step5: Write/Output Sum Step6: End Python program input(‘Enter first number’) Num2 = int(input(‘Enter second number’)) Sum = Num1 + Num2 print(Sum) Num1 = int( )
  • 3. Exercise 3 Problem 2 : Calculate area of a rectangle. Inputs : Width & Height Process : Area = Width * Height Output: Area. Problem 3 : Find the average of three numbers input by user. Inputs : Number1 , Number2, Number3 Process : Average = (Number1 + Number2 + Nmber3) / 3 Output: Average
  • 4. Exercise 4 Problem 2.1 : Write a Python program to enter length and breadth of a rectangle and find its perimeter. Inputs : Length & Breadth Process : Perimeter = (Length + Breadth) * 2 Output: Perimeter. Problem 2.2 : Write a Python program to enter two angles of a triangle and find the third angle. Inputs : Angle1 & Angle2 Process : ThirdAngle = 180 – (Angle1 + Angle2) Output: ThirdAngle
  • 5. Exercise 5 Problem 4 : Write a python program to convert temperature Fahrenheit to Celsius Inputs : Temperature in Fahrenheit Process : C = 5/9 * (F-32) Output: Temperature in Celsius . Problem 5 : Write a python program to Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo. Inputs : Number of kilos of apples Cost of apples per kilo Process : Total cost = Number of kilos of apples * Cost of apples per kilo Output: Total cost of apples
  • 6. Exercise 6 Problem 6 : Write a python program to calculate Body Mass Index(BMI). Body mass index calculator is based on the standard BMI measurement that takes your weight and height and basically tells you if you have an unhealthy body weight or not. The official formula, which is used in this BMI calculator, is BMI = (weight (Kg) / (height (m) x height (m)) Inputs : Weight, Height Process : BMI = Weight / (Height x Height) Output: BMI (Body Mass Index)
  • 7. Exercise 7 Problem 6.1 : Write a python program to enter marks of three subjects and calculate total, average and percentage.
  • 8. Answer 8 Problem 6 – Answer(without formatting): Weight = 0.0 Height = 0.0 BMI = 0.0 Weight = float(input('Enter your body weight... ')) Height = float(input('Enter your Height... ')) BMI = (Weight/(Height * Height)) print('Your BMI index is = ‘, BMI) Problem 6 – Answer(with print formatting): Weight = 0.0 Height = 0.0 BMI = 0.0 Weight = float(input('Enter your body weight... ')) Height = float(input('Enter your Height... ')) BMI = (Weight/(Height * Height)) print('Your BMI index is = ',format(BMI,'.2f'))
  • 9. 9 Problem 7 : Write a Python program to read sum of money, Increase it by 8% and print the original and the increased sum. Exercise Problem 8 : Write a Python program which accepts the radius of a circle from the user and compute the area. Assume π = 3.14 Problem 9 : Write a Python program which accepts the user's first and last name and print them in reverse order with a space between them
  • 10. 10 Problem 7 : OriginalSum = 0.0 IncreasedSum = 0.0 Incremental = 0.0 OriginalSum = float(input('Enter original sum... ')) Incremental = (OriginalSum* 0.08) IncreasedSum = OriginalSum + Incremental print('Original Sum = ',format(OriginalSum,'.2f')) print('Increased Sum = ',format(IncreasedSum,'.2f')) Answer
  • 11. 11 Answer Problem 8 : Radius = 0.0 Area = 0.0 Radius = float(input('Enter radius of a circle... ')) Area = Radius * Radius * 3.14 print('Are is = ',format(Area,'.2f'))
  • 12. 12 Answer Problem 9 : FirstName = 0.0 LastName = 0.0 FirstName = input('Enter first name : ') LastName = input('Enter last name : ') print('Full Name is - ', FirstName, ‘ ', LastName) # print('Full Name is - ', FirstName, end=‘’) # print(' ' , LastName)
  • 13. 13 Problem 10 : Programme is to be written to input the basic salary of an employee and to calculate and display the net salary. Net salary is to be calculated by deducting 8.5% of EPF contribution & 1% of society contribution from the basic salary. a) Define the memory variables needed to hold the data items in this program. b) Write the statement that will assign the input basic salary to respective memory variable. c) Write the statement that will calculate the net salary. d) Write the statement that will display the net salary. Exercise
  • 14. 14 Problem 11 : Write a Python Program to convert days into years, weeks & days. Exercise