Python Statements

Indentation:

Indentation is very import while using python statements. It is important to keep a good understanding of how indentation works in Python to keep you code structure and in proper order to execute the code.


Example:

Simple print statement with if condition:

if True:
   print('This is a True statement')

This is a True statement

Try printing with Logic:

if, else Statements

Syntax:

if case:
    perform action1
else:
    perform action2


Example:

name='Feroz'
if name == 'Feroz':
    print('Hello Feroz!')
else:
    print('Welcome to the Group!')

Hello Feroz!


if, elif, else Statements

Syntax:

if case1:
    perform action1
if case2:
    perform action2
else:
    perform action3


Example:


sports='cricket'
if sports == 'cricket':
   print('Welcome to Cricket Club!')
elif sports == 'Foot Ball':
  print('Sorry! it is currently not available')
else:
   print('This game not present in the list')

Welcome to Cricket Club!


for loops

A for loop act as an iterator. It's generally access the items sequentially from list, tuples, and dictionaries. 

Syntax:

for item in object:
      statement to perform stuff

You can defined any variable name like "item" in the above syntax based on the code requirement or program logic.


Iterating through a list:

Exmaple-1:

my_list=[1,2,3,4,5,6,7,8,9]
for num in my_list:
     print(num)

1
2
3
4
5
6
7
8
9

Example-2: for loop using if statement (Printing even numbers)

my_list=[1,2,3,4,5,6,7,8,9]
for num in my_list:
      if num % 2 == 0:
         print(num)

2
4
6
8



Comments

Popular posts from this blog

Auditing in Oracle database

rs.stepDown() in MongoDB replication

Tuples in Python