python动物代码⼤全_20组常⽤的python代码!速速码了备⽤
~
这次也是⼀⿎作⽓,为了我的⼏⼗个粉丝,我⼜吐⾎整理了20组python代码,赶紧码下~~~
1.for循环求和
sum = 0
for x in list(range(10)):
sum = sum + x
print(sum)
2.while循环
L = []
n = 1
while n <= 99:
L.append(n)
n = n + 2
print(L)
3.if条件判断
age = 20
if age >= 18:
print('your age is', age)
print('adult')
4.写⼊⽂件
with open('yourpath', 'w') as f:
f.write('Hello, world!')
5.列表⽣成式
oops baby i love you 是什么歌print([x * x for x in range(1, 10) if x % 2 == 0])
6.列表切⽚
L = list(range(100))
print(L[:10])
7.正则表达
import re
m = re.search('(?<=abc)def', 'abcdef')
up(0))
8.Lamnda表达式
def make_incrementor(n):
return lambda x: x + n
if __name__ == '__main__':
f = make_incrementor(42)
print(f(0))
print(f(1))
9.快速排序
acoustics是什么意思def quicksort(array):
if len(array) < 2:
return array
else:
pivot = array[0]
smaller = [i for i in array[1: ] if i <= pivot]钢材型号大全及规格
greater = [i for i in array[1: ] if i > pivot]
return quicksort(smaller) + [pivot] + quicksort(greater) print(quicksort([5, 7, 3, 1, 9]))
10.变量交换
a = 1
b = 2
a, b = b, a
print(a, b)
11.@property装饰器
class Wage:
def __init__(self, wage=6000):
self.__wage = wage
@property
def wage(self):
return self.__wage
@wage.setter
def wage(self, value):
if isinstance(value, int):
self.__wage = value
else:
print('error: wage not int type!')
if __name__ == '__main__':
a = Wage(6800)
print(a.wage)
a.wage = 8000
print(a.wage)
12.closure闭包
def make_fact():
def fact(n):
if n == 1:
return 1
else:
return n * fact(n - 1)
return fact
fact = make_fact()
print(fact(7))
13.decorator装饰器
import time
def outer(func):
def inner(*args, **kwargs):
print('%s is called at %s' % (func.__name__, ime())) func(*args, **kwargs)
return inner
python基础代码大全下载@outer
def f(a, b):
print('a + b = ', a + b)
f(1, 2)
14.urllib⽹络爬⾍
quest
request = quest.Request('') response = quest.urlopen(request)
html = ad(50).decode()
print(html, '\n')
print('de() = ', de(), '\n') print('url() = ', url(), '\n')
print('response.info() = ', response.info())
15.fork多进程
import os
pid = os.fork()
if pid < 0:
print('fork failed!')
elif pid == 0:
print('我是⼦进程%s, 我的⽗进程是%s' % (os.getpid(), os.getppid())) else:
openssh和ssh的区别print('我是⽗进程%s, 我的⼦进程是%s' % (os.getpid(), pid))
16.thread多线程
import threading
import time
def action(para):
time.sleep(1)
print('para == %s' % para)
for i in range(10):
t =threading.Thread(target=action,args=(i,))
t.start()
print('main thread end!')
17.运算符重载
添加css的三种方法class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return '({0}, {1})'.format(self.x, self.y)
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Point(x, y)
if __name__ == '__main__':
p1 = Point(3, 5)
p2 = Point(2, -1)
print(p1 + p2)
18.斐波那契数列
def fib(n):
if n <= 2:
return 1
else:
return fib(n-1)+fib(n-2)
print(fib(7))
19.map( )函数
print(list(map((lambda x: x + 10),[1, 2, 3, 4])))
print(list(map(pow, [1, 2, 3], [2, 3, 4])))
print(list(filter((lambda x: x > 0), range(-5, 5))))
import functools
duce((lambda x, y: x + y), [1, 2, 3, 4]))
duce((lambda x, y: x * y), [1, 2, 3, 4]))
如果你还有什么编程类的知识,欢迎私信给我~妥妥的给你解决咯~!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论