INDEX
- Python Programming: Introduction to Loops in Python
- Python programming: Structure of for loop or for-in loop
- Python programming: Structure of while loop
- Python program: Number series: 1,2,3,...,10
- Python program: Display "Python Loops" 10 times
- Python program: Display "Python Loops" 10 times with serial No.
- Python program: First 10 multiples of 3
- Python program: Factorial of n
- Python program: Fibonacci Series (i.e. 1,1,2,3,5,8,13,21,....)
- Python program: Factors of n
- Python program: Perfect number
- Python program: HCF and GCD
- Python program: Prime Number
- Python program: Sum of digits of an integer
- Python program: Reverse number
- Python program: Numeric Pallindrome
- Python program: Armstrong Number
- Python program: Duck Number
- Python program: Infinite For Loop in Python
Introduction to Loops in Python:
Loops are the control-statements, which are used to re-iterate (or repeat) a set of instructions (or
even a single instruction), as long as a specific criteria is met.
The moment the criteria fails, the looping structure ends, and the program-control moves to the first
statement following the loop-struture / loop-block.
There are two(2) kinds of loop control statements in Python language.
They are:
- for loop or the for-in loop
- while loop
Each of the above two loops is an Entry control loop.
Python does not have any Exit control loop.
Entry control loops check the loop condition at the begining of each iteration. for loop and
while loop falls in this category.
Exit control loops check the loop condition at the end of each iteration.
Note: Python does not have a goto statement and hence there does not exist any if-goto loop structure.
Unlike other programming languages like C, C++ and Java; Python allows an else-block with each kind of loops mentioned above.
The else-block is executed only when the condition/criteria of the for-loop or the while-loop fails and causes the end of looping process.
for loop or for-in loop in Python:
The for loop or the for-in loop iterates through a sequence of values. It terminates whenever the sequence
ends. The for loop will strictly flow through the sequence from left-right. Python does not allow any
alteration to this behaviour of for-loop. Any attempt to do so does not take effect.
The for loop or the for-in loop of Python is somewhat similar to the foreach loop of
C++ and Java.
The Syntax of for loop or the for-in loop in python is;
for <variable> in <sequence>:
#statements to be re-iterated
.....
.....
.....
else:
#statements to be processed when the for-sequence ends
.....
.....
.....
Note: The else - block is optional and the programmer may choose to omit it.
The else - block with for loop and while loop is not provided by C++ or Java.
Example:
'''A program to display "Hello" 10 times along with serial number.
'''
for x in range(1,11,1):
print(x,'. Hello',sep='')
else:
print('...Display Complete...')
Output:1. Hello 2. Hello 3. Hello 4. Hello 5. Hello 6. Hello 7. Hello 8. Hello 9. Hello 10. Hello ...Display Complete...
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners
while loop in Python:
The while loop checks for a specific condition/criteria and iterates all the statements written
in its block again and again as long as the condition / criteria returns a TRUE boolean value.
When the Condition / criteria returns FALSE, the program control shifts to the else: block (provided
it exists), and then flows down through the subsequent statements.
If the else: block does not exist, then the program control moves to the first statement following
the while block.
The syntax of while loop:
#initialisation of loop variable(if any)
.....
while <condition>:
#statements to be re-iterated
.....
.....
.....
#incrementation / decrementation of loop-variable(if any)
.....
else:
#statements to be processed when the for-sequence ends
.....
.....
.....
Note: The else - block is optional and the programmer may choose to omit it.
The else - block with for loop and while loop is not provided by C++ or Java.
Example:
'''A program to display "Hello" 10 times along with serial number.
'''
x=1 #initialisation of loop variable
while x<11:
print(x,'. Hello',sep='')
x+=1 #incrementation of loop variable
else:
print('...Display Complete...')
Output:1. Hello 2. Hello 3. Hello 4. Hello 5. Hello 6. Hello 7. Hello 8. Hello 9. Hello 10. Hello ...Display Complete...
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to display 1 to 10
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
for x in range(1,11,1):
print(x)
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to display the phrase 'Python loops' 10 times
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
for x in range(1,11,1):
print('Python loops')
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to display the phrase 'Python loops' 10 times with serial number
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
for x in range(1,11,1):
print(x,'. Python loops',sep='')
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to display the first 10 multiples of 3
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
for x in range(1,11,1):
print(x*3)
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to display factorial of n (i.e. n!)
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter any positive integer:'))
fact=1;
for x in range(n,0,-1):
fact*=x
print(n,'! = ',fact,sep='')
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to display n-terms of fibonacii series 0,1,1,2,3,5,8,....
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter the number of terms in the fibonacii series'))
i=0
j=1
k=0
for x in range(1,n+1,1):
print(k)
if(x!=n)
print(', ')
i=j
j=k
k=i+j
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to accept a number and display its factors
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter any integer(n): '))
print('The factors of',n,'are as follows:')
for i in range(1,n+1,1):
if n%i==0:
print(i)
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to accept a number and display whether its a PERFECT-NUMBER or NOT
(definition): if (the sum of factors of the number) = 2 x Number, then the number is a perfect number.
(example): 6, Sum of Factors of 6 = 1+2+3+6 =12 = 2x6, hence 6 is a perfect number.
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter any integer(n): '))
#Finding factors of n and then adding them
sum=0
for i in range(1,n+1,1):
if n%i==0:
sum+=i
#Validating or checking Perfect Number
if sum==n:
print(n,'is a Perfect Number.')
else:
print(n,'is NOT a Perfect Number.')
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to accept 2 integers from keyboard and display their h.c.f. or g.c.d. (Using Long division Method)
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n1=int(input('Enter integer1 (n1): '))
n2=int(input('Enter integer2 (n2): '))
x=n1
y=n2
if x>y:
x=n2
y=n1
iterations=[1]
for i in iterations:
if y%x==0:
break
iterations.append(i+1) #Extends the loop for 1 more iteration
temp=x
x=y%x
y=temp
print('HCF of',n1,'and',n2,'is =',x)
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to accept a number and display whether its prime or not
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter any integer (n): '))
flag=True
if n==1:
flag=False
for i in range(2,n//2+1,1):
if n%i==0:
flag=False
break
if flag:
print(n,'is a Prime Number.')
else:
print(n,'is NOT a Prime Number.')
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to accept a number and display the sum of its digits.
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter any integer (n): '))
sum=0
n2=n
iterations=[1]
for i in iterations:
if n2<=0:
break
d=n2%10
n2=n2//10
sum+=d
iterations.append(i+1)
print('Sum of digits of',n,'=',sum)
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to accept a number and display the reverse number.
Example: (i/p)243
(o/p)Reverse Number is 342
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter any integer (n): '))
rev=0
n2=n
iterations=[1]
for i in iterations:
if n2<=0:
break
d=n2%10
n2=n2//10
rev=rev*10+d
iterations.append(i+1)
print('Number obtained by reversing the digits of',n,'is =',rev)
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners
'''A program to accept a number and display whether its a Numeric-Pallindrome or Not
(definition): if NUMBER = ITS REVERSE NUMBER, then the NUMBER is a Numeric-Pallindrome.
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter any integer (n): '))
rev=0
n2=n
iterations=[1]
for i in iterations:
if n2<=0:
break
d=n2%10
n2=n2//10
rev=rev*10+d
iterations.append(i+1)
if rev==n:
print(n,'is a Numeric Pallindrome')
else:
print(n,'is NOT a Numeric Pallindrome')
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to accept a number and display whether its an Armstrong number or Not
(definition): if NUMBER = SUM of CUBE of ITS DIGITS, then the number is an ARMSTRONG Number
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
import math
n=int(input('Enter any integer (n): '))
sum=0
n2=n
iterations=[1]
for i in iterations:
if n2<=0:
break
d=n2%10
n2=n2//10
sum+=math.pow(d,3)
iterations.append(i+1)
if sum==n:
print(n,'is an Armstrong Number.')
else:
print(n,'is NOT an Armstrong Number.')
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners '''A program to accept an integer and display whether its a Duck-Number or not
(definition): if the NUMBER contains atleast one 0(Zero) as a digit, then it is called a duck number.
(example): 10,20,301,520,1200,1701,...
Author: Pradeep Das Gupta
Compiler: Python 3.7 compiler.
'''
n=int(input('Enter any integer (n): '))
count=0
n2=n
iterations=[1]
for i in iterations:
if n2<=0:
break
d=n2%10
n2=n2//10
if d==0:
count+=1
iterations.append(i+1)
if count>0:
print(n,'is a Duck Number.')
else:
print(n,'is NOT a Duck Number.')
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners
How to control for loop or for-in loop in Python?
OR
How to make for loop or for-in loop in Python run as long as you want?
OR
How to run for loop or for-in loop in Python infinitely [Caution: Memory Limitation]?
OR
Infinite for loop or for-in loop in Python [Caution: Memory Limitation]
The for loop in Python uses the following syntax;
for <variable> in <sequence>:
#statements to be re-iterated
.....
.....
.....
in which <sequence> is any valid Python - list, tuple, sting, or range object.
At run-time the <variable> traverses through the sequence one-by-one from left to right,
and the loop terminates or ends after the <variable> passes through the last value in the list.
Like other languages i.e. C, C++, Java, ... one may choose to control the value of the <variable>,
but doing so does not work. This is because every time the loop iterates, the <variable> automatically
takes the next value from the <sequence>.
Thus, To regulate the for loop or for-in loop in Python we need to employ a different
methodology. Here I am showing one of the few programming tweaks, that I personally like and use often.
Note: Try not to use this to create infinite-loop, as it would lead to memory insufficiency.
What I do is, to directly control the length of
Use something like this;
<list name>=[1]
for <variable> in <list name>:
#statements to be iterated
...
...
...
if <condition to terminate loop>:
break
<list name>.append(<variable>+1) #This adds another element to the list.
#As a result the for loop will have to iterate one more time.
#The process continues until the loop terminates as a result of the 'break' statement.
Example:
#To display 1 to 10
iterations=[1]
for i in iterations:
print(i)
if i>=10:
break
iterations.append(i+1)
Output:
1 2 3 4 5 6 7 8 9 10
Other Examples:
A program to accept 2 integers from keyboard and display their h.c.f. or g.c.d. (Using Long division Method)
A program to accept a number and display the sum of its digits.
A program to accept a number and display the reverse number.
A program to accept a number and display whether its a Numeric-Pallindrome or Not
(definition): if NUMBER = ITS REVERSE NUMBER, then the NUMBER is a Numeric-Pallindrome.
A program to accept a number and display whether its an Armstrong number or Not
(definition): if NUMBER = SUM of CUBE of ITS DIGITS, then the number is an ARMSTRONG Number
A program to accept an integer and display whether its a Duck-Number or not
(definition): if the NUMBER contains atleast one 0(Zero) as a digit, then it is called a duck number.
(example): 10,20,301,520,1200,1701,...
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners