百万年薪python之路 -- day02

day02

1.while循环 -- while关键字

while 空格 条件 冒号

缩进 循环体

while 5>4:
    print("Hello World!")

数字中非0的都是True

# 正序25~57![](https://img2018.cnblogs.com/blog/1617228/201907/1617228-20190705192654502-2028519452.png)

# count = 25
# while count <= 57:
#     print(count)
#     count += 1

# 倒叙57~25
# count = 57
# while count >= 25:
#     print(count)
#     count -= 1

break 终止当前循环,break下面的循环体代码不执行

continue 跳过本次循环,继续下一次循环(下面的代码不执行) #continue伪装成循环体中的最后一行代码

break和continue的相同之处:

​ 下面的循环体代码不执行

条件可以控制while循环

打断循环的方式:

​ 1.自己修改条件

​ 2.break

题目:

扫描二维码关注公众号,回复: 6758529 查看本文章
# num = int(input("请输入数字:"))
# while num == 1:
#     user = input("请输入用户名:")
#     pwd = input("请输入密码:")
#     if user == "zcy" and pwd == "123":
#         print("登陆成功!")
#         break
#     else:
#         print("用户名或密码错误!")
# else:
#     print("退出成功!")

while else: while 条件成立的时候就不执行了,条件不成立的时候就执行else

2.格式化输出

%s(字符串类型) %d(数字类型)

按位置顺序传递,占位和补位必须要一一对应

如果要在字符串中输出%时,用%%转义

name = input("姓名:")
age = input("年龄:")
msg = '姓名:%s,年龄:%d'%(name,int(age))
print(msg)

​ % -- 占位

​ %s -- 占字符串的位

​ %d -- 占数字位

​ %% -- 转义成普通的%

.format

name = input("姓名:")
age = input("年龄:")
msg = '姓名:{},年龄:{}'.format(name,int(age))
print(msg)


name = input("姓名:")
age = input("年龄:")
msg = '姓名:{1},年龄:{0}'.format(int(age),name)
print(msg)

f'字符串1{},字符串2{}' (python 3.6以上)

name = input("姓名:")
age = input("年龄:")
msg = f'姓名{name},年龄{age}'
print(msg)

3.运算符

算术运算符

​ + 加

​ - 减

​ * 乘

​ / python2获取的是整数 python3获取的是浮点数

​ //(整除--地板除)

​ ** 幂(次方)

​ % 模(取余)

比较运算符

​ > 大于

​ < 小于

​ == 等于

​ != 不等于

​ >= 大于等于

​ <= 小于等于

赋值运算符

​ = 赋值

​ += 自加

​ -= 自减

​ *= 自乘

​ /= 自除

​ //= 自地板除

​ **= 自幂

​ %= 自余

逻辑运算符

​ and 与

​ or 或

​ not 非

and 都为真的时候取and后面的值

​ print( 3 and 4) #4

and 都为假的时候取and前面的值(前面是假的时,and后面的不用判断)

​ print( 0 and False) #0

and 一真一假取假的(前面是假的时,and后面的不用判断)

​ print( 0 and 4) #0

​ print( 4 and 0) #0

or 都为真的时候取or前面的值(前面是真的时,or后面的不用判断)

​ print( 3 or 4) #3

or 都为假的时候取or后面的值

​ print( 0 or False) #False

or一真一假取真的(前面是真的时,or后面的不用判断)

​ print( 0 or 4) #4

​ print( 4 or 0) #4

优先级:

()> not > and > or

成员运算符

​ in 存在

​ not in 不存在

4.编码初始(编码集)

  • ​ Ascii(美国) 不支持中文

  • ​ GBK(国标,也称GBK2312) 英文 8位(1Bytes) 中文 16位(2Bytes)

  • ​ Unicode(万国码) 英文16位(2Bytes) 中文32位(4Bytes)

  • ​ UTF-8(可变长编码) 英文8位(1Bytes) 欧洲文16位(2Bytes) 亚洲(24位)(3Bytes)

  • ​ Linux -- UTF-8

  • ​ Mac -- UTF-8

  • ​ Windows --GBK

  • 单位转换:
    • ​ 1Bytes = 8bit
    • ​ 1KB = 1024Bytes
    • ​ 1MB = 1024KB
    • ​ 1GB = 1024MB
    • ​ 1TB = 1024GB
    • ​ 1PB = 1024TB

其他知识点

python print(a,b,c,d,sep = "\n") #sep = "\n" 换行

猜你喜欢

转载自www.cnblogs.com/zhangchaoyin/p/11140421.html