python程序完整代码_⼲货满满,30个Python源代码!
1.⼗进制转换为⼆进制
>>> bin(10)
'0b1010'
2.⼗进制转换为⼋进制
>>> oct(9)
'0o11'
3.⼗进制转换为⼗六进制
string2>>> hex(15)
'0xf'
4.字符串转换为字节类型
>>> s = "apple"
>>> bytes(s,encoding='utf-8')
b'apple'
5.字符类型、数值型等转换为字符串类型
>>> i = 100
>>> str(i)
'100'
6.⼗进制整数对应的 ASCII 字符
>>> chr(65)
'A'
7.ASCII字符对应的⼗进制数
>>> ord('A')
65
8.整数或数值型字符串转换为浮点数
>>> float(3)
3.0
9.创建数据字典的⼏种⽅法
>>> dict()
{}
>>> dict(a='a',b='b')
{'a': 'a', 'b': 'b'}
>>> dict(zip(['a','b'],[1,2]))
>>> dict([('a',1),('b',2)])
{'a': 1, 'b': 2}
10.排序函数
>>> a = [1,4,2,3,1]
#降序
>>> sorted(a,reverse=True)
[4, 3, 2, 1, 1]
>>> a = [{'name':'xiaoming','age':18,'gender':'male'}, {'name':'xiaohong','age':20,'gender':'female'}]
#按 age升序
>>> sorted(a,key=lambda x: x['age'],reverse=False) [{'name': 'xiaoming', 'age': 18, 'gender': 'male'},
{'name': 'xiaohong', 'age': 20, 'gender': 'female'}] 11.求和函数
>>> a = [1,4,2,3,1]
>>> sum(a)
11
#求和初始值为1
>>> sum(a,1)
12
12.计算字符串型表达式的值
>>> s = "1 + 3 +5"
>>> eval(s)
9
>>> eval('[1,3,5]*3')
[1, 3, 5, 1, 3, 5, 1, 3, 5]
13.获取⽤户输⼊内容
>>> input()
I'm typing
"I'm typing "
14.print ⽤法
>>> lst = [1,3,5]
# f 打印
lst: [1, 3, 5]
# format 打印
>>> print('lst:{}'.format(lst))
lst:[1, 3, 5]
15.格式化字符串常见⽤法
>>> print("i am {0},age {1}".format("tom",18))
i am tom,age 18
>>> print("{:.2f}".format(3.1415926)) # 保留⼩数点后两位
3.14
>>> print("{:+.2f}".format(-1)) # 带符号保留⼩数点后两位
-1.00
>>> print("{:.0f}".format(2.718)) # 不带⼩数位
3
>>> print("{:0>3d}".format(5)) # 整数补零,填充左边, 宽度为3
005
>>> print("{:,}".format(10241024)) # 以逗号分隔的数字格式
10,241,024
>>> print("{:.2%}".format(0.718)) # 百分⽐格式
71.80%
>>> print("{:.2e}".format(10241024)) # 指数记法
websocket在线测试代码python1.02e+07c语言教程视频下载
值(值得注意,⾃定义的实例都可哈希,list, dict, set等可变对象都不可哈希)>>> class Student():
def __init__(self,id,name):
self.id = id
self.name = name
>>> xiaoming = Student('001','xiaoming')
>>> hash(xiaoming)
-9223371894234104688
16.if not x
直接使⽤ x 和 not x 判断 x 是否为 None 或空
x = [1,3,5]
if x:
if not x:
print('x is empty')
17.打开⽂件,并返回⽂件对象
>>> import os
>>> os.chdir('D:/source/dataset')
>>> os.listdir()
['drinksbycountry.csv', 'IMDB-Movie-Data.csv', 'movietweetings',
'titanic_eda_data.csv', 'titanic_train_data.csv']
python基础代码大全加翻译>>> o = open('drinksbycountry.csv',mode='r',encoding='utf-8')
>>> o.read()
"country,beer_servings,spirit_servings,wine_servings,total_litres_of_pur
e_alcohol,continent\nAfghanistan,0,0,0,0.0,Asia\nAlbania,89,132,54,4.9,"
18. 创建迭代器
>>> class TestIter():
def __init__(self,lst):
self.lst = lst
# 重写可迭代协议__iter__
def __iter__(self):
print('__iter__ is called')
return iter(self.lst)
迭代 TestIter 类:
>>> t = TestIter()
>>> t = TestIter([1,3,5,7,9])
>>> for e in t:
print(e)
__iter__ is called
1
3
5
7
9
19.创建range迭代器
>>> t = range(11)
>>> for e in t:
print(e)
2
4
6汇编语言中jpa什么意思
8
10
20.反向
>>> rev = reversed([1,4,2,3,1])
>>> for i in rev:
print(i)
1
3
2
4
1
21.打包
>>> x = [3,2,1]
>>> y = [4,5,6]
>>> list(zip(y,x))
[(4, 3), (5, 2), (6, 1)]
>>> for i,j in zip(y,x):
print(i,j)
4 3
5 2
6 1
22.过滤器
关键的英文critical函数通过 lambda 表达式设定过滤条件,保留 lambda 表达式为True的元素:>>> fil = filter(lambda x: x>10,[1,11,2,45,7,6,13])
>>> for e in fil:
print(e)
11

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