Python 基础速查表
来源:互联网 发布:c语言计算时间间隔 编辑:程序博客网 时间:2024/06/13 12:45
数据类型
Integer
-256, 15
Float
-253.23, 1.253e-10
String
"Hello", 'Goodbye', """Multiline"""
Boolean
True, False
List
[ value, ... ]
Tuple
( value, ... )
Dictionary
{ key: value, ... }
Set
{ value, value, ... }
语句
If 语句
if expression: statementselif expression: statementselse: statements
While Loop
while expression: statements
For Loop
for var in collection: statements
Counting For Loop
for i in range(start, end [, step]): statements(start is included; end is not)
算术运算符
x + y
加
x - y
减
x * y
乘
x / y
除
x % y
取模
x ** y
xy
Assignment shortcuts: x op= y示例: x += 1 递增 x
比较运算符
x< y
小于
x <= y
小于等于
x > y
大于
x >= y
大于等于
x == y
相等
x != y
不等
布尔运算符
not x
x and y
x or y
转换函数
int(expr)
将expr转成整型
float(expr)
将expr转成浮点型
str(expr)
将expr转成字符串
chr(num)
ASCII char num
String / List / Tuple 操作
len(s)
s长度
s[i]
取s中的第i个值 (从0开始)
s[start :end]
从开始(包括)到结束(不包括)的片段
x in s
如果x包含在s中则为true
x not ins
如果x不包含在s中,则为true
s + t
把s与t的相连接
s * n
将s复制n份
sorted(s)
对s进行排序
s.index(item)
item在s中的位置
更多字符串操作
s.lower()
转成小写
s.replace(old,new)
把 s 中的 old 替换成 new
s.split( delim )
由delim分隔的子字符串列表
s.strip()
用于移除字符串头尾的空格
s.upper()
转成大写
更多 http://docs.python.org/library/stdtypes.html#string-methodsMutating List 操作
del lst[i]
删除列表中的第i个项目
lst.append(e)
将e追加到lst中
lst.insert(i, e)
在第i个项目前插入e
lst.sort()
排序lst
字典操作
len(d)
d中的项目数
del d[key]
根据key从d中删除
key in d
如果d包含key,则为true
d.keys()
返回d中的key列表
函数定义
def name(arg1, arg2, ...): statements return expr
Environment
sys.argv
命令行参数列表(argv [0]可执行)
os.environ
环境变量字典
os.curdir
当前目录路径
import sys; print(sys.argv) orfrom sys import argv; print(argv)
实用的函数
exit( code )
使用exitcode终止程序
raw_input("prompt")
从stdin打印 prompt 和 readline()
在Python 3使用input("prompt")字符串格式化
"Hello, {0} {1}".format("abe", "jones")Hello, abe jones"Hello, {fn} {ln}".format(fn="abe", ln="jones")Hello, abe jones"You owe me ${0:,.2f}".format(253422.3)You owe me $253,422.30now = datetime.now()'{:%Y-%m-%d %H:%M:%S}'.format(now)2016-12-16 15:04:33
代码片段
循环序列
for index, value in enumerate(seq):
print("{} : {}".format(index, value))
循环字典
for key in sorted(dict):
print(dict[key])
读取一个文件
with open("filename", "r") as f:
for line in f:
line = line.rstrip("\n") # Strip newline
print(line)
- Python 基础速查表
- Python基础
- Python基础
- Python基础
- Python基础
- python基础
- Python基础
- python基础
- Python基础
- python 基础
- Python 基础
- python基础
- Python基础
- Python基础
- python基础
- Python基础
- python基础
- python基础
- 什么是NoSQL数据库?
- mysql存储过程中常用的一些功能性语句
- iOS collectionView 单行刷新
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- idea 报错maven [ERROR] Maven execution terminated abnormally (exit code 1)
- Python 基础速查表
- 扑克牌逻辑算法
- boostrap 对话框 学习
- Windows下搭建SVN服务器
- Web页面截图或HTML内容保存为图片
- c++ list, vector, map, set 区别与用法比较
- 关于vue+router+webpack项目开发前期报错:找不到 module vue-router
- 转型操作工具类
- 2017六月最新之 Android 身份证的有效验证精华篇