菜鸡自学 Python 笔记(二)
- 五,结构与语句
- 1.选择结构—— if 语句
- 2.循环控制语句
- (1)while 语句
- (2)for 语句
- (3)continue 语句与break 语句
- 六.列表
- 1.列表的定义
- 2.列表相关操作(函数)
- 3.列表操作符 + 和 *
- 七.元组
- 1.元组的创建
- 2.元组的相关操作
- 3.元组的内置函数
- 八.字典
- 1.定义字典键值
- 2.字典的相关操作
- 3.字典的内置函数和方法
五,结构与语句
1.选择结构—— if 语句
( 1 )格式:
if 表达式:
语句1
elif 表达式2:
语句2
……
else:
语句n
!这里注意:
- 每个条件后面的 冒号 : 不能忘记。
- Python 用缩进划分语句块,相同缩进为一块,所以这里要严格规范。
- 在Python中没有switch…case 语句。
下面举个例子:
(表达式是一个布尔值)
is_hot = False
is_clod = True
if is_hot:
print("It's a hot day!")
print("Drink some water.")
elif is_clod:
print("It's a cold day!")
print("Wear warm clothes.")
else:
print("It's a lovely day!")
print("Enjoy your day!")
(表达式是一个比较)
age = 19
if age > 18:
print("You will stay young.")
else:
print("Hi,girl.")
( 2 )if 的嵌套
age = 45
if age > 18:
if age < 40:
print("You will stay young.")
else:
print('Your mature temperament is fascinating.')
else:
print("Hi,girl.")
2.循环控制语句
(1)while 语句
格式:
while 判断条件:
语句
(这里同样要注意,冒号和缩进。)
举个例子:
i = 1
while i <= 5:
print(i, end=' ')
print('*' * i)
i = i+1
print("Done")
while True:
- 当然,while 还可以和 else 配合使用:
表示当while语句的条件表达式为False时,执行 else 的语句块。
i = 1
while i < 4:
print(i, "<4")
i = i+1
else:
print(i, "=4")
(2)for 语句
格式:
for 变量 in 列表或范围 :
语句
跟 while 一样,for 也可以和 else 结合
for 变量 in 列表或范围 :
语句
else:
语句
应用举例:
for item in 'Asu':
print(item, end=' ')
print()
for item in ['Asu', 'Dodo', 'Hi']:
print(item, end=' ')
print()
for item in [1, 2, 3, 4]:
print(item, end=' ')
应用range()范围函数
for item in range(10):
print(item, end=' ')
print()
for item in range(5, 10):
print(item, end=' ')
print()
for item in range(5, 10, 2):
print(item, end=' ')
(3)continue 语句与break 语句
continue语句,Python将跳过当前循环块中的剩余语句,继续进行下一轮循环。
i = 0
while i <= 5:
i = i + 1
if i == 3:
continue
print('*' * i)
print("Done")
而 break 语句是直接跳出循环
i = 0
while i <= 5:
i = i + 1
if i == 3:
break
print('*' * i)
print("Done")
六.列表
在Python 中,我们用方括号[ ]来表示列表,用逗号 , 来分割列表的各个元素。
1.列表的定义
list = [1, "Asu", 1.11, True]
list = [1, "Asu", 1.11, True, ["Dodo", 3, 5+3j]]
(索引列表元素:
list = [1, "Asu", 1.11, True, ["Dodo", 3, 5+3j]]
print(list[1])
print(list[4][0])
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0][1])
for row in matrix:
for item in row:
print(item, end=' ')
2.列表相关操作(函数)
内置函数:
- len() 函数返回列表的长度
- max() 函数返回列表中的最大值
- min() 函数返回列表中的最小值
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(len(numbers))
print(max(numbers))
print(min(numbers))
方法:
numbers = [5, 4, 6, 1, 9]
numbers.append(20)
print(numbers)
numbers = [5, 4, 6, 1, 9]
numbers.insert(1, 10)
print(numbers)
numbers = [5, 4, 6, 1, 9]
numbers.remove(5)
print(numbers)
numbers = [5, 4, 6, 1, 9]
numbers.pop()
print(numbers)
numbers = [5, 4, 6, 1, 9]
print(numbers.index(1))
numbers = [5, 4, 6, 1, 9]
numbers.clear()
print(numbers)
numbers = [5, 4, 6, 5, 1, 9]
print(numbers.count(5))
numbers = [5, 4, 6, 5, 1, 9]
numbers.sort()
print(numbers)
numbers.reverse()
print(numbers)
- copy( ) 复制
复制出的列表如何改变,原列表始终不发生变化
numbers = [5, 4, 6, 5, 1, 9]
numbers2 = numbers.copy()
numbers2.append(10)
print(numbers)
print(numbers2)
3.列表操作符 + 和 *
+号用于组合列表
*号用于重复列表
numbers = [1, 2, 3]
numbers2 = [1, 2, 3] + [1, 2, 3]
numbers3 = [1, 2, 3] * 3
print(numbers2)
print(numbers3)
七.元组
与列表相比,元组的对象不能修改,并且用小括号 ( )来表示元组。一旦修改系统就会报错。
1.元组的创建
numbers = (1, 2, 3)
num = (1,)
元组的创建非常简单,只需在圆括号内用逗号将元素隔开。
! 但是,如果创建的元组对象只有一个元素,就必须在元素之后加上逗号,否则Python 会认为这个是赋值操作。
numbers = (1, 2, 3)
print(numbers[0])
x, y, z = numbers
2.元组的相关操作
numbers = (1, 2, 3)
print(numbers[0])
numbers = (1, 2, 3)
x, y, z = numbers
print(x, y, z)
虽然元组内的元素值不能被修改,但是可以删除
numbers = (1, 2, 3)
numbers = numbers[0], numbers[2]
print(numbers)
元组的组合
num = (1, 2)
num1 =(4, 5)
print(num + num1)
删除整个元组
num = (1, 2, 3)
del num
print(num)
3.元组的内置函数
元组的 len()函数,max()函数,min()函数都跟列表相似。
num = (1, 2, 3)
print(sum(num))
八.字典
字典是一种可变容器模型,可以储存任意类型的对象。
字典的对象使用大括号{}将元素列出。字典的元素排列并没有一定的顺序,因为可以使用键值来取得元素。
1.定义字典键值
注意点:
(1)同一个键值不能多次出现。
创建时如果同一个键值被赋值多次,那么只有最后一个有效,前面重复的会被删除。
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False,
"age": 18
}
print(people)
(2)字典的键值不可变,所有可以用数字,字符串或元组充当,列表不可以。否则,会报错。
2.字典的相关操作
访问:
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
print(people["name"])
print(people.get("Name"))
print(people.get("birthdate", "9 23"))
键值的更新:
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
people["name"] = "Dodo"
print(people["name"])
people["birthdate"] = "9 23"
print(people["birthdate"])
接下来搞个有趣的实例:
message = input(">")
words = message.split(' ')
emojis = {
":)": "😊",
":(": "😢"
}
output =""
for word in words:
output += emojis.get(word, word) + " "
print(output)
3.字典的内置函数和方法
内置函数:
(1)len() 计算字典元素个数
(2)str() 将字典的元素转化为可打印的字符串形式
(3)type() 返回输入的变量类型
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
print(len(people))
print(str(people))
方法:
(1)clear() 清除字典中的所有元素
(2)copy() 复制字典
(3) pop() 删除字典中的最后一个元素
与列表相似,可以参考列表。
(4)get(k,d) k是字典的索引值,d是索引值的默认值。如果k存在,就返回其值,否则返回d。
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
print(people.get("Name"))
print(people.get("birthdate", "9 23"))
(5)items() 使用字典中的元素创建一个由元组对象组成的列表。
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
print(people.items())
(6)keys() 使用字典中的键值创建一个列表对象
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
print(people.keys())
(7)values() 用字典中的键值的数值创建一个列表对象
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
print(people.values())
(8)setdefault(k,d) k是字典的键值,d是键值的默认值。如果k存在就返回其值;否则返回d,并将新的元素添加到字典中。
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
print(people.setdefault("name"))
print(people.setdefault("Name", "Dodo"))
print(people)
(9)update() 更新字典
people = {
"name": 'Asu',
"age": 19,
"is_beautiful": False
}
people.update({"is_rich": "False"})
print(people)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)