Tianhao Cui

It is about code, it is not about code

  • Home
  • Code
  • Apple
  • Others
All Blogs My Verse

Tianhao Cui

It is about code, it is not about code

  • Home
  • Code
  • Apple
  • Others

Python Questions

2020-05-15

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
2
3
4
5
6
def fun(x,y):
for i in range(x,y):
for j in range(x,i+1):
print("%s*%s=%2s" % (j,i,i*j), end="\t")
print()
fun(1,10)

3.Python recursion(递归)

计算机默认的最大递归深度在1000左右,python最大递归深度一般在4000左右(取决于计算机的性能)

递归和循环的区别:

  • 循环:重复数据操作直到达到目标条件
  • 递归:粗略的可理解为在程序函数中再次调用此函数
    1
    2
    3
    4
    5
    #loop
    n=0
    for i in range(101):
    n=n+i
    print(n)
    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))
    在上述两个程序中都实现了0到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
2
3
4
5
user_data=open("accounts.json",'r')readlines()
for line in user_data:
user=json.loads(line)
#print all data from the file
print(user)

5.Dumping data to the file(保存数据到json文件中)

1
2
3
4
5
6
7
#define a new function
def dump_accounts(account_dict):
#open the file 'a'=append
with open("accounts.json",'a') as f:
data=json.dump(account_dict,f)
#the data should be stored in different lines
f.write('\n')

6.Data flow in Python

数据在两个函数间传输及操作
加载函数(load)

1
2
3
4
5
6
7
8
9
10
11
12
def load(filename):
#检验给出的路径是否是一个文件
if os.path.isfile(filename):
#只读模式
with open(filename,'r') as f:
while True:
try:
l=f.read()
return l
#print(l)
except:
break

操作函数(operate)

1
2
3
4
5
6
7
8
def operate(obj):
usr=input("Enter your name:>>").strip()
t=load(obj)
print(t)
if usr in t:
print("Welcome")
else:
print("no result!")

main function

1
2
3
4
5
6
7
8
9
10
def main():
#创建文件
f=open('data.txt','w')
#写文件
f.write('Hao\n')
f.write('Bei')
#关闭文件
f.close()
#调用函数
operate('data.txt')

7.列表内不重复的组合

1
2
3
4
5
6
7
8
9
10
def func(li):
length=len(li)
for i in range(length):
for j in range(length):
if(li[i]!=li[j]):
print(i,j)
else:
pass
my_list=[1,2,3,4,5]
func(my_list)

8.列表内不重复的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def solution(A):
length=len(A)
new_list=[]
index=0
for i in range(length):
if(len(new_list)==0):
new_list.append(A[i])
index=index+1
if A[i] not in new_list:
new_list.append(A[i])
print(new_list)
index=index+1
if A[i] in new_list:
pass
return index
my_list=[7,3,7,3,1,3,4,1,4,5,6,7,8,9,10,19,10,10,10,10,6,7,8,9]
print(solution(my_list))

9.判断是否为等差数列

1
2
3
4
5
6
7
8
def func(n):
l=len(n)
for i in range(l-2):
if(n[i+1]-n[i]!=n[i+2]-n[i+1]):
print("False")
print("True")
my_list=[1,3,5,7,9]
func(my_list)

10.判断是否为素数

1
2
3
4
5
6
7
8
num=int(input("Enter:").strip())
for i in range(2,num+1):
for j in range(2,i):
print("!!!",j)
if i%j==0:
break
else:
print(i)

Updated on 01/07/21

  • Code
  • Python

扫一扫,分享到微信

微信分享二维码
Day1-Python-Basics
Hello World
© 2021 Tianhao Cui
Hexo Theme Yilia by Litten
  • All Blogs
  • My Verse

tag:

  • Code
  • Blockchain
  • Python
  • Others
  • Interview
  • Apple
  • MacOS
  • Algorithm
  • iPhone

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

And now these three remain:faith,hope,love
But the greatest of these is love