1 注释
1.1 以 #号开头的注释
1.2 以 """注释内容"""
1.3 以 '''注释内容'''
2 行与缩进
python 代码块通过缩进对齐表达代码逻辑而不是使用大括号;
缩进表达一个语句属于哪个代码块.
语句块缩进(常用 4 个空格)
3 多行语句
如果书写的语句很长, 可以使用反斜杠 (\) 来实现多行语句, 例如:
- >>> print ("hello \
- world")
- hello world
注意: 在列表 [], 字典{} 或者元组 () 中的多行语句中不需要使用反斜杠
4 数字类型
- int(整数)
- bool(布尔)
- float(浮点)
- complex(复数)
5 字符串
5.1 python 中单引号和双引号使用完全相同
5.2 使用转义字符 \
5.3 使用 r 可以让反斜杠不发生转义
- >>> print ("this is a string \n")
- this is a string
- >>> print (r"this is a srting \n")
this is a srting \n
5.4 python 中字符串有两种索引的方式, 从左往右以 0 开始, 从右往左以 - 1 开始
5.5 字符串截取
- #!/usr/bin/python
- str = 'hello world'
- print (str) #输出字符串 hello world
- print (str[0:-1]) #输出第一个到倒数第二个 hello worl
- print (str[0]) #输出第一个字符 h
- print (str[2:5]) #输出从第三个开始到第五个字符 llo
- print (str * 2) #输出字符串 2 次 hello worldhello world
- print (str + 'add') #连接字符串 hello worldadd
5.6 等待用户输入 input
- >>> input ("please input:")
- please input:hello world
- 'hello world'
6 import 与 from ... import
import: 将整个模块导入
from ... import : 从某个模块中导入函数
7 命令行参数
# python -h
来源: http://www.bubuko.com/infodetail-2658273.html