Python
Chapter 7 - Python
Students are required to download Python and install it
in their laptop. Please visit this website:
[Link]
Intro to Python
What is Python?
Python is a high-level programming language which is open
source and object oriented.
Object-oriented programming is based
on the concept of objects.
Intro to Python
What is Python?
Python was developed by Guido Van Rossum in 1991.
Python can be used to:
Creates web based applications
Handles large amounts of data.
Performs complex calculations
Connects to database, read and modify files.
Some interesting facts about
Python
Writing and executing program in Python
Python has 2 programs, Command line window or Python IDLE
(Integrated Development Learning Environment). It allows you to
edit, run, browse and debug a Python program from the interface.
You can directly type the command in the IDLE, or you can use
print() function.
Variables in Python
Variables are named memory locations whose value may
change during the execution of the program.
Can start with an alphabet or
Can consist of alphabets, underscore but not with a
digits and underscore only. digit/number.
Keywords in Python cannot
be used as variable name, ex:
Cannot have space in it. print, and, not, etc.
Python is a case sensitive
Declaring Variables
Every variable has a type and this type defines the format and
behaviour of the variable.
To declare variable in Python we can just write,
a=3
It will automatically assign the value of 3 in variable a.
Unlike JavaScript you don’t have to give var and no semicolon symbol
needed.
More Values More Variables
You can assign multiple values to multiple variables using a
single Python statement.
Example:
You want to assign 3 variables lets say x, y and z
You can write:
x, y, z = 15, 14.5, “Hi there”
Space between variables are not really needed.
Variables
Multiple variables can be assigned same values with a single
Python statement.
Example:
You want to assign values of 5 to 3 different variables of x, y
and z.
You can write:
x=y=z=5
Space between variables are not really needed.
Using input() Function
Similar like JavaScript and other programming language, variable in
Python can be stored either declared within the program or can be
accepted from the user.
Use input() as example to accept value from the user.
a=input(“Enter your name “)
print(a)
Python will converts any user input into string.
To treat the users input as a numeric value we need to convert it either
using int or float.
Example:
b=int(input(“Enter your age”)
c=b+1
Arithmetic Operators
These operators used as basic operator to perform common arithmetic
calculations:
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation
// Floor division
Challenge!
Create a program to calculate the area of a trapezoid with input function and arithmetic operators!
Arithmetic Operators
Write a python program that takes in a student name, class, and section. It should also take in five
subject marks of the students and find the average (Percentage). Display a result in such a way that
their name, class, section, and average (percentage) are printed.
Assignment Operators
Assignment Operators are used to assign values to variables
on the left.
Example:
a=5
b=10
a+=b
print(a)
Output: 15
a+=b is similar like a=a+b, which will do the addition of value
from variable a and the value of variable b. The result will then
be assigned to variable a.
Assignment Operators
These are the rest of the Assignment Operators
Operator Meaning
+= It adds the right operand to the left operand and assigns the result to the left operand.
It subtracts the right operand from the left operand and assigns the result to the left
-= operand.
It multiplies the right operand with the left operand and assigns the result to the left
*= operand.
It divides the left operand with the right operand and assigns the result to the left
/= operand.
%= It takes modulus using two operands and assigns the result to the left operand.
**= Performs exponential (power) calculation and assigns value to the left operand.
//= It performs floor division on the operand and assigns value to the left operand.
Guess the output!
1. num = 10 3. b=4
num += 50 60 b **= 2 16
print(num) print(b)
2. a = 10 4. c=9
a %= 3 1 c //= 5 1
print(a) print(c)
Conditional Structures
A program usually follow flow of control, which means the
statements written first are executed first in the sequence.
We can change how the flow of control based with conditional
statements. In order words, using if.
Conditional Structures
There are three types of conditional statements in Python:
• if statement
• if-else
• if-elif else
Some notes need to be followed when using if statement in the
program.
If statement must end with A true condition of statement needs
a colon (:) symbol to be indented.
No limit of number of
You can give the condition inside ()
statement can appear
(brackets) but this is not mandatory.
under an if.
If statement
This statement checks if the condition given after is true, then the
statements following the if condition will be executed.
If the condition is false then next statement will be executed.
Syntax: If <condition>:
statement(s)
Example: Create a program where the user have to insert their age.
If the age above or equal to 18 then they are allowed to join the driving
test.
age=int(print(“Enter your age”))
if age>=18:
print(“You are allowed”)
If–else statement
The condition before only executed true condition. To give false or other
condition beside true then we can use else statement.
Syntax: If <condition>:
statement(s)
else:
statement(s)
Example: Create a program where the user have to insert their age.
If the age above or equal to 18 then they are allowed to join the driving
test below than that they are not allowed.
age=int(print(“Enter your age”))
if age>=18:
print(“You are allowed”)
else:
print(“You are not allowed”)
if-elif-else
There are some case where multiple condition needed. We can use if, elif
and else statement.
Syntax: If <condition>:
statement(s)
elif <condition>:
statement(s)
elif <condition>:
statement(s)
else:
statement(s)
Write a python program to accept any city from the user and display landmark of that
city.
Example:
City Landmark
Jakarta Monas
Create at least 10 different cities and their landmarks. (Google the information if you
need).