guess=eval(input())
print("猜{}了".format("对" if guess==99 else "错"))
注意表达式的使用,而不是语句的使用
score=eval(input())
if score >=90:
grade="A"
elif score>=80:
grade="B"
elif score>=70:
grade="C"
elif score>=60:
grade="D"
print("输入成绩属于级别{}".format(grade))
操作符 | 数学符号 | 描述 |
---|---|---|
< | < < < | 小于 |
<= | ≤ \leq ≤ | 小于等于 |
>= | ≥ \geq ≥ | 大于等于 |
> | > > > | 大于 |
== | = = = | 等于 |
!= | ≠ \neq = | 不等于 |
用于条件组合的三个保留字
操作符及使用 | 描述 |
---|---|
x and y | 两个条件x与y的逻辑与 |
x or y | 两个条件x和y的逻辑或 |
not x | 条件x的逻辑非 |
guess=eval(input())
if guess>99 or guess<99:
print("猜对了")
else:
print("猜错了")
if not True:
print("语句块2")
else:
print("语句块1")
guess=eval(input())
if not (guess==99):
print("猜对了")
else:
print("猜错了")
#CalBMIv3.py
height,weight=eval(input("请输入身高(米)和体重(公斤)[逗号隔开]:"))
bmi=weight / pow(height,2)
print("BMI数值为:{:.2f}".format(bmi))
who,nat="",""
if bmi<18.5:
who,nat="偏瘦","偏瘦"
elif 18.5<=bmi<24:
who,nat="正常","正常"
elif 24<=bmi<25:
who,nat="正常","偏胖"
elif 25<=bmi<28:
who,nat="偏胖","偏胖"
elif 28<=bmi<30:
who,nat="偏胖","肥胖"
else:
who,nat="肥胖","肥胖"
print("BMI指标为:国际'{0}',国内'{1}'".format(who,nat))
注1:只要遍历结构是由多个元素构成的数据类型,都可以使用for in的方法
遍历某个结构形成的循环运行方式
for <循环变量> in <遍历结构>:
<语句块>
for i in range(N):
<语句块>
>>> for i in range(4):
print(i)
0
1
2
3
>>> for i in range(4):
print("Hello:",i)
Hello: 0
Hello: 1
Hello: 2
Hello: 3
for i in range(M,N,K):
<语句块>
>>> for i in range(1,6,2):
print("Hello:",i)
Hello: 1
Hello: 3
Hello: 5
for c in s:
<语句块>
>>> for c in "Pyhon123":
print(c,end=",")
P,y,h,o,n,1,2,3,
for item in ls:
<语句块>
>>> for item in [123,"PY",456]:
print(item,end=",")
123,PY,456,
for ine in fi:
<语句块>
由条件控制的循环运行方式
while <条件>:
<语句块>
break和continue
>>> for c in "PYTHON":
if c== "T":
continue
print(c,end="")
PYHON
>>> for c in "PYTHON":
if c== "T":
break
print(c,end="")
PY
>>> s="PYTHON"
>>> while s !="":
for c in s:
print(c,end="")
s=s[:-1]
PYTHON PYTHO PYTH PYT PY P
>>> s="PYTHON"
>>> while s !="":
for c in s:
if c=="T":
break
print(c,end="")
s=s[:-1]
PY PY PY PY PY P
#为了方便看清楚s的变化,修改代码如下:
>>> s="PYTHON"
>>> while s !="":
print("s",s)
for c in s:
if c=="T":
break
print(c,end="")
s=s[:-1]
print()
s PYTHON
PY
s PYTHO
PY
s PYTH
PY
s PYT
PY
s PY
PY
s P
P
for <循环变量> in <遍历结构>:
<语句块1>
else:
<语句块2>
while <条件>:
<语句块1>
else:
<语句块2>
>>> for c in "PYTHON":
if c== "T":
continue
print(c,end="")
else:
print("正常退出")
PYHON正常退出
>>> for c in "PYTHON":
if c== "T":
break
print(c,end="")
else:
print("正常退出")
PY
随机数种子
函数 | 描述 |
---|---|
seed(a=None) | 初始化给定的随机数种子,默认为当前系统时间random.seed(10)# 产生种子10对于的序列 |
random() | 生成一个[0.0,1.0]之间的随机小数random.random() 0.9803957598667813 |
randint(a,b) | 生成一个[a,b]之间的整数>>> random.randint(10,100) 64 |
randrange(m,n,[,k]) | 生成一个[m,n]之间以k为步长的随机整数>>> random.randint(10,100,10) 80 |
getrandbits(k) | 生成一个k比特长的随机整数>>> random.getrandbits(16) 37885 |
uniform(a,b) | 生成一个[a,b]之间的随机小数>>> random.uniform(10,100) 62.02821710210233 |
choice(seq) | 从序列seq中随机选择一个元素>>> random.choice([1,2,3,4,5,6,7,8,9]) 4 |
shuffle(seq) | 将序列seq中元素随机排列,返回打乱后的序列>>> s=[1,2,3,4,5,6,7,8,9];random.shuffle(s);print(s) [4, 5, 6, 1, 2, 3, 7, 9, 8] |
#CalPiV2.py
from random import random
from time import perf_counter
DARTS=1000*1000
hits=0.0
start=perf_counter()
for i in range(1,DARTS+1):
x,y=random(),random()
dist=pow(x**2+y**2,0.5)
if dist<=1.0:
hits=hits+1
pi=4*(hits/DARTS)
print("圆周率值是:{}".format(pi))
print("运行时间是:{:.5f}s".format(perf_counter()-start))