Вы находитесь на странице: 1из 24

Control Structures in Python

Technical Training
Titus
logical conditions from mathematics
• Equals: a == b
• Not Equals: a != b
• Less than: a<b
• Less than or equal to: a <= b
• Greater than: a>b
• Greater than or equal to: a >= b
if statements
• if statement
• if..else statements
• nested if statements
• if-elif ladder
if statement - variants
An if statement consists of a boolean expression followed by one
or more statements.
if...else statements
An if statement can be followed by an optional else statement,
which executes when the boolean expression is FALSE.
nested if statements

You can use one if or else if statement inside another if or


else if statement(s).
if statement
a = 33
b = 200
if b > a:
print("b is greater than a")
print(“bye for now”)
indentation
• Python relies on indentation, using whitespace, to define
scope in the code.
• Other programming languages often use curly-brackets for
this purpose.
• If statement, without indentation (will raise an error):
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
elif
"if the previous conditions were not true, then try this
condition"
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else
• The else keyword catches anything which isn't caught by the
preceding conditions.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
example
• In this example a is greater to b, so the first condition is not true, also
the elif condition is not true, so we go to the else condition and print to
screen that "a is greater than b".
• You can also have an else without the elif:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
one line If
• If there is only one statement to execute, it can be
put it on the same line as the if statement

if a > b: print("a is greater than b")


one line if ... else
• If there is only one statement to execute, one for if,
and one for else, it can be put it all on the same line
print("A") if a > b else print("B")
multiple else statements on the same line

One line if else statement, with 3 conditions:

print("A") if a > b else print("=") if a == b else print("B")


and – logical operator
used to combine conditional statements
• Test if a is greater than b, AND if c is greater than a:
• if a > b and c > a:
print("Both conditions are True")
Or - logical operator
• used to combine conditional statements:
• Test if a is greater than b, OR if a is greater than c:
• if a > b or a > c:
print("At least one of the conditions are True")
is a bigger than 10
a=4
if(a>10):
print('a is bigger than 10')
is a bigger than 10 – variation 1
a=4
if(a>10):
print('a is bigger than 10')
else:
print('a is not bigger than 10')
is a bigger than 10 – variation 1
a=10
if(a>10):
print('a is bigger than 10')
elif(a<10):
print('a is less than 10')
else:
print('a is equal to 10')
positive or negative
number=10
if number < 0:
print("The entered number is negative.")
elif number > 0:
print("The entered number is positive.")
elif number == 0:
print("Number is zero.")
biggest of two numbers
a=10;b=20
if a>b:
print('a is big')
elif b>a:
print('b is big')
biggest of 3 without and operator
a=10; b=20; c=30
if (a>b):
if(b>c):
print("a is the largest ")
else:
print(“c is the largest")
else:
if(b>c):
print("b is the largest")
else:
print("c is the largest")
biggest of 3
a=10; b=20; c=30
if(a>=b):
if(a>=c):
print( 'a is big');
else:
print( 'c is big');
else:
if(b>=c):
print('b is big');
else:
print('c is big');
biggest of 3 with and logical operator
a = 10
b = 14
c = 12
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b>= c):
largest = b
else:
largest = c
print("The largest number between",a,",",b,"and",c,"is",largest)
nested if statements
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

Вам также может понравиться