小白的Python之路 day1 變數,pythonday1

來源:互聯網
上載者:User

小白的Python之路 day1 變數,pythonday1
Python之路,Day1 - Python基礎1變數  

變數用於儲存在電腦程式中引用和操作的資訊。它們還提供了一種用描述性名稱標記資料的方法,這樣我們的程式就能更清晰地被讀者和我們自己理解。將變數看作儲存資訊的容器是很有協助的。它們的唯一目的是在記憶體中標記和儲存資料。然後可以在整個程式中使用這些資料。

聲明變數

123 #_*_coding:utf-8_*_ name = "Many Qian"

上述代碼聲明了一個變數,變數名為: name,變數name的值為:"Many Qian" 

變數定義的規則:

    • 變數名只能是 字母、數字或底線的任意組合
    • 變數名的第一個字元不能是數字
    • (程式編程中的定義的函數名也不能用做變數名,不然會給自己挖個坑)
    • [abs,dict,help,min,setattr,all,dir,hex,next,slice,any,divmod,id,object,sorted,ascii,enumerate,input,oct,staticmethod,bin,eval,int,open,str,bool,exec,isinstance,ord,sum,bytearray,filter,issubclass,pow,super,bytes,float,iter,print,tuple,callable,format,len,property,type,chr,frozenset,list,range,vars,classmethod,getattr,locals,repr,zip,compile,globals,map,reversed,__import__,complex,hasattr,max,round, delattr,hash,memoryview,set,] 
    • 約定俗成的規則:1.不要用中文(雖然Python3支援) 2.不要寫拼音  3.英文單詞最好(單片語合用底線串連最好,不要“駝峰”的首字母大寫)
    • 常量(Python雖然不支援常量,我們會定義為“PIE”)
    • 以下關鍵字不能聲明為變數名
    • ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield',]

變數的賦值

12345678910111213   name = "Many Qian" name2 = name print(name,name2) name = "Tom" print("What is the value of name2 now?") 結果會是:What is the value of Many Qian now? 

 記憶體回收機制

  首先我們先要瞭解什麼是垃圾,記憶體裡面有的值沒有任何的引用,什麼叫引用呢?打比方說掛一個門牌號是不是引用了一次,身上沒有門牌號,沒有引用的值就是垃圾,Python會定期回收這些垃圾,所以我們就不用考慮這些!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.