一、爲什麼學習python
- 服務於大數據、人工智能、自動化運維。
- 簡單易學
- 代碼簡潔
- 薪資高
- 近幾年越來越火
二、Python的安裝
linux 系統默認安裝,
CentOS7 默認安裝了python2.7
安裝ipython
yum install -y epel-release
yum install -y python-pip
pip install ipython==5.3.0
進入ipython
windows 系統中下載軟件,安裝環境變量,測試啓動。
下載地址: https://www.python.org/downloads/windows/
windows中工具pycharm中的快捷鍵
alt + shift +f10 執行
ctrl + / 註釋
ctrl + d 複製上行生成一行或多行
ctrl + c 複製一行
ctrl + x 剪切
ctrl + v 粘貼
ctrl + a 全選
.ctrl + shift + n :在多文件中項目中快速查找文件
ctrl + alt + l: ctrl+a 選中,ctrl + alt + l 快速調整格式。
alt + shift 看虛擬處錯誤,處理
sys.argv[0] 參數設置
sys.argv[1] 參數設置
alt +回車 點中虛線 自動import
tab 光緒行後走
shift + tab 光標行前走
shift + enter 自動跳到一下行,
ctrt + enter 往上開新行,
三、Python的文件類型
- 文件類型:
(1)源代碼(.py):
vim test.py
#!/usr/bin/python
print 'hello world!'
運行方法1:
[root[@localhost](https://my.oschina.net/u/570656) python]# python test.py
hello world!
[root[@localhost](https://my.oschina.net/u/570656) python]#
運行方法2:
[root[@localhost](https://my.oschina.net/u/570656) python]# chmod +x test.py
[root[@localhost](https://my.oschina.net/u/570656) python]# ./test.py
hello world!
[root[@localhost](https://my.oschina.net/u/570656) python]#
(2)字節代碼(.pyc):
python源文件編譯後爲擴展名字爲.pyc
刪除源碼,編譯後的二進制文件可以獨立執行。
編譯方法:
vim 2.py
#!/usr/bin/python
import py_compile
py_compile.compile('test.py')
[root@localhost python]# python 2.py
[root@localhost python]# ls
2.py test.py test.pyc
[root@localhost python]#
[root@localhost python]# python test.pyc
hello world!
[root@localhost python]#
(3)優化的代碼(.pyo):
python -O -m py_compile test.py
[root@localhost python]# python -O -m py_compile test.py
[root@localhost python]# ls
2.py test.py test.pyc test.pyo
[root@localhost python]# python test.pyo
hello world!
[root@localhost python]#
四、Python的變量
Python的變量
變量是計算機內存中的一塊區域,變量可以存儲規定範圍內的值,而且值可以改變結構。
python下變量是對一個數據的引用
(1)變量的命名:
變量名的長度不受限制,但其中的字符必須是字母、數字、或者下劃線(_),而不能使用空格、連字符、標點符號、引號或其他字符。
變量名的第一個字符不能是數字,而必須是字母或下劃線。
Python區分大小寫。
不能將Python關鍵字用作變量名。
例如: a a1 _a
(2)變量的賦值:
是變量的聲明和定義的過程。
a = 123
In [1]: a = 123
In [2]: a
Out[2]: 123
In [3]: id(a)
Out[3]: 7891024
In [4]: a = 456
In [5]: id(a)
Out[5]: 19127624
In [6]:
(3)運算符和表達式:
賦值運算符
算術運算符
關係運算符
邏輯運算符
表達式:
將不同的數據(包括變量、函數)用運算符號按一定的規則連接起來的一種式子。
1)賦值運算符
In [68]: a = 3
In [69]: a
Out[69]: 3
In [70]: a+=3
In [71]: a
Out[71]: 6
In [72]: a-=4
In [73]: a
Out[73]: 2
In [76]: a*=3
In [77]: a
Out[77]: 6
In [78]: a/=2
In [79]: a
Out[79]: 3
In [80]: a%=3
In [81]: a
Out[81]: 0
In [82]:
2)算術運算符
In [82]: 1 + 2
Out[82]: 3
In [83]: 2 - 1
Out[83]: 1
In [84]: 2 * 2
Out[84]: 4
In [85]: 6 / 2
Out[85]: 3
In [86]: 6 % 2
Out[86]: 0
In [88]: 3.999999 / 2
Out[88]: 1.9999995
In [89]: 3.999999 // 2
Out[89]: 1.0
In [90]: 3 ** 2
Out[90]: 9
In [91]:
3)關係運算符:
In [91]: 1 > 2
Out[91]: False
In [92]: 2 < 3
Out[92]: True
In [93]: 2 >= 1
Out[93]: True
In [94]: 3 <= 56
Out[94]: True
In [95]: 3 == 3
Out[95]: True
In [96]: 2 != 34
Out[96]: True
In [97]:
4)邏輯運算符:
In [97]: 1 < 2 and 2 > 0
Out[97]: True
In [98]: 1 == 1 and 2 < 1
Out[98]: False
In [99]: 1 == 1 or 2 < 1
Out[99]: True
In [100]: not 1 > 2
Out[100]: True
5)各種運算符的優先級:
往右越高 上到下越高,
lambda 匿名函數。
練習:
寫一個四則運算器:
要求從鍵盤讀取數字。
input()與raw_input()
查看幫助:help(input)
raw_input()都當然成字符串處理
%s 格式化字符串。
[root@localhost python]# cat 4.py
#!/usr/bin/python
num1 = input("Please input: ")
num2 = input("Please input: ")
print "%s + %s = %s" % (num1,num2,num1+num2)
print "%s - %s = %s" % (num1,num2,num1-num2)
print "%s * %s = %s" % (num1,num2,num1*num2)
print "%s / %s = %s" % (num1,num2,num1/num2)
[root@localhost python]# python 4.py
Please input: 3
Please input: 5
3 + 5 = 8
3 - 5 = -2
3 * 5 = 15
3 / 5 = 0
[root@localhost python]#