You are on page 1of 18

www.hoasen.edu.

vn

Lp trnh Script
n tp
Phm Th Kim Ngn Email: ngon.phamthikim@hoasen.edu.vn
uu
1

www.hoasen.edu.vn

Thi cui k

thi gm 2 phn: Trc nghim: 40 cu (6 im) T lun: 2 bi (4 im) Thi gian: 120 pht Khng c tham kho ti liu

uu

www.hoasen.edu.vn

Ni dung n tp
1. List & Tuple 2. String 3. Dictionary 4. Hm v cu trc iu khin 5. Abstraction 6. Sets 7. re module 8. files 9. Database 10.Lp trnh vi giao din
uu
3

www.hoasen.edu.vn

1. List & Tuple C php to tuples, list Ton t indexing Ton t Slicing Adding Multiplying Cc phng thc ca List

append count extend index

n Tp

insert pop remove reverse sort

uu

www.hoasen.edu.vn

n Tp

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] numbers[4:6] numbers[-3:-1] numbers[-3:0] numbers[-3:] numbers[7:10:1] numbers[7:10:2] [4, 5, 6] + [4, 5, 6] [1, 2, 3] + on tap 5*ontap

uu

www.hoasen.edu.vn

n tp
2. nh dng vi string Find Join Split Replace Strip Translate

uu

www.hoasen.edu.vn

n tp
CONVERSION SPECIFIER ANATOMY Bt u vi k t % Conversion Flag (c chuyn i): l tu chn. C th l +, hoc -, hoc l khong trng thm trc cc s, hoc l s 0 ch rng thm s 0 ng trc khi chuyn i rng ti thiu ca trng (tu chn): chui c chuyn i phi c rng ti thiu l gi tr ny. Nu l * c ngha l rng s bng rng gi tr ca tuple truyn Gi tr theo sau du . (chia phn thp phn) (tu chn): y l trng c rng ti a. Nu chui chuyn i l s thc th gi tr s quy nh sau du chm l gi tr xc nh di ti a ca phn thp phn ca s thc. Nu l mt chui th gi tr sau du chm l chiu di ti a ca chui. Nu l * th chiu di phn thp phn bng chiu di ca gi tr ca tuple truyn Loi chuyn i (Conversion type)
uu
7

www.hoasen.edu.vn

Loi chuyn i

uu

www.hoasen.edu.vn

n tp
3. Dictionary To dictionary Dictionary = {key:value} Cc phng thc ca dictionary copy deepcopy get setdefault has_key pop popitem keys Values

uu

www.hoasen.edu.vn

n tp
4. Hm v cu trc iu khin Cc cch s dng module import somemodule from somemodule import somefunction from somemodule import * from module1 import open as open1 from module2 import open as open2 import math as foobar foobar.sqrt(4) Cc thao tc gn bin hp l

uu

10

www.hoasen.edu.vn

n tp

Cc cu trc iu khin : if, for , while,


>>> [x*x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> [x*x for x in range(10) if x % 3 == 0] [0, 9, 36, 81] >>> [(x, y) for x in range(3) for y in range(3)] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] names = ['anne', 'beth', 'george', 'damon'] ages = [12, 45, 32, 102] >>> zip(names, ages) [('anne', 12), ('beth', 45), ('george', 32), ('damon', 102)] for name, age in zip(names, ages): print name, 'is', age, 'years old'

uu

11

www.hoasen.edu.vn

n tp

Tham s
def print_params_2(title, *params): print title print params >>> print_params_2('Params:', 1, 2, 3) Params: (1, 2, 3) >>> print_params_2('Nothing:') Nothing: ()

uu

12

www.hoasen.edu.vn

n tp

Tham s
def print_params_3(**params): print params >>> print_params_3(x=1, y=2, z=3) {'z': 3, 'x': 1, 'y': 2} def print_params_4(x, y, z=3, *pospar, **keypar): print x, y, z print pospar print keypar >>> print_params_4(1, 2, 3, 5, 6, 7, foo=1, bar=2) 123 (5, 6, 7) {'foo': 1, 'bar': 2}

uu

13

www.hoasen.edu.vn

uu

5. Abstraction C php khai bo class nh ngha hm trong class Truy cp v gn cc thuc tnh ca class K tha Truy xut cc thuc tnh class cha v con issubclass(sub, sup) isinstance(obj, Class) Overloading methods Overriding methods Data hiding Property function

14

www.hoasen.edu.vn

6. sets
Ton t Tng ng Kt qu

l kiu d liu trong cc phn t ca khng trng nhau. S phn t s phn t sp xp tu x in Cc s Kim tra x c l phn t ca s Set: len(s)
x not in s s.issubset(t) s.issuperset(t) s.union(t) s.intersection(t) s.difference(t) s.symmetric_difference(t)
uu

Kim tra x c phi khng l phn t ca s s <= t s >= t s|t s&t s-t s^t Kim tra cc phn t ca s c trong t Kim tra tt c cc phn t ca t c trong s Tr v tp mi bao gm cc phn t c trong t v s Tr v tp mi bao gm cc phn t chung ca t v s Tr v tp mi gm cc phn t c trong s m khng c trong t Tr v tp mi gm cc phn t c trong s m khng c trong t hoc ngc li Tr v tp mi sao chp t s
15

s.copy()

www.hoasen.edu.vn

n tp
7. re module 8. Files Open files
Value 'r' 'w' 'a' 'b' '+' Description Read mode Write mode Append mode Binary mode (added to other mode) Read/write mode (added to other mode)


uu

Cc thao tc trn files: c, ghi Duyt cc ni dung trn file


16

www.hoasen.edu.vn

n tp
9. Tng tc vi c s d liu: Kt ni vi c s d liu Thc hin cc thao tc: truy xut, thm, xo, sa trn c s d liu Cc hm c bn python cung cp tng tc vi c s d liu 10. Lp trnh vi giao din Vit giao din cho cc chc nng ca cc ng dng qun l c bn,
ng thi tng tc vi c s d liu

uu

17

www.hoasen.edu.vn

Question & Answer

uu

18

You might also like