变量类型-python笔记

来源:互联网 发布:windows 升级node版本 编辑:程序博客网 时间:2024/06/05 16:35

变量赋值

  变量的本质是,开辟的一块内存,用来存放值。给变量赋值很容易:
[python] view plaincopy
  1. #!/usr/local/bin/python2.7  
  2. count1=100  
  3. count2=1000.9  
  4. count3="What the fuck."  
  5. print count1,'\n',count2,'\n',count3  

连续赋同一个值:a=b=c=1,那么a b c共享同一块存储区,存储的值为1.
连续分别赋值:a,b,c = 2, 4, "hey"那就是三个不同变量。


基本数据类型


数字Numbers

赋值:val=20   val2=3.242等。不管是什么数值类型,直接赋值即可。
删除赋值:del val,val2
python支持的四种数值类型:int long float complex。例子如下表


字符串String

引号之间的字符,构成字符串。
可以用+将两个字符串连起来,用*使得字符串重复自身,变长。
    

另外,字符串的子串,可以用[:]提取出来。比如:
[python] view plaincopy
  1. #!/usr/bin/python  
  2.   
  3. str = 'Hello World!'  
  4.   
  5. print str          # 打印全字符串  
  6. print str[0]       # 打印第一个字符H  
  7. print str[2:5]     # 打印第三到第五个字符  
  8. print str[2:]      # 打印从第三个字符到末尾的所有字符  
  9. print str * 2      # Prints string two times  
  10. print str + "TEST" # Prints concatenated string  

列表List

列表用[ ]标识。是python最通用的复合数据类型。看这段代码就明白。
[python] view plaincopy
  1. #!/usr/bin/python  
  2.   
  3. list = [ 'abcd'786 , 2.23'john'70.2 ]  
  4. tinylist = [123'john']  
  5.   
  6. print list          # Prints complete list  
  7. print list[0]       # Prints first element of the list  
  8. print list[1:3]     # Prints elements starting from 2nd till 3rd   
  9. print list[2:]      # Prints elements starting from 3rd element  
  10. print tinylist * 2  # Prints list two times  
  11. print list + tinylist # Prints concatenated lists
3.向 list 中增加元素
  1. >>> li
  2. ['a', 'b', 'mpilgrim', 'z', 'example']
  3. >>> li.append("new")              
  4. >>> li
  5. ['a', 'b', 'mpilgrim', 'z', 'example', 'new']
  6. >>> li.insert(2, "new")           
  7. >>> li
  8. ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
  9. >>> li.extend(["two", "elements"])
  10. >>> li
  11. ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']



4.搜索 list
  1. >>> li
  2. ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
  3. >>> li.index("example")
  4. 5
  5. >>> li.index("new")    
  6. 2
  7. >>> li.index("c")      
  8. Traceback (innermost last):
  9.   File "<interactive input>", line 1, in ?
  10. ValueError: list.index(x): x not in list
  11. >>> "c" in li          
  12. False


列表的元素用逗号隔开,是可以对某元素多次赋值的。

5.从 list 中删除元素

>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
>>> li.remove("z")  
>>> li
['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements']
>>> li.remove("new")
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements']
>>> li.remove("c")  
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
ValueError: list.remove(x): x not in list
>>> li.pop()        
'elements'
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
6.list 运算符

>>> li = ['a', 'b', 'mpilgrim']
>>> li = li + ['example', 'new']
>>> li
['a', 'b', 'mpilgrim', 'example', 'new']
>>> li += ['two']               
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
>>> li = [1, 2] * 3             
>>> li
[1, 2, 1, 2, 1, 2]


元组Tuple

元组用()标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。
[python] view plaincopy
  1. #!/usr/bin/python  
  2.   
  3. tuple = ( 'abcd'786 , 2.23'john'70.2  )  
  4. tinytuple = (123'john')  
  5.   
  6. print tuple           # Prints complete list  
  7. print tuple[0]        # Prints first element of the list  
  8. print tuple[1:3]      # Prints elements starting from 2nd till 3rd   
  9. print tuple[2:]       # Prints elements starting from 3rd element  
  10. print tinytuple * 2   # Prints list two times  
  11. print tuple + tinytuple # Prints concatenated lists  

字典Dictionary

字典用{ }标识。字典由索引(key)和它对应的值value组成。
[python] view plaincopy
  1. #!/usr/bin/python  
  2.   
  3. dict = {}  
  4. dict['one'] = "This is one"  
  5. dict[2]     = "This is two"  
  6.   
  7. tinydict = {'name''john','code':6734'dept''sales'}  
  8.   
  9.   
  10. print dict['one']       # Prints value for 'one' key  
  11. print dict[2]           # Prints value for 2 key  
  12. print tinydict          # Prints complete dictionary  
  13. print tinydict.keys()   # Prints all the keys  
  14. 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.

原创粉丝点击