Professional Documents
Culture Documents
for x in range(6):
if (x == 3 or x==6):
continue
print(x,end=' ')
print("\n")
Write a Python program to find those numbers which are divisible by 7 and
multiple of 5, between 1500 and 2700 (both included).
nl=[]
for x in range(1500, 2701):
if (x%7==0) and (x%5==0):
nl.append(str(x))
print (','.join(nl))
Write a Python program that accepts a word from the user and reverse it.
word = input("Input a word to reverse: ")
for char in range(len(word) - 1, -1, -1):
print(word[char], end="")
print("\n")
Write a Python program to count the number of even and odd numbers from a
series of numbers.
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
count_odd = 0
count_even = 0
for x in numbers:
if not x % 2:
count_even+=1
else:
count_odd+=1
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)
count = 0
sum = 0.0
number = 1
while number != 0:
number = int(input(""))
sum = sum + number
count += 1
if count == 0:
print("Input some numbers")
else:
print("Average and Sum of the above numbers are: ", sum /
(count-1), sum)
Write a Python program to sum of two given integers. However, if the sum is
between 15 to 20 it will return 20.
def sum(x, y):
sum = x + y
if sum in range(15, 20):
return 20
else:
return sum
print(sum(10, 6))
print(sum(10, 2))
print(sum(10, 12))
Write a Python program which iterates the integers from 1 to 50. For multiples
of three print "Fizz" instead of the number and for the multiples of five print
"Buzz". For numbers which are multiples of both three and five print
"FizzBuzz".
for fizzbuzz in range(51):
if fizzbuzz % 3 == 0 and fizzbuzz % 5 == 0:
print("fizzbuzz")
continue
elif fizzbuzz % 3 == 0:
print("fizz")
continue
elif fizzbuzz % 5 == 0:
print("buzz")
continue
print(fizzbuzz)
Write a Python program that prints each item and its corresponding type from
the following list.
Sample List : datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12],
{"class":'V', "section":'A'}]
datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12],
{"class":'V', "section":'A'}]
for item in datalist:
print ("Type of ",item, " is ", type(item))
Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.
for x in range(6):
if (x == 3 or x==6):
continue
print(x,end=' ')
print("\n")
While loop
example prints the digits 0 to 4 as we set the condition x < 5.
x = 0;
print(x)
x += 1
never prints out anything since before executing the condition evaluates to
false.
x = 10;
print(x)
x += 1
while (True):
print(x)
x += 1
while loop calculates the sum of the integers from 0 to 9 and after completing
the loop, else statement executes.
x = 0;
s = 0
s = s + x
x = x + 1
else :
x = 1;
s = 0
x = x + 1
if (x == 5):
break
else :
In the following example for loop breaks when the count value is 5. The print
statement after the for loop displays the sum of first 5 elements of the tuple
numbers.
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
num_sum = 0
count = 0
for x in numbers:
num_sum = num_sum + x
count = count + 1
if count == 5:
break
In the following example while loop breaks when the count value is 5. The
print statement after the while loop displays the value of num_sum (i.e.
0+1+2+3+4).
num_sum = 0
count = 0
while(count<10):
count = count + 1
if count== 5:
break
continue statement
The continue statement is used in a while or for loop to take the control to the
top of the loop without executing the rest statements inside the loop. Here is a
simple example.
for x in range(7):
if (x == 3 or x==6):
continue
print(x)