摘要:輸入的模塊上使用。我們看到它包含一個龐大的屬性列表。默認地,它返回當前模塊的屬性列表。
Python Learn Part
More_Info
Content List
1.Python Introduce
1.1 python REPL
1.2 python helloworld.py
1.3 python help()
1.4 to python_string
1.5 difference between input and raw_input
2.Python Preliminary program
2.1 Operators and Expression
2.2 python control flow
3.function
3.1 local var
3.2 global var
3.3 func_key
3.4 DocStrings
4.Module
4.1 sys module
4.2 from..import
4.3 name
4.4 dir()
1. Python Introducepython 少有的一種可以稱得上即簡單又功能強大的編程語言
python 代表簡單主義思想的語言
? python git:(master) python -V Python 2.7.10 ? python git:(master)1.1 python REPL
? python git:(master) python Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 2**3 or pow(2, 3) 8 >>> x = input("x:") x:5 >>> print "hello ", x hello 5 >>> print 3 3 >>> print "3" 3 >>> 0xAF 175 >>> 010 8 >>> round(1.0/3.0) #四舍五入 0.0 >>> round(1.0/2.0) 1.0 >>> import math >>> math.floor(32.9) 32.0 >>> print r"C: owhere""" # 解決最后一個字符是 "" 的情況! 完美解決 C: owhere >>> print u"hello, world" # Unicode 字符串, 在python3.0中,所有的字符串都是 Unicode 字符串 hello, world >>>1.2 python helloworld.py
#!/usr/bin/python # Filename : helloworld.py print "Hello World"1.3 python help()
>>> help() Welcome to Python 2.7! This is the online help utility. help> input Help on built-in function input in module __builtin__: input(...) input([prompt]) -> value Equivalent to eval(raw_input(prompt)). (END)1.4 to python_string
str
repr
backquote
>>> ""hello world" she said" ""hello world" she said" >>> "hello world" "hello world" >>> 10000L 10000L >>> print "hello world" hello world >>> print 10000L 10000 >>> print str("Hello world") Hello world >>> print str(10000L) 100001.5 input and raw_input
>>> raw_input("shuru : ") shuru : 6 "6" >>> input("shuru : ") #默認為合法的python表達式 shuru : 5 52. Python Preliminary program
>>> i = 5 >>> i = i + 1 >>> print ... i 6 >>> print i 6 >>>2.1 Operators and Expression
operator precedence
Expression
#!/usr/bin/python # Filename: expression.py length = 5 breadth = 2 area = length * breadth print "Area is", area print "Perimeter is", 2 * (length + breadth)2.2 python control flow
#!/usr/bin/python # Filename: while.py number = 23 running = True while running: guess = int(raw_input("Enter an integer : ")) if guess == number: print "Congratulations, you guessed it." running = False # this causes the while loop to stop elif guess < number: print "No, it is a little higher than that" else: print "No, it is a little lower than that" else: print "The while loop is over." # Do anything else you want to do here print "Done"
break, continue
#!/usr/bin/python # Filename: continue.py while True: s = raw_input("Enter something : ") if s == "quit": break if len(s) < 3: continue print "Input is of sufficient length" # Do other kinds of processing here...3. function 3.1 local var
#!/usr/bin/python # Filename: func_local.py def func(x): print "x is", x x = 2 print "Changed local x to", x x = 50 func(x) print "x is still", x
Output
$ python func_local.py
x is 50
Changed local x to 2
x is still 50
#!/usr/bin/python # Filename: func_global.py def func(): global x print "x is", x x = 2 print "Changed local x to", x x = 50 func() print "Value of x is", x
Output
$ python func_global.py
x is 50
Changed global x to 2
Value of x is 2
#!/usr/bin/python # Filename: func_key.py def func(a, b=5, c=10): print "a is", a, "and b is", b, "and c is", c func(3, 7) func(25, c=24) func(c=50, a=100)
Output
$ python func_key.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
# #!/usr/bin/python # Filename: func_doc.py def printMax(x, y): """Prints the maximum of two numbers. The two values must be integers.""" x = int(x) # convert to integers, if possible y = int(y) if x > y: print x, "is maximum" else: print y, "is maximum" return y printMax(3, 5) print printMax.__doc__
output
$ python func_doc.py 5 is maximum Prints the maximum of two numbers. The two values must be integers.4. Module 4.1 sys 模塊
#!/usr/bin/python # Filename: using_sys.py import sys print "The command line arguments are:" for i in sys.argv: print i print " The PYTHONPATH is", sys.path, " "4.2 from..import
yourself module
#!/usr/bin/python # Filename: mymodule.py def sayhi(): print "Hi, this is mymodule speaking." version = "0.1" # End of mymodule.py
mymodule_demo.py
#!/usr/bin/python # Filename: mymodule_demo.py import mymodule mymodule.sayhi() print "Version", mymodule.version
from..import..
#!/usr/bin/python # Filename: mymodule_demo2.py from mymodule import sayhi, version # Alternative: # from mymodule import * sayhi() print "Version", version4.3 __name__
#!/usr/bin/python # Filename: using_name.py if __name__ == "__main__": print "This program is being run by itself" else: print "I am being imported from another module"
4.4 dir()每個Python模塊都有它的__name__,如果它是"__main__",這說明這個模塊被用戶多帶帶運行,我們可以進行相應的恰當操作。
$ python >>> import sys >>> dir(sys) # get list of attributes for sys module ["__displayhook__", "__doc__", "__excepthook__", "__name__", "__stderr__", "__stdin__", "__stdout__", "_getframe", "api_version", "argv", "builtin_module_names", "byteorder", "call_tracing", "callstats", "copyright", "displayhook", "exc_clear", "exc_info", "exc_type", "excepthook", "exec_prefix", "executable", "exit", "getcheckinterval", "getdefaultencoding", "getdlopenflags", "getfilesystemencoding", "getrecursionlimit", "getrefcount", "hexversion", "maxint", "maxunicode", "meta_path","modules", "path", "path_hooks", "path_importer_cache", "platform", "prefix", "ps1", "ps2", "setcheckinterval", "setdlopenflags", "setprofile", "setrecursionlimit", "settrace", "stderr", "stdin", "stdout", "version", "version_info", "warnoptions"] >>> dir() # get list of attributes for current module ["__builtins__", "__doc__", "__name__", "sys"] >>> >>> a = 5 # create a new variable "a" >>> dir() ["__builtins__", "__doc__", "__name__", "a", "sys"] >>> >>> del a # delete/remove a name >>> >>> dir() ["__builtins__", "__doc__", "__name__", "sys"] >>>
輸入的sys模塊上使用dir。我們看到它包含一個龐大的屬性列表。
dir() , 默認地,它返回當前模塊的屬性列表。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/37910.html
摘要:程序運行機制兩種核心機制垃圾收集機制虛擬機可以理解成一個以字節碼為機器指令的虛擬機機制屏蔽了底層運行平臺的差別實現了一次編譯隨處運行。采用編碼全球語言統一編碼每個字符占兩個字節面向過程約瑟夫環面向對象約瑟夫環 Chap 0 Preface Java Basic Content List Java data type、 標識符、運算符、表達式和語句、分支、循環、方法 OO Except...
摘要:最近在研究做消息隊列時,順便看了一下做消息隊列的實現。遠程連接時需要認證實例化連接對象實例化鏈接參數對象創建新的通道模式向綁定到指定的中發送消息,消費者從中取出數據,類似于廣播模式發布訂閱模式。 最近在研究redis做消息隊列時,順便看了一下RabbitMQ做消息隊列的實現。以下是總結的RabbitMQ中三種exchange模式的實現,分別是fanout, direct和topic。 ...
摘要:機器學習深度學習與自然語言處理領域推薦的書籍列表人工智能深度學習與相關書籍課程示例列表是筆者系列的一部分對于其他的資料集錦模型開源工具與框架請參考。 showImg(https://segmentfault.com/img/remote/1460000014946199); DataScienceAI Book Links | 機器學習、深度學習與自然語言處理領域推薦的書籍列表 sho...
閱讀 2000·2023-04-25 16:53
閱讀 1442·2021-10-13 09:39
閱讀 606·2021-09-08 09:35
閱讀 1639·2019-08-30 13:03
閱讀 2121·2019-08-30 11:06
閱讀 1831·2019-08-30 10:59
閱讀 3188·2019-08-29 17:00
閱讀 2288·2019-08-23 17:55