Chapter 8 More About Python
A. 1. continue 2. break 3. else 4. True 5. While
B. 1. for x in range(5,10,2):
print (x+10)
2. x=5
while(x<20):
print(x)
x+=5
C. 1. 1, 2, 3, 4,
2. S
t
r
i
n
g
Over
D. 1. n=int(input(“Enter number:”))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print(“Palindrome Number”)
else:
print(“Not a Palindrome Number”)
2. n=int(input(“Enter number:”))
sum = 0
for i in range(1, n-1):
mod = n%i
if mod == 0:
sum = sum + i
if sum == n:
print(“perfect number“)
else:
print(“not a perfect number“)
3. n=int(input(“Enter number:”))
mod = n%10
if mod == 8:
print(“ends with 8“)
elif mod == 4:
print(“ends with 4“)
else:
print(“ends with neither“)
4. n=int(input(“Enter a number: “))
sum1 = 0
while(n > 0):
sum1=sum1+n
n=n-1
print(“The sum of first n natural numbers is”,sum1)
E. 1. Sometimes there is a need to repeat a set of statements more than
once based on a certain condition. This process of repetition is called
loop or iteration in programming. A loop, is thus used to repeat a block
of statements a specific number of times. The loops used in Python are
for loop and while loop.
2. The range() function is used to create a list containing a sequence
of numbers starting from start and ending with one less than the stop.
Syntax: range([start], stop,[step])
Example: >>>range(10); output: [0,1,2,3,4,5,6,7,8,9]
3. for… structure is used when you want to perform a loop a specific
number of times. It uses a counter variable which is incremented or
decremented with each repetition of the loop A while loop executes a
block of code repeatedly as long as the test/control condition of the
loop is true. It is ideally suited when the number of iterations are not
known prior to the execution of the loop.
4. Python offers two jump statements to be used within loops to jump
out of loop iterations. These are break and continue statements. It alters
the flow of control in a loop