Sunteți pe pagina 1din 26

pyLhon 101

uocs
gemng pyLhon
hup://www.pyLhon.org
uocs
luLL - Lhe pyLhon developmenL envlronmenL
pyLhon code
ouLpuL
uocs
prlnL()
prlnL('Pello World!')
prlnL('ulumaLe answer: ', 27 + 13)
Pello World!
ulumaLe answer: 42
Pello World!
<- blank llne
pyLhon code
ouLpuL
uocs
sLrlng
x = 'abcde'
prlnL(x)
abcde
pyLhon code
ouLpuL
uocs
l = 2
[ = 3
prlnL('lnLeger dlvlde 2/3: ', l/[)
lnLeger dlvlde 2/3: 0
lnLeger
pyLhon code
ouLpuL
uocs
l = 2.0 #oaL
[ = 3 # lnLeger
prlnL('oaung polnL dlvlde 2.0/3: ', l/[)
oaung polnL dlvlde 2.0/3: 0.666666666667
oaL
pyLhon code
ouLpuL
uocs
x = 'abcde'
prlnL(x[0])
prlnL(x[-1])
prlnL(x[1:4])
a
e
bcd
sequence - sLrlng
pyLhon sLarLs counung aL zero
pyLhon code
ouLpuL
uocs
x = (1,2,3,4,3)
prlnL(x[0])
prlnL(x[-1])
prlnL(x[1:4])
1
3
(2, 3, 4)
sequence - Luple
pyLhon code
ouLpuL
uocs
x = [1,2,3,4,3]
prlnL(x[0])
prlnL(x[-1])
prlnL(x[1:4])
1
3
[2, 3, 4]
sequence - llsL
pyLhon code
ouLpuL
uocs
x = (1,2,3,4,3)
x[0] = 9
1raceback (mosL recenL call lasL):
llle "/users/[roberLs/ueskLop/LesL.py", llne 2, ln <module>
x[0] = 9
1ypeLrror: 'Luple' ob[ecL does noL supporL lLem asslgnmenL
Luple ls lmmuLable
pyLhon code
ouLpuL
uocs
x = [1,2,3,4,3]
x[0] = 9
prlnL(x)
[9, 2, 3, 4, 3]
llsL ls muLable
pyLhon code
ouLpuL
uocs
x = [1,2,3,4,3]
x[0] = 'a'
x[1] = (4,3,6)
prlnL(x)
['a', (4, 3, 6), 3, 4, 3]
llsLs can have mlxed Lypes
pyLhon code
ouLpuL
uocs
answer = 42
pl = 3.141392634
prlnL('Lhe answer ls l and pl ls approx. f' (answer,pl))
prlnL('Lhe answer ls l and pl ls approx. .2f' (answer,pl))
prlnL('l\L.2f\n' (answer,pl))
Lhe answer ls 42 and pl ls approx. 3.141393
Lhe answer ls 42 and pl ls approx. 3.14
42 3.14
sLrlng formamng
Lab
llne break
pyLhon code
ouLpuL
uocs
prlnL(oaL(42))
prlnL(lnL(3.73))
prlnL(round(3.73))
prlnL(lnL(round(3.73)))
prlnL(range(3))
42.0
3
6.0
6
[0, 1, 2, 3, 4]
funcuons()
pyLhon code
ouLpuL
uocs
lmporL random
numbers = [1,2,3,4,3]
random.shuMe(numbers)
prlnL(numbers)
[2, 3, 4, 1, 3]
funcuons from lmporLed llbrarles
pyLhon code
ouLpuL
uocs
names = '!on 8oberLs\LLrnle Mross'
prlnL(1, names.spllL())
prlnL(2, names.spllL('\L'))
prlnL(3, names.spllL('\L')[0])
prlnL(4, names.lower().spllL('\L')[0].spllL())
1 ['!on', '8oberLs', 'Lrnle', 'Mross']
2 ['!on 8oberLs', 'Lrnle Mross']
3 !on 8oberLs
4 ['[on', 'roberLs']
meLhods - funcuons auached" Lo ob[ecLs
'\L' ls Lhe Lab characLer
pyLhon code
ouLpuL
uocs
words = ['one','Lwo','Lhree']
lf 'Lhree' ln words:
prlnL('found Lhree')
found Lhree
whaL lf?
pyLhon code
ouLpuL
uocs
x = 3
lf x >3:
prlnL('blg x')
else:
prlnL('x ls less Lhan or equal Lo 3')
x ls less Lhan or equal Lo 3
more lf?
pyLhon code
ouLpuL
uocs
x = 1
lf x == 3:
prlnL('Lhree')
ellf x == 3:
prlnL('ve')
else:
prlnL('noL found')
noL found
even more lf?
pyLhon code
ouLpuL
uocs
for num ln range(4):
x = num+10
prlnL(x)
prlnL('done')
10
11
12
13
done
loops
pyLhon code
ouLpuL
uocs
nameLlsL = ['[on','ernle','llnda']
for aname ln nameLlsL:
prlnL('aname -> ', aname)
aname -> [on
aname -> ernle
aname -> llnda
more loops
pyLhon code
ouLpuL
uocs
done = lalse , x = 0
whlle noL done:
x += 3
lf x>9: done = 1rue
prlnL(x)
3
6
9
12
even more loops
pyLhon code
ouLpuL
uocs
lnle = open('LabLexL.LxL', mode='ru')
for aLlne ln lnle:
prlnL(aLlne.spllL('\L'))
lnle.close()
['llne 1', 'abc', 'def\n']
['llne 2', 'ghl', '[kl']
readlng les
pyLhon code
ouLpuL
uocs
lnle = open('LabLexL.LxL', mode='ru')
for aLlne ln lnle:
prlnL(aLlne.sLrlp().spllL('\L'))
lnle.close()
['llne 1', 'abc', 'def']
['llne 2', 'ghl', '[kl']
more readlng les
pyLhon code
ouLpuL
uocs
daLa = [[1,2],[3,4]]
ouulle = open('LabLexLouL.LxL', mode='w')
for subLlsL ln daLa:
ouulle.wrlLe('l\Ll\n' (subLlsL[0],subLlsL[1]))
ouulle.close()
1 2
3 4
wrlung les
'w' for wrlLe, 'a' for append
you musL close Lhe le Lo be cerLaln all daLa are wrluen
Labs
llne breaks

S-ar putea să vă placă și