-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path(3)_flow-control.py
177 lines (157 loc) · 5 KB
/
(3)_flow-control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Python Flow Control
# ========================
# Adapted From: Programiz
# Tutorial Title: Python Flow Control
# URL: https://www.programiz.com/python-programming
# 1. Python if ... else
# ----------------------
# In Python, there are three forms of the 'if ... else' statement:
# i. if statement
# Example 1: Python if Statement
number = 10
# check if number is greater than 0
if number > 0:
print('Number is positive.')
print('The if statement is easy')
# ii. if ... else statement
# Example 2. Python if...else Statement
number = 10
# check if number is greater than 0
if number > 0:
print('Positive number')
else:
print('Negative number')
print('This statement is always executed')
# iii.if ... elif ... else statement
# Example 3: Python if...elif...else Statement
number = 0
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')
print('This statement is always executed')
# 2. Python Nested if statements
# -------------------------------
# Example 4: Python Nested if Statement
number = 5
# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')
# inner else statement
else:
print('Number is positive')
# outer else statement
else:
print('Number is negative')
# Output: Number is positive
# 3. Python for Loop
# -------------------
# Definition: In Python, the for loop is used to run a block of code for a certain number of times.
# It is used to iterate over any sequences such as lists, tuples, string, etc.
# Example: Loop Over Python List
programming_languages = ['Swift', 'Python', 'Go', 'JavaScript']
for language in programming_languages:
print(language)
# Python for Loop with Python range()
# A 'range' is a series of values between two numeric intervals.
# We use Python's built-in function range() to define a range of values. For example,
# use of range() to define a range of values
values = range(4)
# iterate from i = 0 to i = 3
for i in values:
print(i)
# Python for loop with else
# Definition: A 'for' loop can have an optional else block as well. The 'else' part is executed when the loop is finished. For example,
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
# 4. Python while Loop
# ----------------------
# Definition: Python while loop is used to run a specified code until a certain condition is met.
# Example 1: Python while Loop
i = 1
n = 5
while i <= n:
print(i)
i = i + 1
# Example 2: Python while Loop to Display Game Level
current_level = 0
final_level = 5
game_completed = True
while current_level <= final_level:
if game_completed:
print('You have passed level', current_level)
current_level += 1
print('Level Ends')
# Infinite while Loop in Python
# If the condition of a loop is always True, the loop runs for infinite times (until the memory is full). For example,
# infinite while loop
# while True:
# body of the loop
# In the above example, the condition is always True. Hence, the loop body will run for infinite times.
# Python While loop with else
# A while loop can have an optional else block as well.
# The else part is executed after the condition in the while loop evaluates to False. For example,
counter = 0
while counter < 3:
print('Inside loop')
counter = counter + 1
else:
print('Inside else')
# Python for vs while loops
# The for loop is usually used when the number of iterations is known. For example,
# this loop is iterated 4 times (0 to 3)
for i in range(4):
print(i)
# And while loop is usually used when the number of iterations are unknown. For example,
# while condition:
# body of loop
# 5. Python break and continue
# -----------------------------
# The break statement is used to terminate the loop immediately when it is encountered.
# Syntax: break
# Example 1: Python break Statement with for Loop
for i in range(5):
if i == 3:
break
print(i)
# Example 2: Python break Statement with while Loop
# program to find first 5 multiples of 6
i = 1
while (i <= 10):
print('6 * ', (i), '=', 6 * i)
if i >= 5:
break
i = i + 1
# The continue statement is used to skip the current iteration of the loop and the control flow of the program goes to the next iteration.
# Syntax: continue
# Example 1: Python continue Statement with for Loop
for i in range(5):
if i == 3:
continue
print(i)
# Example 2: Python continue Statement with while Loop
# program to print odd numbers from 1 to 10
num = 0
while num < 10:
num += 1
if (num % 2) == 0:
continue
print(num)
# 6. Python Pass
# ---------------
# Definition: In Python programming, the pass statement is a null statement which can be used as a placeholder for future code.
# Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. In such cases, we can use the pass statement.
# Syntax: pass
# Example 1: Using pass With Conditional Statement
n = 10
# use pass inside if statement
if n > 10:
pass
print('Hello')