You are on page 1of 6

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")

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)

Write a Python program to find the median of three values.


a = float(input("Input first number: "))
b = float(input("Input second number: "))
c = float(input("Input third number: "))
if a > b:
if a < c:
median = a
elif b > c:
median = b
else:
median = c
else:
if a > c:
median = a
elif b < c:
median = b
else:
median = c

print("The median is", median)

Write a Python program to calculate the sum and average of n integer


numbers (input from the user). Input 0 to finish.
print("Input some integers to calculate their sum and average. Input
0 to exit.")

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 check a triangle is equilateral, isosceles or scalene.

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 to convert month name to a number of days.


Write a Python program to find numbers between 100 and 400 (both included)
where each digit of a number is an even number. The numbers obtained
should be printed in a comma-separated sequence.
items = []
for i in range(100, 401):
s = str(i)
if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0):
items.append(s)
print( ",".join(items))

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 to get the Fibonacci series between 0 to 50.


x,y=0,1
while y<50:
print(y)
x,y = y,x+y

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;

while (x < 5):

print(x)

x += 1

never prints out anything since before executing the condition evaluates to
false.
x = 10;

while (x < 5):

print(x)

x += 1

while loop is an infinite loop, using True as the condition:


x = 10;

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

while (x < 10):

s = s + x

x = x + 1

else :

print('The sum of first 9 integers : ',s)

while loop with if-else and break statement

x = 1;

s = 0

while (x < 10):


s = s + x

x = x + 1

if (x == 5):

break

else :

print('The sum of first 9 integers : ',s)

print('The sum of ',x,' numbers is :',s)

break in for loop

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

print("Sum of first ",count,"integers is: ", num_sum)

break in while loop

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):

num_sum = num_sum + count

count = count + 1
if count== 5:

break

print("Sum of first ",count,"integers is: ", num_sum)

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)

You might also like