v Python
v Python download
v How to write a program code in
Python
Python
The Python Language
¯ The Python Programming Language was
created by Guido van Rossum in the early
1990s
¯ Its name comes from a 1970s British comedy
show called Monty Python’s Flying Circus
¯ Python is popular for education as well
as IT companies
(e.g., YouTube, Google, Yahoo and NASA)
Guido van Rossum
Python
Python Features
¯ Simple Syntax
¯ Python programs are clear and easy to read
¯ Powerful programming features which can provide
additional capabilities
¯ Can perform significant computation with a few
instructions
Python
How to download Python
¯ Go to [Link]
¯ Download and install the program
¯ Open IDLE (Integrated Development and Learning
Environment)
Python
[Link]
Python
download
Python
Run IDLE
Expressions in Python
Variables and Assignment in Python
>>> age = 20
>>> pi = 3.14
>>> name = ‘Charles’
Expressions in Python
Wrong examples
>>> 2 = two
SyntaxError: can't assign to literal
>>> address
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
address
NameError: name 'address' is not defined
>>> name = Zelda
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
name = Zelda
NameError: name 'Zelda' is not defined
>>> health power = 100
SyntaxError: invalid syntax
Expressions in Python
Operators
¯ An operator is a symbol that tells the computer to perform a task
Operator Function Expression Description
+ addition a=b+c a gets the value of (b+c)
- subtraction a=b-c a gets the value of (b-c)
* multiplication a=b*c a gets the value of (b*c)
/ division a=b/c a gets the value of (b/c)
% remainder a=b%c a gets the value of (b%c)
// quotient a = b // c a gets the value of (b//c)
Expressions in Python
Exercise
>>> age = 20
>>> age = age + 1
>>> years = 4
>>> age + years
>>> future_age = age + years
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
Two things you need to know to write a program
¯ print() displays the value within () on the screen
¯ input() takes a value input by the user
Python
Two things you need to know to write a program
¯ print() displays the value within () on the screen
¯ input() takes a value input by the user
Python
Various ways of using print()
print(text)
print(number)
Display multiple values: print(a, b)
Mathematical operation in (): print(a + b)
Python
Using input()
input() takes the input from the user
var = input() takes the value input by the user and store it into the
variable var
input(prompt) displays the prompt the user before getting the input
Python Program File
Open a new file
Python Program File
Give a file name and save it
Run the Program
Run → Run Module
F5
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
Summary
How to use IDLE
How to convert an algorithm to a program
input(), print() for getting input and display
int(), float() for data type conversion
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 multiplications of two values input by
the user
[Run Example]
Input 1: 2
Input 2: 5
Output: 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 takes the number of months as input from
the user and prints how many years and months it represents.
[Run Example]
Input: 30
Output: 2 years 6 months
Input: 13
Output: 1 year 1 month
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