Python基本语法
基本运算
-
除法运算/总是返回浮点数,如果要得到一个整数结果可以使用 // 运算符,要计算余数可以使用%,
而且python全面支持浮点数,混合类型运算数的运算会把整数转化为浮点数
17 / 3 # classic division returns a float 5.666666666666667 17 // 3 # floor division discards the fractional part 5 17 % 3 # the % operator returns the remainder of the division 2 5 * 3 + 2 # floored quotient * divisor + remainder 17
-
python使用**运算符计算乘方
5 ** 2 # 5 squared 25 2 ** 7 # 2 to the power of 7 128
-
交互模式下,上一次输出的表达式会赋值给变量_。把python当作计算器使用时,用该变量实现下一步计算更为简单
tax = 12.5 / 100 price = 100.50 price * tax 12.5625 price + _ 113.0625 round(_, 2) 113.06
字符串
-
字符串可以使用 + 合并在一起,也可以使用*进行重复
# 3 times 'un', followed by 'ium' 3 * 'un' + 'ium' 'unununium'
-
相邻两个或多个字符串字面值会自动合并
text = ('Put several strings within parentheses ' 'to have them joined together.') text 'Put several strings within parentheses to have them joined together.'
-
字符串支持索引(下标访问),第一个字符串的索引是0。单字符没有专门的类型,就是长度为1的字符串
word = 'Python' word[0] # character in position 0 'P' word[5] # character in position 5 'n'
-
Python使用负数是倒着进行访问的因为-0和0都是第一个所以倒数第一个是-1
-
Python切片
word[0:2] # characters from position 0 (included) to 2 (excluded) 'Py' word[2:5] # characters from position 2 (included) to 5 (excluded) 'tho' word[:2] # character from the beginning to position 2 (excluded) 'Py' word[4:] # characters from position 4 (included) to the end 'on' word[-2:] # characters from the second-last (included) to the end 'on' word[:2] + word[2:] 'Python' word[:4] + word[4:] 'Python'
-
还可以这样理解切片,就是i~j的一段的字符
+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1
-
索引越界会进行报错,但是切片会自动处理索引越界,而且切片与索引都不能对字符串进行修改,如果要生成不同的字符串应该新建一个字符串
word[42] # the word only has 6 characters Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range word[4:42] 'on' word[42:] '' word[0] = 'J' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment word[2:] = 'py' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment 'J' + word[1:] 'Jython' word[:2] + 'py' 'Pypy'
列表
-
Python 支持多种 复合 数据类型,可将不同值组合在一起。最常用的 列表 ,是用方括号标注,逗号分隔的一组值。列表 可以包含不同类型的元素,但一般情况下,各个元素的类型相同:
squares = [1, 4, 9, 16, 25] squares [1, 4, 9, 16, 25]
-
列表和其他内置的squence类型一样,列表支持切片,索引和合并操作
-
但是列表与字符串不同,列表是mutable就是可以修改的
cubes = [1, 8, 27, 65, 125] # something's wrong here 4 ** 3 # the cube of 4 is 64, not 65! 64 cubes[3] = 64 # replace the wrong value cubes [1, 8, 27, 64, 125]
-
python中的简单赋值绝对不会复制数据,当你将一个链表赋值给一个变量时,该变量将引用现有的列表,你通过一个变量对列表的任何更改都会被引用它的所有变量看到
rgb = ["Red", "Green", "Blue"] rgba = rgb id(rgb) == id(rgba) # they reference the same object True rgba.append("Alph") rgb ["Red", "Green", "Blue", "Alph"]
-
切片操作返回包含请求元素的新列表,以下切片操作会返回列表的浅拷贝,为切片赋值可以改变列表的大小,甚至可以清空整个列表
correct_rgba = rgba[:] correct_rgba[-1] = "Alpha" correct_rgba ["Red", "Green", "Blue", "Alpha"] rgba ["Red", "Green", "Blue", "Alph"] letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] letters ['a', 'b', 'c', 'd', 'e', 'f', 'g'] # replace some values letters[2:5] = ['C', 'D', 'E'] letters ['a', 'b', 'C', 'D', 'E', 'f', 'g'] # now remove them letters[2:5] = [] letters ['a', 'b', 'f', 'g'] # clear the list by replacing all the elements with an empty list letters[:] = [] letters []
-
列表还可以嵌套列表
a = ['a', 'b', 'c'] n = [1, 2, 3] x = [a, n] x [['a', 'b', 'c'], [1, 2, 3]] x[0] ['a', 'b', 'c'] x[0][1] 'b'