review

作者: 水果坚果燕麦片 | 来源:发表于2019-01-12 14:50 被阅读0次
一阶段基础知识回顾:

变量

运算符:
1.数学运算符: +,-, *, /, //, %

2.比较运算符: >,<, ==,!=,>=,<=

(补充: is 的使用
一个变量有三要素,类型(变量中存储的数据类型),值(储存变量的数据),地址(变量中真正的地址))
== 比较的是变量的值是否相等,is 比较的是变量的地址是否相等

3.逻辑运算符: and, or, not

4.赋值运算符:=

5.位运算:&(与), |(或), ^(异或), ~(非), >>(右移), <<(左移)

运算符顺序:数学运算符>比较运算符>逻辑运算符>赋值运算符

数据类型
int,float,str,complex,bool
a.chr(编码值) - 获取编码对应字符

print(chr(0xA020))
结果如下:
ꀠ

b.ord(字符) - 获取字符的编码值,返回的是十进制

print(ord('巧'))
结果如下:
24039

字符串、列表、元组、字典

str1 = 'abcdefg'
list1 = [123, 'abcd', (1,2,3), {'name':'sxc'},[4]]
tuple1 = (123, [5], {'age':'18'})
dict1 = {'gs':'zqm','cl':'lss'}

# 访问
# 字符串通过下边获取,不能越界
print(str1[:])
print(str1[::-1])

# 列表以及元组和字符串获取方法一样,下标和切片,以及循环
print(list1[3])
print(list1[:])
print(tuple1[2])
print(tuple1[:])
# 字典需要通过键值进行访问
print(dict1['gs'])

# 增加
str1 += 'hijkl'
print(str1)

list1.append([5,6])
print(list1)
list1.insert(2,'光年之外')
print(list1)
#元组中的元素是不能修改和删除的但是可以对元组进行连接组合,可以用del语句来删除整个元组
 # 删(del 是python的关键字,可以删除任何东西)
list1.pop(-1)
print(list1)
# remove()中的值若不在列表中则会报错
list1.remove([4])
print(list1)
# dict1.clear()
# print(dict1)

# 改
list1[0] = '1'
print(list1)

for items in list1:
    print(items)

# 字典遍历的时候items只遍历了键值key
for items in dict1:
    print(items)
    print(dict1[items])
# 变成列表
print(dict1.keys())
print(dict1.values())
print(dict1.items())

for items in dict1.items():
    print(items)

def main():
    pass


if __name__ == '__main__':
    main()

函数

all_students = [
    {'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
    {'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
    {'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
    {'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
]

# 删除年龄小于18岁的学生
for stu in all_students:
    if stu['age'] < 18:
        all_students.remove(stu)
print(all_students)

for stu in all_students:
    if stu['age'] < 18:
        all_students.clear()
print(all_students)

dict1 = {'a':1, 'b':2, 'c':3}
# 边写一个函数交换字典的key和value的值
def change_key(dict1:dict):
    for key in dict1.copy():
        value = dict1.pop(key)
        dict1[value] = key
    print(dict1)

change_key(dict1)
dict1 = {'a':1, 'b':2, 'c':3}
def chaneg_key2(dict1:dict):
    dict1 = {value : key for key,value in dict1.items()}
    print(dict1)

chaneg_key2(dict1)
结果如下:
[{'name': 'stu1', 'age': 19, 'score': 81, 'tel': '192222'}, {'name': 'stu2', 'age': 29, 'score': 90, 'tel': '211222'}, {'name': 'stu4', 'age': 30, 'score': 45, 'tel': '900012'}]
[{'name': 'stu1', 'age': 19, 'score': 81, 'tel': '192222'}, {'name': 'stu2', 'age': 29, 'score': 90, 'tel': '211222'}, {'name': 'stu4', 'age': 30, 'score': 45, 'tel': '900012'}]
{1: 'a', 2: 'b', 3: 'c'}
{1: 'a', 2: 'b', 3: 'c'}

匿名函数

is_leap = lambda year: year % 4 == 0 and year % 100 != 0 or year % 400 == 0
print(is_leap(2000))
print(is_leap(2008))

相关文章

网友评论

      本文标题:review

      本文链接:https://www.haomeiwen.com/subject/tcuqdqtx.html