conditional and loop statement in Python-if, for, while

conditional statement and loop statement in Python

conditional statement (if statement)

One Condition

  • Keyword: if

if [condition] : [space]code you want to learn

Multiple Condition

  • Keyword: elif
  • if must there before use elif if [condition] : [space]code you want to learn elif [condition] : [space]code you want to learn elif [condition] : [space]code you want to learn

Other than Condition

  • Keyword: else
  • if must there before use else if [condition] : [space]code you want to learn else : [space]code you want to learn

if Statement Example

a = 3
if a > 5:
    print("a is bigger than 5")
elif a > 0:
    print("a is bigger than 0 but smaller than 5")
else:
    print("a is negative")

Loop Statement (for Statement)

Repeat Example

  • 10 times loop
for i in range(10): 
    print("Hi")

list loop Example

  • Repeat as much as elements in list
a = ["A", "B", "C"]
for i in a:
    print(i)
# A B C

list loop with index Example

  • Repeat as much as elements in list
for i, j in enumerate(a):
    print(i, ":", j)
# 0 : A
# 1 : B
# 2 : C

add two list and loop Example

  • The pair’s data type is tuple
numbers = [9, 7, 7]
letters = ["z", "x", "y"]
for pair in zip(numbers, letters):
    print(pair)
# (9, 'z')
# (7, 'x')
# (7, 'y')

Loop with certain index Example

  • 5 to 9
for i in range(5, 10):  # 5 ~ 9
    print("Hello", i)
# Hello 5
# Hello 6
# Hello 7
# Hello 8
# Hello 9

loop until certain condition

  • loop until i is “o”
a = "balloon"

for i in a:
    print(i)
    if i == "o":
        break
# b a l l o

Loop Statement (while Statement)

loop until certain condition

  • loop until i is bigger than 5
i = 0
while i < 5:
    print(i)
    i = i + 1
# 0 1 2 3 4

 Share!

 
comments powered by Disqus