Professional Documents
Culture Documents
Python Control Flow Statements and Loops: Pynative
Python Control Flow Statements and Loops: Pynative
com/python-control-flow-statements/
PYnative
Python Programming
Learn Python Exercises Quizzes Code Editor Tricks
Python Variables
Also See:
Python Operators
1 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
The flow control statements are divided into
Learn Python Exercises Quizzes Code Editor Tricks
three categories
1. Conditional statements
2. Iterative statements.
3. Transfer statements
Conditional statements
1. if statement
2. if-else
3. if-elif-else
4. nested if-else
Iterative statements
2 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
statements.
Learn Python Exercises Quizzes Code Editor Tricks
1. for loop
2. while loop
Transfer statements
1. break statement
2. continue statement
3. pass statements
If statement in Python
In control statements, The if statement is the
simplest form. It takes a condition and evaluates
to either True or False .
3 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Learn Python
if condition: Exercises Quizzes Code Editor Tricks
statement 1
statement 2
statement n
Python if statements
Example
number = 6
if number > 5:
# Calculate square
print(number * number)
print('Next lines of code')
Run
Output
36
Next lines of code
If – else statement
4 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
condition is True, and if the condition is False, it
Learn Python Exercises Quizzes Code Editor Tricks
will execute the else block of code.
if condition:
statement 1
else:
statement 2
Example
if password == "PYnative@#29":
print("Correct password")
else:
print("Incorrect Password")
Run
Output 1: Output 2:
5 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Enter Learn Python
password Exercises
PYnative@#29
Enter Quizzes
passwordPYnative Code Editor Tricks
Correct password Incorrect Password
if condition-1:
statement 1
elif condition-2:
stetement 2
elif condition-3:
stetement 3
...
else:
statement
Example
def user_check(choice):
if choice == 1:
print("Admin")
elif choice == 2:
print("Editor")
elif choice == 3:
print("Guest")
else:
print("Wrong entry")
user_check(1)
user_check(2)
user_check(3)
user_check(4)
6 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Learn
RunPython Exercises Quizzes Code Editor Tricks
Output:
Admin
Editor
Guest
Wrong entry
if conditon_outer:
if condition_inner:
statement of inner if
else:
statement of inner else:
statement ot outer if
else:
Outer else
statement outside if block
7 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Learn Python Exercises Quizzes Code Editor Tricks
num1 = int(input('Enter first number '))
num2 = int(input('Enter second number '))
Run
Output 1: Output 2:
Example
number = 56
if number > 0: print("positive")
else: print("negative")
Example
x = 1
8 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
while x <= 5: print(x,end=" "); x = x+1
Learn Python Exercises Quizzes Code Editor Tricks
Run
Output
1 2 3 4 5
9 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Learn
for element inPython
sequence: Exercises Quizzes Code Editor Tricks
body of for loop
Run
Output
1
2
3
4
5
6
7
8
9
10
10 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Learn Python Exercises Quizzes Code Editor Tricks
Syntax of while-loop
while condition :
body of while loop
num = 10
sum = 0
i = 1
while i <= num:
sum = sum + i
i = i + 1
print("Sum of first 10 number is:", sum)
Run
Output
11 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
exit out of the loop. It is useful when we want to
Learn Python Exercises Quizzes Code Editor Tricks
terminate the loop as soon as the condition is
fulfilled instead of doing the remaining iterations.
It reduces execution time. Whenever the
controller encountered a break statement, it
comes out of that loop immediately
Run
Output
0
1
2
3
4
5
stop processing.
Continue statement in
python
The continue statement is used to skip the
current iteration and continue with the next
iteration.
12 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Learn
in
for num Python 8): Exercises
range(3, Quizzes Code Editor Tricks
if num == 5:
continue
else:
print(num)
Run
Output
3
4
6
7
Example
Run
Output
13 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Learn
['January', Python 'March',
'June', Exercises
'April'] Quizzes Code Editor Tricks
About Vishal
Founder of PYnative.com I am a
Python developer and I love to
write articles to help
developers. Follow me on Twitter. All the best
for your future Python endeavors!
14 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative.com is for Python • Learn Python To get New Python Tutorials, • About Us
lovers. Here, You can get • Python Basics Exercises, and Quizzes • Contact Us
Tutorials, Exercises, and
• Python Databases • Twitter We use cookies to improve
Quizzes to practice and
• Python Exercises your experience. While using
improve your Python skills. • Facebook
PYnative, you agree to have
• Python Quizzes • Sitemap
read and accepted our Terms
• Online Python Code Editor
Of Use, Cookie Policy, and
• Python Tricks Privacy Policy.
Copyright © 2018–2022
pynative.com
15 of 16 12/20/2022, 4:10 PM
Python Control Flow Statements and Loops – PYnative https://pynative.com/python-control-flow-statements/
PYnative
Python Programming
Learn Python Exercises Quizzes Code Editor Tricks
16 of 16 12/20/2022, 4:10 PM