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

Data Processing and Visulisation with Python

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)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值