Introduction to python
programming
[Link] Oo (ITech Education Founder)
Outlines
▪ Print method
▪ Comments
▪ Variable
▪ Data Type
▪ Expression
▪ Arithmetic Operator
[Link] Oo (ITech Education Founder) 2
Print()
▪ to print use print method
print(10)
print(20)
print(“hello”)
[Link] Oo (ITech Education Founder) 3
Comment
▪ The # symbol is used for writing a comment.
▪ The comments are notes that are ignored by the computer
▪ These to help you read the code and understand
▪ You just put hashtag (#) in front of the text you want to comment.
[Link] Oo (ITech Education Founder) 4
Variable
▪ The variable name is a named location used to store data in the
memory.
▪ variable as a box to store toys in and those toys can be replaced at
any time.
▪ num = 10
print(num)
▪ name = “Mg Mg”
print(name)
[Link] Oo (ITech Education Founder) 5
Variable (Cont’d)
▪ a variable name can be almost anything you want, with a few
exceptions.
▪ You can’t name them something that is already a word in
python. (keyword)
▪ Two variables with the same name in the same block of code can
not use
▪ The same name for two variables in Python, but the value will be
based on the latest assignment.
[Link] Oo (ITech Education Founder) 6
Data type
▪ There are various data types in Python.
▪ Numeric – Integer and floating-point numbers. They are defined as int
and float respectively.
number = 12 # this is an example of int
decimalNumber = 12.543243 # this is an example of float
print(type(number))
[Link] Oo (ITech Education Founder) 7
Data type
▪ Numeric Type
int Integer number x=10
float Floating y=3.14
complex complex z = 2+3j
▪ Complex -> scientific engineering quatum physis
a = 3+2j
b = 1+7j
print(a+b)
print(a-b)
[Link] Oo (ITech Education Founder) 8
Data type
▪ Text Type
string name = “mgmg” string of character
▪ Boolean Type
bool logical value True/False flag=True
[Link] Oo (ITech Education Founder) 9
Arithmetic Operators
▪ Arithmetic Operator
+ - * / % // **
num1 = 10
num2 = 20
num3 = num1 + num2
Print(num3)
[Link] Oo (ITech Education Founder) 10
Operators Precedence
▪ High precedence means that operator will be calculated first.
▪ */%
▪ +-
▪ When two operators share the same precedence order.
▪ For instance, plus and minus, calculate the expression with left-to-
right calculation
[Link] Oo (ITech Education Founder) 11
Expression
x = 5 + 2 * 3
y = 100 * 20 - 5 / 100 + 0.05
[Link] Oo (ITech Education Founder) 12
Operator precedence exercise
▪ The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same?
▪ ((((13+5)*2)-4)/2)-13
▪ result = 3 + 2 * 2 ** 2 / 2 - 1
[Link] Oo (ITech Education Founder) 13
Exercises
w = 12 % 4
print(w)
x = 10 % 7
print(x)
y = x % (w + 1)
print(y)
z = z % 2
print(z)
[Link] Oo (ITech Education Founder) 14
Exercises
a = 10
b = 5
c = 2
result = a + b * c ** 2 - (a // b) + c
result = a + b // c * a - c
[Link] Oo (ITech Education Founder) 15
Compound assignment operator
▪ These are “shortcut” operators that combine a
▪ mathematic operator with the assignment operator (=). For
example, this expression below
a= a+b
a+=b
▪ += -= *= /=
16
[Link] Oo (ITech Education Founder)
Compound assignment operator
animalsOnZoo = 0
numberOfMonkey = 4
animalsOnZoo += numberOfMonkey
numberOfElephants = 2
animalsOnZoo += numberOfElephants
numberOfTiger = 8
animalsOnZoo += numberOfTiger
17
[Link] Oo (ITech Education Founder)
Exercises
1. Convert minutes to hours and remaining minutes.
2. Convert years into days (ignore leap years).
18
[Link] Oo (ITech Education Founder)