0% found this document useful (0 votes)
23 views14 pages

Python For Loop Basics and Examples

The document provides a comprehensive overview of using for loops in Python, including initialization, increment/decrement, and stopping conditions. It includes various examples demonstrating how to print numbers, calculate squares, and determine even or odd numbers within specified ranges. Additionally, it covers user input scenarios and functions to enhance the understanding of loop operations.

Uploaded by

aushankhya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views14 pages

Python For Loop Basics and Examples

The document provides a comprehensive overview of using for loops in Python, including initialization, increment/decrement, and stopping conditions. It includes various examples demonstrating how to print numbers, calculate squares, and determine even or odd numbers within specified ranges. Additionally, it covers user input scenarios and functions to enhance the understanding of loop operations.

Uploaded by

aushankhya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

FOR LOOP

you will be in the loop


when you will enter into the loop
how much time you will stay in the loop
how you will come out from the loop

STEPS :

I N I T I ALI ZAT I ON

I N CREM EN T /DECREM EN T

CON DI T I ON T O ST OP T H E LOOP

In [ ]: #I want to print first 10 numbers


#intial point:0
#increment
#cond=when we reach 10 code should stop

P attern − 1

In the bracket if we have only single value that is consider as stop value
the default start value is =0
Python index always start with zero
if direction sigh (+/-) is not mentoin :take increment(+) sign
if direction is (+) side ,last value =stop-1

In [26]: for i in range(10):#for i in range(stop):


#print(0)
#print(1)
#print(2)
#.....
# generalized:print(i)
print(i)

0
1
2
3
4
5
6
7
8
9

In [ ]: for i in range(20)
1) initial point=0,2)increment,3)stop:20
In [28]: print(0,end='')
print(1,end='')

0 1 2

In [33]: for i in range(20):


print(i,end=' ')

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

P attern − 2

for i in range of (start,stop)

start:start of the loop


direction sign not mentoined:+ve direction [Link]
end=stop-1

In [37]: for i in range (10,20):


print(i,end=' ')

10 11 12 13 14 15 16 17 18 19

P attern − 3

for i in range(start,stop,step)

start:initial
direction:what is sign of step value,that is the direction
direction will not be definr by stop and step value,but will be provided by step value
if step size is positive direction
then end =stop-1
otherwise end=stop+1

In [50]: for i in range(2,20,2):


print(i,end=' ')
# start=2
#direction:step size=+2
#end=stop-1

2 4 6 8 10 12 14 16 18

In [1]: for i in range(2,20,-2):


print(i,end=' ')
# start=2
#direction:step size=-2
#end=stop+1

In [3]: for i in range(-2,-20,-2):


print(i,end=' ')
# -2 -4 -6 -8 ....

-2 -4 -6 -8 -10 -12 -14 -16 -18


In [5]: print(10,20,sep='&')

10&20

In [7]: print('name','python',sep=':')

name:python

WAP ask the user print hello 3 times/iteration===>loop

In [6]: for i in range(3):


print('hello')

hello
hello
hello

WAP ask the user print A 3 times,B 2 times,C 3 times

In [10]: for i in range(3):


print('A')

for i in range(2):
print('B')

for i in range(3):
print('C')

A
A
A
B
B
C
C
C

WAP ask the user print A ,B alternate 3 times,,C 3 times

In [19]: for i in range(3):


print('A')
print('B')

for i in range(3):
print('C')

A
B
A
B
A
B
C
C
C
WAP ask the user get the square of the numbers between 10 to 15

In [48]: for i in range(10,16):


print('The square of the number {} :'.format(i,i*i))

The square of the number 10 :


The square of the number 11 :
The square of the number 12 :
The square of the number 13 :
The square of the number 14 :
The square of the number 15 :

In [56]: for i in range(10,16):


print('The square of the number {} is {}'.format(i,i*i))

The square of the number 10 is 100


The square of the number 11 is 121
The square of the number 12 is 144
The square of the number 13 is 169
The square of the number 14 is 196
The square of the number 15 is 225

Same above question

take the number from keyboards

In [3]: n1=eval(input('Enter a number:'))


n2=eval(input('Enter second number:'))
for i in range(n1,n2):
print('The square of the number {} is {}'.format(i,i*i))

The square of the number 10 is 100


The square of the number 11 is 121
The square of the number 12 is 144
The square of the number 13 is 169
The square of the number 14 is 196
The square of the number 15 is 225

Function without arguments

In [11]: def sqr():


n1=eval(input('Enter a number:'))
n2=eval(input('Enter second number:'))
for i in range(n1,n2):
print('The square of the number {} is {}'.format(i,i*i))

sqr()

The square of the number 10 is 100


The square of the number 11 is 121
The square of the number 12 is 144
The square of the number 13 is 169
The square of the number 14 is 196
The square of the number 15 is 225
Function with arguments

In [14]: def sqr(n1,n2):


for i in range(n1,n2):
print('The square of the number {} is {}'.format(i,i*i))

sqr(10,16)

The square of the number 10 is 100


The square of the number 11 is 121
The square of the number 12 is 144
The square of the number 13 is 169
The square of the number 14 is 196
The square of the number 15 is 225

WAP ask the user to print square of the five numbers and these five numbers you need
ask user every time

In [5]: for i in range(5):


n1=eval(input('Enter a number:'))
print('the square of {} is {}'.format(n1,n1*n1))

the square of 12 is 144


the square of 13 is 169
the square of 14 is 196
the square of 15 is 225
the square of 16 is 256

WAP ask the user to print five times square of number

this time you need to take every time a random number between 1 to 20

In [10]: import random


for i in range(5):
[Link](1,20)
a=[Link](1,20)
print('The square of number {} is {}'.format(a,a*a))

The square of number 19 is 361


The square of number 17 is 289
The square of number 10 is 100
The square of number 18 is 324
The square of number 8 is 64

WAP ask the user print given number is even or odd

take no between 10 to30


apply condition every times

In [15]: for i in range (10,31):


if i%2==0:
print('The number {} is even'.format(i))
else:
print('The number {} is odd'.format(i))

The number 10 is even


The number 11 is odd
The number 12 is even
The number 13 is odd
The number 14 is even
The number 15 is odd
The number 16 is even
The number 17 is odd
The number 18 is even
The number 19 is odd
The number 20 is even
The number 21 is odd
The number 22 is even
The number 23 is odd
The number 24 is even
The number 25 is odd
The number 26 is even
The number 27 is odd
The number 28 is even
The number 29 is odd
The number 30 is even

WAP ask the user print given number is even or odd

take random no between 10 to 30


apply conditional statements every times.i.e 5 times

In [20]: import random


for i in range (5):
[Link](10,30)
b=[Link](10,30)
if b%2==0:
print('The number {} is even'.format(b))
else:
print('The number {} is odd'.format(b))

The number 16 is even


The number 27 is odd
The number 12 is even
The number 26 is even
The number 13 is odd

Apply executing function

In [23]: def _():


if b%2==0:
print('The number {} is even'.format(b))
else:
print('The number {} is odd'.format(b))
import random
for i in range (5):
[Link](10,30)
b=[Link](10,30)
_()

The number 19 is odd


The number 26 is even
The number 12 is even
The number 12 is even
The number 26 is even

execute fourlines

take a random number 1 to 10


3* ask the user to enter number
if both number matches each other print won else lose
mentoin no of chance left,whenever he lose

In [12]: import random


[Link](1,10)
c=[Link](1,10)
for i in range (3):
n6=eval(input('Enter a number:'))
if c==n6:
print('You win')
else:
print('You lose')
chances=2-i
print('The no of chances left:',chances)

You lose
The no of chances left: 2
You lose
The no of chances left: 1
You lose
The no of chances left: 0

OR

In [ ]: import random
[Link](1,10)
c=[Link](1,10)
chances=eval(input('Enter a number:'))
for i in range (3):
n6=eval(input('Enter a number:'))
if c==n6:
print('You win')
else:
print('You lose')
chances=2-i
print('The no of chances left:',chances-1-i)

Improvize upper code and whenever the user lost all the chances print all chances are
over

state plz try after 10 mins


In [34]: ##### import random
[Link](1,10)
c=[Link](1,10)
for i in range (3):
n6=eval(input('Enter a number:'))
if c==n6:
print('You win')
else:
print('You lose')
chances222=2-i
print('The no of chances left:',chances222)
if chances222==0:
print('Try again after 10 mins')

else:
print('You still have your chances left')

You lose
The no of chances left: 2
You still have your chances left
You lose
The no of chances left: 1
You still have your chances left
You lose
The no of chances left: 0
Try again after 10 mins

WAP ask the user get sum of 10 natural numbers

In [12]: summ=0
for i in range(1,11):
summ=summ+i
print(summ)

1
3
6
10
15
21
28
36
45
55

In [18]: summ=0
for i in range(1,11):
summ=summ+i
print('{} + {} is : {}'.format(summ,i,summ+i))
1 + 1 is : 2
3 + 2 is : 5
6 + 3 is : 9
10 + 4 is : 14
15 + 5 is : 20
21 + 6 is : 27
28 + 7 is : 35
36 + 8 is : 44
45 + 9 is : 54
55 + 10 is : 65

In [22]: for i in range(10):


print(i) # the last value in the loop will be printed ou of the loop

0
1
2
3
4
5
6
7
8
9
Out[22]: 9

In [26]: for i in range(10):


num=i
print(i)

num

0
1
2
3
4
5
6
7
8
9
Out[26]: 9

Whenever if you want implement counter program

summ program
initialize the variable with zero
like:summ=0 or count=0
inside the loop based on the problem update the variable
like:summ=summ+i or count=count+i
: summ+=i or count+=i
In [ ]: **WAP ask the user print given number is even or odd**
- take no between 10 to30
- apply condition every times
- BY count the number of even and number of odds you are getting

In [44]: count=0
count1=0
for i in range (10,31):
if i%2==0:
print('The number {} is even'.format(i))
count=count+1
else:
print('The number {} is odd'.format(i))
count1=count+1

print('The total count for even number:',count)


print('The total count for odd number:',count1)

The number 10 is even


The number 11 is odd
The number 12 is even
The number 13 is odd
The number 14 is even
The number 15 is odd
The number 16 is even
The number 17 is odd
The number 18 is even
The number 19 is odd
The number 20 is even
The number 21 is odd
The number 22 is even
The number 23 is odd
The number 24 is even
The number 25 is odd
The number 26 is even
The number 27 is odd
The number 28 is even
The number 29 is odd
The number 30 is even
The total count for even number: 11
The total count for odd number: 11

WAP ask the user take a random number between 1 to 99

iterate the loop 10 times


count how many times a number is coming and greater than 50

In [66]: import random


count6=0
count7=0
for i in range(10):
[Link](1,99)
m=[Link](1,99)
if m>=50:
print('The number {} is greater than 50'.format(m))
count6=count6+1
else:
print('The number {} is lesser than 50'.format(m))
count7=count7+1
print('The total number of count for the number greater than 50:',count6)
print('The total number of count for the number lesser than 50:',count7)

The number 48 is lesser than 50


The number 55 is greater than 50
The number 14 is lesser than 50
The number 57 is greater than 50
The number 50 is greater than 50
The number 13 is lesser than 50
The number 10 is lesser than 50
The number 40 is lesser than 50
The number 15 is lesser than 50
The number 9 is lesser than 50
The total number of count for the number greater than 50: 3
The total number of count for the number lesser than 50: 7

Number of divisors program

10 is divisible by what numbers

In [64]: count0=0
num=eval(input('Enter which numbe divisers you want to know'))
for i in range(1,num):
if num%i==0:
count0=count0+1
print('{} is divisible by {}'.format(num,i))
print('The divisors of number:{}'.format(count0))

20 is divisible by 1
20 is divisible by 2
20 is divisible by 4
20 is divisible by 5
20 is divisible by 10
The divisors of number:5

Use functions

counter program must execut at the same variable

count and d_count are local and global variable .So, make same variable

In [9]: count0=0
d_count=0
num=eval(input('Enter which numbe divisers you want to know'))
def counter2():
for i in range(1,num):
if num%i==0:
count0=count0+1
print('{} is divisible by {}'.format(num,i))
else:
d_count=d_count+1
print('The divisors of number:{}'.format(count0))
print('The non-divisors of number:{}'.format(d_count))
return(count0,d_count)

val1,val2=counter2()
print(val1,val2)

---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
Cell In[9], line 15
12 print('The non-divisors of number:{}'.format(d_count))
13 return(count0,d_count)
---> 15 val1,val2=counter2()
16 print(val1,val2)

Cell In[9], line 7, in counter2()


5 for i in range(1,num):
6 if num%i==0:
----> 7 count0=count0+1
8 print('{} is divisible by {}'.format(num,i))
9 else:

UnboundLocalError: cannot access local variable 'count0' where it is not associated


with a value

In [12]: count0=0
d_count=0
num=eval(input('Enter which numbe divisers you want to know'))
def counter2():
count0=0
d_count=0
for i in range(1,num):
if num%i==0:
count0=count0+1
print('{} is divisible by {}'.format(num,i))
else:
d_count=d_count+1
print('The divisors of number:{}'.format(count0))
print('The non-divisors of number:{}'.format(d_count))
return(count0,d_count)

val1,val2=counter2()
print(val1,val2)

10 is divisible by 1
10 is divisible by 2
10 is divisible by 5
The divisors of number:3
The non-divisors of number:6
3 6

Using with arguments

In [65]: def counter(count1,d,num,i):


for i in range(1,num):
if num%i==0:
count1=count1+1
print('{} is divisible by {}'.format(num,i))
else:
d=d+1
print('The divisors of number:{}'.format(count1))
print('The non-divisors of number:{}'.format(d))
return(count1,d)

val1,val2=counter(0,0,10,(1,10))
print(val1,val2)

10 is divisible by 1
10 is divisible by 2
10 is divisible by 5
The divisors of number:3
The non-divisors of number:6
3 6

Using function in function

completef unctionisabove

Now main program

Implimenting function in function

In [61]: def program(i,count1,d,num10):


if num10%i==0:
count1=count1+1
print('{} is divisible by {}'.format(num10,i))
else:
d=d+1
return(count1,d)

program(5,0,0,10)

10 is divisible by 5
Out[61]: (1, 0)

In [65]: def counter8():


count2=0
d2=0
num100=10
for i in range(1,num100):
count2,d2=program(i,count2,d2,num100)
return(count2,d2)

val1,vl2=counter8()
print(val1,vl2)

10 is divisible by 1
10 is divisible by 2
10 is divisible by 5
3 6
The mini function program is returning 2 values
SO,if mini function is executed in main program;it should also execute 2 values
NEXT
the 4 variables in mini function are already assigned with values
but the main function might have different [Link], if it id the case ,the variables
must be replaced

In [ ]:

You might also like