单行语句:在同一行写多个语句可以用;隔开
多行语句:如果语句很长,可以用反斜杠 \ 来实现换行
例如:
item_one,item_two,item_three=1,2,3
total = item_one +\
item_two +\
item_three
print(total)
也可以用[]、{}、()来进行换行
例如:
item_one,item_two,item_three,item_four,item_five=1,2,3,4,5
total2 = (item_one +
item_two +
item_three +
item_four +
item_five)
print(total2,type(total2))
数字(Number)类型
python中数字有四种类型:整数、布尔型、浮点数和复数。
- int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。
- bool (布尔), 如 True。
- float (浮点数), 如 1.23、3E-2
- complex (复数), 如 1 + 2j、 1.1 + 2.2
字符串(String)
- 转义符 \。
- 反斜杠可以用来转义,使用 r 可以让反斜杠不发生转义。 如 r"this is a line with \n" 则 \n 会显示,并不是换行。
- 按字面意义级联字符串,如 "this " "is " "string" 会被自动转换为 this is string。
- 字符串可以用 + 运算符连接在一起,用 * 运算符重复
- Python 中的字符串有两种索引方式,从左往右以 0 开始,从右往左以 -1 开始。
- Python 中的字符串不能改变。
- Python 没有单独的字符类型,一个字符就是长度为 1 的字符串。
- 字符串的截取的语法格式如下:变量[头下标:尾下标:步长]
print 输出
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="":
x="a"
y="b"
# 换行输出
print( x )
print( y )
print('---------')
# 不换行输出
print( x, end=" " )
print( y, end=" " )
print()
import 与 from...import
在 python 用 import 或者 from...import 来导入相应的模块。
将整个模块(somemodule)导入,格式为: import somemodule
从某个模块中导入某个函数,格式为: from somemodule import somefunction
从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc
将某个模块中的全部函数导入,格式为: from somemodule import *