彩渐变图片python 实现 30 个案例
1. 身份验证
输⼊密码时,终端中没有回显,可以使⽤getpass模块的getpass函数。
# 身份验证
import getpass
username = input('请输⼊⽤户名: ')
password = pass('请输⼊密码: ')
if username == 'admin'and password == '123456':
print('登录成功!')
else:
print('登录失败!')
2. 列表去重
使⽤ python 的内置函数 set 可以实现去重。
x = [1, 1, 2, 2, 3, 2, 3, 4, 5, 6]
print(list(set(x))) # [1, 2, 3, 4, 5, 6]
3. 列表压缩
去除列表中所有布尔值为 false 的元素。
def filter_false(lst):
return list(filter(bool, lst))
res = filter_false([None, 0, False, '', [], 'martin', [1, 2]]) print(res)  # ['martin', [1, 2]]
4 . 列表等分
将⼀个列表等分为多个列表
from math import ceil
def divide(lst: list, size: int) ->list:
"""
:param lst:  要拆分的列表
:param size: 拆分的⼤⼩
:return: ⼆维数组
"""
if size<= 0:
return [lst]
return [lst[i*size:(i+1) *size] for i in range(0, ceil(len(lst) / size))]
res = divide([1, 2, 3, 4, 5, 7, 8, 9], 2)
print(res)  # [[1, 2], [3, 4], [5, 7], [8, 9]]
res = divide([1, 2, 3, 4, 5, 7, 8, 9], 3)
print(res)  # [[1, 2, 3], [4, 5, 7], [8, 9]]常见的简单的网站制作
res = divide([1, 2, 3, 4, 5, 7, 8, 9], 0)
print(res)  # [[1, 2, 3, 4, 5, 7, 8, 9]]
res= divide([1, 2, 3, 4, 5, 7, 8, 9], -2)
print(res)  # [[1, 2, 3, 4, 5, 7, 8, 9]]建站公司介绍
在得到了⼀个多个列表之后,可能会遇到要查这⼏个列表中最⼤那个值是多少?
def max_lists(*lst):
return max(max(*lst, key=lambda v: max(v)))
res = max_lists([[1, 2, 3], [4, 9, 7], [8, 5]])
installing update是什么意思
print(res) # 9
5. 两个列表转为字典
根据两个列表,将其组合成为⼀个字典。
li1 = ['a', 'b']
li2 = [1, 2]
print(dict(zip(li1, li2))) # {'a':1, 'b':2}
6. 字典的键值对颠倒并⽣产新的字典
将字典的 key,value 颠倒调换位置。
dic = {'a': 1, 'b': 2, 'c': 3}
new_dict = {v:k for k, v in dic.items()}
print(new_dict)  # {1: 'a', 2: 'b', 3: 'c'}
7. 合并字典
合并⼏个字典成为⼀个新的⼤字典。
def merge_dict(dic1, dic2):
return {**dic1, **dic2}  # python3.5后⽀持的⼀⾏代码实现合并字典
merge_dict({'a': 1, 'b': 2}, {'c': 3})  # {'a': 1, 'b': 2, 'c': 3}
8. 筛选出值最⼤的字典
如果⼀个字典中的 value 都是不同的整数,怎么样筛选出这些值中的最⼤值呢?
def max_pairs(dic):
if len(dic) == 0:
return dic
max_val = max(map(lambda v: v[1], dic.items()))
return [{k:v} for k,v in dic.items() if v == max_val]
res = max_pairs({'a': 6, 'b': -3, 'c': 6, 'd': 5.1})
print(res) # [{'a': 6}, {'c': 6}]
9. 不⽤else和if实现计算器
operator模块提供了⼀套与Python的内置运算符对应的⾼效率函数。例如,operator.add(x, y)与表达式x+y相同。
from operator import*
def calculator(a, b, k):
return {
'+': add,
'-': sub,
'*': mul,
'/': truediv,
'**': pow
}.get(k, add)(a, b)
# 如果输⼊的不是加减乘除和取幂则默认为加
python基础代码大全黑客
calculator(2,3,'s')    # 7
accepted intocalculator(3, 4, '+')  # 7
calculator(3, 4, '**')  # 81
10. 输⼊三⻆形三个边⻓其计算周⻓和⾯积
判断输⼊的边⻓能否构成三⻆形,如果能则计算出三⻆形的周⻓和⾯积。代码使⽤了math模块的sqrt 函数来计算平⽅根。⽤边⻓计算三⻆形⾯积的公式叫做海伦公式。三⻆形⾯积 A公式如下:
其中。
中国南宋末年数学家秦九韶发现或知道等价的公式,其著作《数书九章》卷五第⼆题即三斜求积。“问沙⽥⼀段,有三斜,其⼩斜⼀⼗三⾥,中斜⼀⼗四⾥,⼤斜⼀⼗五⾥,⾥法三百步,欲知为⽥⼏何?”答⽈:“三百⼗五顷.”其术⽂是:“以⼩斜幂并⼤斜幂,减中斜幂,余半之,⾃乘于上;以⼩斜幂乘⼤斜幂,减上,余四约之为实,……开平⽅得积。”若以⼤斜记为,中斜记为,⼩斜记为,秦九韶的⽅法相当于下⾯的⼀般公式:
其中。
import math
a = float(input('a = '))
b = float(input('b = '))
c = float(input('c = '))
if a+b>c and a+c>b and b+c>a:
print('周⻓: %.2f'% (a+b+c))
p = (a+b+c) /2
area = math.sqrt(p* (p-a) * (p-b) * (p-c))
print('⾯积: %.2f'% (area))
else:
print('不能构成三⻆形')
11. 计算最⼤公约数和最⼩公倍数
输⼊为两个正整数。最⼤公约数:(Greatest Common Divisor),也称最⼤公因数、最⼤公因⼦,指两个或多个整数共有约数中最⼤的⼀个,简称gcd。最⼩公倍数;两个整数公有的倍数称为它们的公倍数,其中最⼩的⼀个正整数称为它们两个的最⼩公倍数(Least Common Multiple),简称lcm。
最⼩公倍数=两数的乘积/最⼤公约(因)数。最⼤公约数可以使⽤欧⼏⾥得算法即辗转相除法求得,gcd(a,b) = gcd(b,a mod b)。mod == % 。
x = int(input('x = '))
y = int(input('y = '))
# 设置 x 为⼩的数
if x>y:
x, y = y, x
for factor in range(x, 0, -1):
if x%factor == 0and y%factor == 0:
print('%d和%d的最⼤公约数是%d'% (x, y, factor))
print('%d和%d的最⼩公倍数是%d'% (x, y, x*y//factor))
# 最⼩公倍数或者可以写成下⾯
print('%d和%d的最⼩公倍数是%d'% (x, y, x/factor*y))
break
函数版:
# 最⼤公约数
def gcd(x, y):
(x, y) = (y, x) if x>y else (x, y)
for factor in range(x, 0, -1):
if x%factor == 0and y%factor == 0:
return factor
# 最⼩公倍数
def lcm(x, y):
return x*y//gcd(x, y)
12. 打印三⻆形图案
打印下⾯所有形状的星星
"""
*
**
***
****
*****
*
**
***
****
*****
*
***
*****
*******
*********
"""
row = int(input('请输⼊⾏数: '))
# 形状 1
for i in range(row):
for_in range(i+1):
print('*', end='')
print()
# 形状 2
for i in range(row):
for j in range(row):
if j<row-i-1:
print(' ', end='')
else:
print('*', end='')
print()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。