next up previous contents
Next: 單底線及雙底線 Up: Python Previous: Python 簡介   Contents

Python 變數

  1. python 變數
    1. 變數直接使用,不需宣告:
      #!/usr/bin/python
      
      counter = 100          # An integer assignment
      miles   = 1000.0       # A floating point
      name    = "Devid"      # A string
      
    2. 多個變數一起指定:
      a=b=c=1
      a,b,c=1,2,"Devid"
      
    3. 變數指定後,變數型態就固定,重新指定新型態的值,等同重新宣告:
      a=1
      a="Devid"
      
    4. 變數的刪除:
      a=b=c=1
      del a
      
  2. python 字串處理
    1. 字串變數以單引號 ' 或雙引號 " 括起來。
      name    = "Devid"      # A string
      
    2. 字串變數為字元陣列:
      [dywang@deyu glade]$ cat str.py
      #!/usr/bin/python
      # coding: utf-8
      
      name = "David"
      print name				# 印出整個字串
      print name[0]			# 印出第 1 個字元
      print name[1:3]			# 印出第 2 至 3 個字元
      print name[1:]			# 印出第 2 至最後一個字元
      print name * 3			# 印出字串三次
      print name + " csie"	# 印出連續字串
      
      [dywang@deyu glade]$ python str.py
      David
      D
      av
      avid
      DavidDavidDavid
      David csie
      
  3. python 陣列處理
    1. lists 陣列
      [dywang@deyu glade]$ cat lists.py
      #!/usr/bin/python
      # coding: utf-8
      
      list = [ "David", 123, 23.4, 'abc', 4.321 ]
      tinylist = [ 98, 'Rita' ]
      print list				# 印出整個陣列
      print list[0]			# 印出陣列第 1 個元素
      print list[1:3]			# 印出陣列第 2 至 3 個元素
      print list[1:]			# 印出陣列第 2 至最後一個元素
      print tinylist * 2		# 印出陣列二次
      print tinylist + list	# 印出連續陣列
      
      [dywang@deyu glade]$ python lists.py
      ['David', 123, 23.399999999999999, 'abc', 4.3209999999999997]
      David
      [123, 23.399999999999999]
      [123, 23.399999999999999, 'abc', 4.3209999999999997]
      [98, 'Rita', 98, 'Rita']
      [98, 'Rita', 'David', 123, 23.399999999999999, 'abc', 4.3209999999999997]
      
    2. tuples 陣列:唯讀的 lists
      [dywang@deyu glade]$ cat tuples.py
      #!/usr/bin/python
      # coding: utf-8
      
      list = ( "David", 123, 23.4, 'abc', 4.321 )
      tinylist = ( 98, 'Rita' )
      print list				# 印出整個陣列
      print list[0]			# 印出陣列第 1 個元素
      print list[1:3]			# 印出陣列第 2 至 4 個元素
      print list[1:]			# 印出陣列第 2 至最後一個元素
      print tinylist * 2		# 印出陣列二次
      print tinylist + list	# 印出連續陣列
      
      [dywang@deyu glade]$ python tuples.py
      ('David', 123, 23.399999999999999, 'abc', 4.3209999999999997)
      David
      (123, 23.399999999999999)
      (123, 23.399999999999999, 'abc', 4.3209999999999997)
      (98, 'Rita', 98, 'Rita')
      (98, 'Rita', 'David', 123, 23.399999999999999, 'abc', 4.3209999999999997)
      
    3. lists 與 tuples 陣列比較
      [dywang@deyu glade]$ cat list_tuple.py 
      #!/usr/bin/python
      # coding: utf-8
      
      list = [ "David", 123, 23.4, 'abc', 4.321 ]
      tuple = ( "David", 123, 23.4, 'abc', 4.321 )
      list[3] = 'xyz'			# 有效語法
      tuple[3] = 'xyz'		# 無效語法
      
      [dywang@deyu glade]$ python list_tuple.py 
      Traceback (most recent call last):
        File "list_tuple.py", line 7, in <module>
          tuple[3] = 'xyz'		# 無效語法
      TypeError: 'tuple' object does not support item assignment
      
    4. dictionaries 陣列:
      [dywang@deyu glade]$ cat dict.py 
      #!/usr/bin/python
      # coding: utf-8
      
      dict = {}
      dict['peter'] = "1234"
      dict[2]  = "rita"
      
      tinydict = {'name': 'devid','code':4538, 'dept': 'csie'}
      
      print dict['peter']		# Prints value for 'peter' 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
      
      [dywang@deyu glade]$ python dict.py 
      1234
      rita
      {'dept': 'csie', 'code': 4538, 'name': 'devid'}
      ['dept', 'code', 'name']
      ['csie', 4538, 'devid']
      # dictionaries 沒有順序概念
      



2017-06-14