1. python的流程控制和循环
1.1 if…else
条件判断
单分支
if 满足条件:
执行这段代码
例:
>>> if ('t' in 'test'):
... print('t in test')
...
t in test
双分支
if 满足条件:
执行这段代码
else:
不满足就执行这一行
例:
>>> if ('t' in 'hello'):
... print('t in hello')
... else:
... print('t not in hello')
...
t not in hello
多分支
if 满足条件1:
执行代码1
elif 满足条件2:
执行代码2
elif 满足条件3:
执行代码3
...
else:
不满足上面的条件就执行这一行
例:
age = 24
guess_age = int(input("Input your guess of Anna's age: "))
if guess_age == age:
print("Well done. You guessed right.")
elif guess_age > age:
print("too bigger")
elif guess_age < age:
print("too little")
else:
print("Wrong input!")
输出如下:
Input your guess of Anna's age: 25
too bigger
1.2 while
条件判断
while 条件为真:
执行这部分代码
例1:
num = 0
while num < 4:
print("num is %d" % num)
num += 1
输出:
num is 0
num is 1
num is 2
num is 3
例2:
num = 0
while num < 4:
age = 24
guess_age = int(input("Input your guess of Anna's age: "))
if guess_age == age:
print("Well done. You guessed right.")
elif guess_age > age:
print("too bigger")
elif guess_age < age:
print("too little")
else:
print("Wrong input!")
num += 1
输出如下:
Input your guess of Anna's age: 25
too bigger
Input your guess of Anna's age: 24
Well done. You guessed right.
Input your guess of Anna's age: 26
too bigger
Input your guess of Anna's age: 12
too little
可以看到上面才了两次,已经猜中了,可是还需要再猜,明显是不合理的,那么现在要求猜中后跳出循环,这就需要使用break。
break
例3:
num = 0
while num < 4:
age = 24
guess_age = int(input("Input your guess of Anna's age: "))
if guess_age == age:
print("Well done. You guessed right.")
break #使用break
elif guess_age > age:
print("too bigger")
elif guess_age < age:
print("too little")
else:
print("Wrong input!")
num += 1
输出如下:
Input your guess of Anna's age: 23
too little
Input your guess of Anna's age: 24
Well done. You guessed right.
while...else...
while后面的else作用是指,当while循环正常执行完,中间没有被break中止的话,就会执行else语句
count = 0
while count <= 4:
count += 1
print("Loop", count)
else:
print("循环执行完了")
print("跳出while循环语句")
输出如下:
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
循环执行完了
跳出while循环语句
continue
例:
count = 0
while count <= 4:
count += 1
if count == 3:
continue
print("Loop", count)
else:
print("循环执行完了")
print("跳出while循环语句")
输出如下:
Loop 1
Loop 2
Loop 4
Loop 5
循环执行完了
跳出while循环语句
可以看到,如果当count为3时,使用了continue,这样下面的print语句就不执行了
死循环
while True:
print("dead loop")
2. for循环
循环
>>> name_list = ['Alex', 'ZhangSan', 'LiSi', 'WangWu']
>>> for name in name_list:
... print("name", name)
...
name Alex
name ZhangSan
name LiSi
name WangWu
例2:
>>> [x for x in range(5)]
[0, 1, 2, 3, 4]
>>> {x:x*x for x in range(5)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2. python中的字符编码
2.1 ASCII编码
python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)。
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,所以,ASCII码最多只能表示 255 个符号。
2.2 Unicode与UTF-8
显然ASCII码无法将世界上的各种文字和符号全部表示,所以,就需要新出一种可以代表所有字符和符号的编码,即:Unicode。
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,
注:此处说的的是最少2个字节,可能更多
UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存…
所以,python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)。
python 2.x默认编码是
ASCII
python 3.x默认编码是UTF-8
3. 进制转换
二进制,八进制,十进制,十六进制数相互转换
十进制转换
# 十进制转8进制
>>> oct(10)
'0o12'
>>> oct(1)
'0o1'
>>> oct(7)
'0o7'
>>> oct(8)
'0o10'
# 十进制转16进制
>>> hex(1)
'0x1'
>>> hex(10)
'0xa'
>>> hex(9)
'0x9'
>>> hex(15)
'0xf'
>>> hex(16)
'0x10'
>>> hex(32)
'0x20'
十六进制
In [4]: a = '中文'
In [5]: a
Out[5]: '\xe4\xb8\xad\xe6\x96\x87'
为什么要用到16进制?
方便阅读,缩减字节数
转换:
>>> hex(32) # 10->16
'0x20'
>>> bin(0x20) # 16-> 2
'0b100000'
>>> bin(10) # 2->10
'0b1010'
>>> oct(0x20) # 16->8
'0o40'

4. 其它
4.1 hash
hash,一般翻译做散列,就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来唯一的确定输入值。简单的来说,就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。
特征:
hash值得计算过程是依据这个值得一些特征计算的,这就要求hash的值必须固定,因此被hash的值必须是不可变的。
用途:
- 文件签名
- md5加密
- 密码验证
本文详细介绍了Python中的流程控制语句,包括条件判断(if...else)、循环(while、for)及其控制语句(break、continue),并探讨了字符编码(ASCII、Unicode、UTF-8)的概念及应用。此外,还介绍了不同进制之间的转换方法。
2755

被折叠的 条评论
为什么被折叠?



