Python之路,Day01

Python介紹java


Python的創始人爲吉多·範羅蘇姆(Guido van Rossum)。
Python能夠應用於衆多領域,如:數據分析、組件集成、網絡服務、圖像處理、數值計算和科學計算等衆多領域。目前業內幾乎全部大中型互聯網企業都在使用Python,如:Youtube、Dropbox、BT、Quora(中國知乎)、豆瓣、知乎、Google、Yahoo!、Facebook、NASA、百度、騰訊、汽車之家、美團等。python

目前Python主要應用領域:linux

雲計算:雲計算最火的語言,典型應用OpenStack
WEB開發:衆多優秀的WEB框架,衆多大型網站均爲Python開發,Youtube,Dropbox,豆瓣,典型WEB框架有Django
科學運算、人工智能:典型庫NumPy,SciPy,Matplotlib,Enthoughtlibrarys,pandas
系統運維:運維人員必備語言
金融:量化交易,金融分析,在金融工程領域,Python不但在用,且用的最多,並且重要性逐年提升。緣由:做爲動態語言的Python,語言結構清晰簡單,庫豐富,成熟穩定,科學計算和統計分析都很牛逼,生產效率遠遠高於c,c++,java,尤爲擅長策略回測
圖形GUI:PyQT,WxPython,TkInterc++

 

編譯型vs解釋型shell

編譯型
優勢:編譯器通常會有預編譯的過程對代碼進行優化。由於編譯只作一次,運行時不須要編譯,因此編譯型語言的程序執行效率高。能夠脫離語言環境獨立運行。vim

缺點:編譯以後若是須要修改就須要整個模塊從新編譯。編譯的時候根據對應的運行環境生成機器碼,不一樣的操做系統之間移植就會有問題,須要根據運行的操做系統環境編譯不一樣的可執行文件。網絡

解釋型
優勢:有良好的平臺兼容性,在任何環境中均可以運行,前提是安裝瞭解釋器(虛擬機)。靈活,修改代碼的時候直接修改就能夠,能夠快速部署,不用停機維護。框架

缺點:每次運行的時候都要解釋一遍,性能上不如編譯型語言。less

 

Python的解釋器不少,但使用最普遍的仍是CPython。運維

 

Python 2 or 3?


In summary : Python 2.x is legacy, Python 3.x is the present and future of the language

Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010, with a statement of

extended support for this end-of-life release. The 2.x branch will see no new major releases after that. 3.x is

under active development and has already seen over five years of stable releases, including version 3.3 in 2012,

3.4 in 2014, and 3.5 in 2015. This means that all recent standard library improvements, for example, are only

available by default in Python 3.x.

Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement is the better Unicode support (with all text strings being Unicode by default) as well as saner bytes/Unicode separation.

Besides, several aspects of the core language (such as print and exec being statements, integers using floor division) have been adjusted to be easier for newcomers to learn and to be more consistent with the rest of the language, and old cruft has been removed (for example, all classes are now new-style, "range()" returns a memory efficient iterable, not a list as in 2.x).

 

py2與3的詳細區別


PRINT IS A FUNCTION

The statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old statement (PEP 3105). Examples:

Old: print "The answer is", 2*2 New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
Old: print >>sys.stderr, "fatal error" New: print("fatal error", file=sys.stderr)
Old: print (x, y) # prints repr((x, y))
New: print((x, y)) # Not the same as print(x, y)!
You can also customize the separator between items, e.g.:

print("There are <", 2**32, "> possibilities!", sep="")
ALL IS UNICODE NOW

今後再也不爲討厭的字符編碼而煩惱


Python安裝

Windows
一、下載安裝包
https://www.python.org/downloads/
二、安裝
默認安裝路徑:C:\python27
三、配置環境變量
【右鍵計算機】--》【屬性】--》【高級系統設置】--》【高級】--》【環境變量】--》【在第二個內容框中找到 變量名爲Path 的一行,雙擊】 --> 【Python安裝目錄追加到變值值中,用 ; 分割】
如:原來的值;C:\python27,切記前面有分號

Linux、Mac

無需安裝,原裝Python環境

ps:若是自帶2.6,請更新至2.7

 

 

Hello World程序


在linux 下建立一個文件叫hello.py,並輸入

print("Hello World!")
而後執行命令:python hello.py ,輸出

localhost:~ jieli$ vim hello.py
localhost:~ jieli$ python hello.py
Hello World!

指定解釋器

上一步中執行 python hello.py 時,明確的指出 hello.py 腳本由 python 解釋器來執行。

若是想要相似於執行shell腳本同樣執行python腳本,例: ./hello.py ,那麼就須要在 hello.py 文件的頭部指定解釋器,以下:

#!/usr/bin/env python

print "hello,world"如此一來,執行: ./hello.py 便可。

ps:執行前需給予 hello.py 執行權限,chmod 755 hello.py

在交互器中執行 

除了把程序寫在文件裏,還能夠直接調用python自帶的交互器運行代碼, 

localhost:~ jieli$ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World!")
Hello World!


變量、字符編碼


Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

聲明變量

#_*_coding:utf-8_*_

name = "Alex Li"
上述代碼聲明瞭一個變量,變量名爲: name,變量name的值爲:"Alex Li" 

變量定義的規則:

變量名只能是 字母、數字或下劃線的任意組合
變量名的第一個字符不能是數字
如下關鍵字不能聲明爲變量名
['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']

 

變量的賦值

name = "Alex Li"

name2 = name
print(name,name2)

name = "Jack"

print("What is the value of name2 now?")

 

字符編碼


Python解釋器在加載 .py 文件中的代碼時,會對內容進行編碼(默認ascill)

ASCII碼沒法將世界上的各類文字和符號所有表示,因此,就須要新出一種能夠表明全部字符和符號的編碼,即:Unicode

Unicode(統一碼、萬國碼、單一碼)是一種在計算機上使用的字符編碼。Unicode 是爲了解決傳統的字符編碼方案的侷限而產生的,它爲每種語言中的每一個字符設定了統一而且惟一的二進制編碼,規定雖有的字符和符號最少由 16 位來表示(2個字節),即:2 **16 = 65536,
注:此處說的的是最少2個字節,可能更多

UTF-8,是對Unicode編碼的壓縮和優化,他再也不使用最少使用2個字節,而是將全部的字符和符號進行分類:ascii碼中的內容用1個字節保存、歐洲的字符用2個字節保存,東亞的字符用3個字節保存...

因此,python解釋器在加載 .py 文件中的代碼時,會對內容進行編碼(默認ascill),若是是以下代碼的話:

報錯:ascii碼沒法表示中文

#!/usr/bin/env python

print "你好,世界"
改正:應該顯示的告訴python解釋器,用什麼編碼來執行源代碼,即:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print "你好,世界"

 

註釋
  當行注視:# 被註釋內容

  多行註釋:""" 被註釋內容 """

 

用戶輸入 


#!/usr/bin/env python
#_*_coding:utf-8_*_


#name = raw_input("What is your name?") #only on python 2.x
name = input("What is your name?")
print("Hello " + name )
輸入密碼時,若是想要不可見,須要利用getpass 模塊中的 getpass方法,即:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import getpass

# 將用戶輸入的內容賦值給 name 變量
pwd = getpass.getpass("請輸入密碼:")

# 打印輸入的內容
print(pwd)

 

# 僅做記錄。

相關文章
相關標籤/搜索