目录
- 注释
- Numbers
- Strings
- List
注释
print('Hello World') # 打印 Hello World
Numbers
数字支持的操作
+
-
*
-
/
两个数相除,总是返回浮点数 -
//
相除,但是返回整数(向下取整) -
%
取余 -
**
乘方
In [10]: 4 / 2
Out[10]: 2.0
# 向下取整
In [12]: 5 / 3
Out[12]: 1.6666666666666667
In [13]: 5 // 3
Out[13]: 1
In [14]: 5 /- 3
Out[14]: -1.6666666666666667
In [15]: 5 // -3
Out[15]: -2
# 乘方
In [16]: 2 ** 3
Out[16]: 8
在交互模式中,上一次数字运算的结果会被保存在变量 _
中
In [23]: 5 + 1
Out[23]: 6
In [24]: _ + 4
Out[24]: 10
Python 支持的数字类型包括: int
, float
,Decimal
,Fraction
, complex numbers
复数可以用 j
或 J
表示虚数部分: 3+5j
字符串
字符串可以用 单/双引号 表示;
如果字符串里面包含一种引号(单引号),则要用另一种引号(双引号)来表示字符串;若包含两种引号,则需要用到转移序列符号 \
In [26]: 'Hello world'
Out[26]: 'Hello world'
In [27]: "Hello world"
Out[27]: 'Hello world'
当需要是字符串中的 \
成为普通字符是,除了转义本身外(\\
),还可以使用 raw string
,raw string
将 \
当做普通字符对待,常用于正则表达式中;
In [29]: print('c:\some\name')
c:\some
ame
In [30]: print(r'c:\some\name') # raw string; 在字符串前面加上 r 标识
c:\some\name
多行字符串
用三个引号可以表示跨多行字符串;跨多行字符串会自动包含换行符,可以在行尾使用 \
阻止自动包含换行符
In [33]: print('''
...: Student:
...: Tom,
...: John''')
Student:
Tom,
John
In [34]: print('''\
...: Student:
...: Tom,
...: John''')
Student:
Tom,
John
字符串操作符
-
+
连接字符串 -
*
重复字符串
In [35]: 'Hello ' + 'world'
Out[35]: 'Hello world'
In [36]: 'Hello ' * 3
Out[36]: 'Hello Hello Hello '
字符串索引(index)和切片(slice)
In [37]: word = 'python'
# 索引
In [38]: word[0]
Out[38]: 'p'
In [39]: word[-1]
Out[39]: 'n'
# 切片
In [40]: word[2:4]
Out[40]: 'th'
# 切片默认值 [0:-1]
In [41]: word[2:]
Out[41]: 'thon'
字符串属于不可变类型(immutable),即创建后就不能被修改
len()
返回字符串的长度
In [43]: len(word)
Out[43]: 6
See also
Text Sequence Type — str: Strings are examples of sequence types, and support the common operations supported by such types.
String Methods: Strings support a large number of methods for basic transformations and searching.
Formatted string literals: String literals that have embedded expressions.
Format String Syntax: Information about string formatting with
str.format()
.printf-style String Formatting: The old formatting operations invoked when strings are the left operand of the
%
operator are described in more detail here.
List 列表
列表可以容纳不同类型的属性,但一般列表里面的数据都是相同类型的
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
列表支持索引和切片
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
返回一个列表的副本
>>> squares[:]
[1, 4, 9, 16, 25]
列表是一种可变类型(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]
列表支持连接操作:再列表后面添加一个列表
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
list.append()
方法在列表后面添加一个元素
>>> cubes.append(216) # add the cube of 6
>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
列表支持切片赋值
>>> 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
[]
函数 len()
返回列表的长度
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4