Python中变量赋值与数据类型

Python解释器内存回收机制

变量与赋值

如上图所示:
当变量a赋于值时,内存中分配置一个id为值的空间坐标
每当一个变量指向它时,纪录+1 ,就至关于文件系统中的硬链接同样
当没有变量指向它时,内存空间就被回收。python

[sxooky@sxooky ~]$ python
Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 3
>>> b = a
>>> print a , b
3 3
>>> id(a),id(b)
(19254328, 19254328)
>>> a = 5
>>> print a , b
5 3
>>> id(a),id(b)
(19254280, 19254328)
>>> exit()

变量名称

    显示(通俗易懂)
    nums_of_alex_gf = 19
    NumsOfAlexGf = 20
    names-of-alex-gf = 22 (不合法 “-” 会被解析为 减号)
    5name = 数字不能开头
    !name 特殊字符不能有, ! ~ & * …… % ^ $
    name of teacher = 不能有空格
    如下关键字不能声明为变量名linux

[ 'and' , 'as' , 'assert' , 'break' , 'class' , 'continue' , 'for' ] 
[ 'def' , 'elif' , 'if' , 'else' , 'import' , 'in' , 'is' , 'lambda' ] 
[ 'not' , 'or' , 'pass' , 'print' , 'raise' , 'yield' , 'except' ] 
[ 'exec' , 'finally' ,  'from' , 'global' , 'try' , 'while' , 'with' ]

变量读取

Python2.x

  • input:经常使用读取数值(若读取字符串,输入时要加上引号)
  • raw_input:经常使用读取字符串
[sxooky@sxooky ~]$ python
Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = input("Please input of the value: ")
Please input of the value: 3
>>> print a
3
>>> name = input("Please input your name: ")
Please input your name: sxooky
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'sxooky' is not defined
>>> name = input("Please input your name: ")
Please input your name: "sxooky"
>>> print name
sxooky
>>> name2 = raw_input("Please input your name2: ")
Please input your name2: kylin
>>> print name,name2
sxooky kylin

Python3.x

  • eval(input()):至关于Python2.7中的input功能,读取进来的变量为数值类型。
  • input():至关于Python2.7中的raw_input功能,读取进来的变量为字符串类型。
  • eval():引用“读取的变量”:为数值时eval(value) , 为字符串时加上引号eval(“string”)。
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> name1 = input("Please input : ")
Please input : sxooky
>>> name2 = eval(input("Please input: "))
Please input: kylin
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'kylin' is not defined
>>> name2 = eval(input("Please input: "))
Please input: "kylin"
>>> print (name1,name2)
sxooky kylin
>>> eval(name2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'kylin' is not defined
>>> eval("name2")
'kylin'
>>> b = input("input b : ")
input b : 6
>>> eval(b)
6
>>> a = 5
>>> eval(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: eval() arg 1 must be a string, bytes or code object
>>> eval('a')
5

数据类型

数值型:

  • INT(整型)

在32位机器上,整数的位数为32位,取值范围为-2³¹~2³¹-1,即-2417483648~2147483647
在64位机器上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~922337203685477580775807数组

  • LONG(长整型)

跟C语言不一样,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上因为机器内存有限。咱们使用的长整数数值不可能无限大。spa

注意:自从Python2.2起,若是整数发生溢出,Python会自动捋整数数据转换为长整数,因此现在在长整数数据后面不加字母L,也不会致使严重后果了。code

  • FLOAT(浮点型)

浮点数用来处理实数,即带有小数的数字,相似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,下的一位表示符号。orm

64位机器(Python2.7)ip

[sxooky@sxooky ~]$ uname -a
Linux sxooky 3.10.0-514.6.2.el7.x86_64 #1 SMP Thu Feb 23 03:04:39 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[sxooky@sxooky ~]$ python
Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**63
9223372036854775808L
>>> 2**62
4611686018427387904

32位机器(Python2.7)内存

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**31
2147483648L
>>> 2**30
1073741824

注意:Python3.X ,不在显示L了utf-8

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**64
18446744073709551616
>>> 2**120
1329227995784915872903807060280344576
>>> 2**31
2147483648
>>> 2**200
1606938044258990275541962092341162602522202993782792835301376

布尔型:

非空非0为真,0与空为假。字符串

  • 真或假
[sxooky@sxooky ~]$ python
Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> True == 1
True
>>> True == 2
False
>>> False == 1
False
>>> False == 2
False

字符串:

  • 万恶的字符串拼接(字符串不能和数值相加)

print ( ” Name:” +name+”\nAge:” +age+”\nJob:”+job )
Python中的字符串在C语言中体现为一个字符数组,每次建立字符串时候须要在内存中开辟一块连续的空间,而且修改字符串的话,就须要再次开辟空间,万恶的+号每出现一次就会在内存中从新开辟一块空间

  • 字符串格式化

print ( ” %s  , %f , %d ” ) %(value1,value2,value3)
%s为字符串 ,%f 为浮点型,%d 为整型。

#!/usr/bin/env python3
# This script of string
# Author: sxooky
# Date: 2017-03-09
# -*- coding:utf-8 -*-
 
# .strip() 去掉首未空格
# .strip("x") 去字母x
name = input("Name: ").strip()
age = input("Age: ")
job = input("Job: ").strip("L")
 
print ("Infomation of:" + name + "\nName:" + name + "\nAge:" + age + "\nJob:" + job )
 
# %f    float
# %d    int
# %s    string
mmc = '''
Infomation of: %s:
Name: %s
Age: %s
Job: %s
''' %(name,name,age,job)
 
print (mmc)
  • 列表

待续......

相关文章
相关标签/搜索