首页 / PYTHON / python_day02_练习
python_day02_练习
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python_day02_练习,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8379字,纯文字阅读大概需要12分钟。
内容图文
选择
布尔类型、数值和表达式
- 注意:比较运算符的相等是两个等号,一个等到代表赋值
- 在Python中可以用整型0来代表False,其他数字来代表True
- 后面还会讲到 is 在判断语句中的用发
[[[41.86550961 15.53771491 17.69573087] [47.04474791 22.01176278 21.58015959] [52.22398621 28.48581066 25.46458832] ... [95.3843054 93.65789263 63.01406601] [95.3843054 93.65789263 63.01406601] [94.52109902 92.79468625 61.28765324]] [[42.2971128 15.9693181 18.12733406] [47.04474791 22.01176278 21.58015959] [52.22398621 28.48581066 25.46458832] ... [95.3843054 93.65789263 63.01406601] [95.3843054 93.65789263 63.01406601] [94.52109902 92.79468625 61.28765324]] [[42.72871599 16.83252448 17.69573087] [47.4763511 22.44336598 22.01176278] [52.65558941 28.91741385 25.89619151] ... [95.3843054 93.65789263 63.01406601] [95.3843054 93.65789263 63.01406601] [94.95270221 93.22628944 62.58246282]] ... [[65.60368516 67.33009793 56.54001813] [71.21452666 73.37254261 63.4456692 ] [79.4149873 83.29941603 75.09895538] ... [28.91741385 15.10611171 24.16977874] [29.34901705 15.9693181 23.73817555] [29.78062024 18.99054044 28.05420747]] [[67.33009793 69.0565107 58.2664309 ] [72.94093942 75.09895538 65.60368516] [81.14140007 85.02582879 76.82536815] ... [30.64382662 16.83252448 25.89619151] [29.34901705 15.53771491 24.60138194] [28.91741385 18.12733406 27.62260428]] [[56.54001813 58.2664309 52.22398621] [66.03528835 70.78292346 63.87727239] [80.27819368 87.61544795 79.4149873 ] ... [33.66504896 22.87496917 31.07542981] [30.64382662 19.85374683 28.91741385] [31.50703301 22.01176278 27.62260428]]]Out[64]:
<matplotlib.image.AxesImage at 0x111f5bcf8>? ?
EP:
- 产生两个随机整数number1和number2,然后显示给用户,使用户输入数字的和,并判定其是否正确
- 进阶:写一个随机序号点名程序
number_1 = random.randrange(0,10)
number_2 = random.randrange(0,10)?
while 1:
sum_ = eval(input('>>'))
if sum_ == (number_1 + number_2):
print('Congratulations! Correct~')
else:
print('Sorry~SB.')? . . .
if语句
-
如果条件正确就执行一个单向if语句,亦即当条件为真的时候才执行if内部的语句
-
Python有很多选择语句:
- 单向if
- 双向if-else
- 嵌套if
- 多向if-elif-else
-
注意:当语句含有子语句的时候,那么一定至少要有一个缩进,也就是说如果有儿子存在,那么一定要缩进
-
切记不可tab键和space混用,单用tab 或者 space
-
当你输出的结果是无论if是否为真时都需要显示时,语句应该与if对齐
input_ = eval(input('>>'))
if input_ > number:
print('太大啦')
if input_ < number:
print('太小啦')
if number == input_:
print('正好')
print('不要灰心')
>>1 正好 不要灰心
李文浩相亲测试树
年龄
老 年轻
拜拜
帅 否 是 考虑一下 老婆 没有 有 马上结婚 回家的诱惑
代码写不出来的立马分手,从此社会上有多出一个渣男/渣女.
In?[4]: ?age = input('年轻嘛[y/n]')
if age == 'y':
handsome = input('帅否[y/n]')
if handsome == 'y':
wife = input('有没有老婆[y/n]')
if wife == 'y':
print('回家的诱惑')
else:
print('立马结婚')
else:
print('考虑一下')
else:
print('拜拜~')?
年轻嘛[y/n]y 帅否[y/n]y 有没有老婆[y/n]y 回家的诱惑?
EP:
- 用户输入一个数字,判断其实奇数还是偶数
- 进阶:可以查看下4.5实例研究猜生日
双向if-else 语句
- 如果条件为真,那么走if内部语句,否则走else内部语句
EP:
- 产生两个随机整数number1和number2,然后显示给用户,使用户输入数字,并判定其是否正确,如果正确打印“you‘re correct”,否则打印正确错误
嵌套if 和多向if-elif-else
In?[?]: ?if score >= 80:
gread = 'B'
elif score>=90:
gread = 'A'
In?[9]:
tizhong = eval(input('体重'))
shengao = eval(input('身高'))
BMI = tizhong / shengao ** 2
if BMI<18.5 :
print('超清')
elif 18.5<=BMI<25 :
print('标准')
elif 25<=BMI<30 :
print('超重')
else:
print('超级无敌胖')
体重500 身高10000 超清
逻辑运算符
EP:
- 判定闰年:一个年份如果能被4整除但不能被100整除,或者能被400整除,那么这个年份就是闰年
- 提示用户输入一个年份,并返回是否是闰年
- 提示用户输入一个数字,判断其是否为水仙花数
实例研究:彩票
In?[16]: ?
import random? In?[25]: ?
number = random.randint(10,99)
print(number)
N = input('>>')
number_shi = number // 10
number_ge = number % 10
?
if N[0] == '0':
N_shi = 0
else:
N_shi = int(N) // 10
N_ge = int(N) % 10
?
if number == int(N):
print('10000')
# elif (number_shi == N_shi or number_shi==N_ge) and (number_ge == N_shi or number_ge==N_ge):
elif number_shi + number_ge == N_shi + N_ge:
print('3000')
elif (number_ge ==N_ge or number_ge == N_shi) or (number_shi == N_ge or number_shi == N_shi):
print('1000')
24 >>40 1000In?[3]: ?
a = "05"
a[0]? ? Out[3]:
'o'In?[22]: ? ?
05 // 10? ? Out[22]:
'J'In?[14]: ? ?
Number = eval(input('>>'))?
bai = Number // 100
shi = Number // 10 % 10
ge = Number % 10
?
if bai**3 + shi **3 + ge **3 == Number:
print('水仙花')
else:
print('不是水仙花'
>>123 不是水仙花In?[15]: ? ?
223 // 10? Out[15]:
2
Homework
In?[3]: ?import math
a,b,c = map(float,input('Enter a, b, c :').split(','))
?
if b **2 - 4 *a * c > 0:
r1 = (-b + math.sqrt (b **2 - 4 * a *c) ) /(2 *a)
r2 = (-b - math.sqrt ( b **2 - 4 * a *c )) /(2 *a)
print('The roots are %f and The roots are %f' % (r1, r2))
elif b **2 - 4 *a * c == 0:
r3 = (-b) /(2 *a)
print('r3:%d' % r3)
else:
print('The equation has no real roots')
Enter a, b, c :1,3,1 The roots are -0.381966 and The roots are -2.618034?
import random
?
a =random.randrange(0,99)
b =random.randrange(0,99)
print(a,b)
?
c = int(input('请输入两个数的和:'))
if c == a + b :
print('程序为真')
else:
print('程序为假')? ? ?
11 28 请输入两个数的和:39 程序为真?
- 3
today = int(input("Enter today's day:"))
future = int(input("Enter the number of days elapsed since today:"))
week = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
futureweek = today + future
a = futureweek % 7
print("Today is %s and the future day is %s"%(week[today],week[a]))?
Enter today's day:1 Enter the number of days elapsed since today:31 Today is Monday and the future day is Thursday
- 4
In?[5]:
a,b,c = map(int,input('Enter a, b, c :').split(','))
if a < b < c:
print(a,b,c)
elif a < c < b:
print(a,c,b)
elif b< a< c:
print(b,a,c)
elif b < c < a:
print(b,c,a)
elif c < a < b:
print(c,a,b)
else:
print(c,b,a)?
Enter a, b, c :2,7,1 1 2 7
- 5
a,b = map(float,input('Enter weight and price 1:').split(','))
c,d = map(float,input('nter weight and price 2 :').split(','))
e =b / a
f = d / c
print(e,f)
if e > f:
print('package 2 has better price')
else:
print('package 1 has better price')
Enter weight and price 1:50,24.59 nter weight and price 2 :25,11.99 0.4918 0.4796 package 2 has better price?
- 6
a,b = map(float,input('请输入年和月份: ').split(','))
if a % 4 == 0 and a % 100 != 0 or a % 400 == 0:
c = 29
else:
c = 28
if b == 1 or b == 3 or b == 5 or b == 7 or b == 8 or b == 10 or b == 12:
d = 30
elif b == 2:
d = c
else:
d = 31
print( a,'年', b,'月份有', d,'天')?
请输入年和月份: 2005,3 2005.0 年 3.0 月份有 30 天?
- 7
import numpy as np
res1 = np.random.choice(['正面','反面'])
print(res1)
res2 = input('请输入“正面,反面”')
if res1 == res2:
print('猜测正确')
else:
print('猜测错误')
反面 请输入“正面,反面”反面 猜测正确?
- 8
import numpy as np ?
res1 = np.random.choice(['0','1','2'])?
res2 = input('请输入“0,1,2”')?
print('scissor ({}) ,rock ({}) '.format(res1,res2))?
if res1 == '0' and res2 == '1':?
print('res1 胜')?
elif res1 == '1' and res2 == '0':?
print('res2 胜')?
elif res1 == '0' and res2 == '2':?
print('res1 胜') ?
elif res1 == '2' and res2 == '0':?
print('res2 胜') ?
elif es1 == '1' and res2 == '2':?
print('res1 胜')?
elif es1 == '2' and res2 == '1':?
print('res2 胜') ?
else:?
print('平局')
请输入“0,1,2”1 scissor (0) ,rock (1) res1 胜?
- 9
year = int(input('Enter year:(e.g.,2008):'))
m = int(input('Enter month:1-12:'))
q = int(input('Enter the day of the month:1-31:'))
week = ['Sunday','Monday','Thursday','Wednesday','Thursday','Friday','Saturday']
if m == 1 or m == 2:
year= year - 1
m = m+12
k = year % 100
j = year / 100
h = int(q + (26 * (m+1))/10 + k +(k/4) + (j/4) + (5*j)) % 7
print('Day of the week is %s'%(week[h]))
else:
m = m+12
k = year % 100
j = year / 100
h = int(q + (26 * (m+1))/10 + k +(k/4) + (j/4) + (5*j)) % 7
print('Day of the week is %s'%(week[h]))?
Enter year:(e.g.,2008):2012 Enter month:1-12:5 Enter the day of the month:1-31:12 12 20.12 4 Day of the week is Wednesday?
- 10
import numpy as np ?
res1 = np.random.choice(['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King'])
res2 = np.random.choice(['梅花''红桃','方片','黑桃'])
print('The card you picked is the ',res1,'of',res2)
The card you picked is the King of 方片?
- 11
shu = int(input('请输入一个三位数整数:'))
a = shu // 100
b = shu % 10
if a == b:
print(shu,'是一个回文数')
else:
print(shu,'不是一个回文数')
请输入一个三位数整数:121 121 是一个回文数
- 12
-
import math?
a,b,c= map(float,input('Enter three edges:').split(','))
if a + b > c and a + c > b and b + c > a:
print('The perimeter is %d' % (a + b + c))
p = (a + b + c) / 2
else:
print('不能构成三角形')?
Enter three edges:1,1,1 The perimeter is 3? ? ? ? ? ?
?? ?
内容总结
以上是互联网集市为您收集整理的python_day02_练习全部内容,希望文章能够帮你解决python_day02_练习所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。