day02:Python基础一(2)
1.昨日内容回顾
编译型:一次性将全部代码编译成二进制文件
c,c++
优点:运行效率高
缺点:开发速度慢,不能跨平台。
解释型:当程序运行时,从上至下一行一行的解释成二进制。
优点:开发速度快,效率高,可以跨平台。
缺点:运行效率低。
Python2 和 Python3 宏观上的区别
Python2源码重复率高,不规范,而Python崇尚的是简单优美
在Python2 首行:#-*-encoding:utf-8 -*- 解决Python2 中文的问题
变量: 由数字字母下划线任意组合,且不能以数字开头,不能用Python中的关键字,
不能用中文,不能用拼音。
常量:约定俗成 不可更改,全部是大写字母。
注释: 单行注释 :# 多行注释:''' ''' """ """
用户交互input:数据类型全部都是str
基础数据类型:bool True False
int 运算:+ - * / % // **
str: 加引号的就是str,可以与数字相乘
if 条件:
结果
if 条件:
结果
else:
结果
if 条件:
结果
elif 条件:
结果
elif 条件:
结果
......
else: 结果
if 条件:
if 条件:结果
if ...
else :结果
while 条件:
结果
结束循环:改变条件,或者break强制退出循环,continue:结束本次循环,继续下次循环。
pycharm使用:创建文件夹的路径不要有中文。
注释:Ctrl+?
复制到下一行:Ctrl+d
找bug:debug
2.作业讲解
(1)使用while循环输入1~6,8~10
i = 0 while i < 10: i += 1 if i == 7: print(' ') else: print(i) i = 0 while i < 10: i += 1 if i == 7: continue print(i) i = 0 while i < 10: i += 1 if i == 7: pass else: print(i)
(2)求1~100的和
i = 1 count = 0 while i <= 100: count += i i += 1 print(count)
(3)输出1~100中的奇数
i = 1 while i <= 100: print(i) i += 2 i = 1 while i <= 100: if i % 2 == 1: print(i) i += 1
(4)输出1~100中的偶数
i = 1 while i <= 100: if i % 2 == 0: print(i) i += 1
(5)求1-2+3-4...100的和
count = 0 i = 1 while i < 100: if i % 2 == 0: count = count - i else: count += i i += 1 print(count)
(6)用户登录三次机会重试 input 心中要有账户密码 while循环
i = 0 while i < 3: username = input('请输入账户:') password = int(input('请输入密码:')) if username == '小小' and password == 123: print('登录成功') else: print('用户名或密码错误,请重新登录') j = 2 - i print('您还有', j, '次机会') i += 1
3.pycharm下载安装
4.格式化输出
name1 = input('请输入姓名') age1 = input('请输入年龄') sex1 = input('请输入性别') msg = '我是%s,今年%s,性别%s' %(name1, age1, sex1) print(msg) Name = input('请输入姓名:') Age = int(input('请输入年龄:')) Job = input('请输入工作:') Hobbie = input('请输入爱好:') msg = '''----info of %s---- Name :%s Age :%d Job :%s Hobbie :%s --------end-------''' %(Name,Name,Age,Job,Hobbie) print(msg) # 格式化输出中若含有%,就再加一个% Name = input('请输入姓名:') Age = int(input('请输入年龄:')) Sex = input('请输入性别:') msg = '我是%s,今年%s,性别%s 学习进度3%%' %(Name, Age, Sex) print(msg)
5.while else
当while循环被break打断,就不会执行else的结果
count = 0 while count <= 5: count += 1 if count == 3:break print("loop", count) else: print("循环正常执行完啦") print("----out of while loop----") # count = 0 while count <= 5: count += 1 if count == 3:pass print("loop", count) else: print("循环正常执行完啦") print("----out of while loop----")
6、初始编码
电脑的传输还有储存实际上都是01010101001
8位bit == 1个字节(byte)
1024byte(字节) == 1kb
1024kb == 1MB
1024MB == 1GB
1024GB == 1T
ASCII码都是八位,最左边的是0
ASCII码(美国创建) 为了解决这个全球化的文字问题,创建了一个万国码Unicode
Unicode(最开始):
1个字节就能表示所有的英文,特殊字符,数字等等
2个字节:16位表示一个中文。中文9万多字,不够。
4个字节表示一个中文 4个字节太多了
升级版 utf-8
用三个字节表示一个中文
gbk国内使用,一个中文用2个字节
7.运算符
# and or not 优先级:()>not > and > or print(2 > 1 and 1 < 4) #False 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 print(3 > 4 or 4 < 3 and 1 == 1) # false print(1 < 2 and 3 < 4 or 1 > 2) # true print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # true print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # false print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # false print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # false # x or y 当x为非0,则返回x print(1 or 2) # 1 print(0 or 2) # 2 print(3 or 2) # 3 print(0 or 100) # 100 print(2 or 100 or 3 or 4) # 2 print(0 or 4 and 3 or 2) # 0 or 3 or 2 ---> 3 or 2 ---> 3 # x and y 当x为非0,则返回y print(1 and 2) # 2 print(0 and 2) # 0 print(3 and 2) # 2 print(0 and 100) # 0 # or和and混合 print(1 or 1 > 3) # 1 print(0 or 1 > 3) # False print(1 > 3 and 2) # False print(0 or 1 > 3 and 2) # 0 or False ---> False print(1 > 2 and 3 or 4 and 3 < 2) # False or False ---> False # ps int转化成bool:非0转换成bool是TRUE,0转换成bool是False print(bool(2)) print(bool(-2)) print(bool(0)) # bool-->int print(int(True)) # 1 print(int(False)) # 0

浙公网安备 33010602011771号