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)
  • 列表

待續......

相關文章
相關標籤/搜索