What is Python?
Python is an interpreted(解释型),interacive(交互性),object-oriented-programming (面向对象)language,which can be used in many ways such as artifical intelligence, GUI development, data analytics, and so on.
1.Define a function to convert variabes(通过函数实现参数形式转换)
- IP 地址转换
1
2
3
4
5
6
7
8
9
10
11
12
13#定义函数
def func(x):
#带入参数去除空格,已'.'分割,存入list
lis = x.strip().split('.')
#将list中的参数转全部换成二进制形式
li = [bin(int(i)) for i in lis]
#根据list中参数长度将参数中的‘0b’转换成不同位数的0
li2 = [i.replace('0b',(10-len(i))*'0') for i in li]
#将二进制转换成十进制并整合
return int(''.join(li2),2)
ip_address=func('10.24.3.12')
print(ip_address) 
2.Deine a function to display the multiplication table(通过函数实现乘法表)
1  | def fun(x,y):  | 
3.Python recursion(递归)
计算机默认的最大递归深度在1000左右,python最大递归深度一般在4000左右(取决于计算机的性能)
递归和循环的区别:
- 循环:重复数据操作直到达到目标条件
 - 递归:粗略的可理解为在程序函数中再次调用此函数
1
2
3
4
5#loop
n=0
for i in range(101):
n=n+i
print(n)在上述两个程序中都实现了0到100的求和,也区别出循环和递归的不同1
2
3
4
5
6
7#recursion
def func(x):
if x==1:
return 1
else:
return x+func(x-1)
print(func(100)) 
4.Reading data from multiple lines in the json file(从json文件中读取多行数据)
在.json文件中如何逐行读取数据(How to handle Python json.loads shows ValueError:Extra data)
if you have more than one data in your json file,json.loads()would not be able to decode more than one
1  | user_data=open("accounts.json",'r')readlines()  | 
5.Dumping data to the file(保存数据到json文件中)
1  | #define a new function  | 
6.Data flow in Python
数据在两个函数间传输及操作
加载函数(load)
1  | def load(filename):  | 
操作函数(operate)
1  | def operate(obj):  | 
main function
1  | def main():  | 
7.列表内不重复的组合
1  | def func(li):  | 
8.列表内不重复的数据
1  | def solution(A):  | 
9.判断是否为等差数列
1  | def func(n):  | 
10.判断是否为素数
1  | num=int(input("Enter:").strip())  | 
Updated on 01/07/21