当前位置 : 主页 > 编程语言 > python >

Python基础教程:7个经典程序示例

来源:互联网 收集:自由互联 发布时间:2022-06-18
return result print(li) print('插入排序:',insertSort3(li)) print('选择排序:',selectSort(li)) print('冒泡排序:',bubbleSort(li)) que6() 2.调换字典键值 1. 调换元素.\ def que1(): d={1:one,2:two} 方法1 --- 动态赋值

return result

print(li)

print('插入排序:',insertSort3(li))

print('选择排序:',selectSort(li))

print('冒泡排序:',bubbleSort(li))

que6()

2.调换字典键值

1. 调换元素.\

def que1():

d={1:"one",2:"two"}

方法1 --- 动态赋值

def method1(d):

d = d.copy()

result = {}

for k,v in d.items():

result[v] = k

return result

方法2 --- 生成器

def method2(d):

d = d.copy()

result = {v:k for k,v in d.items()}

return result

方法3 --- 由值寻找键

def method3(d):

d = d.copy()

由键寻找值

def match(dic, b):

return [k for k,v in dic.items() if v == b]

先生成key-None,再赋值

result = {}

result = result.fromkeys(d.values())

for k in result.keys():

result[k] = match(d, k)[0]

return result

方法4 --- 列表转字典 < 直接转换/动态赋值 >

def method4(d):

d = d.copy()

key = d.keys()

val = d.values()

data = list(zip(key, val))

方法4-1

result1 = {}

for i in range(len(data)):

result1[data[i][1]] = data[i][0]

方法4-2

result2 = dict(zip(val, key))

return result1, result2

print('新列表动态赋值方法:{}'.format(method1(d)))

print('生成器方法:{}'.format(method2(d)))

print('由键寻值方法:{}'.format(method3(d)))

print('动态赋值列表转字典方法:{}'.format(method4(d)[0]))

print('直接列表转字典方法:{}'.format(method4(d)[1]))

que1()

3.删除列表中的重复元素

'''

遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025

寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!

'''

3. 删除重复元素list =[1,2,5,4,1,5,6,8,0,2,5]

a = np.random.randint(-100, 100, size=10)

a = a.tolist()

def method1(a):

a = a.copy()

a = set(a)

return a

def method2(a):

b = a.copy()

c = 0

for i in range(len(a)-1):

if b[i+c] in b[:i+c]+b[i+c+1:]:

b.pop(i+c)

c -= 1

return b

print('集合法:',method1(a))

print('遍历法:',method2(a))

4.输出质数

def prime(end):

prime_list = []

if end <= 1:

print('必须大于1')

else:

prime_list.append(2)

for i in range(2, end+1, 1):

count = 0

if i == 2:

if i%2 != 0:

prime_list.append(2)

else:

for m in range(2, i):

能够整除,则跳出循环

if (i % m) == 0:

print(i, m)

break

否则计数+1

else:

count += 1

判断是否整除完成(0/n)

if count == i - 2:

prime_list.append(i)

print(count, i, m)

return (prime_list)

num = int(input('想输出2到多少?'))

print(prime(num))

5.判断是一年中第几天

def que3():

3.输入某年某月某日,判断这一天是这一年的第几天?:

闰年判断函数

def judge_leap(num):

date = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

(四年一闰 and not百年) or 四百年闰

if (num % 4 == 0 and num % 100 != 0) or num % 400 ==0:

date[1] =29

return date

格式转换

date = (input('请输入一个日期,格式如:“2018.02.12”:'))

date_list = (list(map(int, (date.split('.')))))

遍历计算天数

day = date_list[2]

for i in range(date_list[1]):

day += judge_leap(date_list[0])[i]

print('{}月{}日是{}年的第{}天\n'.format(date_list[1], date_list[2], date_list[0], day))

que3()

6.猜数字

重新猜数字

import random

def judge_num(num, num_random):

if num > num_random:

print('It\'s too big')

return 1

elif num < num_random:

print('It\'s too small')

return 1

else:

print("Congratulation!! That\' right!")

return 0

产生随机数

num_start = int(input('Digital lower limit of guess number:\n'))

num_end = int(input('Digital upper limit of guess number:\n'))

num_random = random.randint(num_start, num_end)

参数初始化

result = 1 # 判断结果

i = 0 # 循环次数

frequency = 3 # 循环限制次数

提示总猜测次数、剩余次数

print('WARNING: You have【{}】 chances you guess '.format(frequency), end = '--&&>>--')

print('【{}】 chances left now:\n'.format(frequency - i +1))

while result and i != frequency:

猜数字

num = int(input('Please guess a int_number:\n'))

result = judge_num(num, num_random)

i += 1

7.进制转换

任意进制转十进制

def other_to_decimal(hex, num):

整型转化为列表,

num_str = str(num)

map()将List对象中的元素(list类型)转化为集合(set)类型

网友评论