Giáo trình python cơ bản dành cho người mới giáo trình python cơ bản - Công nghệ Website | Đại học Bách Khoa, Đại học Đà Nẵng

Giáo trình python cơ bản dành cho người mới giáo trình python cơ bản - Công nghệ Website | Đại học Bách Khoa, Đại học Đà Nẵng giúp sinh viên tham khảo, ôn luyện và phục vụ nhu cầu học tập của mình cụ thể là có định hướng, ôn tập, nắm vững kiến thức môn học và làm bài tốt trong những bài kiểm tra, bài tiểu luận, bài tập kết thúc học phần, từ đó học tập tốt và có kết quả cao cũng như có thể vận dụng tốt những kiến thức mình đã học

+MЪMXLMЖY
,
MЖRRE]4]XLSRPɧQШXXVSRKRLоRKRKɺRRKоPϼTXVɳRL
ч
ERKчвGGLɾʁFЮMXɴRLчEHϰRKZАиRKHдRKXLZMЖR
T
LSRKTLɾZɧGШRKчТRKчɺRKчϲS
ɇ
ɪPɧQZMЖGZЪM4,4RʆQZɧGɹRLоRKXɨGZдQɧ4,4
O
LɹQɧXLрGLMЖRXРMYчвGOLMЎRQɳRLTLϲMXTGϼRZЪM
4
]XLSRXVSRKKMEMчSϰRRɧ]
'
YРRWɨGLRLОRɧ]чвGZMЎXXVSRKUYɨXVɳRLQɳRLFϾXч϶Y
L
МG4]XLSRZɧKMϲMUY]ЎXGɨGFɧMXSɨRGFϲRXLISRLYG϶Y
G
жEQɳRL
,
]ZМRKRLоRKKLMGLɰTGжEQɳRLGˢRKWЌKMɾTɴGLGLS
R
LоRKEMчERKUYERXɩQчЎRZMЖGиRKHдRK4]XLSRZɧS
G
ɺRKZMЖGZɧ\мPʁLMЖRXϰM
1
дGPдG
7
ɨGLчвGGLMEPɧQGLRKQЦMGLRKWЌXVɳRLFɧ]
O
LɴEGϰRLGжE4]XLSRQɧQɳRLWЌKІTTLϲMZɧWЌLоYɴGL
OLMFMЎXGɨGOMЎRXLиGRɧ]XVSRKZMЖGɨTHдRK4]XLSRZɧS
G
ɺRKZMЖGXVSRKXRKPEM
 ,IPPS[SVPH
 'ɾTLɨT
 4LɩRGLMEQS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
X
VɳRLTLɩRXɴGLGɾTLɨTHɸRKPЖRLHIFYKŷXVSRKTLϰQZM
G
YРRWɨGLRLОRɧ]XLɳQɳRLLЪRKчЎRGɨGLXLрGXLM
T
]XLSRFЀRKHɸRKPЖRL
,
IPPS[SVPH
8
ϰSQШXJMPIGɹXɱRPɧhelloworld.pyZɧGɹRШMHYRKRL
W
EY
print 'Hello world'
printPɧPЖRLGFϲRRLϴXчВ\YϴXQШXFMЎRVEXLЬRKPɧ
Q
ɧRLɳRL
7
EYчɹZɧSQɧRLɳRLGSQQERHPMRIHMGLY]ВRчЎRXL
Q
дGGLиEJMPIRɧ]ZɧKɻ
$ helloworld.pypython
2
ЎYXLϴ]\YϴXLMЖRHɸRKGLоHello world XиGPɧFϰRчɪ
L
SɧRXLɧRLZMЖGZMЎXиRKHдRKT]XLSRч϶YXR
'LRK'ɾTLɨT
&MЎRWР
/
LEMFɨSFMЎRFЀRKQШXGɩYPЖRLKɨR
a = 1
F
ϰRGɹXLВKɨRRLMАYPSϰMKMɨXVКWРGLYЦMGLSQШXFMЎR
a = 1
a = 'Hello World'
a [ , , = 1 2 3]
a [ , , , ] = 1.2 'Hello' 'W' 2
8SɨRXмWРLМG
4
]XLSRGˢRKLЦXVвQШXWРXSɨRXмXSɨRLМGXLɺRKHдRK
R
L
+ TLɰTGШRK
- TLɰTXVк
* TLɰTRLɩR
/ TLɰTGLME
% TLɰTGLMEPϴ]HQSHYPS
&SSPIERZɧ8SɨRXмPSKMG
+
MɨXVКчɾRKZɧWEMXRKиRKPɧ Zɧ True False
notчВчϲSKMɨXVК
andTLɰTXɴRLPSKMGZɧ%2(
orTLɰTXɴRLPSKMGLSІG36
1
ШXWРTLɰTWSWɨRLXRKXLЬRKRL FɰLR< <=
L
RLSІGFЀRK PЪRLR PЪRLRLSІGFЀRK> >= ==
FЀRK!=OLɨGчВWSWɨRLKMɨXVК
,
ЦXVвHϰRKWSWɨRLOɰTRL
x = 2
1 3 x < < # True
10 20 x < < # False
3 2 x > <= # True
2 4 x == < # True
8
SɨRXмOMВQXVETL϶RXмXVSRKQШXXϼTLвT inOMВQXVE
G
ɹXТRXϰM not in OMВQOLɺRKXТRXϰM
'good' in 'this is a greate example' # F
alse
'good' not in 'this is a greate example' # True
'ϴYXVɾGчMАYOLMВR
4
]XLSRLЦXVвQШXWРGϴYXVɾGчMАYOLMВRXLɺRKHдRK,϶Y
L
ЎXGɨGGϴYXVɾGчMАYOLMВRчАYHрEZɧSXLдXч϶YHɸRK
MRHIRXMSRчВXϰSXLɧRLQШXFPSGO\мPʁXLE]ZɳWмHдRK
{ }ŷ RLGɨGRKɺRRKоOLɨG4,4.EZEWGVMTX
-
JŷIPMJŷIPWI
if condition1 :
indentedStatementBlockForTrueCondition1
elif condition2 :
indentedStatementBlockForFirstTrueCondition2
elif condition3 :
indentedStatementBlockForFirstTrueCondition3
elif condition4 :
indentedStatementBlockForFirstTrueCondition4
else:
indentedStatementBlockForEachConditionFalse
7[MXGLŷGEWI
4
]XLSROLɺRKGɹGϴYXVɾGswitchŷcase
*SVŷMR
for iterating_var sequence:in
statements(s)
:
ɴHд
for letter : in 'Python' # First Example
print , letter'Current Letter :'
fruits [ , , = 'banana' 'apple' 'mango']
for fruit fruits: in # Second Example
print , fruit'Current fruit :'
print "Good bye!"
/
ЎXUYϲLMВRXLКGжEZɴHдXVɱR
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
;LMPI
while expression:
statement(s)
:
ɴHд
count = 0
while (count < 9):
print , count'The count is:'
count count = + 1
print "Good bye!"
/
ЎXUYϲLMВRXLКGжEZɴHдXVɱR
The count : is 0
The count : is 1
The count : is 2
The count : is 3
The count : is 4
The count : is 5
The count : is 6
The count : is 7
The count : is 8
Good bye!
,ɧQ
/
LEMFɨSLɧQXLISGɾTLɨT
def functionname(param, param2,..):
statements(s)
,
ɧQRЎYOLɺRKXVϲHоPMЖYXLɳQІGчКRLWЌXVϲZАKMɨX
None
:
ɴHдOLEMFɨSLɧQXɴRLZɧXVϲKMɨXVКXФRKGжEXLEQ
W
Рч϶YZɧS
def sum(a, b):
(a b) return +
'
ɨGLKМMLɧQ
sum(1, 2)
(trҥ v giá tr ) Ӄ Ӎ 3
,
ɧQGɹLЦXVвKMɨXVКQІGчКRLGLSXLEQWРOLMOLɺRK
X
VY]АRZɧS:ɴHдLɧQWEY
def plus(c, d = 10):
(creturn +d)
2
ЎYKМMLɧQXVɱRRLWEY
plus(2)
(kӁt qu tr v ҥ ҥ Ӄ 12)
1
ШXOLɨGFMЖXXVSRKGɨGLKМMLɧQGжE4]XLSRWSZЪM4,4
P
ɧGLɾRKXEGɹXLВXLE]чФMXLиXрXLEQWРXVY]АRZɧSFЀRK
GɨGLчІXXɱRXLEQWРOLMKМMLɧQ:ɴHдXEGɹXLВKМMLɧQ
sum(a,b)ЮZɴHдXVɱRFЀRKGɨGLXVY]АRXLEQWР XVЪGb a
R
LWEY
sum(b , a = 1 = 10)
<мPʁGLYЦM
1
ШXGLYЦMGɹXLВOLEMFɨSFЀRKHϴYRLɨ]чɺM" LSІGчR
' :ɴHдGɨGGLYЦMWEY
str1 = "Hello"
str2 = 'world'
'
ɹXLВXVY]\YϴXXкRKOʁXрXVSRKQШXGLYЦMXLISLɳRLXLиG
M
RHI\ZɴHд  ŷstr1[0] str1[1]
'
ɹXLВWмHдRKHϴYRLɨ]чɺMLSІGчRчВOLEMFɨS
G
LYЦMXVɱRRLMАYHɸRK:ɴHд
paragraph = """This is line 1
This is line 2
This is line 3"""
2РMGLYЦM
'
ɹXLВXϰSQШXGLYЦMHɧMXкZMЖGRРMGɨGGLYЦMPϰMXLISGɾ
T
LɨT
str str1 str2= + " " +
8VɴGL\YϴXGLYЦMGSR
'
ɹXLВXϰSGɨGGLYЦMGSRXLɺRKUYEXSɨRXмPϴ]OLSϲR
[start:end]VERKI1ІGчКRL startPɧXкZКXVɴч϶YGLYЦM
0 Zɧ endPɧчЎRZКXVɴGYРMGLYЦM:ɴHд
str = 'Hello world'
print str[ :0 4]
(HiӅn th ) Ӎ "Hell"
print str[:4]
(HiӅn th ) Ӎ "Hell"
print str[-3:]
(HiӅn th Ӎ "rld")
print str[ :6 -3]
(HiӅn th Ӎ "wo")
0ϴ]чШHɧMGжEGLYЦM
7
мHдRKLɧQ len(...)чВXVϲZАчШHɧMGжEGLYЦM:ɴHд
count len( ) = "Hello world"
(count có giá tr Ӎ 11)
8ɳQXLE]XLЎRШMHYRK
'
ɹXLВXɳQZɧXLE]XXVSRKGLYЦMFЀRKGɨGLKМMTLRK
XLиG
replace(search, replace[, max])
GжEQШXGLYЦM:ɴ
H
д
str = 'Hello world'
newstr str.replace( , = 'Hello' 'Bye')
print newstr
(Sҿ hi n th chu i trên màn nh)Ӆ Ӎ ə "Bye world"
8ɳQZКXGLYЦMGSR
'
ɹXLВXɳQZКXVɴGжEQШXGLYЦMGSRXVSRKGLYЦMPЪRFЀRK
G
ɨGLKМMTLRKXG find(str, beg=0
e
nd=len(string))&ϾXч϶YPɧZКXVɴ 0 RЎYOLɺRKXɳQVEX
X
VϲZА -1:ɴHд
str = 'Hello world'
print str.find('world')
(hiӅn th Ӎ 6)
print str.find( )'Bye' ;
(hiӅn th Ӎ -1)
,
ɧQ find()WЌXɳQXLISXXрXкXVɨMUYETLϲMGжEGLYЦM
XиGPɧXкP϶R\YϴXLMЖRч϶YXMɱR'ɹXLВHɽRKLɧQ
rfind()
ч
ВXɳQXLISZКXXкGYРMGLYЦMTLɴEXVЪG
8ɨGLGLYЦM
'
ɹXLВXɨGLGLYЦMHрEXLISQШXGLYЦMHIPMQIXIVFЀRKGɨGL
K
МMTLRKXLиG split(str="", num=string.count(str))
:
ɴHд
str = 'Hello world'
print str.split(' ')
(Trҥ v m t m ng ph n t là chu i vàӃ ӛ ҥ 2 ҩ ӯ 2 ə "Hello"
"world")
'
ɹXLВWмHдRKLɧQ splitlines()чВXɨGLGLYЦMXLIS
X
кRKL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RPɧ helloworld.pyZɧGɹRШMHYRKRL WEY print 'Hello world'
printPɧPЖRLGFϲRRLϴXчВ\YϴXQШXFMЎRVEXLЬRKPɧ QɧRLɳRL
7EYчɹZɧSQɧRLɳRLGSQQERHPMRIHMGLY]ВRчЎRXL
QдGGLиEJMPIRɧ]ZɧKɻ $ python helloworld.py
2ЎYXLϴ]\YϴXLMЖRHɸRKGLо Hello world XиGPɧFϰRчɪ
LSɧRXLɧRLZMЖGZMЎXиRKHдRKT]XLSRч϶YXMɱR
'LRK'ɾTLɨT &MЎRWР
/LEMFɨSFMЎRFЀRKQШXGɩYPЖRLKɨR a = 1
FϰRGɹXLВKɨRRLMАYPSϰMKMɨXVКWРGLYЦMGLSQШXFMЎR a = 1 a = 'Hello World' a = [1, 2, 3] a = [1.2, 'Hello', 'W', 2]
8SɨRXмWРLМG
4]XLSRGˢRKLЦXVвQШXWРXSɨRXмXSɨRLМGXLɺRKHдRK RL + TLɰTGШRK - TLɰTXVк * TLɰTRLɩR / TLɰTGLME
% TLɰTGLMEPϴ]HQSHYPS
&SSPIERZɧ8SɨRXмPSKMG
+MɨXVКчɾRKZɧWEMXRKиRKPɧ TrueZɧ False
notчВчϲSKMɨXVК
andTLɰTXɴRLPSKMGZɧ%2(
orTLɰTXɴRLPSKMGLSІG36
1ШXWРTLɰTWSWɨRLXLɺRKXLЬRKRL < FɰLR <=Fɰ
LRLSІGFЀRK > PЪRLR >=PЪRLRLSІGFЀRK ==
FЀRK !=OLɨGчВWSWɨRLKMɨXVК
,ЦXVвHϰRKWSWɨRLOɰTRL x = 2 1 < x < 3 # True 10 < x < 20 # False 3 > x <= 2 # True 2 == x < 4 # True
8SɨRXмOMВQXVETL϶RXмXVSRKQШXXϼTLвT inOMВQXVE
GɹXТRXϰM not in OMВQOLɺRKXТRXϰM
'good' in 'this is a greate example' # F alse
'good' not in 'this is a greate example' # True
'ϴYXVɾGчMАYOLMВR
4]XLSRLЦXVвQШXWРGϴYXVɾGчMАYOLMВRXLɺRKHдRK,϶Y
LЎXGɨGGϴYXVɾGчMАYOLMВRчАYHрEZɧSXLдXч϶YHɸRK
MRHIRXMSRчВXϰSXLɧRLQШXFPSGO\мPʁXLE]ZɳWмHдRK
{ ŷ } RLGɨGRKɺRRKоOLɨG4,4.EZEWGVMTX -JŷIPMJŷIPWI if condition1 :
indentedStatementBlockForTrueCondition1 elif condition2 :
indentedStatementBlockForFirstTrueCondition2 elif condition3 :
indentedStatementBlockForFirstTrueCondition3 elif condition4 :
indentedStatementBlockForFirstTrueCondition4 else:
indentedStatementBlockForEachConditionFalse
7[MXGLŷGEWI
4]XLSROLɺRKGɹGϴYXVɾG switchŷ case *SVŷMR for iterating_var in sequence: statements(s) :ɴHд
for letter in 'Python': # First Example
print 'Current Letter :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # Second Example
print 'Current fruit :', fruit print "Good bye!"
/ЎXUYϲLMВRXLКGжEZɴHдXVɱR Current Letter : P Current Letter : y Current Letter : t Current Letter : h Current Letter : o Current Letter : n Current fruit : banana Current fruit : apple Current fruit : mango Good bye! ;LMPI while expression: statement(s) :ɴHд count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"
/ЎXUYϲLMВRXLКGжEZɴHдXVɱR The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye! ,ɧQ
/LEMFɨSLɧQXLISGɾTLɨT
def functionname(param, param2,..): statements(s)
,ɧQRЎYOLɺRKXVϲHоPMЖYXLɳQІGчКRLWЌXVϲZАKMɨXVК None
:ɴHдOLEMFɨSLɧQXɴRLZɧXVϲZАKMɨXVКXФRKGжEXLEQ WРч϶YZɧS def sum(a, b): return (a+b) 'ɨGLKМMLɧQ sum(1, 2) (trҥ vӃ giá trӍ là 3)
,ɧQGɹLЦXVвKMɨXVКQІGчКRLGLSXLEQWРOLMOLɺRK
XVY]АRZɧS:ɴHдLɧQWEY def plus(c, d = 10): return (c+d)
2ЎYKМMLɧQXVɱRRLWEY plus(2) (kӁt quҥ trҥ vӃ là 12)
1ШXOLɨGFMЖXXVSRKGɨGLKМMLɧQGжE4]XLSRWSZЪM4,4
PɧGLɾRKXEGɹXLВXLE]чФMXLиXрXLEQWРXVY]АRZɧSFЀRK
GɨGLчІXXɱRXLEQWРOLMKМMLɧQ:ɴHдXEGɹXLВKМMLɧQ
sum(a,b)ЮZɴHдXVɱRFЀRKGɨGLXVY]АRXLEQWР b XVЪG a RLWEY sum(b = 1, a = 10)
<мPʁGLYЦM
1ШXGLYЦMGɹXLВOLEMFɨSFЀRKHϴYRLɨ]чɺM " LSІGчR
' :ɴHдGɨGGLYЦMWEY str1 = "Hello" str2 = 'world'
'ɹXLВXVY]\YϴXXкRKOʁXрXVSRKQШXGLYЦMXLISLɳRLXLиG
MRHI\ZɴHд str1[0] str1[1]ŷ
'ɹXLВWмHдRKHϴYRLɨ]чɺMLSІGчRчВOLEMFɨS
GLYЦMXVɱRRLMАYHɸRK:ɴHд paragraph = """This is line 1 This is line 2 This is line 3"""
2РMGLYЦM
'ɹXLВXϰSQШXGLYЦMHɧMXкZMЖGRРMGɨGGLYЦMPϰMXLISGɾ TLɨT str = str1 + " " + str2
8VɴGL\YϴXGLYЦMGSR
'ɹXLВXϰSGɨGGLYЦMGSRXLɺRKUYEXSɨRXмPϴ]OLSϲR
[start:end]VERKI1ІGчКRL startPɧXкZКXVɴч϶YGLYЦM
 0 Zɧ endPɧчЎRZКXVɴGYРMGLYЦM:ɴHд str = 'Hello world' print str[0:4] (HiӅn thӍ "Hell") print str[:4] (HiӅn thӍ "Hell") print str[-3:] (HiӅn thӍ "rld") print str[6:-3] (HiӅn thӍ "wo")
0ϴ]чШHɧMGжEGLYЦM
7мHдRKLɧQ len(...)чВXVϲZАчШHɧMGжEGLYЦM:ɴHд count = len("Hello world") (count có giá trӍ 11)
8ɳQXLE]XLЎRШMHYRK
'ɹXLВXɳQZɧXLE]XLЎXVSRKGLYЦMFЀRKGɨGLKМMTLRK
XLиG replace(search, replace[, max])
GжEQШXGLYЦM:ɴ Hд str = 'Hello world'
newstr = str.replace('Hello', 'Bye') print newstr
(Sҿ hiӅn thӍ chuəi "Bye world" trên màn hình)
8ɳQZКXVɴGLYЦMGSR
'ɹXLВXɳQZКXVɴGжEQШXGLYЦMGSRXVSRKGLYЦMPЪRFЀRK
GɨGLKМMTLRKXLиG find(str, beg=0
e nd=len(string))&ϾXч϶YPɧZКXVɴ 0 RЎYOLɺRKXɳQVEXLɳ
XVϲZА -1:ɴHд str = 'Hello world' print str.find('world') (hiӅn thӍ 6) print str.find('Bye'); (hiӅn thӍ -1)
,ɧQ find()WЌXɳQXLISXLиXрXкXVɨMUYETLϲMGжEGLYЦM
XиGPɧXкP϶R\YϴXLMЖRч϶YXMɱR'ɹXLВHɽRKLɧQ rfind()
чВXɳQXLISZКXVɴXкGYРMGLYЦMZАTLɴEXVЪG
8ɨGLGLYЦM
'ɹXLВXɨGLGLYЦMHрEXLISQШXGLYЦMHIPMQIXIVFЀRKGɨGL
KМMTLRKXLиG split(str="", num=string.count(str))  :ɴHд str = 'Hello world' print str.split(' ')
(Trҥ vӃ mӛt mҥng có 2 phҩn tӯ là 2 chuəi "Hello" và "world")
'ɹXLВWмHдRKLɧQ splitlines()чВXɨGLGLYЦMXLIS
XкRKLɧRKZɧPSϰMFОOʁXр2);0-2)
8VMQOʁXрOLSЂRKXVϾRK
'ɹXLВPSϰMFОGɨGOʁXрQІGчКRLPɧOʁXрOLSϲRKXVϾRK
XVЪGZɧWEYQШXGLYЦMFЀRKGɨGLKМMGɨGTLRKXLиG WEY