0% found this document useful (0 votes)
5 views2 pages

Python Iteration Statements Explained

The document provides an overview of iteration statements in Python, specifically focusing on loops such as for loops and their syntax. It explains the difference between .py and .pyc files, the use of the range() function, and includes example outputs for various for loop scenarios. Additionally, it presents programming exercises to print even numbers, calculate sums of even and odd integers, and print factors of a number.

Uploaded by

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

Python Iteration Statements Explained

The document provides an overview of iteration statements in Python, specifically focusing on loops such as for loops and their syntax. It explains the difference between .py and .pyc files, the use of the range() function, and includes example outputs for various for loop scenarios. Additionally, it presents programming exercises to print even numbers, calculate sums of even and odd integers, and print factors of a number.

Uploaded by

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

Std.

VII Subject:-Computer Applications


============================================
Q1. What are iteration statements in Python?
Ans: A loop or iterative statements allows us to execute a statement or group of statements repeatedly for a
finite number of times.
Python provides two kinds of loop.
a) For loop-loops that repeat a certain number of times.
b) Conditional loop-loops that repeat until a certain thing happens.

Q2) How is .pyc file different from a .py file?


Ans: The programs in python are called as source code. The extension of source code is .py and when the
script/program is executed byte code is [Link] extension of byte code is .pyc(compiled python code).
Q3) What is for loop?
The for loop executes a block of code repeatedly until the condition is valid.
Syntax of for loop:
for <variable> in range(intial value,final value,step value):
loop body
Q4) What do we use range () function?
The built in range() function is used to loop through a sequence of numbers. Python range() do not support
float numbers.
Syntax:
range(intial value,final value,step)
Intial value-It specifies the start of the counter variable.
Final value-It specifies the value which is used as a check to stop the loop.
Step-It specifies the value of increment or decrement of the counter.
Default value for intial value is 0 and step is 1.
Q5) Give the output for the following:
i) for index in range(5):
print (index)
Output is:-
0
1
2
3
4
ii) for index in range(5):
print (index + 1)
Output is:-
1
2
3
4
5
iii) for index in range(0,11,2):
print (index)
Output is:
0
2
4
6
8
10

--2
-2-
iv) sum=0
for i in range(1,20):
sum=sum+i
print("sum =" ,sum)
out put is: sum = 190

v) for i in range(3.3):
print (i)
Ouput: error ‘float’ cannot be used in range()

Programs using for with if statements

1) Write a program to print even numbers from 1 to 10.


Ans: for i in range(1,11):
if i%2==0: 2
print (i) 4
6
8
10
2) Wrtie a program to calculate and print the sum of even and odd integers for the first n numbers.

Ans: n=int(input("please enter the value"))


even_total=0 please enter the value10
odd_total=0
for number in range(1,n+1): the sum of even number from 1 to n+1 = 30
if (number %2==0): the sum of odd number from 1 to n+1 = 25
even_total=even_total+number
else:
odd_total=odd_total+number
print("the sum of even number from 1 to n+1 =",even_total)
print("the sum of odd number from 1 to n+1 =",odd_total)

3) Print the factors of a number.


n=int(input("please enter the value"))
print("the factors of n are:")
for i in range(1,n+1):
please enter the value 12
if(n%i==0): the factors of n are:
print(i,end=' ') 1 2 3 4 6 12

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

You might also like