第四章 Python外壳:代码结构

时间:2023-03-09 16:34:35
第四章 Python外壳:代码结构

Python的独特语法:

  • 不使用分号结束语句,而是回车;
  • 通过代码缩进来区分代码块;
  • if、while、for等,都不用括号,但不能没有冒号(:)。

如何将一行命令分为多行?

>>> myNameIs = "Li\
Zhi\
Xin.\
"
>>> myNameIs
'LiZhiXin.'

4.1 条件语句和循环语句

如何使用if、elif和else进行比较?

>>> a = 1
>>> if a < 2:
print ("yes")
elif a > 2:
print ("no")
else:
print ("chaos") yes

如何连接条件表达式?

python中没有!、&& 和 || 运算符,分别用 not、and 和 or代替。

>>> not 1
False
>>> 5 < 6 and 1 < 2
True
>>> 5 < 6 or 1 > 2
True

python中有哪些假值?

>>> False
False
>>> None
>>> 0
0
>>> 0.0
0.0
>>> ''
''
>>> []
[]
>>> ()
()
>>> {}
{}
>>> set()
set()

如何使用while进行循环?

>>> count = 1
>>> while count <= 5:
print(count)
count += 1 1
2
3
4
5

如何跳出循环?

>>> while True:
stuff = input("string to calpitalize [type q to quit]:")
if stuff == 'q':
break
print(stuff.capitalize()) string to calpitalize [type q to quit]:li zhi xin
Li zhi xin
string to calpitalize [type q to quit]:q
>>>

如何跳到循环开始(用于跳过特定条件的循环)?

>>> while True:
value = input("Interger, please [q to quit]: ")
if value == 'q':
break
number = int(value)
if number % 2 == 0:
continue
print(number, "squared is ", number*number) Interger, please [q to quit]: 1
1 squared is 1
Interger, please [q to quit]: 2
Interger, please [q to quit]: 3
3 squared is 9
Interger, please [q to quit]: 4
Interger, please [q to quit]: q

循环外的else是如何使用的?

>>> numbers = [1,3,5]
>>> position = 0
>>> while position < len(numbers):
number = numbers[position]
if number % 2 == 0:
print('Found even number', number)
break
position += 1
else:
print("No even number found.") No even number found.

*如何使用for进行迭代(很独特)?

python中的for很牛逼,可以在数据结构和具体实现未知的情况下,遍历整个数据结构:

>>> rabbits = ['a', 'b', 'c', 'd']
>>> current = 0
>>> while current < len(rabbits):
print(rabbits[current])
current += 1 a
b
c
d
>>> for rabbit in rabbits:
print(rabbit) a
b
c
d
>>> word = 'cat'
>>> for letter in word:
print(letter) c
a
t
>>> a = {'a':'b', 'c':'d', 'e':'f'}
>>> for key in a:
print(key) a
c
e
>>> for value in a.values():
print(value) b
d
f
>>> for item in a.items():
print(item) ('a', 'b')
('c', 'd')
('e', 'f')
>>> for key, value in a.items():
print(key, value) a b
c d
e f

如何用zip()函数进行迭代?

对多个序列使用并行迭代,就是一起迭代,等价于将多个序列有序合并了。

>>> c = 0.1, 0.2, 0.3
>>> a = (1, 2, 3)
>>> b = ['a', 'b', 'c', 'd']
>>> c = 0.1, 0.2, 0.3, 0.4, 0.5,
>>> for e, f, g in zip(a, b, c):
print(e, ' ', f, ' ', g) 1 a 0.1
2 b 0.2
3 c 0.3

list()、zip()和 dict()可以灵活运用。

如何生成特定区间的自然数序列?

range()的使用方法类似切片,但是却是使用‘,’分隔,而不是‘:’。

>>> for x in range(0,3):
print(x) 0
1
2
>>> list(range(0,3))
[0, 1, 2]

 

4.2 推导式(python特有)

从一个或多个迭代器创建数据结构,可以将循环和条件结合。

列表推导式

>>> number_list = [number for number in range(1,6)]
>>> number_list
[1, 2, 3, 4, 5]
>>> a_list = [number for number in range(1,6) if number % 2 ==1]
>>> a_list
[1, 3, 5]
>>> rows = range(1,4)
>>> cols = range(1,3)
>>> cells = [(row, col) for row in rows for col in cols]
>>> for cell in cells:
print(cell) (1, 1)
(1, 2)
(2, 1)
(2, 2)
(3, 1)
(3, 2)

字典推导式

>>> word = 'letters'
>>> letter_counts = {letter:word.count(letter) for letter in word}
>>> letter_counts
{'l': 1, 't': 2, 'r': 1, 's': 1, 'e': 2}

 

函数

python中参数有哪几种使用方法?

>>> def func(a, b, c):
print('a is ', a, 'b is ', b, 'c is ', c) >>> func(1, 2, 3)
a is 1 b is 2 c is 3
>>> func(c = 1, b = 2, a = 3)
a is 3 b is 2 c is 1
>>> d = [1, 2, 3]
>>> func(*d)
a is 1 b is 2 c is 3
>>> e = {'c':1, 'b':2, 'a':3}
>>> func(**e)
a is 3 b is 2 c is 1

生成器

 

装饰器

 

命名空间和作用域

 

异常