python学习笔记

六眼飞鱼酱① 提交于 2020-03-06 04:28:42

元组

定义一个元素的元组. t = (1,) 逗号不能少
命名元组:
from collections import namedtuple
Student = namedtuple(‘Student’, ‘name age’)
tom = Student(‘tom’, 20)
tom.name

字符串:
center/zfill/rjust/ljust

函数参数

顺序:普通参数、默认参数、可变位置参数、key-world only参数、关键字参数
def fun(x, y, z=1, *args, m, *kwargs):
print(“x={0}, y={1}, z={2} m={3}”.format(x, y, z,m))
print(args)
print(kwargs)

format

   "{}  {1} {xxx}".format(*agrs, **kwagrs) -> str
    args为位置参数;kwargs为关键字参数
    "{0[0]}.{0[1]}".format(('a','com')) -> a.com      

from collections import namedtuple
Point = namedtuple(‘Point’,‘x,y’)
p = Point(4,5)
“{{{0.x},{0.y}}}”.format§
‘{4,5}’

"{0}{1}={2:<2}".format(3,2,23)
'3*2=6 ’

"{0}{1}={2:<02}".format(3,2,23)
‘3*2=60’

"{0}{1}={2:>02}".format(3,2,23)
‘3*2=06’

“{:^30}”.format(“centered”)
’ centered ’

“{:*^30}”.format(“centered”)
centered

显示十六进制/八进制/二进制
“int:{0:d}; hex:{0:#x} oct:{0:#o} bin:{0:#b}”.format(42)
‘int:42; hex:0x2a oct:0o52 bin:0b101010’

算法

[打印三角形]

def printSJX(n):
tail = " “.join([str(i) for i in range(n, 0, -1)])
width = len(tail)
for i in range(1, n):
#print(”{0:>{1}}".format(" “.join([str(j) for j in range(i, 0, -1)]), width))
#print(”{:>{}}".format(" “.join([str(j) for j in range(i, 0, -1)]), width))
stri = ’ '.join( [str(j) for j in range(i, 0, -1)] )
print(”{0}{1}".format(’ ’ * (width - len(stri)), stri))
print tail

def printSJX2(n):
head = " ".join([str(i) for i in range(n, 0, -1)])
print head
width = len(head)
for i in range(width):
if head[i] == ’ ':
print ’ ’ * i, head[i+1:]

[打印杨辉三角]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!