Python statements and loops

Assignment for Python statements and loops

  1. If statement:  the program will execute only if the code statement is true

Syntax:

if test expression:

statement(s)

Example:

A = 10;

If A %5 =0;

    print (“number is divisible of 5”)

Output: number is divisible of 5

If A is divisible of 5 only than the code is execute. Suppose the A=6 than its reminder is 1 if we divide it with 5 than no code is execute. If u want to execute the code you have to write another if statement.

  1. If….else statement: it will execute body of if only when the condition is trure. If condition is false then body of else will execute.

Syntax:

if text expression:

    body of if

else:

    body of else

Example:

A = 10;

if A%5==0

    print(“ number is divisible by 5”)

else:

    print(“number is not divisible of 5”)

output:

number is divisible by 5

If A is divisible of 5 only than the code of if statement will execute other then the else code will be executed. Suppose the A=6 than its reminder is 1 if we divide it with 5 than else statement code is execute.

  1. If…. Elif… else: if the condition of if is false, it checks the condition of the next elif block and so on. If all the conditions are false, body of else is executed.

Syntax:

If test expression:   

    Body of if

Elif test expression:

    Body of elif

Else:

    Body of else

 

Example:

A = 10;

if A%5==0

    print(“ number is divisible by 5”)

elif A==0

    print(“ division not possible”)

else:

    print(“number is not divisible of 5”)

output:

number is divisible by 5

If A is divisible of 5 only than the code of if statement will execute other then the else code will be executed. Suppose the A=6 than its reminder is 1 if we divide it with 5 than else statement code is execute. Suppose if the A= 0 then 0 cannot be divided by 5 so it will execute the code of elif statement

  1. Nested if:  one if can be nested in another if. Any number of these statements can be nested inside one another.

Syntax:

If test expression

Example:

year = 2017

if (year % 4) == 0:

if (year % 100) == 0:

              if (year % 400) == 0:

                 print(“it is a leap year)

            else:

                 print(“it is not a leap year”)

      else:

              print (“it is a leap year”)

else:

     print("it is not a leap year”)

 

Output:

it is not a leap year

 

Loops:

  1. For loop: Execute a sequence of statements multiple times and val that manages the loop variable.

Syntax:

For iterating_var in sequence:

    statements

Example:

A = 5

Fact = 1

If A < 0;

    Print(“factorial for negative number does not exit”)

Elif A == 0;

    Print(“the factorial of 0 is 1”)

Else:

    for i in range(1, A + 1”)

    fact = fact * i

    print(“the factorial of “, A, “is” , fact)

 

output: the factorial of 5 is 120s

  1. While loop: if we want to execute the statement again and again only if the condition is true then run the while loop.

Syntax:

While expression:

    Statements

 

Example:

 

A = 0

While (A < 10):

    Print(“the number is:”, A )

    A = A +1

Print(“ good night”)

 

Output:

the number is: 0

the number is: 1

the number is: 2

the number is: 3

the number is: 4

the number is: 5

the number is: 6

the number is: 7

the number is: 8

the number is: 9

Good night

   

  1. Break statement: if we want to terminate the statement in between and transfers to another statement then break statement is used.

Syntax:

break

Example:

For val in “Python”:

    If val == “o”:

    break

    print(val)

print(“ good night”)

output:

p

y

t

h

Good night

  1. Continue statement: the continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.

 

Syntax:

Continue

Example:

For val in “Python”:

    If val == “o”:

    continue

    print(val)

print(“ good night”)

output:

p

y

t

h

n

Good night

 

Share this post