Loop:Data Processing and Visulisation with Python (Python Exercise 6)

Data Processing and Visulisation with Python

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)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值