1. 程式人生 > >第1章python基礎

第1章python基礎

python

一、變量

1.變量定義規則

1.1.變量名只能是 字母、數字或下劃線的任意組合

1.2 變量名的第一個字符不能是數字

1.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‘]


2.定義方式

駝峰體

NumberOfStudents = 80

下劃線

age_of_oldboy = 56

number_of_students = 80



二、基本數據類型:

1.字符串:

定義: ‘, ", ‘‘‘, """引起來的一堆字符

‘hi‘ ,"hello world"

基本運算: +/*

常用方法:

‘count‘, =>子字符串出現的次數

‘index‘, ‘rindex‘, => 子字符串第一次出現的索引位置,如果不存在報錯

‘find‘, ‘rfind‘, => 子字符串第一次出現的索引位置,如果不存在返回-1

‘endswith‘,‘startswith‘, 以某個字符串開始或者以某個字符串結束時,返回True,否則返回False

‘format‘ 字符串格式化

‘join‘,

‘split‘, ‘rsplit‘,


‘lower‘, ‘upper‘

‘strip, ‘lstrip‘, ‘rstrip‘,

‘replace‘,


‘splitlines‘,


‘isalpha‘, ‘isdigit‘, ‘isalnum‘,, ‘islower‘, ‘isupper‘,




2.布爾運算


a and b | a(True) | a(False)

---------------------------------

b(True) | True | False

---------------------------------

b(False)| False | False


not a | a(True)| a(False)

--------------------------

| False | True



3.列表:

定義: []包含, 每個值之間用逗號分隔,每個值可以是任意類型

編號: 0-N 索引

-6 -5 -4 -3 -2 -1

0 1 2 3 4 5 6

ages = [25, 26, 28, 25, 34, 27, 29]


訪問:

通過索引去訪問list中的值

ages[0]

ages[6]


修改:

ages[idx] = value


增加:

pass

刪除:

del ages[idx]


1. 使用的索引必須在list索引範圍內

2. list_name[idx]

list_num[idx] = value

del list_num[idx]


個數: len()

判斷是否在list中 in


=> list(seq)

range(start, end, step)

start 開始 每次+step, 直到end(不包含end)

start, start + step, start +2step = end


start + n * step

start = 1, end = 10, step = 2


start = 1


while True:

print start

start += step

if start >= end:

break



range(start, end, step)

range(end), start = 0, step=1

range(start, end), step = 1



四則運算

+ [1, 2] + [True, False]

* ‘abc‘*5


切片

從list中獲取其中一部分元素做為一個新的list

nums = range(10)

nums = [0, ..., 9]

nums[start:end:step]

[nums[start], nums[start + step], ..., nums[start + n * step < end]]


nums[0:9:2]


nums[start:end:step]

nums[start:end] =>step=1

nums[start:] => end=>len(N)

nums[:]

nums[:end]

nums[:end:step]

nums[::step]



nums[:]


nums = range(10)

nums2 = nums

nums3 = nums[:]

地址, 值

基本的數據類型 => 值類型

a = 1

b = 1

c = b


a = []

a = b


b發生變化 del, update, 會影響a, 地址傳遞

nums = range(10)

nums[start:end] = []


list的方法

1. 更新list中的元素, 無返回值

2 不更新list中元素,但是有返回值

3. 更新list中元素,有返回值


a = 1

b = 2

temp = a

a = b

b = temp


1. while

2. raw_input

3. if

if ... else ...

if ... elif ...

if ... elif ... else ...

4. append

5. len() == 0

6. pop(0)


4.元組:

用()包含起來, 用逗號分隔沒一個元素, 只有一個元素時,最後的逗號不能省略

(1, )


元組是不能修改的序列(指向不能變,值不能變)

地址, 值


mylist = []

mytuple = (1, 2, 3, mylist, 4)

tuple()



三、流程控制


if 邏輯表達式0:

子語句0

elif 邏輯表達式1:

子語句1

elif 邏輯表達式2:

子語句2

....

else:

子語句N


if 邏輯表達式0:

子語句0

elif 邏輯表達式1:

子語句1

elif 邏輯表達式2:

子語句2

....


while 邏輯表達式:

子語句

else:

子語句


for var in seq:

子語句


range(N) => [0, ..., N-1]


for name in [‘kk‘, ‘woniu‘]:

print name


for i in range(10):

if i == 5:

break/continue

print i


第1章python基礎