Python学习日记 --day2
1、格式化输出:% s d (%为占位符 s为字符串类型 d为数字类型)
name = input('请输入姓名') age = int(input('请输入年龄')) height = input('请输入身高') job = input('请输入工作') hobbie = input('我的爱好是') msg = '''-------info of %s -------- 姓名: %s 年龄: %d 身高: %s 工作: %s 爱好: %s ---------end-------- ''' % (name,name,int(age),height,job,hobbie) print(msg) name = input('请输入姓名') age = input('请输入年龄') height = input('请输入身高') msg = '我叫%s,今年%s,身高%s 学习进度3%%' %(name,age,height) #在格式化输出需要单纯的表示%时 需要%来转译 第一个%表示转译 输出的是第二个 print(msg)
2、while else:while循环下套else
# 当循环被Break打断,就不会执行else的结果。 count = 0 while count <= 5: count += 1 if count == 3: continue print(count) else: print('执行完毕') print('while else 循环')
3、初始编码:
电脑的传输,还有储存实际上都是二进制(0和1).
美国:使用ASCII码。为了解决全球化的文字问题,创建了万国码—Unicode
最开始:
1个字节 表示所有英文,数字,特殊字符等等
2个字节 16位表示一个中文 不够,Unicode 一个中文用4个字节表示,32位
eg: 你 00000000 00000000 00000000 00000001
中文一共九万多字。
升级版:utf-8 一个中文 3个字节去表示。
gbk:中国人自己的编码方式,只限国内使用 一个中文用2个字节。
4、逻辑运算:not and or
# 逻辑运算 # 优先级 ()> not > and > or print(2 > 1 and 3 > 2) print(2 > 1 or 3 < 4 and 2 > 1 or 1 > 0 ) print(not 2 < 1 or 3 < 4 and 2 > 1 or 1 > 0 ) # x or y x为非零 则返回x,x为零则返回y,and与之相反 print(1 or 2) #1 print(0 or 3 or 2 or 1 or 4) #3 print(3 and 4) #4 print(2 and 0) #2 print(0 and 2) #0 # int 转换成bool 非零转换成bool为True 0转换成bool为False print(bool(2)) print(bool(0)) print(bool(-1)) # # bool 转换成int True为1 False为0 print(int(True)) print(int(False)) print(1 or 2 and 3 < 4 or 3) #1 print(1 > 2 or 2 and 3 or 3) #0 print(0 or 3 > 2) #True print(1 or 3 > 2) #1 print(3 > 2 or 3) #True print(3 < 2 or 3) #3 print(3 < 2 and 3) #False print(3 > 2 and 3) #3 print(3 > 2 and 0) #0
代码练习:
# 计算1-2+3-4+5.。。。。+99中除了88之外的所有数的和 # i = 1 # sum = 0 # while i < 100: # if i == 88: # i += 1 # continue # if i %2 == 0: #取余 余数为0则为偶数 # sum = sum - i #偶数相减 # else: # sum = sum + i #奇数相加 # i += 1 # print(sum) # 用户登录(三次输错机会)且每次输入错误时显示剩余输入次数 i = 1 while i <= 3: username = input('请输入账号') password = input('请输入密码') if username == 'admin' and password == '123': print('登录成功,欢迎%s登录。'%(username)) break if 3 - i == 0: print('你的次数已到,再见') answer = input('再试试? Y') if answer == 'Y': i = 0 else:print('你的次数已到,再见') else: print('登录失败,你还有%s次机会' %(3-i)) i += 1 else:print('大哥 别试了,你好像傻')