%: 占位符python
s: 字符串ide
d: 数字学习
%%: 表示一个%, 第一个%是用来转义编码
实例:spa
name = input('姓名:') age = int(input('年龄:')) print('我叫%s, 个人年龄:%d,个人学习进度3%%.' %(name, age)) # 执行结果: # 姓名:hkey # 年龄:20 # 我叫hkey, 个人年龄:20,个人学习进度3%.
最初的编码是由美国提出,当时只规定了 ASCII码用来存储字母及符号,后来为了解决全球化文字的差别,建立了万国码:unicodecode
在 unicode中,blog
1个字节表示了全部的英文、特殊字符、数字等等;utf-8
一个中文须要 4个字节表示,32位 就很浪费。unicode
后来,从 unicode 升级到 utf-8, UTF-8 是Unicode的实现方式之一字符串
在 utf-8 中,一个文字用 3 个字节来存储。
00000001 8位bit == 1个字节(byte)
1byte 1024byte(字节) == 1KB
1KB 1024KB == 1MB
1MB 1024MB == 1GB
1GB 1024GB == 1TB
判断优先级(重点):() > not > and > or
练习1: 判断下面返回结果 (提示:根据 () > not > and > or 来进行判断)
1,3>4 or 4<3 and 1==1 # 3>4 or False # False 2,1 < 2 and 3 < 4 or 1>2 # True or 1>2 # True 3,2 > 1 and 3 < 4 or 4 > 5 and 2 < 1 # True or False # True 4,1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 # False or False or 9 < 8 # False 5,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 # False or False and 9 > 8 or 7 < 6 # False or False or 7 < 6 # False or False # False 6,not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 # False and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 # False or False and 9 > 8 or 7 < 6 # False or False or 7 < 6 # False or False # False
上面是条件判断,也能够直接进行数字的判断:
x or y x为非零,则返回x, 不然返回 y
print(1 or 2) # 1 print(3 or 2) # 3 print(0 or 2) # 2 print(0 or 100) # 100 当 or 前面的数字不为0的时候,则返回前面的数字; 当 or 前面的数字为0,则返回后面的数字。
x and y x为True,则返回y,与 or 正好相反
print(1 and 2) # 2 print(0 and 2) # 0 当 and 前面的数字非0,则返回后面的数字; 当 and 前面的数字为0,则返回0.
数字和布尔值之间的转换,遵循如下两条规则:
(1)数字转换为 bool值:非零转为bool值为:True;0 转换为bool值为:False
(2)bool值转换为数字:True 为:1; False 为 0
做业题:
1. 使用while循环输入1,2,3,4,5,6 8,9,10
2. 求 1-100 的全部数的和
3. 输出 1-100 的全部奇数
4. 输出 1-100 的全部偶数
5. 1-2+3-4+5 ...99的全部数的和
6. 用户登陆(三次机会重试)
#!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: hkey # 做业题: # 1. 使用while循环输入1,2,3,4,5,6 8,9,10 count = 0 while count < 10: count += 1 # 等价于 count = count + 1 if count == 7: continue # continue 结束本次循环,开始下一次循环,continue 如下的代码都再也不执行 print(count) # 2. 求 1-100 的全部数的和 num = 0 for i in range(1, 101): # range取一个范围 1, 100的全部数字,经过for循环遍历相加 num += i print(num) # 3. 输出 1-100 的全部奇数 print(list(range(1, 101, 2))) # 4. 输出 1-100 的全部偶数 print(list(range(2, 101, 2))) # 5. 1-2+3-4+5 ...99的全部数的和 sum = 0 count = 1 while count < 100: if count % 2: # count 对 2取余若是为 0 则该条件不成立,说明 count 为偶数,count 对 2取余若是不为 0 则该条件成立,说明 count 为奇数 sum += count # 奇数作加法 else: sum -= count # 偶数作减法 count += 1 print(sum) # 总结: # 在bool值中,0 None 空 为 False,其余都为 True # 6. 用户登陆(三次机会重试) count = 0 while True: user = input('username:') pwd = input('password:') if user == 'admin' and pwd == '123': print('登陆成功.') break else: print('用户名密码不正确,请重试。') count += 1 if count == 3: print('登陆验证超过三次,登陆失败.') break