天天看點

Python檔案類型,變量及字元串

1. 檔案類型:

(1)源代碼:

    vim test.py

    #!/usr/bin/python

    print 'hello world!'

運作方法1:

    [root@localhost python]# python test.py

    hello world!

    [root@localhost python]#

運作方法2:

    [root@localhost python]# chmod +x test.py

    [root@localhost python]# ./test.py

(2)位元組代碼:

python源檔案編譯後為擴充名字為.pyc

删除源碼,編譯後的二進制檔案可以獨立執行。

編譯方法:

    vim 2.py

    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]# python test.pyc

(3)優化的代碼:

    python -O -m py_compile test.py

    [root@localhost python]# python -O -m py_compile test.py

    2.py  test.py  test.pyc test.pyo

    [root@localhost python]# python test.pyo

2.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)邏輯運算符:

Python檔案類型,變量及字元串

    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 匿名函數。

Python檔案類型,變量及字元串

練習:

寫一個四則運算器:

要求從鍵盤讀取數字。

input()與raw_input()

    檢視幫助:help(input)

    raw_input()都當然成字元串處理

    %s 格式化字元串。

    [root@localhost python]# cat 4.py

    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

3.Python的數值和字元串

資料類型:

    數值

    字元串

    清單

    元組

    字典

(1)數值類型:

    整型

        In [6]: a = 123

        In [7]: type(a)

        Out[7]: int

        In [8]:

    長整型

        In [8]: a = 199999999999999999999999999999

        In [9]: a

        Out[10]: 199999999999999999999999999999L

        In [11]: type(a)

        Out[12]: long

        In [13]:

     浮點型

        0.0, 12.0   -18.8   3e+7等

        科學計數法是浮點型

        In [11]: 3e+7

        Out[11]: 30000000.0

        In [12]: type(3e+7)

        Out[12]: float

        In [13]: 3.0/2

        Out[13]: 1.5

        In [14]: type(3.0/2)

        Out[14]: float

        In [15]:

    複數型

        python對複數提供内嵌支援,這是大部分軟體沒有的。

        In [8]: a = 3.14j

        Out[9]: 3.14j

        In [10]: type(a)

        Out[10]: complex

(2)字元串類型:

        In [12]: a = 'abc'

        In [13]: a

        Out[13]: 'abc'

        In [14]: type(a)

        Out[14]: str

        三重引号還可以做注釋:.

        In [28]: a = 'hello\nworld'

        In [29]: a

        Out[29]: 'hello\nworld'

        In [30]: a = "hello\nworld"

        In [31]: a

        Out[31]: 'hello\nworld'

        In [39]: a = '''hello\nworld'''

        In [40]: a

        Out[40]: 'hello\nworld'

        In [41]: print a

        hello

        world

        In [42]:

        In [43]: type(a)

        Out[44]: str

    序列索引:

        In [42]: a = 'abcde'

        In [43]: a[0]

        Out[43]: 'a'

        In [44]: a[1]

        Out[44]: 'b'

        In [45]: a[-1]

        Out[45]: 'e'

        In [46]: a[-2]

        Out[46]: 'd'

    序列切片:

        In [47]: a[0:2]

        Out[47]: 'ab'

        In [48]: a[0:4]

        Out[48]: 'abcd'

        In [49]: a[0:3]

        Out[49]: 'abc'

        In [50]: a[1:3]

        Out[50]: 'bc'

        In [56]: a[0] + a[1]

        Out[56]: 'ab'

        In [57]: a[:2]

        Out[57]: 'ab'

        In [58]: a[:]

        Out[58]: 'abcde'

        In [59]: a[:-1]

        Out[59]: 'abcd'

        In [60]: a[::-1]

        Out[60]: 'edcba'

        In [61]: a[::1]

        Out[61]: 'abcde'

        In [62]: a[:3:1]

        Out[62]: 'abc'

        In [63]: a[::2]

        Out[63]: 'ace'

        In [64]: a

        Out[64]: 'abcde'

        In [65]: a[-4::-2]

        Out[65]: 'b'

        In [66]: a[-4:-2]

        Out[66]: 'bc'

        In [67]: a[-2:-4:-1]

        Out[67]: 'dc'

        In [68]:

1.将 “123” 轉換成整數

    In [10]: a = int(123)

    In [11]: print a

    123

    In [12]:

2.将 “9999999999999999999” 轉換成長整數

    In [18]:  a = long(9999999999999999999)

    In [19]: print a

    9999999999999999999

    In [20]: a

    Out[20]: 9999999999999999999L

    In [21]:

3.将 “3.1415926” 轉換成一個浮點數

    In [21]:  a = float(3.1415926)

    In [22]: print a

    3.1415926

    In [23]:

4.将 123 轉換成一個字元串

    In [23]:  a = str(123)

    In [24]: print a

    In [25]:

5.現有以下字元串

字元串1:" abc deFGh&*ijkl opq mnrst((uvwxyz "

字元串2:" ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ "

使用字元串的各種方法轉換成如下方式

ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba

    In [25]: str1 = " abc deFGh&*ijkl opq mnrst((uvwxyz "

    In [26]: str2 = " ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ "

    In [27]: print filter(lambda x:x.isalpha(),str2+str1[::-1].lower())

    ABCDEFGHIJMNOPQKLRSTUVWXYZzyxwvutsrnmqpolkjihgfedcba

    In [28]:

本文轉自 楓葉雲  51CTO部落格,原文連結:http://blog.51cto.com/fengyunshan911/2052744

繼續閱讀