🐍 Python Basics 4/6: Conditions and Loops

date
Mar 29, 2023
slug
python-basics-conditions-and-loops
status
Published
tags
Python Basics
summary
Learn about Conditions and Loops in Python
type
Post
Last updated
Mar 30, 2023 12:48 AM
👋 Today we are gonna learn about conditions and loops in Python! This lesson is shorter than the previous ones, so let's spare 5 minutes!!
 

 

0) Conditions

 
There is just one condition block in Python: if-elif-else!!!!
 
#
# ---- if-elif-else block ----
#
a = 4
b = 8
c = 1

if a == 4: print("a is equals 4")
    
if a != 4: print("a is different than 4")
else: print("a is equals 4")
    
if a > b: print("a is greather than b")
elif a >= c: print("a is greather than or equals c")
else: print("a is smaller than b and c")
 

 

1) Loops

 
About the Loops, Python has two: while-block and for-block. Also, break keyword is used to force a loop to be stopped, and continue keyword is used to skip a iteration.
 
#
# ---- while-block ----
#
n = 0

while n < 10:
    print(n)
    n += 1
    if n == 5: break


#
# ---- for-block ----
#
for n in range(10):
    if n == 5: continue
    print(n)
 

 
Hope you enjoyed! See ya at functions and lambdas lesson! 👋