Skip to content

Commit e20ebcc

Browse files
committed
add Python_practice1.ipynb
1 parent a0b034b commit e20ebcc

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed

Python_practice1.ipynb

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
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": 1,
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"output_type": "stream",
33+
"name": "stdout",
34+
"text": "The variables before swapping are: 20 35\nThe variables after swapping are: 35 20\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: var_one, var_two\n",
52+
"#\n",
53+
"# @Return: var_two, var_one\n",
54+
"\n",
55+
"\n",
56+
"def swap(var_one, var_two):\n",
57+
" return var_two, var_one\n",
58+
"\n",
59+
"\n",
60+
"variable_one = input(\"Please input the first variable:\")\n",
61+
"variable_two = input(\"Please input the second variable:\")\n",
62+
"print(\"The variables before swapping are:\", variable_one, variable_two)\n",
63+
"variable_one, variable_two = swap(variable_one, variable_two)\n",
64+
"# variable_one, variable_two = variable_two, variable_one\n",
65+
"print(\"The variables after swapping are:\", variable_one, variable_two)\n"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 9,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"output_type": "stream",
75+
"name": "stdout",
76+
"text": "23 converts to Fahrenheit is 73.4\n"
77+
}
78+
],
79+
"source": [
80+
"# @FullName: cel_to_fah\n",
81+
"#\n",
82+
"# @Brief: 摄氏度转华氏度\n",
83+
"#\n",
84+
"# @Parameter: 摄氏度\n",
85+
"#\n",
86+
"# @Return: 华氏度\n",
87+
"\n",
88+
"\n",
89+
"def cel_to_fah(cel):\n",
90+
" return cel * 1.8 + 32\n",
91+
"\n",
92+
"\n",
93+
"celsius = eval(input(\"Please input celsius:\"))\n",
94+
"fahrenheit = cel_to_fah(celsius)\n",
95+
"print(celsius, \"converts to Fahrenheit is\", fahrenheit)\n",
96+
""
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 11,
102+
"metadata": {},
103+
"outputs": [
104+
{
105+
"output_type": "stream",
106+
"name": "stdout",
107+
"text": "23.456: The sum of the individual digits is 20\n"
108+
}
109+
],
110+
"source": [
111+
"# @FullName: digit_sum\n",
112+
"#\n",
113+
"# @Brief: 计算正整数各个位数之和\n",
114+
"#\n",
115+
"# @Parameter: 正整数\n",
116+
"#\n",
117+
"# @Return: 各位数之和\n",
118+
"\n",
119+
"\n",
120+
"def digit_sum(num):\n",
121+
" if num >= 10:\n",
122+
" remainder = num % 10\n",
123+
" rest = int(num / 10)\n",
124+
" sum = remainder + digit_sum(rest)\n",
125+
" else:\n",
126+
" sum = num\n",
127+
" return sum\n",
128+
"\n",
129+
"\n",
130+
"# @FullName: num_split\n",
131+
"#\n",
132+
"# @Brief: 将实数依据小数点拆分为左右两个正整数\n",
133+
"#\n",
134+
"# @Parameter: 数字字符串\n",
135+
"#\n",
136+
"# @Return: 拆分后的两个正整数\n",
137+
"\n",
138+
"\n",
139+
"def num_split(str_num):\n",
140+
" if str_num.count('.') == 1:\n",
141+
" left = abs(int(str_num.split('.')[0]))\n",
142+
" right = int(str_num.split('.')[1])\n",
143+
" return left, right\n",
144+
" else:\n",
145+
" return abs(int(str_num)), 0\n",
146+
"\n",
147+
"\n",
148+
"number_str = input(\"Please input a number:\")\n",
149+
"num_left, num_right = num_split(number_str)\n",
150+
"sum = digit_sum(num_left)\n",
151+
"sum += digit_sum(num_right)\n",
152+
"print(\"%s: The sum of the individual digits is %d\" % (number_str, sum))\n",
153+
""
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 14,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"output_type": "stream",
163+
"name": "stdout",
164+
"text": "88 is a composite number\n2 * 44 is 88\n"
165+
}
166+
],
167+
"source": [
168+
"MIN_PRIME_COMPOSITE = 2\n",
169+
"\n",
170+
"\n",
171+
"# @FullName: is_prime\n",
172+
"#\n",
173+
"# @Brief: 判断数字为素数or合数\n",
174+
"#\n",
175+
"# @Parameter: 实数\n",
176+
"#\n",
177+
"# @Return: 0:既不是素数也不是合数 / 1:素数 / else:合数\n",
178+
"\n",
179+
"\n",
180+
"def is_prime(num):\n",
181+
" if num < MIN_PRIME_COMPOSITE:\n",
182+
" return 0\n",
183+
" low_bound = 2\n",
184+
" up_bound = int(num / 2 + 1)\n",
185+
" for i in range(low_bound, up_bound):\n",
186+
" if num % i == 0:\n",
187+
" return i\n",
188+
" return 1\n",
189+
"\n",
190+
"\n",
191+
"num_str = input(\"Please input a number:\")\n",
192+
"if num_str.count('.') >= 1:\n",
193+
" print(\"%s is neither prime nor composite\" % num_str)\n",
194+
"else:\n",
195+
" number = int(num_str)\n",
196+
" val = is_prime(number)\n",
197+
" if val == 0:\n",
198+
" print(\"%d is neither prime nor composite\" % number)\n",
199+
" elif val == 1:\n",
200+
" print(\"%d is a prime number\" % number)\n",
201+
" else:\n",
202+
" print(\"%d is a composite number\" % number)\n",
203+
" print(\"%d * %d is %d\" % (val, number / val, number))\n",
204+
""
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": 13,
210+
"metadata": {},
211+
"outputs": [
212+
{
213+
"output_type": "stream",
214+
"name": "stdout",
215+
"text": "The random number is 6\n"
216+
}
217+
],
218+
"source": [
219+
"from random import randint\n",
220+
"min_num = int(input(\"Please input a min number\"))\n",
221+
"max_num = int(input(\"Please input a max number\"))\n",
222+
"if min_num > max_num:\n",
223+
" print(\"Input error!\")\n",
224+
"else:\n",
225+
" print(\"The random number is %d\" % randint(min_num, max_num))\n",
226+
""
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": 6,
232+
"metadata": {},
233+
"outputs": [
234+
{
235+
"output_type": "stream",
236+
"name": "stdout",
237+
"text": "The original list is: ['a', 'b', 'a', 'd', 'd', 'c']\nThe list after removing duplicates is: ['a', 'b', 'd', 'c']\n"
238+
}
239+
],
240+
"source": [
241+
"# @FullName: remove_list_duplicates\n",
242+
"#\n",
243+
"# @Brief: 删除列表重复项\n",
244+
"#\n",
245+
"# @Parameter: 原列表\n",
246+
"#\n",
247+
"# @Return: 删除重复项后的列表\n",
248+
"\n",
249+
"\n",
250+
"def remove_list_duplicates(ori_list):\n",
251+
" temp_list = []\n",
252+
" for element in ori_list:\n",
253+
" if element not in temp_list:\n",
254+
" temp_list.append(element)\n",
255+
" return temp_list\n",
256+
"\n",
257+
"\n",
258+
"original_list = ['a', 'b', 'a', 'd', 'd', 'c']\n",
259+
"print(\"The original list is:\", original_list)\n",
260+
"final_list = remove_list_duplicates(original_list)\n",
261+
"print(\"The list after removing duplicates is:\", final_list)\n",
262+
"\n",
263+
"# *=====End File=====* #\n"
264+
]
265+
}
266+
]
267+
}

0 commit comments

Comments
 (0)