Skip to content

Commit 8d0069a

Browse files
author
bianxiaokai
committed
feat: python init
0 parents  commit 8d0069a

File tree

12 files changed

+558
-0
lines changed

12 files changed

+558
-0
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"python.formatting.provider": "yapf",
3+
"python.linting.flake8Enabled": true,
4+
"python.linting.flake8Args": ["--max-line-length=248"],
5+
"python.linting.pylintEnabled": false
6+
}

__pycache__/function.cpython-310.pyc

785 Bytes
Binary file not shown.

for.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from collections.abc import Iterable
2+
3+
d = {'a': 1, 'b': 2, 'c': 3}
4+
for value in d.values():
5+
print(value)
6+
7+
for k, v in d.items():
8+
print('key:', k, 'value:', v)
9+
10+
isIns = isinstance('abc', Iterable)
11+
12+
for i, value in enumerate(['A', 'B', 'C']):
13+
print('i:', i, 'value:', value)
14+
15+
16+
def findMinAndMax(L):
17+
if len(L) == 0:
18+
return (None, None)
19+
min = L[0]
20+
max = L[0]
21+
for num in L:
22+
if num < min:
23+
min = num
24+
if num > max:
25+
max = num
26+
return (min, max)
27+
28+
29+
# 测试
30+
if findMinAndMax([]) != (None, None):
31+
print('测试失败!')
32+
elif findMinAndMax([7]) != (7, 7):
33+
print('测试失败!')
34+
elif findMinAndMax([7, 1]) != (1, 7):
35+
print('测试失败!')
36+
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
37+
print('测试失败!')
38+
else:
39+
print('测试成功!')

function.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import math
2+
3+
# 内置函数
4+
abs(-200)
5+
max(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
6+
# 数据类型转换
7+
int('123')
8+
float('123.123')
9+
str(1.23)
10+
bool(1)
11+
bool('')
12+
13+
a = abs
14+
x = a(123.45)
15+
print(x)
16+
17+
n1 = 255
18+
n2 = 1000
19+
print(hex(n1))
20+
21+
22+
def my_abs(x):
23+
if not isinstance(x, (int, float)):
24+
raise TypeError('x must be an integer or float')
25+
if x >= 0:
26+
return x
27+
else:
28+
return -x
29+
30+
31+
def nop(age):
32+
if age >= 18:
33+
pass
34+
35+
36+
def move(x, y, step, angle=0):
37+
nx = x + step * math.cos(angle)
38+
ny = y + step * math.sin(angle)
39+
return nx, ny
40+
41+
42+
r = move(100, 100, 60, math.pi / 6)
43+
44+
45+
def quadratic(a, b, c):
46+
if not isinstance(a * b * c, (float, int)):
47+
raise TypeError("quadratic must be a float or int")
48+
q = b**2 - 4 * a * c
49+
if q < 0:
50+
print('no solution')
51+
elif q == 0:
52+
print('此方程的解是唯一的')
53+
x = (-b) / (2 * a)
54+
print(x)
55+
else:
56+
x1 = ((-b + math.sqrt(q)) / (2 * a))
57+
x2 = ((-b - math.sqrt(q)) / (2 * a))
58+
return x1, x2
59+
60+
61+
def enroll(name, gender, age=6, city='Beijing'):
62+
pass
63+
64+
65+
def add_end(L=None):
66+
if L is None:
67+
L = []
68+
L.append('END')
69+
return L
70+
71+
72+
# 可变参数
73+
# 可变参数在函数调用时自动组装为一个tuple
74+
75+
76+
def calc(*numbers):
77+
sum = 0
78+
for n in numbers:
79+
sum += n**2
80+
return sum
81+
82+
83+
nums = [1, 2, 3]
84+
calc(*nums)
85+
86+
# 关键字参数
87+
# 允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict
88+
89+
90+
def person(name, age, **kw):
91+
print('name:', name, 'age:', age, 'other:', kw)
92+
93+
94+
person('Bob', 35, city='Beijing')
95+
# name: Bob age: 35 other: {'city': 'Beijing'}
96+
97+
extra = {'city': 'Beijing', 'job': 'Engineer'}
98+
person('Bob', 35, **extra)
99+
100+
# 命名关键字参数
101+
# 函数的调用者可以传入任意不受限制的关键字参数, *后面的参数被视为命名关键字参数
102+
103+
104+
def person_2(name, age, *, city='Beijing', job):
105+
print(name, age, city, job)
106+
107+
108+
# 参数组合
109+
110+
111+
def f1(a, b, c=0, *args, **kw):
112+
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)
113+
114+
115+
# 使用命名关键字参数时,要特别注意,如果没有可变参数,就必须加一个*作为特殊分隔符
116+
117+
118+
def f2(a, b, c=0, d=1, *arg, e, **kw):
119+
print('a =', a, 'b =', b, 'c =', c, 'e =', e, 'kw =', kw, 'arg = ', arg)
120+
121+
122+
f2(1, 2, 3, 4, 56, 7, 8, 9, 0, e=4, f=9)
123+
124+
125+
# 练习
126+
def mul(*numbers):
127+
if not len(numbers) == 0:
128+
sum = 1
129+
for n in numbers:
130+
if not isinstance(n, (int, float)):
131+
continue # 跳出本次循环,执行下次循环
132+
sum *= n
133+
return sum
134+
else:
135+
raise TypeError('numbers must be a list')
136+
137+
138+
# 测试
139+
print('mul(5) =', mul(5))
140+
print('mul(5, 6) =', mul(5, 6))
141+
print('mul(5, 6, 7) =', mul(5, 6, 7))
142+
print('mul(5, 6, 7, 9) =', mul(5, 6, 7, 9))
143+
144+
if mul(5) != 5:
145+
print('测试失败!')
146+
elif mul(5, 6) != 30:
147+
print('测试失败!')
148+
elif mul(5, 6, 7) != 210:
149+
print('测试失败!')
150+
elif mul(5, 6, 7, 9, 'test') != 1890:
151+
print('测试失败!')
152+
else:
153+
try:
154+
mul()
155+
print('测试失败!')
156+
except TypeError:
157+
print('测试成功!')
158+
159+
160+
# 递归
161+
def fact_1(n):
162+
if n == 1:
163+
return 1
164+
else:
165+
return n * fact(n - 1)
166+
167+
168+
# 尾递归优化
169+
def fact(n):
170+
return fact_iter(n, 1)
171+
172+
173+
def fact_iter(num, product):
174+
if num == 1:
175+
return product
176+
return fact_iter(num - 1, num * product)
177+
178+
179+
print('fact_iter', fact(10))
180+
181+
182+
# 汉诺塔问题实现
183+
# a存放起始柱,b存放辅助柱、c存放目标柱
184+
def move(n, a, b, c):
185+
if n == 1:
186+
print(a, '--->', c)
187+
else:
188+
move(n - 1, a, c, b) # 借助c把第 num 个以外的圆盘从a移动到b
189+
move(1, a, b, c) # 把第num个从a移动到c
190+
move(n - 1, b, a, c) # 借助a把第 num 个以外的圆盘从b移动到c
191+
192+
193+
move(3, 'A', 'B', 'C')

hello.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
3+
print('hello world')
4+
# name = input()
5+
print('1024 * 768 = ', 1024 * 768)
6+
print('I\'m learning\nPython.')
7+
print(r'\\\\\1233\\\\')
8+
print(r'''
9+
... hell\no
10+
... world
11+
''')
12+
print(3 > 2 and 2 > 3)
13+
print(not 3 > 2 or 2 > 3)
14+
15+
age = 11
16+
if age >= 18:
17+
print('adult')
18+
else:
19+
print('teenager')
20+
21+
x = 10
22+
x = x + 2
23+
print(x)
24+
print(10 / 3)
25+
26+
s4 = r'''Hello,
27+
Lisa!'''
28+
print(s4)
29+
30+
x = b'Hello'
31+
print(x)
32+
33+
print('%2d-%2d' % (3002.22222, 1103.44444))
34+
print('%.2f-%.2f' % (3002.22222, 1103.44444))
35+
36+
print('Hello, %s , I miss you, Do you miss me %.1f' %
37+
('zhaowenjuan', 1103.44444))
38+
39+
r = 2.5
40+
s = 3.14 * r**2
41+
print(f'The area of a circle with radius {r} is {s:.2f}')

if.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# s = input('birth: ')
2+
# birth = int(s)
3+
# if birth < 2000:
4+
# print('00前')
5+
# else:
6+
# print('00后')
7+
8+
print(84.5 / (1.75 ** 2))
9+
10+
height = 1.75
11+
weight = 83.8
12+
13+
bmi = weight / (height ** 2)
14+
print('bmi = %.f' % bmi)
15+
if bmi <= 18.5:
16+
print('过轻')
17+
elif bmi > 18.5 and bmi <= 25:
18+
print('正常')
19+
elif bmi > 25 and bmi <= 28:
20+
print('过重')
21+
elif bmi > 28 and bmi <= 32:
22+
print('肥胖')
23+
elif bmi > 32:
24+
print('严重肥胖')

import.py

Whitespace-only changes.

list.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
# list 列表生成式
3+
classmates = ['Michael', 'Bob', 'Tracy']
4+
classmates.append('Admin')
5+
classmates.insert(2, 'Jack')
6+
# classmates.pop()
7+
print(classmates)
8+
print('classmates length : %d' % len(classmates))
9+
print('classmates length :', len(classmates))
10+
11+
# tuple 元组
12+
tupleList = ('Michael', 'Bob', 'Tracy')
13+
print('tupleList length : %d' % len(tupleList))
14+
15+
print([x * x for x in range(1, 11)])
16+
print([x * x for x in range(1, 11) if x % 2 == 0])
17+
print([m + n for m in 'ABC' for n in 'XYZ'])
18+
19+
print([d for d in os.listdir('.')])
20+
21+
L = ['Hello', 'World', 'IBM', 'Apple']
22+
print([x.lower() for x in L])
23+
24+
25+
print([x if x % 2 == 0 else -x for x in range(1, 11)])
26+
27+
L1 = ['Hello', 'World', 18, 'Apple', None]
28+
L2 = [x.lower() for x in L1 if isinstance(x, str)]
29+
print('L2', L2)

0 commit comments

Comments
 (0)