Data Processing and Visulisation with Python
Python Exercise 6
Even numbers in an interval
Write a Python program to output all the even numbers in closed interval [a, b] (integers a and b are input by user, and 100 >= b >= a >= -100).
Hint:
Maybe you want to modify end
param in print
function to make your output look nice. However, it is not required.
a = int(input("Please input a: "))
b = int(input("Please input b: "))
print(f"The even numbers in closed interval [{a}, {b}]:")
for c in range(a,b+1):
if c%2 == 0 :
print(c,end ="\t")
Prime number
Write a Python prgram to input a positive integer n and output whether the integer is prime.
Note:
Try to check the divisibility of n with integers from 2 to n \sqrt{n} n. If none is divisible, then n is a prime, otherwise not.
Hint:
Maybe you want to use ceil
and sqrt
functions from math
module.
from math import ceil,sqrt
n = int(input("Please input a positive integer: "))
sqrt_n = int(sqrt(n))
for i in range(2,sqrt_n+1):
if n%i == 0:
print(f"{n} is not a prime.")
break
else:
print(f"{n} is a prime.")
### 注:此处的else与for对应
Digit 3 in a positive integer
Write a Python program to count the number of digit 3 in an input positive integer.
For example: there are 2 '3’s in 2354534 and 5 '3’s in 35673873383.
s = int(input("Please input a positive integer: "))
n = 0
b = s
while s !=0:
if s%10 == 3:
n += 1
s = s // 10
print(f"Altogether {n} '3's in {b}")
#Q3
s = input("Please input a positive integer: ")
n = 0
for i in s :
if i == "3":
n += 1
print(f"Altogether {n} '3's in {s}")
Sum of arbitrary numbers of integers
Write a Python program to input arbitrary numbers of integers from user (end with input of 0) and output the sum of these integers.
total=0
cin=None
while cin!=0 or cin is None:
cin=int(input('Please input an integer (finish inputting with "0"): '))
total=total+cin
print('The sum of all the inputs is:',total)
total=0
cin=1
while cin!=0 :
cin=int(input('Please input an integer (finish inputting with "0"): '))
total=total+cin
print('The sum of all the inputs is:',total)
Mean of arbitrary numbers of integers
Write a Python program to input arbitrary numbers of integers from user (end with input of 0) and output the mean of these integers.
a=int(input('Please input an integer (finish inputting with "0"): '))
sum=0
i=0
while a!=0:
a=int(input('Please input an integer (finish inputting with "0"): '))
sum+=a
i+=1
print(f"The mean of all the inputs is: {sum/i}")
Variance of arbitrary numbers of integers
Write a Python program to input arbitrary numbers of integers from user (end with input of 0) and output the variance of these integers.
Hint:
variance σ 2 = E ( X 2 ) − E 2 ( X ) \sigma^2=E(X^2)-E^2(X) σ2=E(X2)−E2(X)
s1 = 0
s2 = 0
count = 0
x = int(input("Please input an integer (finish inputting with '0'): "))
while x != 0:
s1 = s1 + x
s2 = s2 + x**2
count = count + 1
x = int(input("Please input an integer (finish inputting with '0'): "))
e1 = (s1/count)**2
e2 = s2/count
var = e2-e1
print(f"The variance of all the inputs is: {var}")
Triangle of stars
Write a Python program to print out a triangle of stars with length n (100>=n>1) input by user.
Example:
when n = 5
*
**
***
****
*****
n = int(input("Please input an integer n (100>=n>1):"))
for i in range(1,n+1):
print(i*'*')
a = int(input("Please input an integer n (100>=n>1):"))
i = 0
while i <= a:
i = i + 1
print("*"*i)
Diamond of stars
Write a Python program to print out a diamond of stars with length n (n is odd and 100>n>1) input by user.
Example:
when n = 9
*
***
*****
*******
*********
*******
*****
***
*
a = int(input("Please input an odd number between (1,100):"))
i = 1
while i <= a:
k = "*"*i
i = i + 2
print(k.center(a))
b = i
while b >=1:
b = b - 2
k = "*"*b
print(k.center(a))
#Q8
i = int(input("Please input an odd number between (1,100): "))
for j in range(1,i+1):
if j % 2 == 1:
print(" "*int((i-j)/2),"*"*j," "*int((i-j)/2))
for j in range(i-1,0,-1):
if j % 2 == 1:
print(" "*int((i-j)/2),"*"*j," "*int((i-j)/2))
#Q8
i = int(input("Please input an odd number between (1,100): "))
for j in range(1,i+1,2):
print(" "*int((i-j)/2),"*"*j)
for j in range(i-2,0,-2):
print(" "*int((i-j)/2),"*"*j)