Professional Documents
Culture Documents
Informatique2 - 2 Python
Informatique2 - 2 Python
− 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")