python 基礎教程(第二版)

Magnus Lie Hetland 著

第一章 基礎知識

1. 安裝python 

下載地址:https://www.python.org/downloads/

2. Linux下輸入python 進入python交互式解釋器, 按ctrl+D 退出

root@cdndev:/share/test# python
Python 2.7.9 (default, Sep 14 2019, 20:00:08) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

1.9 模塊

模塊使用import 命令來導入模塊, 然後按照“模塊.函數”的格式來使用這個模塊中的函數

>>> import math
>>> math.floor(32.9)
32.0

虛數: 1J 結尾

python 程序以.py 結尾

 

1.10.2 讓腳本像普通程序一樣運行

在test.py 代碼的首行加入

#!/usr/bin/env python

然後直接執行./test.py 腳本就可以了

1.10.3 註釋

以# 號開頭

1.11 字符串

雙引號或者單引號括起來的就是字符串

1.11.2 拼接字符串

用空格或者+ 將兩個字符串拼起來

>>> "let's go" "hello world"
"let's gohello world"
>>> "hello" + "world"
'helloworld'

1.11.3 字符串表示, str 和repr

str 函數會把值轉換爲合理形式的字符串

repr 函數會創建一個字符串,它以合法的python表達式的形式來表示值

>>> print repr("hello, world")
'hello, world'
>>> print str("hello, world")
hello, world

1.11.4 input 和raw_input 的比較

input 會假設用戶輸入的是合法的python表達式

raw_input 會把所有的輸入當作原始數據(raw data)

>>> input("enter a nuber:")
enter a nuber:1
1
>>> raw_input("enter a nuber: ")
enter a nuber: 2
'2'
>>> 

1.11.5 長字符串、原始字符串和Unicode

1. 長字符串

三個單引號''' ''''或者三個雙引號""" """

2. 原始字符串

原始字符串以r 開頭

3. Unicode

python中的普通字符串在內部是以8位的ASCII 碼存儲的, 而Unicode字符串則存儲爲16位Unicode字符。

1.12.1 本章的新函數

第二章 列表和元組

在python中,最基本的數據結構是序列(sequence)。序列中的每個元素被分配一個序號--即元素的位置,也被稱爲索引。第一個索引是0.

2.1 序列概覽

列表和元組的區別在於:列表可以修改,元組則不能。

 

 

 

 

 

p45

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章