0% found this document useful (0 votes)
0 views19 pages

Python

The document provides a series of exercises for writing Python programs, focusing on user input, output, and data type conversion. Key functions discussed include input(), print(), int(), and float(). It includes examples for various calculations and conversions, such as distance, price adjustments, and BMI.

Uploaded by

b93344622
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)
0 views19 pages

Python

The document provides a series of exercises for writing Python programs, focusing on user input, output, and data type conversion. Key functions discussed include input(), print(), int(), and float(). It includes examples for various calculations and conversions, such as distance, price adjustments, and BMI.

Uploaded by

b93344622
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

v How to write a program code in

Python
Python

Convert the Algorithm to Program

Write an algorithm to print the user’s name

1. Print “Name: ”
2. Get the input and assign the value to the variable userName
3. Print userName
Python

Exercise

Write a program to display ‘Hello, the name input by the user’

[Run Example]
Sonny
Hello Sonny
Python

Example

Write a program to print the double of a value input by the user


Python

Input() stores everything as text

The value input by the user is stored as text


The operators +, * work differently for text

>>> '1'+'1'
'11'
>>> '1'*10
'1111111111'
Python

Converting text to number


To convert text into a number, use int() and float()
int(text) converts a text to an integer
float(text) converts a text to a float

>>> int('1')
1
>>> float('1.1')
1.1
>>> float('1')
1.0
Python

Exercise

Write a program to print the triple of a value input by the user

[Run Example]
Input: 3
Output: 9
Python

Exercise

Write a program to print the addition and multiplication of two


values input by the user

[Run Example]
Input 1: 2
Input 2: 5
Output: 7 10
Python

Exercise

Write a program that prints the distance a car travels when the
speed of a car in km/hour and the hours it travels are given as
input
[Run Example]
Speed in km/hour: 60
Time in hours: 5
Distance: 300
Python

Exercise

Write a program that gets a number in minutes and prints it


converting to hour and minutes

[Run Example]
Number in minutes: 150
2 hours 30 minutes
Python

Your turn

Write a program that gets a number in months and converts to


years and months

[Run Example]
Number in months: 100
8 years 4 months
Python

Exercise

Write a program that gets a price of an item and print the value
after a 30% reduction

[Run Example]
Item price: 50
35
Python

Exercise

Write a program that gets the price of an item and computes the
final price when the tax rate is 8%

[Run Example]
Item price: 50
Final price: 54
Python

Exercise

Write a program that gets the price of an item and the tax rate, and
computes the final price

[Run Example]
Item price: 50
Tax rate: 20
Final price: 60
Python

Exercise

Write a program that converts a percentage to a decimal

[Run Example]
Percentage: 120
Decimal: 1.2
Python

Exercise

Write a Python program to calculate the Body Mass Index (BMI) of an individual.
BMI is a simple calculation using a person's height and weight. The formula for
BMI is: BMI = weight (kg) / (height (m))^2

[Run Example]
Weight: 70 kg
Height: 1.75 m
Your BMI is 22.86
Summary

Python program exercises

input(), print() for getting input and display

int(), float() for data type conversion

You might also like