白云岛资源网 Design By www.pvray.com

"htmlcode">

var = 520
print(type(var))  	 # <class 'int'>

float(浮点型):

var = 5.20
print(type(var))  	 # 输出:<class 'float'>

bool(布尔型):

var = true
print(type(var))   # 输出:<class 'bool'>

complex(复数):

var = complex(13,14)
print(type(var)) 	  # 输出:<class 'complex'>

(2)String(字符串)
"htmlcode">

var = “pig”
print(type(var)) 	  # 输出:<class 'str'>
print(var[0:3])   # 正向访问,输出:'pig'
print(var[-1])   # 反向访问,输出:'g'

(3)List(列表)
"htmlcode">

var = [ 'pig' , 1 , 2.2 ]
print(type(var)) 	  # 输出:<class 'list'>
print(var[0]) 	  # 获得第一个元素,输出:'pig'
print(var+var) 		 # 打印组合的列表,输出:[ 'pig', 1 , 2.2,'pig', 1 , 2.2 ]

(4)Tuple(元组)
"htmlcode">

var = ( 'pig', 1 , 2.2 )
print(type(var)) 	  # 输出:<class 'tuple'>
print(var[0]) 	  # 获得第一个元素,输出:'pig'
print(var+var) 		 # 打印组合的元组,输出:( 'pig', 1 , 2.2,'pig', 1 , 2.2 )
var[0] = 'dog'			 # 出错!不能被二次赋值

(5)Dictionary(字典)
"{ }"标识,字典由索引key和它对应的值value组成。

dic = {'name':'张三','age':18}
print(dic ['name'])  # 得到键为'name' 的值,输出:'张三'
print(dic [age])   # 得到键为'age' 的值,输出:18
print(dic)    # 得到完整的字典,输出:{'name':'张三','age':18}
print(dic.keys())   # 得到所有键,输出:dict_keys:(['name','age'])
print(dic.values())  # 输出所有值,输出:dict_values:(['张三',18])

二、python变量的存储

 1、变量与储存地址的关系

"htmlcode">

#include<iostream>
using namespace std;
int main()
{
	int test = 1;
	cout << &test << ":" << test << endl;
	test = 2;
	cout << &test << ":" << test << endl;
	return 0;
}

运行结果:

0020FDC8:1
0020FDC8:2

"htmlcode">

str = 'girls are pig'
print(id(str))
str = 'boys are dog'
print(id(str))

运行结果:

113811696
113812464

"htmlcode">

list1 = [1,2,3]
print(list1)			#输出:[1,2,3]
print(id(list1))		#输出:112607104(不同电脑分配给变量的地址不同)
list1[0] = "hello"
print(list1)			#输出:['hello',2,3]
print(id(list1))		#输出:112607104
list1.append(4)
print(list1)			#输出:['hello',2,3,4]
print(id(list1))		#输出:112607104
list1 = ['hello',4]
print(list1)			#输出:['hello',4]
print(id(list1))		#输出:112925120

"text-align: center">深入理解Python变量的数据类型和存储

"htmlcode">

list1 = ['hello',2,3,4]
print(id(list1[0]))		#输出:112926064
print(id(list1[3]))		#输出:8791404644096
list2 = ['hello',4]
print(id(list2[0]))		#输出:112926064
print(id(list2[1]))		#输出:8791404644096

3、变量的赋值——浅拷贝和深拷贝

(1)变量赋值的安全隐患

"htmlcode">

list1 = [1,2,3]
list2 = list1
print(list1)			#输出:[1,2,3]
print(list2)			#输出:[1,2,3]
print(id(list1))		#输出:112607104
print(id(list2))		#输出:112607104
list1[0] = 'hello'
print(list1)			#输出:['hello',2,3]
print(list2)			#输出:['hello',2,3]
print(id(list1))		#输出:112607104
print(id(list2))		#输出:112607104

"text-align: center">深入理解Python变量的数据类型和存储

"htmlcode">

import copy
people = ['girl','boy']
list1 = [1,2,people]
list2 = copy.copy(list1)
print(list1)				#输出:[1,2,['girl','boy']]
print(list2)				#输出:[1,2,['girl','boy']]
list1.append('hello')		
list2.append('hi')			
print(list1)				#输出:[1,2,['girl','boy'],'hello']
print(list2)				#输出:[1,2,['girl','boy'],'hi']
people[0] = 'pig'
print(list1)				#输出:[1,2,['pig','boy'],'hello']
print(list2)				#输出:[1,2,['pig','boy'],'hi']

"text-align: center">深入理解Python变量的数据类型和存储

(3)深拷贝

"htmlcode">

import copy
people = ['girl','boy']
list1 = [1,2,people]
list2 = copy.deepcopy(list1)
print(list1)				#输出:[1,2,['girl','boy']]
print(list2)				#输出:[1,2,['girl','boy']]
list1.append('hello')		
list2.append('hi')			
print(list1)				#输出:[1,2,['girl','boy'],'hello']
print(list2)				#输出:[1,2,['girl','boy'],'hi']
people[0] = 'pig'
print(list1)				#输出:[1,2,['pig','boy'],'hello']
print(list2)				#输出:[1,2,['girl','boy'],'hi']

"text-align: center">深入理解Python变量的数据类型和存储

三、python变量数据类型的大小

"htmlcode">

import sys
print(sys.getsizeof(0))  # 输出:24
print(sys.getsizeof(1))  # 输出:28 
print(sys.getsizeof(2))  # 输出:28
print(sys.getsizeof(2**15)) # 输出:28
print(sys.getsizeof(2**30)) # 输出:32
print(sys.getsizeof(2**128)) # 输出:44

"text-align: center">深入理解Python变量的数据类型和存储

"htmlcode">

struct PyLongObject {
 long ob_refcnt;    // 8 bytes
 struct _typeobject *ob_type; // 8 bytes
 long ob_size;     // 8 bytes
 unsigned int ob_digit[1];  // 4 bytes * abs(ob_size)
};

"htmlcode">

struct PyLongObject {
 int ob_refcnt;    // 4 bytes
 struct _typeobject *ob_type; // 4 bytes
 int ob_size;     // 4 bytes
 unsigned short ob_digit[1]; // 2 bytes * abs(ob_size)
};

"1" color="#000000" />

Tips:本人能力有限,如有错误之处麻烦指出。放弃不难,但坚持一定很酷!

白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com