01b.Language Component and Loop
01b.Language Component and Loop
Loop
By Aksadur Rahman
[email protected]
Agenda
Arithmetic Operations
Operator Precedence
Math Functions
Indentation
If Statements
Logical Operators
Comparison/Relational/Conditional Operators
Assignment Operators
Ternary Operators
While Loops
Break & Continue Statement
Sum of n Numbers Program
For Loops
For-While Comparison
For with Range Function
Nested Loops
Arithmetic Operations
Arithmetic Operations
Example
val1 = 3
val2 = 2
print(val1 + val2)
print(val1 - val2)
print(val1 * val2)
print(val1 / val2)
print(val1 // val2)
print(val1 % val2)
print(val1 ** val2)
Operator Precedence
Operators Meaning
() Parentheses
** Exponent
Multiplication, Division,
*, /, //, %
Floor division, Modulus
+, - Addition, Subtraction
print(10+3*2**2+45)
Math Functions/Module
Python math Functions
Python Math functions is one of the most used functions in Python Programming. In python there are
different built-in math functions. Beside there is also a math module in python.
x=2.9
print(round(x))
print(abs(-2.9))
import math
x=2.9
print(math.ceil(x))
print(math.floor(x))
https://www.w3schools.com/python/module_math.asp
Python Indentation
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
if 5 > 2:
print("Five is greater than two!")
If Statements
if statement
if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of
statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
if If……else If……elif……else
If Statements
Example
if If……else If……elif……else
i = 10 i = 20 i = 20
if i < 15: if i == 10:
if i > 15: print("i is smaller than 15") print("i is 10")
print("10 is less than 15") print("i'm in if Block") elif i == 15:
print("I am Not in if") else: print("i is 15")
print("i is greater than 15") elif i == 20:
print("i'm in else Block") print("i is 20")
print("i'm not in if and not in else Block") else:
print("i is not present")
Match Case
Example
num = 3
match num:
# pattern 1
case 1:
print("One")
# pattern 2
case 2:
print("Two")
# pattern 3
case 3:
print("Three")
# default pattern
case _:
print("Number not between 1 and 3")
Comparison/Relational/Conditional Operators
Comparison/Relational/Conditional Operators
Example:
a=9
b=5
print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
Logical Operators
and
a = 10
b = 10
c = -10
or not
a = 10 a = 10
b = -10
c=0 if not (a%3 == 0 or a%5 == 0):
print("10 is not divisible by either 3 or 5")
if a > 0 or b > 0: else:
print("Either of the number is greater than 0") print("10 is divisible by either 3 or 5")
else:
print("No number is greater than 0")
if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
Assignment Operators
Numeric Data Types
Python Number Types: int, float, complex
Complex
A complex number is a number with real and imaginary components. For example, 5 + 6j is a complex number
where 5 is the real component and 6 multiplied by j is an imaginary component.
Complex data types is used while developing scientific applications where complex mathematical operation is
required.
a=6+7j
print(a)
print(a.real)
print(a.imag)
print(type(a))
b=5+5j
print(a+b)
While
Loops
A while loop statement in Python programming language repeatedly executes a target statement as
long as a given condition is true.
count = 0
while count < 9:
print('The count is:', count)
count = count + 1
print("Good bye!")
Sum of n Numbers Program
while i <= n:
sum = sum + i
i = i +1
print(sum)
Break & Continue Statement
Break Continue
i=1 i=0
while i < 6: while i < 6:
print(i) i += 1
if i == 3: if i == 3:
break continue
i += 1 print(i)
sum = 0
while True:
num = input("Enter a Number: ")
if num == "quit":
break
try:
num = int(num)
except:
print("Enter a valid number please.")
continue
sum = sum + num
print(sum)
For Loops
For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
For Loops
#Using the range() function: #Using the start parameter: #Increment the sequence with 3 (default is 1):
for i, x in enumerate(num):
print(i, x)
Nested Loops
for x in adj:
for y in fruits:
print(x, y)