You are on page 1of 12

− Control structures

− Functions
− math import

Algorithmic * Python
1. Control structures
The block if Conditions
if conditions are true Operators: ==, >, <, <=, !=, and >=
Srce:Link
| statements
endif x == y is the equality operator, it returns True if x
_______________________________________________ and y are equal
if conditions are true
| statements x != y is the not-equal operator, it returns True if x
else and y are not equal
| statements # conditions are not true
endif Similar with >, <, <=, and >=
_______________________________________________
_ Combinations:
if conditions 1 are true
| statements and: The expression A and B is true when A and B
else if conditions 2 are true # conditions1 are not true are true at the same time, otherwise A and B is not
| | statements true
| else # conditions1 and condition 2 are not true or: The expression A or B is true when A is true or
| | statements B is true or the two are true
| endif not: The expression not A is true if A is false, and
endif false if A is true
2. Control structures
The block if Conditions
if conditions are true Examples:
Srce:Link
| statements
endif x=0 x>=1 and x<=26 is not true
_______________________________________________ x=27 x>=1 and x<=26 is not true
if conditions are true x=27 x>=1 or x<=26 is true
| statements
else x=2 y=3 x!=y is true
| statements # conditions are not true not (x==2) is false
endif not (x==y) is true
_______________________________________________
_
if conditions 1 are true
| statements
else if conditions 2 are true # conditions1 are not true
| | statements
| else # conditions1 and condition 2 are not true
| | statements
| endif
endif
3. Control structures
The block if Python
if conditions are true mark=int(input("enter the mark: "))
Srce:Link
| statements if (mark >=75) and (mark <= 85) :
endif print(“you have passed”)
_______________________________________________ -----------------------------------------------
if conditions are true a=int(input("Enter a="))
| statements if a < 0 :
else print("negative")
| statements # conditions not are true else :
endif print("positive or equal to zero")
________________________________________________ -------------------------------------------------
if conditions 1 are true
| statements x=int(input(“x=”))
else if conditions 2 are true # conditions1 not are true if (x % 2) == 0 :
| | statements print(x, “is even”)
| else # conditions1 and condition 2 not are true elif (x % 3) == 0 :
| | statements print(x, "is an odd multiple of 3")
| endif else:
endif print(x, "not divisible by 2 or 3")
4.Control structures Srce: Link
The block while Python
counter initialized outside is used to execute a block of statements repeatedly until a given
while conditions are true condition is satisfied
statement(s)
counter should be modified count = 0 count = 0
endwhile while (count < 3) : # {0,1,2} while (count < 3) :
count = count + 1 count = count + 1
In Python print(“loop three times") print(“loop one time")

while conditions : Result: n=3 Result:


statement(s) loop three times count = 0 loop one time
loop three times max=int(input((“enter a number”)))
-----------------------------------------
loop three times while (count < n) :
while conditions :
i=int(input(“enter a number”))
statement(s)
if i > max:
else : max = i
statements count =count+1
------ nested while loop statement ----- else :
while: count =count+1
while : else :
print(“number of integer expired”)
print("the max is “, max)
5. Control structures
The block for Python
Src: Link
for counter in a range : is used to execute a block of statements repeatedly until the upper
statement(s) bound of an interval is reached

In Python count = 0 for i in range (3) :


while (count < 3) : # {0,1,2} count = count + 1
for counter in range(b) : #{0 to b-1} +1 count = count + 1 print(“loop one time")
statement(s) print(“loop three times")
----------------------------------------- n=3
for counter in range(a,b) : #{a to b-1} +1 count = 0 n=3
statements max=int(input((“enter a number”))) max=int(input((“enter a number”)))
-------------------------------------------- while (count < n) : for count in range(n) :
for counter in range(a,b,2): #{a to b-1} +2 i=int(input(“enter a number”)) i=int(input(“enter a number”))
statement(s) if i > max: if i > max:
max = i max = i
count =count+1 else :
------ nested while for statement ----- else : pass
for counter in range(…): count =count+1 else :
for counter in range(…): else : print(“number of integer expired”)
print(“number of integer expired”) print(max)
print(max)
6. Functions
Objectives Examples
• It is a block of statements which can be reused def square(x):
within the core program return x*x

Syntax # call of the function in the core program


a=5
def function-name (argument-list): print(square(a))
print(square(square(a-3)))
return variable_value # output of the function
import math def add(x, y):
def logarithm(x): return (x+y)
return math.log2(x)
def subtract(x, y):
def function(f,y): return (x-y)
return y+f
#call #call
result=function(logarithm(16),5) a=add(5,6)
print(result) b=subtract(a,6)
print(a)
print(b)
7. Modules
Objectives Details
• Groups of functions used to achieve an objective os
• Always group functions require for a specific
sys
actions datetime
random
• Example: math
- Franctions
- addition numpy # Numeric Python
- multiplication …
- substraction …
• Reusability: The functions can be reused in

different programs and by different developers
• Modularity: break large code into smaller and Source:
more manageable pieces https://www.geeksforgeeks.org/python-
• User-defined Modules math-module/
• Built-in Modules
Modules
8. Math module
factorial() Details
# factorial() finds the factorial of a number
# importing math for mathematical operations
import math
a = 10
Result: 3628800
#the factorial of 10
print("The factorial of 10 is : ", end="") N.B: If you forgot to import math or if you
# call of the function just put factorial(10)
print(math.factorial(a))
# or f=math.factorial(a) then print(f) NameError: name 'factorial' is not defined

Tip: Type math. and


you can have the list
of math functions
with the number of
arguments.
9. Math module
factorial() Details
# factorial() finds the factorial of a number
# importing math for mathematical operations
from math import factorial # from math import *
a = 10
Result: 3628800
#the factorial of 10
print("The factorial of 10 is : ", end="")
print(factorial(a))
# or f=factorial(a) then print(f)

Tip: Type math. and


you can have the list
of math functions
10. Math module
gcd(), exp(), pow() Details
# importing math for mathematical operations gcd(): finds the gcd of two numbers
import math exp(): exponential of y: e^y
a = 12 pow(): computes x**y
b=20
print ("The gcd of 12 and 20 is : ", end="") Result: 4
print (math.gcd(b, a)) Result: 81
print (pow(3,4)) Result: 162754.79141900392
print (math.exp(a))
log2(a), log10(a), sin() Details
# importing math for mathematical operations log2(a): value of log a with base 2
import math log10(a): value of log a with base 10
a = math.pi/2 sin(): Sine of value passed as the argument
print (math.sin(a))
Results: 1.0
print (math.log2(16))
Results: 4
print (math.log10(10000)) Results: 4

You might also like