創始人Guido·van·Rossum,1989年爲了打發無聊的聖誕節而編寫的一個編程語言。python
Python與C
C語言:代碼編譯獲得機器碼,機器碼在處理器上直接執行。
Python:爲解析後生成字節碼,而後再解析爲機器碼,因此比C語言慢。Python是C開發的。linux
Python和其餘語言比較
使用:Linux原裝Python。
速度:Python在速度上可能稍顯遜色。編程
Cpython:Python的官方版本,使用C語言實現,使用最爲普遍,CPython實現會將源文件(py文件)轉換成字節碼文件(pyc文件),而後運行在Python虛擬機上。
Jyhton:Python的Java實現,Jython會將Python代碼動態編譯成Java字節碼,而後在JVM上運行。
IronPython:Python的C#實現,IronPython將Python代碼編譯成C#字節碼,而後在CLR上運行。(與Jython相似)
PyPy(特殊):Python實現的Python,將Python的字節碼字節碼再編譯成機器碼。ubuntu
其餘:RubyPython、Brython ...框架
簡化語法運維
1 Old: print "The answer is", 2*2 New: print("The answer is", 2*2) 2 Old: print x, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline 3 Old: print # Prints a newline 4 New: print() # You must call the function! 5 Old: print >>sys.stderr, "fatal error" New: print("fatal error", file=sys.stderr) 6 Old: print (x, y) # prints repr((x, y)) 7 New: print((x, y)) # Not the same as print(x, y)!
1 #!/usr/bin/env python #指定哪一個程序來運行,須要有執行權限
2 #-*-coding:utf-8-*- #萬國碼,UTF08
3 print("Hello World!")
安裝過程:省略。編程語言
PyCharm是一種Python IDE,帶有一整套能夠幫助用戶在使用Python語言開發時提升其效率的工具,好比調試、語法高亮、Project管理、代碼跳轉、智能提示、自動完成、單元測試、版本控制。此外,該IDE提供了一些高級功能,以用於支持Django框架下的專業Web開發。工具
默認Python版本2.6.6單元測試
PS:安裝以前須要安裝readline-devel包,解決python命令行下方向鍵顯示^[[C^[[D的問題。測試
1 yum groupinstall 'Development Tools' 2 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel 3 yum install easy_install readline-devel #安裝此包可解決python命令行內沒法使用鍵盤方向鍵的問題 4 wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz 5 tar -xf Python-3.5.1.tgz 6 cd Python-3.5.1 7 ./configure --prefix=/usr/local/python3.5.1 8 make && make install 9 ln -s /usr/local/python3.5.1/bin/python3 /usr/bin/python3 [root@Python ~]# python3 Python 3.5.1 (default, May 10 2016, 15:36:08) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
hello.py
1 #!/usr/bin/env python 2 #-*-coding:utf-8-*- 3 4 print("Hello World!")
輸出結果
[root@Python ~]# python3 hello.py Hello World!
全部引號內的內容都被認爲字符串
1 >>> name = "Dalong" 2 >>> name2 = name 3 >>> print(name,name2) 4 Dalong Dalong 5 >>> name = "Jack" 6 >>> print(name,name2) 7 Jack Dalong 8 >>>
練習1:輸入用戶密碼,並打印結果 getpass模塊
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 4 import getpass 5 username = input("username:") 6 password = getpass.getpass("password:") 7 print(username,password)
執行結果
[root@Python ~]# python3 pass.py username:dalong password: dalong 123456
練習2:if 判斷用戶名密碼是否正確
#!/usr/bin/env python #_*_coding:utf-8_*_ user = 'Dalong' passwd = 'Dalong123' username = input("username:") password = input("password:") if user == username and passwd == password: print("Welcome login") else: print("invalid username or password")
執行結果
[root@Python ~]# python if_v2.py username:dalong password:123 invalid username or password [root@Python ~]# python if_v2.py username:long password:dalong123 invalid username or password [root@Python ~]# python if_v2.py username:dalong password:dalong123 Welcome login
練習3:猜數字
猜錯3次提示是否繼續
#!/usr/bin/env python #_*_coding:utf-8_*_ age = 22 counter = 0 for i in range(10): if counter <3: guess_num = int(input("Input your guess num:")) if guess_num == age : print("Congratulations!You got it.") break #不繼續運行,跳出整個循環 elif guess_num >age: print("Think Smaller!") else: print("Think Big!") else: continue_confirm = input("Do you want Y:") if continue_confirm == 'y': counter = 0 continue else: print("bye") break counter += 1 #counter = counter + 1
執行結果
Input your guess num:11 Think Big! Input your guess num:33 Think Smaller! Input your guess num:55 Think Smaller! Do you want Y:y Input your guess num:11 Think Big! Input your guess num:33 Think Smaller! Input your guess num:22 Congratulations!You got it.
Tab命令補全
>>> import sys #加載sys模塊 >>> print(sys.path) #查看pthong環境變量路徑 ['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages'] >>> exit() ubuntu@ubuntu:~$ cat /usr/lib/python3.5/distutils/tab.py #新建tab.py文件 #!/usr/bin/env python # python startup file import sys import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind('tab: complete') # history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter >>> in #再次運行輸入命令時,按tab鍵結果 in input( int(