day002 pycharm和编码介绍
一、pycharm相关
pycharm是比较好用的python IDE,推荐下
下载地址(附安装、破解方法):JetBrains PyCharm Professional 2017.3.3破解版
二、格式化输出
1 name = input('请输入姓名:') 2 age = input('请输入年龄:') 3 job = input('请输入工作:') 4 hobby = input('你的爱好:') 5 6 msg = '''------------ info of %s ----------- 7 Name : %s 8 Age : %d 9 job : %s 10 Hobbie: %s 11 ------------- end -----------------''' %(name,name,int(age),job,hobby) 12 13 print(msg)
三、编码简介
主要介绍Byte、ASCII、UNICODE、UTF-8和GBK的知识。(ACSII--->UNICODE(GBK)--->UTF-8)
计算机最初是用来进行数学运算的,它首先要解决数字怎么表示,在计算机硬件的组成基本是电路。电路只有2种状态(带电、不带电)。那么,数字怎么表示?科学家是聪明的,00000000代表8个电位,00000001表示1,00000010表示2,00000110表示6....

二进制中的一个数字是一个位(bit),8个位是一个字节(Byte)。(常识:一个字母占一个byte,一个汉字占2个字节)
ASCII码是美国人发明的,网上有ASCII码表,涵盖了英文字母大小写,特殊字符,数字。
ascii 只能表示256种可能,太少,
创办了万国码 unicode
16表示一个字符不行,32位表示一个字符。
A 01000001010000010100000101000001
B 01000010010000100100001001000010
我 01000010010000100100001001000010
Unicode 升级 utf-8 utf-16 utf-32
8位 = 1字节bytes
utf-8 一个字符最少用8位去表示,英文用8位 一个字节
欧洲文字用16位去表示 两个字节
中文用24 位去表示 三个字节
utf-16 一个字符最少用16位去表示
gbk 中国人自己发明的,一个中文用两个字节 16位去表示。
四、python中的运算
算术运算、比较运算、赋值运算、逻辑运算
逻辑运算中优先级:()> not > and >or
1 print(2 > 1 and 1 < 4) 2 print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2) 3 # T or T or F 4 #T or F 5 print(3>4 or 4<3 and 1==1) # F 6 print(1 < 2 and 3 < 4 or 1>2) # T 7 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # T 8 print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # F 9 print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F 10 print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
x or y 如 x 为 True 则返回 y;
x and y 如 x 为 True,则返回 y
1 print(1 or 2) # 1 2 print(3 or 2) # 3 3 print(0 or 2) # 2 4 print(0 or 100) # 100 5 print(2 or 100 or 3 or 4) # 2 6 print(0 or 4 and 3 or 2) 7 8 9 print(1 and 2) 10 print(0 and 2) 11 print(2 or 1 < 3) 12 print(3 > 1 or 2 and 2
五、作业
1、计算 1 - 2 + 3 ... -99 中除了88意外所有数的总和
1 i=1 2 sum=0 3 4 5 while i <100: 6 7 if i==88: 8 i += 1 9 continue 10 11 if i%2!=0: 12 sum=sum+i 13 else: 14 sum=sum-i 15 i += 1 16 17 print(sum)
2、⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
1 username = "wangspy" 2 password = "123456" 3 4 i = 3 5 6 while i > 0: 7 zh = input("请输入你的账号:") 8 i -= 1 9 10 if zh == username: 11 mm = input("请输入你的密码:") 12 13 if mm == password: 14 print("验证成功.正在登陆......") 15 print('''恭喜你登陆成功! 16 欢迎用户进入 17 用户名 :%s 18 密码 :%s 19 '''%(zh,mm)) 20 break 21 else: 22 if i == 0: 23 print("你的机会已经没了!game over 下次见!") 24 answer = input('再试试?Y or N') 25 if answer == 'Y': 26 i = 3 27 print("密码错误,请重新输入") 28 print("你还有"+str(i)+"次机会") 29 30 else: 31 print("请输入正确的用户名!") 32 33 if i == 0: 34 print("你的机会已经没了!") 35 answer = input('再试试?Y or N') 36 if answer == 'Y': 37 i = 3 38 print("你还有" + str(i) + "次机会") 39 40 else: 41 42 print('你TM要不要脸')

浙公网安备 33010602011771号