num1=11 num2=11.3456 print("num1's width is limited 5,the result is:%5d"%num1) #小于数字宽度不会生效 print("num1's width is limited 1,the result is:%1d"%num1) #精度控制,进行四舍五入 print("num2's width is limited 5 and the precision is limited 2,the result is:%7.2f"%num2)
输出
1 2 3
num1's width is limited 5,the result is: 11 num1's width is limited 1,the result is:11 num2's width is limited 5 and the precision is limited 2,the result is: 11.35
快速格式化:f{}
1 2 3
num1=11 num2=11.3456 print(f"{num1},{num2}")
输出
1
11,11.3456
11.字符串格式化小练习
1 2 3 4 5 6 7
name="A" stock_price=19.99 stock_code="123456" stock_price_daily_growth_factor=1.2 growth_days=7 print(f"company is {name},stock code is {stock_code},stock price is {stock_price}") print("daily growth factor is %.2f,after %d days of growth,the stock price is %.2f"%(stock_price_daily_growth_factor,growth_days,(stock_price*stock_price_daily_growth_factor**growth_days)))
12.数据输入:input() 输入的一切均为字符串,需要进行符号转换
1 2
name=input("What's your name?\n") print("you are "+name)
输出
1 2 3
What's your name? Zhao you are Zhao
二、判断语句
1.if elif else
1 2 3 4 5 6 7 8 9 10
age=18 if age>=18: print("I am 18 years old") print("I will go to college") elif age>=10: print("I am 10 years old") print("I will go to high school") else: print("you are younger than 12 years old") print("How time flies")
import random num=random.randint(1,10) guess=int(input("Please guess a number:")) if guess==num: print("You are right") elif guess>num: guess=int(input("Please guess a smaller number:")) if guess==num: print("You are right") elif guess>num: guess=int(input("Please guess a smaller number:")) if guess==num: print("You are right") else: print("The correct number is %d"%num) elif guess<num: guess=int(input("Please guess a bigger number:")) if guess==num: print("You are right") else: print("The correct number is %d"%num) elif guess<num: guess=int(input("Please guess a bigger number:")) if guess==num: print("You are right") elif guess>num: guess=int(input("Please guess a smaller number:")) if guess==num: print("You are right") else: print("The correct number is %d"%num) elif guess<num: guess=int(input("Please guess a bigger number:")) if guess==num: print("You are right") else: print("The correct number is %d"%num)
三、循环语句
1.while语句
1 2 3 4 5 6 7 8 9 10 11
import random num=random.randint(1,100) guess=int(input("Please enter a number:")) while1: if guess>num: guess=int(input("Please enter a smaller number:")) elif guess<num: guess=int(input("Please enter a bigger number:")) else: print("Correct!") break
1 2 3 4 5 6 7 8 9
m=1 n=1 while n<=9: m=1 while m<=n: print("%d*%d=%d"%(m,n,m*n),end="\t") m+=1 print("") n+=1
2.for语句
for 临时变量 in 序列类型
练习:数一数输入的字符串有几个a
1 2 3 4 5 6
name=input("Please enter a string:") cnt=0 for x in name: if x=='a': cnt+=1 print(name+" has "+str(cnt)+" letter a")
for x inrange(10): print(x,end=" ") print("") for x inrange(5,10): print(x,end=" ") print("") for x inrange(0,10,2): print(x,end=" ")
三者输出如下:
1 2 3
0 1 2 3 4 5 6 7 8 9 5 6 7 8 9 0 2 4 6 8
3.循环语句综合练习
1 2 3 4 5 6 7 8 9 10 11
import random salary=10000 for x inrange(19): if salary==0: break score=random.randint(1,10) if score>=5: salary-=1000 print("Employee %d get his salary:1000 and his score is %d,total salary remain %d"%(x+1,score,salary)) else: print("Employee %d's score is %d,less than 5"%(x+1,score))
list=[1,2,3,4,5,6,7,8,9,10] index=0 while index<len(list): if(list[index]%2==0): print(list[index],end=" ") index+=1 print("") for ele inlist: if(ele%2==0): print(ele,end=" ")