一. python介紹相關
1. Python簡介
Python 是一個高層次的結合瞭解釋性、編譯性、互動性和麪向對象的腳本語言。 - Python 的設計具備很強的可讀性,相比其餘語言常常使用英文關鍵字,其餘語言的一些標點符號,它具備比其餘語言更有特點語法結構。 - Python 是一種解釋型語言: 這意味着開發過程當中沒有了編譯這個環節。相似於PHP和Perl語言。 - Python 是交互式語言: 這意味着,您能夠在一個Python提示符,直接互動執行寫你的程序。 - Python 是面嚮對象語言: 這意味着Python支持面向對象的風格或代碼封裝在對象的編程技術。
2. Python主要應用領域
- 雲計算: 雲計算最火的語言, 典型應用OpenStack; - WEB開發: 衆多優秀的WEB框架,衆多大型網站均爲Python開發、Youtube、 Dropbox、豆瓣、知乎等典型WEB框架有Django、Flask、Web2py、AngularJS; - 科學運算、人工智能: 典型庫NumPy、 SciPy、Matplotlib、Enthought、librarys、pandas; - 系統運維: 運維人員必備語言,升職加薪必備之選; - 金融:量化交易,金融分析,在金融工程領域,Python不但在用,且用的最多,並且重要性逐年提升。緣由:做爲動態語言的Python,語言結構清晰簡單,庫豐富,成熟穩定,科學計算和統計分析都很牛逼,生產效率遠遠高於c、c++、java、尤爲擅長策略回測; - 圖形GUI:PyQT、WxPython、TkInter。
2、Python基礎
3. Python2與Python3相關
1 Python2.X和Python3.X的區別
2
3 What are the differences?
4
5 Short version: Python 2.x is legacy, Python 3.x is the present and future of the language
6
7 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.
8
9 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.
10
11 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).
12
13 The What's New in Python 3.0 document provides a good overview of the major language changes and likely sources of incompatibility with existing Python 2.x code. Nick Coghlan (one of the CPython core developers) has also created a relatively extensive FAQ regarding the transition.
14
15 However, the broader Python ecosystem has amassed a significant amount of quality software over the years. The downside of breaking backwards compatibility in 3.x is that some of that software (especially in-house software in companies) still doesn't work on 3.x yet.
16
17 詳細地址:https://wiki.python.org/moin/Python2orPython3
18
19 - 區別一:
20 Python2:print 'Hellow world!'
21 Python3: print ('Hellow world!')
22 - 區別二:
23 Python2:raw_input
24 Python3:input
4. Python安裝
5. 第一行Python代碼
1 - IDE中執行:
2 print ("Hello world!")
3 - Linux環境,將"print ('Hello world!')"代碼寫入到first.py文件中:
4 python first.py
5 - 指定解釋器執行:
6 #/usr/bin/env python
7 print ('Hello world!')
8 chmod +x first.py
9 ./first.py
6. 編碼格式和二進制
- 二進制十進制轉換
1 2 4 8 16 .... 65535
1 1 1 1 1 .... 1*16
- Python2.X默認字符編碼是ASSCII,Python3.X默認字符編碼UFT-8.- 聲明字符編碼:
#-*- coding:uft-8 -*-
#-*- coding:gbk -*-
- asscii碼 255 1bytes 一個字符佔8個字節
- unicode萬國碼 2bytes
- utf-8 en:1byte cn:3bytes
7. 變量的定義
- 變量:一個在內存儲存數據的容器,以便後面的程序調用,變量先定義後引用;
-變量定義規則:
- [1] 變量名只能是 字母、數字或下劃線的任意組合
- [2] 變量名的第一個字符不能是數字
- [3] 如下關鍵字不能聲明爲變量名
['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']
- [4] 寫法
age_of_oldboy = 56
ageOfOldboy = 56 #駝峯寫法
AGE_OF_OLDBOY = 56 #常量,定義不變的量
8. 字和字符串
數字不須要加引號
字符串必須加引號
9. 註釋
單行註釋:#
多行註釋:"""或者'''
多行字符串:""" (標註段落)
10. 字符串格式
****Python默認輸入都是字符串****
int() #interger,字符串轉數字
str() #string,數字轉字符串
type() #查看數據類型:print(type())
strip() #去掉換行符和空格
11. 字符串格式化拼接
1 %s 變量能夠是字符和數字(string)
2 %d 變量只能是數字,能夠用來檢測數據類型
3 %f 變量只能是小數(浮點型)
4
5 方案一:
6 name = input("name:")
7 age = input("age:")
8 job = input("job:")
9 salary = input("salary:")
10
11 info = '''
12 -----info of '''+name+'''----
13 Name:'''+name+'''
14 Age:'''+age+'''
15 Job:'''+job+'''
16 Salary:'''+salary+'''
17 '''
18 print(info)
19
20 方案二:
21 name = input("name:")
22 age = input("age:")
23 job = input("job:")
24 salary = input("salary:")
25
26 info = '''
27 ------ info of %s------
28 Name:%s
29 Age:%s
30 Job:%s
31 Salary:%s
32 ----------END----------'''%(name,name,age,job,salary)
33 print(info)
34
35 方案三:
36 name = input("name:")
37 age = input("age:")
38 job = input("job:")
39 salary = input("salary:")
40
41 info2 = '''
42 ------ info of {_name}------
43 Name:{_name}
44 Age:{_age}
45 Job:{_job}
46 Salary:{_salary}
47 '''.format(_name=name,_age=age,_job=job,_salary=salary)
48 print(info2)
49
50 方案四:
51 name = input("name:")
52 age = input("age:")
53 job = input("job:")
54 salary = input("salary:")
55 info2 = '''
56 -----info of {0}-----
57 Name:{0}
58 Age:{1}
59 Job:{2}
60 Salary:{3}
61 ------END----'''.format(name,age,job,salary)
62 print(info2)
12. if判斷和while循環
if判斷,判斷帳號密碼是否正確;
1 user = 'lain'
2 passwd = '123456'
3 username = input("username:")
4 password = input("password:")
5
6 if username == user and password == passwd:
7 print("歡迎登錄!")
8 else:
9 print("帳戶名或密碼錯誤,請重試!")
while循環,猜年齡,猜3次後可選擇是否繼續;
1 age_of_oldboy = 56
2 count = 0
3 while count < 3:
4 guess_age = int(input("guess age:"))
5 if guess_age == age_of_oldboy:
6 print("yes.you got it.")
7 break
8 elif guess_age > age_of_oldboy:
9 print("think smaller...")
10 else:
11 print("think bigger.")
12 count +=1
13 if count == 3:
14 countine_confirm = input("do you want to keep guessing? y/n")
15 if countine_confirm != "n":
16 count = 0
17 else:
18 print("error,you idiot!")
Pycharm小記
1 Ctrl /
2
3 註釋(取消註釋)選擇的行
4
5
6 Shift + Enter
7 開始新行
8
9 Ctrl + Enter
10 智能換行
11
12 TAB Shift+TAB
13 縮進/取消縮進所選擇的行
14
15 Ctrl + Alt + I
16 自動縮進行
17
18 Ctrl + Y
19 刪除當前插入符所在的行
20
21 Ctrl + D
22 複製當前行、或者選擇的塊
23
24 Ctrl + Shift + J
25 合併行
26
27 Ctrl + Shift + V
28 從最近的緩存區裏粘貼
29
30 Ctrl + Delete
31 刪除到字符結尾
32
33 Ctrl + Backspace
34 刪除到字符的開始
35
36 Ctrl + NumPad+/-
37 展開或者收縮代碼塊
38
39 Ctrl + Shift + NumPad+
40 展開全部的代碼塊
41
42 Ctrl + Shift + NumPad-
43 收縮全部的代碼塊