Day 2 python笔记 1.while循环 2.格式化输出 3.运算符 4.初识编码(一)

while--关键字

while 条件:
循环体

例 while True:
print("痒")
print("鸡你太美")
print("卡路里")

falg = True
while falg:
print(1)

print (bool(0)) print(bool(0))
数字中非零的都是ture

正序
count = 1
whlie count <= 5:
print(count)
count = count + 1

倒序
count == 5
while count:
print(count)
count = count + 1

正序打印从25 - 57
倒序打印从57 - 25

count == 25
while a <= 32:
print(count)
count =count + 1
a = a + 1

break

while 3>2:
​ print(1)
​ break # 终止当前循环,break下方的代码不会执行
​ print(2)
print(4)

continue

while True:
print(123)
print(123)
continue #伪装成循环体中最后一行代码,跳出当前循环继续继续下次循环。
print(123)

while else
while True:
print(111)
else:
print(222)

总结:
打断循环方式:
1.自己修改条件
2.break # 打破当前循环
break # 打破当前循环
continue # 跳出当前循环,伪装成循环的最后一行代码,继续下次循环
相同之处:他们以下的代码都不执行

字符串格式化
s = """ ------- info --------
name:%s
age:%s
job:%s
------------- end ----------
"""
name = input("name")
age = int(input("age"))
job = input("job")
print(s%(name,age,job))
%s 占字符串类型
%d 占数字类型
%% 转义 转换成普通的%号
按照位置顺序占位与补位必须要一一对应

num = input("学习进度")
d ="学习进度为:%s %%"
print(d%(num))

s = f"今天下雨了{input('>>>')}"
print(s)

s = "学习进度为: %s"
print(s%(input("请输入数字")))

s = f"{1}{2}{3}"
print(s)

运算符
算数运算符
+,-,*,/,//(整除---地板除)
"""python3 / 5/2=2.5 浮点数
python2   5\2=2""" 整数
幂(次方)
print(3
2) # = 9
% 模 (取余)
print(5%2) # 得出2(商)...1(余) 答案为1

比较运算符

<
==
!=(不等于)
=
<=

赋值运算符
= 赋值
+= 自加
a = 10
a += 1 #a = a + 1
print(a) # a =11

-= 自减
*=
/=
//=
**=
%=

逻辑运算符
and 与
or 或
not 非

and 练习
print(3 and 4)
print(0 and 4)
print(3 and False)

and 都为真的时候 取and后面的值
都为假的时候 取and前面的值
一真一假的时候,取假的

print(3 and 5 and 9 and 0 and false) # 0
print(5 and False and 9 and 0) # False
print(1 and 2 and 5 and 9 and 6)# 6

or 练习
print(1 or 0) #1
print(1 or 2)#1
print(o or False)#False

or都为真的时候取or前边的值
or都为假的时候取or后边的值
or一真一假 取真的

not 练习
/#()>not>and>or
/#从左向右执行
print(9 and 1 or not False and 8 or 7 and False)#1

成员运算符
in 存在
not in 不存在

s "asdasadasd"
print("sd" in s)#True

s "asdasadasd"
if "sd" not in s:
print(Ture)
else:
print(False) #False

编码初始

今0001 # 在字典中一个文字占8位缺失位数由0添补,添在最前面例00000001
天0010
取0100
便1000
利1001
店1010

ascii密码本 不支持中文(美国)
gbk(国标)英文 8位
中文 16位
linux utf-8
mac utf-8
window gbk(国标)
unicode(万国码)英文 16位
中文 32位
utf-8 (可变长的编码)
英文8位
欧洲16位
亚洲24位

单位转换:
1字节 = 8位
1Bytes = 8bit
1024Bytes = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024PB = 1EB

今日总结
1.while 循环--死循环
while条件:
循环体

打断死循环:
1.break 终止当前循环
改变条件--自动定义修改控制执行次数

关键字:
break--终止当前循环
continue--伪装成循环体中最后一行代码(跳出本次循环,继续下次循环)
while else:while条件成立的时候就不执行,条件不成立的时候执行else

2.字符串格式化

%--占位
%s--占字符串的位
%d--占数字位
%%-- 转义乘普通的%
d = "你好%s"
print(d%("我好"))
f"{变量名}{字符串}" 3.6版本以上才能执行

3.运算符
算是运算符:+ - * / // ** %
比较运算符: > < >= <= == !=
赋值预算符:= += -= *= /= //= %=
逻辑运算符: and or not 运算优先级()>not >and >or
成员运算符:in, not in

4.编码:
编码集(密码本)
ascii:
不支持中文(美国)
gbk:
英文 8位 1字节
中文 1位 2字节
unicode:
英文 16位 2字节
中文 32位 4字节

    utf-8: 
        英文 8位 1字节
        欧洲 16位 2字节
        亚洲 24位 3字节

猜你喜欢

转载自www.cnblogs.com/zhuzhizheng/p/11138087.html