首页 / PYTHON / python day 2
python day 2
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python day 2,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5224字,纯文字阅读大概需要8分钟。
内容图文
一、昨日内容回顾(自己检测)
二、pycharm 的安装(大量讲解视频,不多说了)
三、作业讲解:
1、我的作业:
# 1、使用while循环 输入1 2 3 4 5 6 8 9 10 count = 1 while count < 10 : if count == 7: print(' ') else:print(count) count += 1 # 2、求 1——100 所有数的和 sum = 1 count = 1 while count <= 100: count += 1 if count > 100: break sum = sum + count print(sum) count = 1 sum = 0 while count <= 100: sum = sum + count count += 1 print(sum) # 3、输出 1——100 内的所有奇数 count = 1 while count < 100: if count%2 == 1: print(count) count += 1 # 4、输出 1——100 内的所有偶数 count = 1 while count < 100: if count%2 == 0: print(count) count += 1 # 5、求 1-2+3-4+5 ... ... +99 的结果 count = 1 sum = 0 while count <= 100: if count%2 == 1: sum = sum + count else:sum = sum - count count += 1 print(sum) # 6、用户登录三次机会重试''' name = input('请输入你的账户:') password = input('请输入你的密码:') x = 3 if name == '小亮': if password == '123456': print('请稍后... ...') else:print('密码错误,你还有'+ x +'次机会,请重新输入密码') else:print('用户名不存在,请重新输入')
2、教师讲解:
# 1、使用while循环 输入1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 # count = count + 1 if count == 7: print (' ') else:print(count) count = 0 while count < 10: count += 1 # count = count + 1 if count == 7: continue print(count) count = 0 while count < 10: count += 1 # count = count + 1 if count == 7: pass else:print(count) # 2、求 1——100 所有数的和 (已讲) count = 1 sum = 0 while count <= 100: sum = sum + count count += 1 print(sum) # 3、输出 1——100 内的所有奇数 # 首先要先输出1——100 count = 1 while count <100: print(count) count += 2 count = 1 while count <100: if count%2 == 1: print(count) count += 1 # 4、输出 1——100 内的所有奇数 模仿3即可 # 5、求 1-2+3-4+5 ... ... +99 的结果 sum = 0 count = 1 while count < 100: if count%2 == 0: sum = sum - count else:sum = sum + count count += 1 print (sum) # 6、用户登录三次机会重试 # 用户交互 用input 心中有账号,密码。三次机会重试,需要while i = 0 while i < 3: username = input('请输入你的用户名:') password = int(input('请输入你的密码:')) if username == '亮' and password == 123: print('登陆成功') break # 讲解中无这句,自己加的。 else:print('登陆失败,请重新登陆') i += 1
# 三次登陆机会(本期学完后附加)
username = '亮'
password = '123'
i = 0
while i < 3:
name = input('请输入你的用户名:')
pwd = input('请输入你的密码:')
if username == name and password == pwd:
print('登陆成功')
break
else: print('登陆失败,你还有%d次登陆机会'%(2-i))
if (2-i) == 0:
result = input('是否继续尝试?yes')
if result == 'yes':
i = 0
continue
i += 1
else: print('不要脸')
四、格式化输出:
1、% 表示占位
2、s 字符串
3、d 数字
name = input('请输入你的编号:') age = input('请输入你的生产时间:') msg = '我编号是%s,生产时间是%s,进化程度为3%%'%(name,age) # 此处若为一个%,则会报错,第一个% 表示转译,第二个%表示真正的百分号。 print(msg)
name = input('请输入你的姓名:') age = input('请输入你的年龄:') job = input('请输入你的工作:') hobbie = input('请输入你的爱好:') msg = '''---------- info of %s ---------- Name :%s Age :%d Job :%s Hobbie :%s ---------- end ----------'''%(name,name,int(age),job,hobbie) print(msg) # 注意按照顺序来,有几个占位符,就需要几个量 年龄为数字要用int()可在最后加也可在age处加 ''' '''内为模板
五、while 与 else 的关系
count = 0 while count <= 5: count += 1 if count == 3:break print('loop',count) else: print('循环正常执行') print('------- out of while lop -------') count = 0 while count <= 5: count += 1 if count == 3:pass print('loop',count) else: print('循环正常执行') print('------- out of while lop -------') # while循环被break打断,不执行else,while循环被pass,则else正常执行。
六、初始编码
电脑的传输,还有储蓄实际上都是010101010101
美国:ascii码 8位,2**8 美国字母大小写及特殊符号用 2**7(128个)即可表示,为了发展多加了一位,变为2**8
中文:九万多字
为了解决全球化的问题,美国创建了一个万国码,Unicode
最开始:
一个字节8位,表示所有的英文,特殊字符,数字等,
两个字节16位,表示一个中文,不够,Unicode出现,四个字节32位表示一个中文
比如:一 00000000 00000000 00000000 00000001 太浪费空间了,
升级版:utf-8 三个字节表示一个中文
gbk 中国人自己创建的,国内使用,一个中文用两个字节表示,用的很少。
中文:九万多字,
00000001 8位 bit == 1个字节(byte) 1024 byte == 1 KB 1024 KB == 1 MB 1024MB == 1 GB 1 GB == 1TB
七、运算符
1、基本运算符:百度一下你就知道
2、and or not
# 优先级的顺序 () > not > and > or print (2>1 and 1<4 or 2<3 and 9>6 or 2<4 and 3<2) # T or T or F # T or F # T
练习1:
# 逻辑运算符前后都是大于或小于有比较的时候返回 True False
print(3>4 or 4<3 and 1==1) # F print(1<2 and 3<4 or 1>2 ) # T print(2>1 and 3<4 or 4>5 and 2<1) # T print(1>2 and 3<4 or 4<5 and 2>1 or 2<1) # T print(1>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) # F print(not 2>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) # F print(not 2>1) 为False
练习2:
# int ----> bool 非零转换为bool True 0转换为bool 是 False # 1、 int --> bool print(bool(2)) # T print(bool(-2)) # T print(bool(0)) # F # 2、bool ——> int print(int(True)) # 1 print(int(False)) # 0
# x or y x为真,则返回x 逻辑运算符前后都是数字返回的就是数字
print(2 or 1) # 2
print(1 or 2) # 1
print(0 or 1) # 1
# x and y x为真,则返回y
print(1 and 2) # 2
print(0 and 2) # 0
练习3:
print(0 or 2 or 3 or 4 or 5) #2 print(0 or 4 and 3 or 2) # 3 print(1 > 2 and 3 or 4 and 3 < 2) # 1>2 or 3<2 F print(2 or 1 < 3 and 3) # 2
内容总结
以上是互联网集市为您收集整理的python day 2全部内容,希望文章能够帮你解决python day 2所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。