2 變數型別 python筆記

2021-06-16 18:42:46 字數 3924 閱讀 7002

變數的本質是,開闢的一塊記憶體,用來存放值。給變數賦值很容易:

#!/usr/local/bin/python2.7

count1=100

count2=1000.9

count3="what the ****."

print count1,'\n',count2,'\n',count3

連續賦同乙個值:a=b=c=1,那麼a b c共享同一塊儲存區,儲存的值為1.

連續分別賦值:a,b,c = 2, 4, "hey"那就是三個不同變數。

賦值:val=20   val2=3.242等。不管是什麼數值型別,直接賦值即可。

刪除賦值:del val,val2

python支援的四種數值型別:int long float complex。例子如下表

引號之間的字元,構成字串。

可以用+將兩個字串連起來,用*使得字串重複自身,變長。

另外,字串的子串,可以用[:]提取出來。比如:

#!/usr/bin/python

str = 'hello world!'

print str # 列印全字串

print str[0] # 列印第乙個字元h

print str[2:5] # 列印第三到第五個字元

print str[2:] # 列印從第三個字元到末尾的所有字元

print str * 2 # prints string two times

print str + "test" # prints concatenated string

列表用[ ]標識。是python最通用的復合資料型別。看這段**就明白。

#!/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

列表的元素用逗號隔開,是可以對某元素多次賦值的。

元組用()標識。內部元素用逗號隔開。但是元素不能二次賦值,相當於唯讀列表。

#!/usr/bin/python

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )

tinytuple = (123, 'john')

print tuple # prints complete list

print tuple[0] # prints first element of the list

print tuple[1:3] # prints elements starting from 2nd till 3rd

print tuple[2:] # prints elements starting from 3rd element

print tinytuple * 2 # prints list two times

print tuple + tinytuple # prints concatenated lists

字典用標識。字典由索引(key)和它對應的值value組成。

#!/usr/bin/python

dict = {}

dict['one'] = "this is one"

dict[2] = "this is two"

tinydict =

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

對字典dict,one 、2就是索引。而"this is one"和"this is two"就是相對應的值。

字典的元素是沒有排序的。

dict.keys()返回所有key,dict.values()返回所有value值。

int(x [,base])

converts x to an integer. base specifies the base if x is a string.

long(x [,base] )

converts x to a long integer. base specifies the base if x is a string.

float(x)

converts x to a floating-point number.

complex(real [,imag])

creates a complex number.

str(x)

converts object x to a string representation.

repr(x)

converts object x to an expression string.

eval(str)

evaluates a string and returns an object.

tuple(s)

converts s to a tuple.

list(s)

converts s to a list.

set(s)

converts s to a set.

dict(d)

creates a dictionary. d must be a sequence of (key,value) tuples.

frozenset(s)

converts s to a frozen set.

chr(x)

converts an integer to a character.

unichr(x)

converts an integer to a unicode character.

ord(x)

converts a single character to its integer value.

hex(x)

converts an integer to a hexadecimal string.

oct(x)

converts an integer to an octal string.

學習筆記 python的變數型別

本文是學習筆記,大量內容摘抄自 變數賦值 python中的變數賦值不需要型別宣告。每個變數在記憶體中建立,都包括變數的標識,名稱和資料這些資訊。每個變數在使用前都必須賦值,變數賦值後該變數才會被建立。等號 用來給變數賦值。等號 運算子左邊是乙個變數名,運算子右邊是儲存在變數中的值。我覺得這些非常簡單...

python入門2 變數 資料型別

變數 變數是存放資料值的容器 沒有宣告,首次為其賦值時,才會建立 不需要使用任何特定型別宣告,甚至可以在設定後更改其型別 字串變數可以使用單引號或雙引號進行宣告 命名規則 必須以字母或下劃線字元開頭,不能以數字開頭,只能包含字母數字字元和下劃線,區分大小寫 允許在一行中為多個變數賦值 x,y,z o...

python合法變數型別 Python變數和型別

1.pytonn中的數字 數字資料型別用於儲存數值。他們是不可改變的資料型別,這意味著改變量字資料型別會分配乙個新的物件。當你指定乙個值時,number物件就會被建立 var1 1 var2 10 您也可以使用del語句刪除一些物件引用。del語句的語法是 del var1 var2 var3 va...