Table of contents
Conditional statement
Expressions that used in if, elif statements must be estimated through True or False.
-
Use simple if statement
if <expression>: # statements
For example:
age = 10 if age > 6: print("Junior")
-
Use if..else statement
if <expression>: # statements else: # other statements
-
Use if .. elif .. else statement
if <expression>: # statements elif <expression>: # statements elif <expression>: # statements else: # statements
Python does not have switch statement.
Loop statement
-
while loop
while <expression>: # statements
For example:
x = 0 while x < 10: x = x + 1 print('Current value: ' + str(x))
-
For loop
for <iter> in <somethings>: # statements
For example:
for letter in 'Hello, world': print(letter)
Wrapping up
-
Understanding about how to use the common statements in Python.
-
Use pass statement for empty function, class, loop or if .. else statements to avoid the error notifications from interpreter.