Skip to content

Commit 79b6a54

Browse files
committed
learning python
1 parent 8da25e0 commit 79b6a54

File tree

69 files changed

+877
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+877
-0
lines changed

exercise/condition_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
num_a = int(input('请输入第一个整数'))
2+
num_b = int(input('请输入第二个整数'))
3+
print(str(num_a) + '大于等于' + str(num_b) if num_a >= num_b else str(num_a) + '小于' + str(num_b))
4+
5+
if num_a:
6+
pass
7+
else:
8+
pass

exercise/dict_test.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 字典是python内置的数据结构之一,与列表一样是一个可变序列
2+
# 以键值对的方式存储数据,字典是一个无序的序列
3+
4+
# 字典的创建方式
5+
# 使用{}创建字典
6+
scores = {'张三': 100, '李四': 98, '王五': 45}
7+
print(scores, type(scores))
8+
# 使用dict()创建字典
9+
student = dict(name='jack', age=20)
10+
print(student, type(student))
11+
# 创建一个空字典
12+
null_dict = {}
13+
print(null_dict)
14+
15+
# 字典中元素的获取
16+
scores = {'张三': 100, '李四': 98, '王五': 45}
17+
# 方式一:使用[]
18+
print(scores['张三'])
19+
# print(scores['陈六'])
20+
# 方式二:使用get()方法
21+
print(scores.get('张三'))
22+
print(scores.get('陈六'))
23+
print(scores.get('陈六', 99))
24+
25+
# 字典中键的判断:in、not in
26+
scores = {'张三': 100, '李四': 98, '王五': 45}
27+
print('张三' in scores)
28+
print('张三' not in scores)
29+
30+
# 字典元素的删除
31+
del scores['张三']
32+
print(scores)
33+
# 清空字典的元素
34+
scores.clear()
35+
print(scores)
36+
37+
# 字典元素的添加
38+
scores = {'张三': 100, '李四': 98, '王五': 45}
39+
print(scores)
40+
scores['陈六'] = 98
41+
print(scores)
42+
43+
# 字典的视图操作
44+
scores = {'张三': 100, '李四': 98, '王五': 97, '王五': 9}
45+
# 获取所有的键
46+
keys = scores.keys()
47+
print(keys, type(keys))
48+
# 将所有的键组成的视图转换成列表
49+
print(list(keys))
50+
# 获取所有的值
51+
values = scores.values()
52+
print(values, type(values))
53+
# 将所有的值转换成列表
54+
print(list(values))
55+
# 获取所有的键值对
56+
items = scores.items()
57+
print(items, type(items))
58+
# 将所有的键值对转换成列表
59+
print(list(items))
60+
61+
# 字典元素的遍历
62+
scores = {'张三': 100, '李四': 98, '王五': 45}
63+
for item in scores:
64+
print(item, scores[item], scores.get(item))
65+
66+
# 字典的特点:
67+
# 1.字典中的所有元素都是一个key-value对,key不允许重复,value可以重复
68+
d = {'name': '张三', 'name': '李四'}
69+
print(d)
70+
d = {'name': '张三', 'nickname': '张三'}
71+
print(d)
72+
# 2.字典中的元素是无序的
73+
# 3.列表中的key必须是不可变对象
74+
lst = [10, 20, 30]
75+
lst.insert(1, 100)
76+
print(lst)
77+
# d = {lst: 100}
78+
# print(d)
79+
# 4.字典可以根据需要动态地伸缩
80+
# 5.字典会浪费较大的内存,是一种使用空间换时间的数据结构
81+
82+
# 字典生成式
83+
items = ['Fruits', 'Books', 'Others']
84+
prices = [96, 78, 85, 100, 120]
85+
lst = zip(items, prices)
86+
print(list(lst))
87+
d = {item.upper(): price for item, price in zip(items, prices)}
88+
print(d)

exercise/extend_test.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 继承
2+
# python支持多重继承
3+
4+
# 默认继承object类
5+
class Person(object):
6+
def __init__(self, name, age):
7+
self.name = name
8+
self.age = age
9+
10+
def info(self):
11+
print('姓名:{0},年龄:{1}'.format(self.name, self.age))
12+
13+
14+
# 定义子类
15+
class Student(Person):
16+
def __init__(self, name, age, stu_no):
17+
super().__init__(name, age)
18+
self.stu_no = stu_no
19+
print(self.stu_no)
20+
21+
def info(self):
22+
super().info()
23+
print('学号:{0}'.format(self.stu_no))
24+
25+
26+
class Teacher(Person):
27+
def __init__(self, name, age, teachofyear):
28+
super().__init__(name, age)
29+
self.teachofyear = teachofyear
30+
31+
def info(self):
32+
super().info()
33+
print('教龄:{0}'.format(self.teachofyear))
34+
35+
36+
# 多重继承
37+
class A(object):
38+
pass
39+
40+
41+
class B(object):
42+
pass
43+
44+
45+
class C(A, B):
46+
pass
47+
48+
49+
# 测试
50+
stu = Student('jack', 20, '1001')
51+
stu.info()
52+
teacher = Teacher('李四', 34, 10)
53+
teacher.info()

exercise/for_test.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
for item in 'Python':
2+
print(item)
3+
4+
for i in range(10):
5+
print(i)
6+
7+
# 如果在循环体中不需要使用到自定义变量,可将自定义变量写为_
8+
for _ in range(5):
9+
print('人生苦短,我用Python')
10+
11+
sum = 0
12+
for item in range(1, 101):
13+
if sum % 2 == 0:
14+
sum += item
15+
print('1到100之间的偶数和为:', sum)
16+
17+
for i in range(1, 10):
18+
for j in range(1, i + 1):
19+
print(i, '*', j, '=', i * j, end='\t')
20+
print()
21+
22+
# for-else
23+
for item in range(3):
24+
pwd = input('请输入密码:')
25+
if pwd == '8888':
26+
print('密码正确')
27+
break
28+
else:
29+
print('密码不正确')
30+
else:
31+
print('对不起,三次密码均输入错误')
32+
33+
# while-else
34+
# 略

exercise/func_test.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# 函数
2+
3+
def calc(a, b):
4+
c = a + b
5+
return c
6+
7+
8+
print('----------------------参数的传递的两种方式--------------------------')
9+
# 传参方式一:位置实参
10+
result = calc(10, 20)
11+
print(result)
12+
# 传参方式二:关键字实参
13+
result = calc(b=10, a=15)
14+
print(result)
15+
16+
17+
def fun(arg1, arg2):
18+
print('arg1', arg1)
19+
print('arg2', arg2)
20+
arg1 = 100
21+
arg2.append(10)
22+
print('arg1', arg1)
23+
print('arg2', arg2)
24+
25+
26+
print('----------------------值传递--------------------------')
27+
n1 = 11
28+
n2 = [22, 33, 44]
29+
fun(n1, n2)
30+
print('n1', n1)
31+
print('n2', n2)
32+
33+
34+
def odd_even(num):
35+
odd = []
36+
even = []
37+
for i in num:
38+
if i % 2:
39+
odd.append(i)
40+
else:
41+
even.append(i)
42+
return odd, even
43+
44+
45+
print('----------------------函数的返回值--------------------------')
46+
lst = [10, 29, 34, 23, 44, 53, 55]
47+
# 函数若有多个返回值,则返回值是元组;若只有一个返回值,则直接返回原值
48+
print(odd_even(lst))
49+
a, b = odd_even(lst)
50+
print(a, b)
51+
52+
53+
def add(a, b=10):
54+
return a + b
55+
56+
57+
print('----------------------函数参数的默认值--------------------------')
58+
print(add(10))
59+
print(add(10, 20))
60+
61+
62+
# 使用*定义的可变位置参数是一个元组
63+
# 可变位置参数只能有一个
64+
def variable(*args):
65+
print(args)
66+
67+
68+
# 使用**定义的可变关键字参数是一个字典
69+
# 可变关键字参数只能有一个
70+
def variable2(**args):
71+
print(args)
72+
73+
74+
print('----------------------可变的位置参值--------------------------')
75+
variable(10)
76+
variable(10, 30)
77+
variable(10, 30, 50)
78+
variable2(a=10)
79+
variable2(a=10, b=30, c=40)
80+
81+
82+
def call_fun(a, b, c):
83+
print('a=', a)
84+
print('b=', b)
85+
print('c=', c)
86+
87+
88+
print('----------------------函数的调用--------------------------')
89+
# 将列表中的每个元素都转换为位置实参传入
90+
lst = [11, 22, 33]
91+
call_fun(*lst)
92+
# 将字典中的每个键值对都转换为关键字实参传入
93+
dic = {'a': 111, 'b': 222, 'c': 333}
94+
call_fun(**dic)

0 commit comments

Comments
 (0)