Python基础语法知识归纳(持续更新)

查阅

Python3 循环语句

range() 函数可创建一个整数列表,一般用在 for 循环中

#函数语法
    range(start, stop[, step])
start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);
stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5;
step:步长,默认为+1。例如:range(0,5) 等价于 range(0, 5, 1),range(5,0,-1)递减1时必须标明

#range在 for 中的使用
    str0 = 'crime'
    for i in range(len(x)) :
    print(x[i])
    #输出
    'crime'
     
#注意事项
Python3.x 中 range() 函数返回的结果是一个整数序列的对象,而不是列表,但是可以利用 list 函数返回列表,即:
    list(range(10))
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Python3 字符串

#字符串转列表str >>>list
特殊
    str1 = "hi hello world"
    print(str1.split(" "))
    #输出:
    ['hi', 'hello', 'world']
通用
    str="12345";
    number=[];
    for s in str:
        number.append(s);
    print(number)
    #输出:
    ['1','2','3']

#列表转字符串list>>>str 
    l = ["hi","hello","world"]
    print(" ".join(l))
    #输出:
    'hi hello world'

Python3 正则表达式查阅

猜你喜欢

转载自www.cnblogs.com/crime/p/10510363.html