基础永远值得花费90%的精力去学习加强。认识实践的重要性。
Ax Python Variable Types | 类型
创建变量意味着你将在内存保留一些空间,而为了这个空间是你所需要的合适大小范围之内,所以提供了不同的类型,你可以在这些变量中存储小数、整数、字符。
赋值
python不需要声明,只需要用(=)赋值即可,声明会自动发生。左边是变量名,右边是值。
#!/usr/bin/python
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
执行
100
1000.0
John
多重赋值
一个值赋给多个变量
a = b = c = 1
多个值赋给多个变量
a,b,c = 1,2,"john"
标准数据类型
有五种类型
- Numbers
- String
- List
- Tuple
- Dictionary
Numbers | 数字
数字有这些类型
- int (有符号整数)
- long (长整型可以用八进制和十六进制表示)
- float (浮点型实数)
- complex (复数)
赋值给变量,分配空间
var1 = 1
var2 = 10
删除引用,删除的是变量!
del var1[,var2[,var3[....,varN]]]]
你可以删除单个或多个。
del var
del var_a, var_b
不同的类型大小不同
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
建议使用大写的L表示,避免误读成1.
String | 字符串
用单引号或双引号表示一串连续的字符。可以使用切片字符对文字进行检索[] 和 [:]
+是连续操作符,*是重复操作符
#!/usr/bin/python
str = 'Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
执行 −
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Lists | 列表
#!/usr/bin/python
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
执行
['abcd', 786, 2.23, 'john', 70.2]
abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Tuple | 元组
元组是不能更新的。
#!/usr/bin/python
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints the complete tuple
print tuple[0] # Prints first element of the tuple
print tuple[1:3] # Prints elements of the tuple starting from 2nd till 3rd
print tuple[2:] # Prints elements of the tuple starting from 3rd element
print tinytuple * 2 # Prints the contents of the tuple twice
print tuple + tinytuple # Prints concatenated tuples
执行
('abcd', 786, 2.23, 'john', 70.2)
abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
Dictionary | 字典
#!/usr/bin/python
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
执行
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
类型转换
Sr.No. | Function | Description |
---|---|---|
1 | int(x [,base]) | 转为整数,如果是字符串,指定基数 |
2 | long(x [,base] ) | 转为长整数,如果是字符串,指定基数 |
3 | float(x) | 转为浮点数 |
4 | complex(real [,imag]) | 创建一个复数 |
5 | str(x) | 转为字符串 |
6 | repr(x) | 转为表达式字符串 |
7 | eval(str) | 计算一个字符串并返回一个对象 |
8 | tuple(s) | 转为元组 |
9 | list(s) | 转为列表 |
10 | set(s) | 转为集合 |
11 | dict(d) | 创建一个字典,d是由键和值组成的元组 |
12 | frozenset(s) | 转为一个冻结的集合 |
13 | chr(x) | 将整数转为字符 |
14 | unichr(x) | 将整数转为unicode |
15 | ord(x) | 单个字符转为整数值 |
16 | hex(x) | 整数转为十六进制字符串 |
17 | oct(x) | 整数转为八进制字符串 |