Search This Blog

Wednesday, 15 December 2021

For Loop in Python

 

Python For Loop

build a site

Python for loop is used to iterate the elements of a collection in the order that they appear. This collection can be a sequence(list or string).

Python For Loop Syntax :

for [variable] in [sequence]:
1

Firstly, the first value will be assigned in the variable.

2

Secondly all the statements in the body of the loop are executed with the same value.

3

Thirdly, once step second is completed then variable is assigned the next value in the sequence and step second is repeated.

4

Finally, it continues till all the values in the sequence are assigned in the variable and processed.

Python For Loop Simple Example :

num=2

for a in range (1,6):
    print num * a

output of above example  :

2
4
6
8
10

Python Example to Find Sum of 10 Numbers :

sum=0
for n in range(1,11):
    sum+=n
print sum

output of above example  :

55

Python Nested For Loops

Example of Nested for loop :

for i in range(1,6):
    for j in range (1,i+1):
        print i,
    print

output of above example  :

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

For each value of Outer loop the whole inner loop is executed.

For each value of inner loop the Body is executed each time. 


lets see one more example of patter program :

for i in range (1,6):
    for j in range (5,i-1,-1):
       print "*",
    print

output of above example  :

* * * * *
* * * *
* * *
* *
*

While loop in Python

 

Python While Loop

free css templates

In Python, while loop is used to execute number of statements or body till the specified condition is true. Once the condition is false, the control will come out of the loop.

while loops are constructed like so :

while [a condition is True]:
    [do something]

Here, loop Body will execute till the expression passed is true. The Body may be a single statement or multiple statement.

Python While Loop Example :

a=10

while a>0:
    print "Value of a is",a
    a=a-2
print "Loop is Completed"

output of above example  :

Value of a is 10
Value of a is 8
Value of a is 6
Value of a is 4
Value of a is 2
Loop is Completed

Explanation:

1

Firstly, the value in the variable is initialized.

2

Secondly, the condition/expression in the while is evaluated. Consequently if condition is true, the control enters in the body and executes all the statements . If the condition/expression passed results in false then the control exists the body and straight away control goes to next instruction after body of while.

3

Thirdly, in case condition was true having completed all the statements, the variable is incremented or decremented. Having changed the value of variable step second is followed. This process continues till the expression/condition becomes false.

4

Finally Rest of code after body is executed.

Python While Loop Example 2 :

n=153
sum=0

while n>0:
    r=n%10
    sum+=r
    n=n/10
print sum

output of above example  :

9

An infinite loop occurs when a program keeps executing within one loop, never leaving it. To exit out of infinite loops on the command line, press CTRL + C.

This tutorial went over how while loops work in Python and how to construct them. While loops continue to loop through a block of code provided that the condition set in the while statement is True

Nested IF statement in python

 

 Python Nested If Statement

simple web creator

We can use nested if statements for situations where we want to check for a secondary condition if the first condition executes as true. For this, we can have an if-else statement inside of another if-else statement. Let’s look at the syntax of a nested if statement:

if expression1:
   statement(s)
   if expression2:
      statement(s)
   elif expression3:
      statement(s)
   elif expression4:
      statement(s)
   else:
      statement(s)
else:
   statement(s)

lets run the following example :

a=10
if a>=20:
    print "Condition is True"
else:
    if a>=15:
        print "Checking second value"
    else:
        print "All Conditions are false"
output of above example :
All Conditions are false.

If and else statement in Python

 

Python If Else Statements

Mobirise

It is likely that we will want the program to do something even when an if statement evaluates to false. In our grade example of previous tutorial, we will want output whether the grade is passing or failing.

Python If Else Syntax :

if condition:
   statement
else:
   statement

To do this, we will add an else statement to the grade condition above that is constructed like this:

grade = 60

if grade >= 65:
    print("Passing grade")
else:
    print("Failing grade")

Since the grade variable above has the value of 60, the if statement evaluates as false, so the program will not print out Passing grade. The else statement that follows tells the program to do something anyway.

When we save and run the program, we’ll receive the following output:

Failing grade

If we then rewrite the program to give the grade a value of 65 or higher, we will instead receive the output Passing grade.

If statement in Python

 

Python if statement

easy website builder

The Python if statement is a statement which is used to test specified condition. We can use if statement to perform conditional operations in our Python application. The if statement executes only when specified condition is true. We can pass any valid expression into the if parentheses.

There are various types of if statements in Python.

  1.   if statement
  2.   if-else statement
  3.   Else if statement
  4.   nested if statement

If Statement Syntax

if statement, which will evaluate whether a statement is true or false, and run code only in the case that the statement is true.

if(condition):
   statements

Example of if statement :

grade = 70
if grade >= 65:
    print("Passing grade")

With this code, we have the variable grade and are giving it the integer value of 70. We are then using the if statement to evaluate whether or not the variable grade is greater than or equal ( >= ) to 65. If it does meet this condition, we are telling the program to print out the string Passing grade. 

output of above example :

Passing grade

Again, if we change the grade to 50 or a < 65 number, we will receive no output.

how to implement YOLOv3 using Python and TensorFlow

Object Detection with YOLOv3 Introduction YOLOv3 (You Only Look Once version 3) is a real-time object detection algorithm that can detect ob...