Data Processing and Visulisation with Python
Python Exercise 7
String format
Write a Python program using ‘\n’ and ‘\t’ to format and print the following string in a specific format (see the output).
The original string:
I’m a lumberjack, and I’m okay.I sleep all night and I work all day.He’s a lumberjack, and he’s okay.He sleeps all night and he works all day.
Number filter
Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1000 and 2800 (both included).
for c in range(1000,2801):
if c%35 == 0 :
print(c,end ="\t")
# Q1
for i in range(1000,2800+1):
if i % 5 == 0 and i % 7 == 0 :
print(i,end = "\t")
All even
Write a Python program to find numbers between 100 and 400 (both included) where each digit of a number is an even number.
for i in range(100,400+1):
a = i % 10
b = (i//10) % 10
c = (i//10//10)%10
if a % 2 == 0 and b % 2 == 0 and c % 2 == 0:
print(i,end = "\t")
All but 3
Write a Python program to print all the numbers between 100 and 999 except those contain digit ‘3’.
Hint:
Use continue
.
for i in range(100,1000):
if "3" in str(i):
continue
print(i,end = "\t")
Arrow head of stars
Write a Python program to construct the following pattern, using a nested for loop.
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
for i in range(1,6):
print("* "*i)
for i in range(4,0,-1):
print("* "*i)
Prime numbers in an interval
Write a Python program to output all the prime numbers in a closed interval [a, b] (integer a and b are input from user and 0<a<=b<=10000).
from math import *
a=int(input("Please input a:"))
b=int(input("Please input b:"))
for n in range( a , b+1 ):
count = 0
sqrt_n = int(sqrt(n))
for i in range(2,sqrt_n+1):
if n % i == 0:
count = count + 1
if count == 0:
print( n , end = "\t" )
else:
continue
Multiplication table
Write a Python program to print multiplication table as output.
for a in range(1,10):
for b in range(1,a+1):
if b != a :
print(f"{a}*{b}=",a*b,end="\t")
else :
print(f"{a}*{b}=",a*b,end="\n")
Fibonacci series
Write a Python program to get the Fibonacci series between 0 to 1000.
Note:
The Fibonacci Sequence is the series of numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, …
Every next number is found by adding up the two numbers before it.
a = 0
print(a,end="\t")
b = 1
c = 1
while c < 1000 :
print(c,end="\t")
c = a + b
a = b
b = c
All posible sentences
Pick 4 phrases each from a list respectively to form a sentence. Write a Python program to form all posible sentences.
The 4 phrase lists are:
- Tom, A cat, His car
- stands, climbs
- on, over, into
- a house, a tree, a pond
N = ["Tom", "A cat", "His car"]
V = ["stands", "climbs"]
P = ["on", "over", "into"]
W = ["a house", "a tree", "a pond"]
for n in N :
for v in V :
for p in P :
for w in W :
print(n,v,p,w)