- 数据类型
- 容器类型
- 强制类型转换
- 标识符
- 变量赋值
数据类型 Base Types: integer, float, bollean, string, bytes
数据类型1、整型 int: 123 , 0 , -121
2、浮点型 float: 2.5 , 0.0 , -1.2
3、布尔类型 bool: False True
4、字符串 str: "ab" , "a\nb" , "I'm" "a\tb\tc"
5、字节类型 bytes: b"toto\xfe\775"
容器类型:list[], tuple(), str bytes(), dict{:}, set()
容器类型#list数组
[1,3,5,7,"9"][:2]
[1, 3]
#数组函数
list((1,3,5,7,"9"))[:2]
[1, 3]
#tuple元组
(1,2,"3",)[1]
2
#元组函数
tuple((1,2,"3",))[1]
2
#dict字典
{"apple":1,"pear":2,"orange":3}
{'apple': 1, 'pear': 2, 'orange': 3}
#字典函数
dict(apple=1,pear=2,orange=3)
{'apple': 1, 'pear': 2, 'orange': 3}
#set集
{"a","b",1,2}
{1, 2, 'a', 'b'}
#集函数
set({"a","b",1,2})
{1, 2, 'a', 'b'}
强制类型转换 Conversions
强制类型转换int("123")
123
#二进制转换成十进制
int("0101",2)
5
#六进制转换成十进制
int("12",16)
18
int(12.3)
12
float("12345")
12345.0
round(12.345,1)
12.3
#bool(x):
#False for null x, empty container x, None or False x;
#True for other x
bool(0)
False
bool(None)
False
bool([])
False
str("abc")
'abc'
#code ↔ char
chr(35)
'#'
ord("@")
64
ord("%")
37
repr("one")
"'one'"
bytes([72,9,64])
b'H\t@'
list("abcde")
['a', 'b', 'c', 'd', 'e']
list((1,2,3,4,5))
[1, 2, 3, 4, 5]
dict([("apple",1),("pear",2)])
{'apple': 1, 'pear': 2}
dict(apple=1,pear=2)
{'apple': 1, 'pear': 2}
set(["apple","pear"])
{'apple', 'pear'}
#separator str and sequence of str → assembled str
"@".join(["name","net.com"])
'name@net.com'
#str splitted on whitespaces → list of str
"this is an apple".split()
['this', 'is', 'an', 'apple']
#str splitted on separator str → list of str
"this is an apple".split("a")
['this is ', 'n ', 'pple']
#str splitted on separator str → list of str
[int(x) for x in ('1','2',3,3.3)]
[1, 2, 3, 3]
[int(x) for x in ['1','2',3,3.3]]
[1, 2, 3, 3]
标识符 Identifiers 用作变量,函数,模块,类......名称
标识符标识符必须以字母(大小写均可)或者" _ "开头,接下来可以重复0到多次(字母|数字|" _ ") a…zA…Z_ followed by a…zA…Z_0…9
- diacritics allowed but should be avoided
- 禁止使用关键字(python内部已经使用了的标识符)(language keywords forbidden)
- 区分大小写 (lower/UPPER case discrimination)
- 没有长度限制
变量赋值 Variables assignment =
变量赋值#"变量名称"="赋值"
a=120
a=b=50
x,y,z=12,11,10
x+=3
y/=2
z%=3
n=None
#remove name x
del x
#values swap
y,z=z,y
网友评论