1. 源代碼--直接由Python解析
1
2
3
|
vi 1.py #!/usr/bin/python print 'hello world' |
這里的1.py就是源代碼
執(zhí)行方式和shell腳本類似:
chmod +x 后,./1.py
Python 1.py
2. 字節(jié)代碼
Python源碼文件經(jīng)編譯后生成的擴(kuò)展名為pyc的文件
編譯方法:
1
2
3
4
|
[root@t1 py]# cat 2.py #!/usr/bin/python import py_compile py_compile.compile('1.py') |
寫一個(gè)2.py腳本,執(zhí)行,界面沒(méi)有輸出,但是看下文件列表,多了一個(gè)1.pyc
1
2
3
4
5
6
|
[root@t1 py]# python 2.py [root@t1 py]# ll 總用量 12 -rw-r--r-- 1 root root 38 12月 20 21:06 1.py -rw-r--r-- 1 root root 112 12月 20 21:10 1.pyc -rw-r--r-- 1 root root 66 12月 20 21:09 2.py |
怎么執(zhí)行?還是python 2.py。
而且,如果源碼文件1.py不在了,2.py照樣可以執(zhí)行
3. 優(yōu)化代碼
經(jīng)過(guò)優(yōu)化的源碼文件,擴(kuò)展名為pyo
python –O –m py_compile 1.py
1
2
3
|
[root@t1 py]# python -O -m py_compile 1.py [root@t1 py]# ls 1.py 1.pyc 1.pyo 2.py |
執(zhí)行優(yōu)化代碼后,生成1.pyo。執(zhí)行1.pyo
1
2
|
[root@t1 py]# python 1.pyo hello world |
2.python的變量
變量是計(jì)算機(jī)內(nèi)存中的一塊區(qū)域,變量可以存儲(chǔ)規(guī)定范圍內(nèi)的值,而且值可以改變。
Python下變量是對(duì)一個(gè)數(shù)據(jù)的引用
變量的命名
變量名由字母、數(shù)字、下劃線組成。
變量不能以數(shù)字開頭
不可以使用關(guān)鍵字
a a1 _a
變量的賦值
是變量的聲明和定義的過(guò)程
a = 1
id(a) #id顯示a在內(nèi)存的位置號(hào)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
In [1]: a = 123 In [2]: id(a) Out[2]: 25933904 In [3]: a = 456 In [4]: id(a) Out[4]: 33594056 In [5]: x = 'abc' In [6]: x = abc --------------------------------------------------------------------------- NameError Traceback (most recent call last) < ipython-input-6-c455442c5ffd > in < module >() ----> 1 x = abc NameError: name 'abc' is not defined |
上面報(bào)錯(cuò)的解釋,默認(rèn)情況下:
1
2
3
4
|
Python不需要事先聲明變量的類型,自動(dòng)判斷
1
2
3
|
In [7]: a = 456 In [8]: type(a) Out[8]: int |
type查出a的變量類型是整數(shù)int
1
2
3
|
In [9]: a = '456' In [10]: type(a) Out[10]: str |
type查出a的變量類型是字符串str
Python運(yùn)算符包括
1.賦值運(yùn)算符
1
2
3
4
5
6
|
=: x = 3, y = ‘abcd' #等于 +=: x += 2 #x=x+2 -=: x -= 2 #x=x-2 *=: x *= 2 #x=x*2 /=: x /= 2 #x=x/2 %=: x %= 2 #取余數(shù) |
2.算術(shù)運(yùn)算符
1
2
3
4
5
6
7
|
+ - * / // % ** |
舉例1:
1
2
3
4
5
|
In [20]: a = 1 ;b = 2 In [21]: a+b Out[21]: 3 In [22]: 'a' + 'b' Out[22]: 'ab' |
ab賦值后,a+b是數(shù)字。直接加兩個(gè)字符就是合在一起的字符串
舉例2:
1
2
3
4
|
In [24]: 4 / 3 Out[24]: 1 In [25]: 4.0 / 3 Out[25]: 1.3333333333333333 |
4直接除3,因?yàn)槟J(rèn)是整數(shù),所以結(jié)果取整數(shù)1
要想得到小數(shù),將4變成浮點(diǎn)數(shù)4.0
特別的,//表示強(qiáng)制取整
1
2
|
In [26]: 4.0 // 3 Out[26]: 1.0 |
舉例3:
1
2
3
4
|
In [27]: 2 ** 3 Out[27]: 8 In [28]: 2 * 3 Out[28]: 6 |
一個(gè)*是乘,兩個(gè)**是冪
3.關(guān)系運(yùn)算符
1
2
3
4
5
6
|
> : 1 > 2 < : 2 < 3 >=: 1 >= 1 <=: 2 <= 2 ==: 2 == 2 !=: 1 != 2 |
1
2
3
4
|
In [33]: 1 > 2 Out[33]: False In [34]: 1 < 2 Out[34]: True |
成立就是true,不成立false
4.邏輯運(yùn)算符
1
2
3
|
and邏輯與: True and False or邏輯或: False or True not邏輯非: not True |
舉例:
1
2
3
4
5
6
|
In [35]: 1 < 2 and 1 >2 Out[35]: False In [36]: 1 < 2 or 1 >2 Out[36]: True In [37]: not 1 > 2 Out[37]: True |
運(yùn)算優(yōu)先順序:
input和raw_input
input適合數(shù)字,raw_input適合字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
In [41]: input("input num:") input num:123 Out[41]: 123 In [42]: input("input num:") input num:abc --------------------------------------------------------------------------- NameError Traceback (most recent call last) < ipython-input-42-3cd60768312e > in < module >() ----> 1 input("input num:") < string > in < module >() NameError: name 'abc' is not defined In [43]: input("input num:") input num:'abc' Out[43]: 'abc' In [44]: raw_input("input num:") input num:abc Out[44]: 'abc' |
有上面可以看出在input下面,直接輸入abc報(bào)錯(cuò),但是raw_input正常顯示。
由此可以寫一個(gè)計(jì)算腳本
1
|
[root@t1 py]# cat cal.py |
!/sur/bin/python
num1 = input("please input a num :")
num2 = input("please input a num :")
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)
1
2
|
%s分別對(duì)應(yīng)后面的數(shù)值 執(zhí)行腳本 |
[root@t1 py]# python cal.py
please input a num :5
please input a num :6
5 + 6 = 11
5 - 6 = -1
5 * 6 = 30
5 / 6 = 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
### 3.Python的數(shù)值和字符串 數(shù)值類型: - [ ] 整形int 整型int可以存儲(chǔ)2^32個(gè)數(shù)字,范圍-2,147,483,648到2147483647 例如:0,100,-100 - [ ] 長(zhǎng)整型long Long的范圍很大,幾乎可以說(shuō)任意大的整數(shù)均可以存儲(chǔ)。 為了區(qū)分普通整型,需要在整數(shù)后加L或l。 例如: 2345L,0x34al - [ ] 浮點(diǎn)float 例如:0.0,12.0,-18.8,3e+7等 - [ ] 復(fù)數(shù)型complex Python對(duì)復(fù)數(shù)提供內(nèi)嵌支持,這是其他大部分軟件所沒(méi)有的。 復(fù)數(shù)例子:- 3.14j,8.32e-36j - [ ] 字符串 string 有三種方法定義字符串類型 - str = ‘this is a string' #普通字符串 - str = “this is a string” #能夠解析\n等特殊字符 - str = ‘'‘this is a string'‘' #可以略去\n 三重引號(hào)(docstring)除了能定義字符串還可以用作注釋。 舉例: |
In [3]: a = '''hello
...: world'''
In [4]: a
Out[4]: 'hello\nworld'
In [5]: print a
hello
world
1
|
- 字符串索引,0開始,-1表示最后一個(gè),-2倒數(shù)第二個(gè),類推 |
In [6]: a = 'abcde'
In [7]: a[0:2]
Out[7]: 'ab'
1
|
a[]表示取索引指定的字符,[0:2]可以類比數(shù)學(xué)中的0<=a<2,即0<=a<=1,就是取第一個(gè)和第二個(gè),不包括第三個(gè) |
In [8]: a[:2]
Out[8]: 'ab'
In [9]: a[:]
Out[9]: 'abcde'
1
2
|
0或者-1可以省略 - 字符串切片 |
In [11]: a[0:3:2]
Out[11]: 'ac'
1
2
|
只取a和c,首先應(yīng)該是a[0:3],但是這樣的結(jié)果是abc,a[0:3:2]的2表示步進(jìn)2個(gè),跳過(guò)b。同理,如果是a[0:3:3]表示步進(jìn)3。 **如果要將整個(gè)字符串倒過(guò)來(lái),需要用-1 |
In [17]: a[::-1]
Out[17]: 'edcba'
可以類比下面的
In [16]: a[:-1]
Out[16]: 'abcd'
來(lái)個(gè)更加直觀的
In [13]: a[-2:-4:-1]
Out[13]: 'dc'
1
2
3
|
取倒數(shù)第2個(gè)和倒數(shù)第三個(gè),而且倒序顯示 ### 4.作業(yè) |
將 “123” 轉(zhuǎn)換成整數(shù)
將 “9999999999999999999” 轉(zhuǎn)換成長(zhǎng)整數(shù)
將 “3.1415926” 轉(zhuǎn)換成一個(gè)浮點(diǎn)數(shù)
將 123 轉(zhuǎn)換成一個(gè)字符串
現(xiàn)有以下字符串
字符串1:" abc deFGh&ijkl opq mnrst((uvwxyz "
字符串2:" ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(&YZ "
使用字符串的各種方法轉(zhuǎn)換成如下方式
ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba
1
2
|
解答: 1.將 “123” 轉(zhuǎn)換成整數(shù) |
num = int(123)
print num
1
|
2.將 “9999999999999999999” 轉(zhuǎn)換成長(zhǎng)整數(shù) |
num = long(9999999999999999999)
print num
1
|
3.將 “3.1415926” 轉(zhuǎn)換成一個(gè)浮點(diǎn)數(shù) |
num = float(3.1415926)
print num
1
|
4.將 123 轉(zhuǎn)換成一個(gè)字符串 |
num = str(123)
print num
1
2
3
|
5.最后一題 分析思路:兩個(gè)字符串都要剔除首尾空格,特殊字符,轉(zhuǎn)換大小寫,切片,相加 - [ ] 剔除首尾空格,特殊字符 |
str1 = " abc deFGh&ijkl opq mnrst((uvwxyz "
str2 = " ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(&YZ "
str1 = str1.strip()
str2 = str2.strip()
print str1
print str2
1
2
3
|
strip()剔除首尾分隔符,默認(rèn)是空格,可以自定義,自定義用'XX'例子見(jiàn)圖示  執(zhí)行結(jié)果 |
C:\Users\chawn\PycharmProjects\171220\venv\Scripts\python.exe C:/Users/chawn/.PyCharmCE2017.3/config/scratches/scratch.py
abc deFGh&ijkl opq mnrst((uvwxyz
ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(&YZ
1
|
還可以用替換來(lái)剔除空格、其他字符 |
str1 = " abc deFGh&ijkl opq mnrst((uvwxyz "
str2 = " ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(&YZ "
str1 = str1.replace(' ','').replace('&','').replace('((','')
str2 = str2.replace(' ','').replace('#','').replace('%','').replace('&&','').replace('(&','')
print str1
print str2
1
2
|
replace可以替換任意位置的空格,還有字符 - [ ] 大小寫轉(zhuǎn)換+切片 |
str1 = " abc deFGh&ijkl opq mnrst((uvwxyz "
str2 = " ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(&YZ "
str1 = str1.replace(' ','').replace('&','').replace('((','')
str2 = str2.replace(' ','').replace('#','').replace('%','').replace('&&','').replace('(&','')
str1 = str1.lower()
str1 = str1[0:12]+str1[15:17]+str1[12:15]+str1[17:]
str2 = str2[0:10]+str2[10:15]+str2[15:17]+str2[17:]
print str2 + str1[::-1]
1
|
執(zhí)行結(jié)果 |
C:\Users\chawn\PycharmProjects\171220\venv\Scripts\python.exe C:/Users/chawn/.PyCharmCE2017.3/config/scratches/scratch.py
ABCDEFGHIJMNOPQKLRSTUVWXYZzyxwvutsrqponmlkjihgfedcba
以上這篇基于Python的文件類型和字符串詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/c1awn/archive/2017/12/20/8075968.html