Skip to content

Commit 564a692

Browse files
committed
add practice1
1 parent b68a764 commit 564a692

File tree

1 file changed

+284
-0
lines changed

1 file changed

+284
-0
lines changed

python_practice1.ipynb

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 2,
4+
"metadata": {
5+
"language_info": {
6+
"name": "python",
7+
"codemirror_mode": {
8+
"name": "ipython",
9+
"version": 3
10+
},
11+
"version": "3.7.6-final"
12+
},
13+
"orig_nbformat": 2,
14+
"file_extension": ".py",
15+
"mimetype": "text/x-python",
16+
"name": "python",
17+
"npconvert_exporter": "python",
18+
"pygments_lexer": "ipython3",
19+
"version": 3,
20+
"kernelspec": {
21+
"name": "python3",
22+
"display_name": "Python 3"
23+
}
24+
},
25+
"cells": [
26+
{
27+
"cell_type": "code",
28+
"execution_count": 23,
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"output_type": "stream",
33+
"name": "stdout",
34+
"text": "The variables before swapping are: 12 34\nThe variables after swapping are: 34 12\n"
35+
}
36+
],
37+
"source": [
38+
"# Filename: python_practice1.ipynb\n",
39+
"#\n",
40+
"# Summary: 交换两个变量值 / 摄氏温度转为华氏温度 / 求数字中各位数之和 / 判断数字为质数or合数 / 生成随机数 / 删除列表重复项\n",
41+
"#\n",
42+
"# Date: 2020/4/1\n",
43+
"#\n",
44+
"# author by: 崇森([email protected])\n",
45+
"\n",
46+
"\n",
47+
"# @FullName: swap\n",
48+
"#\n",
49+
"# @Brief: 交换变量值\n",
50+
"#\n",
51+
"# @Parameter: x, y\n",
52+
"#\n",
53+
"# @Return: y, x\n",
54+
"def swap(x, y):\n",
55+
" return y, x\n",
56+
"\n",
57+
"assert(swap('a', 'b') == ('b', 'a'))\n",
58+
"\n",
59+
"var_one = input(\"Please input the first variable:\")\n",
60+
"var_two = input(\"Please input the second variable:\")\n",
61+
"print(\"The variables before swapping are:\", var_one, var_two)\n",
62+
"\n",
63+
"var_one, var_two = swap(var_one, var_two)\n",
64+
"# var_one, var_two = var_two, var_one\n",
65+
"print(\"The variables after swapping are:\", var_one, var_two)\n"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 15,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"output_type": "stream",
75+
"name": "stdout",
76+
"text": "100 converts to Fahrenheit is 212.0\n"
77+
}
78+
],
79+
"source": [
80+
"# @FullName: cel2fah\n",
81+
"#\n",
82+
"# @Brief: 摄氏度转华氏度\n",
83+
"#\n",
84+
"# @Parameter: 摄氏度\n",
85+
"#\n",
86+
"# @Return: 华氏度\n",
87+
"def cel2fah(cel):\n",
88+
" return cel * 1.8 + 32\n",
89+
"\n",
90+
"\n",
91+
"assert(cel2fah(0) == 32)\n",
92+
"assert(cel2fah(100) == 212)\n",
93+
"\n",
94+
"celsius = eval(input(\"Please input celsius:\"))\n",
95+
"if celsius < -273:\n",
96+
" print(\"Input Error!\")\n",
97+
"else:\n",
98+
" fahrenheit = cel2fah(celsius)\n",
99+
" print(celsius, \"converts to Fahrenheit is\", fahrenheit)\n",
100+
""
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 28,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"output_type": "stream",
110+
"name": "stdout",
111+
"text": "-12.34: The sum of the individual digits is 10\n"
112+
}
113+
],
114+
"source": [
115+
"# @FullName: sum_digits\n",
116+
"#\n",
117+
"# @Brief: 计算正整数各个位数之和\n",
118+
"#\n",
119+
"# @Parameter: 正整数\n",
120+
"#\n",
121+
"# @Return: 各位数之和\n",
122+
"def sum_digits(num):\n",
123+
" if num >= 10:\n",
124+
" remainder = num % 10\n",
125+
" rest = int(num / 10)\n",
126+
" sum = remainder + sum_digits(rest)\n",
127+
" else:\n",
128+
" sum = num\n",
129+
" return sum\n",
130+
"\n",
131+
"\n",
132+
"assert(sum_digits(1234) == 10)\n",
133+
"\n",
134+
"# @FullName: split_num\n",
135+
"#\n",
136+
"# @Brief: 将实数依据小数点拆分为左右两个正整数\n",
137+
"#\n",
138+
"# @Parameter: 数字字符串\n",
139+
"#\n",
140+
"# @Return: 拆分后的两个正整数\n",
141+
"def split_num(target):\n",
142+
" if target.count('.') == 1:\n",
143+
" left = abs(int(target.split('.')[0]))\n",
144+
" right = int(target.split('.')[1])\n",
145+
" return left, right\n",
146+
"\n",
147+
" else:\n",
148+
" return abs(int(target)), 0\n",
149+
"\n",
150+
"\n",
151+
"assert(split_num(\"-23.45\") == (23, 45))\n",
152+
"\n",
153+
"num_str = input(\"Please input a number:\")\n",
154+
"if num_str.count('.') > 1:\n",
155+
" print(\"Input Error!\")\n",
156+
"else:\n",
157+
" num_left, num_right = split_num(num_str)\n",
158+
" \n",
159+
" sum = sum_digits(num_left)\n",
160+
" sum += sum_digits(num_right)\n",
161+
" print(\"%s: The sum of the individual digits is %d\" % (num_str, sum))\n"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": 31,
167+
"metadata": {},
168+
"outputs": [
169+
{
170+
"output_type": "stream",
171+
"name": "stdout",
172+
"text": "33 is a composite number\n"
173+
}
174+
],
175+
"source": [
176+
"NO_PRIME_COMPOSITE = 0\n",
177+
"PRIME = 1\n",
178+
"MIN_PRIME_COMPOSITE = 2\n",
179+
"COMPOSITE = 3\n",
180+
"\n",
181+
"\n",
182+
"# @FullName: is_prime_composite\n",
183+
"#\n",
184+
"# @Brief: 判断数字为素数or合数\n",
185+
"#\n",
186+
"# @Parameter: 整数\n",
187+
"#\n",
188+
"# @Return: 既不是素数也不是合数 / 素数 / 合数\n",
189+
"def is_prime_composite(num):\n",
190+
" if num < MIN_PRIME_COMPOSITE:\n",
191+
" return NO_PRIME_COMPOSITE\n",
192+
"\n",
193+
" low_bound = 2\n",
194+
" up_bound = int(num / 2 + 1)\n",
195+
" for i in range(low_bound, up_bound):\n",
196+
" if num % i == 0:\n",
197+
" return COMPOSITE\n",
198+
"\n",
199+
" return PRIME\n",
200+
"\n",
201+
"\n",
202+
"assert(is_prime_composite(1) == NO_PRIME_COMPOSITE)\n",
203+
"assert(is_prime_composite(2) == PRIME)\n",
204+
"assert(is_prime_composite(4) == COMPOSITE)\n",
205+
"\n",
206+
"num_str = input(\"Please input a number:\")\n",
207+
"if num_str.count('.') >= 1:\n",
208+
" print(\"%s is neither prime nor composite\" % num_str)\n",
209+
"else:\n",
210+
" num_int = int(num_str)\n",
211+
" \n",
212+
" val = is_prime_composite(num_int)\n",
213+
" if val == NO_PRIME_COMPOSITE:\n",
214+
" print(\"%d is neither prime nor composite\" % num_int)\n",
215+
" elif val == PRIME:\n",
216+
" print(\"%d is a prime number\" % num_int)\n",
217+
" else:\n",
218+
" print(\"%d is a composite number\" % num_int)\n"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": 13,
224+
"metadata": {},
225+
"outputs": [
226+
{
227+
"output_type": "stream",
228+
"name": "stdout",
229+
"text": "The random number is 6\n"
230+
}
231+
],
232+
"source": [
233+
"from random import randint\n",
234+
"min_num = int(input(\"Please input a min number\"))\n",
235+
"max_num = int(input(\"Please input a max number\"))\n",
236+
"\n",
237+
"if min_num > max_num:\n",
238+
" print(\"Input error!\")\n",
239+
"else:\n",
240+
" print(\"The random number is %d\" % randint(min_num, max_num))\n"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 38,
246+
"metadata": {},
247+
"outputs": [
248+
{
249+
"output_type": "stream",
250+
"name": "stdout",
251+
"text": "The original list is: ['a', 'b', 'a', 'd', 'd', 'c']\nThe list after removing duplicates is: ['a', 'b', 'd', 'c']\n"
252+
}
253+
],
254+
"source": [
255+
"# @FullName: rm_dups\n",
256+
"#\n",
257+
"# @Brief: 删除列表重复项\n",
258+
"#\n",
259+
"# @Parameter: 原列表\n",
260+
"#\n",
261+
"# @Return: 删除重复项后的列表\n",
262+
"def rm_dups(target):\n",
263+
" temp_list = []\n",
264+
" for element in target:\n",
265+
" if element not in temp_list:\n",
266+
" temp_list.append(element)\n",
267+
" return temp_list\n",
268+
"\n",
269+
"\n",
270+
"assert(rm_dups([1, 2, 1]) == [1, 2])\n",
271+
"\n",
272+
"original_list = ['a', 'b', 'a', 'd', 'd', 'c']\n",
273+
"print(\"The original list is:\", original_list)\n",
274+
"\n",
275+
"final_list = rm_dups(original_list)\n",
276+
"# final_list = list(set(original_list))\n",
277+
"print(\"The list after removing duplicates is:\", final_list)\n",
278+
"\n",
279+
"# *=====End File=====* #\n",
280+
""
281+
]
282+
}
283+
]
284+
}

0 commit comments

Comments
 (0)