Python Basic Controls

Print statement:

print('hello') 

If statement:

if a==1:
  print('a is 1') 

If-then-else statement:

if a==1:
  print('a is 1') 
else:
  print('a is not 1')

For-loop statement:

for i in range(5):
  print(i) 
# output:
# 0
# 1
# 2
# 3
# 4 

The third parameter in the range function is the step counter, by default it is 1.

for i in range(1,6,2):
  print(i) 
# output:
# 1
# 3
# 5

We can count backward by specifying step -1

for i in range(7,4,-1):
  print(i) 
# output:
# 7
# 6
# 5

While-loop statement

a='abhxuy' 
i=0
while a[i] != 'x':
  print(a[i]) 
  i += 1
# output:
# a
# b
# h

Create a function

# function definition
def mynum(num):
  print(f'my number is {num}') 

# to call the function
mynum(7)

# output:
# my number is 7

👍 Have fun while coding with Python!

👌 Please Subscribe to not miss any future posts

💬 If you have any questions simply write a comment down below.

Leave a Comment