Python3变量类型
变量不过是用于存储值的保留内存位置。这意味着在创建变量时,将在内存中保留一些空间。
解释器根据变量的数据类型分配内存,并确定可以在保留内存中存储的内容。因此,通过为变量分配不同的数据类型,可以在这些变量中存储整数,小数或字符。
给变量赋值
Python变量不需要显式声明即可保留内存空间。当你将值分配给变量时,声明会自动发生。等号(=)用于将值分配给变量。
=运算符左侧的操作数是变量的名称,=运算符右侧的操作数是存储在变量中的值。例如:
#!/usr/bin/python3 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”分别是分配给计数器,里程和名称变量的值。这将产生以下结果:
100 1000.0 John
多重分配
Python允许你同时为多个变量分配一个值。
例如:
a = b = c = 1
在这里,将创建一个整数对象,其值为1,并且所有三个变量都分配给相同的存储位置。你还可以将多个对象分配给多个变量。例如:
a, b, c = 1, 2, "john"
在此,分别将两个具有值1和2的整数对象分配给变量a和b,并将一个具有值“ john”的字符串对象分配给变量c。
标准数据类型
存储在内存中的数据可以有多种类型。例如,一个人的年龄存储为数字值,而他或她的地址存储为字母数字字符。 Python具有各种标准数据类型,这些数据类型用于定义可能的操作以及每种操作的存储方法。
Python有五种标准数据类型:
- Numbers
- String
- List
- Tuple
- 字典
Python数字
数字数据类型存储数值。数字对象是在你为其分配值时创建的。例如:
var1 = 1 var2 = 10
你还可以使用以下命令删除对数字对象的引用 del 语句。的语法 del 声明是:
del var1[,var2[,var3[....,varN]]]]
你可以使用删除单个对象或多个对象 del 语句。
例如:
del var del var_a, var_b
Python支持三种不同的数字类型:
- int (signed integers)
- 浮点数(浮点实数值)
- 复数(复数)
Python3中的所有整数都表示为长整数。因此,没有长的单独的数字类型。
Examples
以下是一些数字示例:
int | float | complex |
---|---|---|
10 | 0.0 | 3.14j |
100 | 15.20 | 45.j |
-786 | -21.9 | 9.322e-36j |
080 | 32.3+e18 | .876j |
-0490 | -90. | -.6545 + 0J |
-0x260 | -32.54e100 | 3e+26J |
0x69 | 70.2-E12 | 4.53e-7j |
复数由x + yj表示的有序实数浮点数对组成,其中x和y是实数,j是虚数单位。
Python字符串
Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows either pair of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 to the end.
加号(+)是字符串连接运算符,星号(*)是重复运算符。例如:
#!/usr/bin/python3 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
Python列表
列表是Python的复合数据类型中功能最丰富的。列表包含用逗号分隔并括在方括号([])中的项目。在某种程度上,列表类似于C中的数组。它们之间的区别之一是,属于列表的所有项目都可以具有不同的数据类型。
可以使用切片运算符([]和[:])访问列表中存储的值,其中的索引从列表开头的0开始,一直到-1结束。加号(+)是列表串联运算符,星号(*)是重复运算符。例如:
#!/usr/bin/python3 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.200000000000003] abcd [786, 2.23] [2.23, 'john', 70.200000000000003] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']
Python元组
元组是类似于列表的另一种序列数据类型。元组由多个用逗号分隔的值组成。但是,与列表不同,元组被括在括号内。
列表和元组之间的主要区别是:列表放在方括号([])中,并且它们的元素和大小可以更改,而元组放在括号(())中,不能更新。元组可以认为是 只读 列表。例如:
#!/usr/bin/python3 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print (tuple) # Prints complete tuple print (tuple[0]) # Prints first element of the tuple print (tuple[1:3]) # Prints elements starting from 2nd till 3rd print (tuple[2:]) # Prints elements starting from 3rd element print (tinytuple * 2) # Prints tuple two times print (tuple + tinytuple) # Prints concatenated tuple
这将产生以下结果:
('abcd', 786, 2.23, 'john', 70.200000000000003) abcd (786, 2.23) (2.23, 'john', 70.200000000000003) (123, 'john', 123, 'john') ('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')
以下代码对元组无效,因为我们试图更新一个元组,这是不允许的。列表可能有类似情况:
#!/usr/bin/python3 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax with tuple list[2] = 1000 # Valid syntax with list
Python字典
Python的字典是一种哈希表类型。它们的工作方式类似于在Perl中找到的关联数组或哈希,并且由键值对组成。字典键几乎可以是任何Python类型,但通常是数字或字符串。另一方面,值可以是任意Python对象。
字典用花括号({})括起来,可以使用方括号([])分配和访问值。例如:
#!/usr/bin/python3 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 {'name': 'john', 'dept': 'sales', 'code': 6734} dict_keys(['name', 'dept', 'code']) dict_values(['john', 'sales', 6734])
字典在元素之间没有顺序的概念。说元素是“乱序的”是不正确的。他们只是无序的。
数据类型转换
有时,你可能需要在内置类型之间执行转换。要在类型之间进行转换,你只需将类型名称用作函数即可。
有几种内置函数可以执行从一种数据类型到另一种数据类型的转换。这些函数返回一个表示转换后值的新对象。
序号 | 功能说明 |
---|---|
1 |
int(x [,base]) 将x转换为整数。如果x是字符串,则基数指定基数。 |
2 |
float(x) 将x转换为浮点数。 |
3 |
复合(实数[,imag]) 创建一个复数。 |
4 |
str(x) 将对象x转换为字符串表示形式。 |
5 |
repr(x) 将对象x转换为表达式字符串。 |
6 |
评估(str) 计算字符串并返回一个对象。 |
7 |
tuple(s) 将s转换为元组。 |
8 |
list(s) 将s转换为列表。 |
9 |
set(s) 将s转换为集合。 |
10 |
dict(d) 创建字典。 d必须是(键,值)元组的序列。 |
11 |
Frozenset 将s转换为冻结集合。 |
12 |
chr(x) 将整数转换为字符。 |
13 |
unichr(x) 将整数转换为Unicode字符。 |
14 |
ord(x) 将单个字符转换为其整数值。 |
15 |
hex(x) 将整数转换为十六进制字符串。 |
16 |
oct(x) 将整数转换为八进制字符串。 |