Python tunnis tehtud näited: Difference between revisions
From ICO wiki
Jump to navigationJump to search
No edit summary |
No edit summary |
||
Line 89: | Line 89: | ||
import random | import random | ||
fo = open("fail.txt", ' | fo = open("fail.txt", 'w') | ||
for x in range(1, 1001): | for x in range(1, 1001): | ||
Line 96: | Line 96: | ||
fo.close() | fo.close() | ||
</source> | |||
Skript sorteerib fail.txt-s olevad numbrid ja väljastab uute faili | |||
<source lang="python"> | |||
#!/usr/bin/python | |||
# -*- coding: utf-8 -*- | |||
#Skript sorteerib fail.txt-s olevad numbrid ja väljastab | |||
# uute faili | |||
fo2 = open("fail.txt", "r" ) | |||
fo = open("sorteeritud.txt", 'w') | |||
lines = fo2.readlines() | |||
results = map(int, lines) | |||
results.sort() | |||
for item in results: | |||
fo.write("%s\n" % item) | |||
fo.close() | |||
fo2.close() | |||
</source> | |||
Skript sorteerib fail.txt-s olevad numbrid ja väljastab ainult unikaalsed numbrid | |||
<source lang="python"> | |||
#!/usr/bin/python | |||
# -*- coding: utf-8 -*- | |||
#Skript sorteerib fail.txt-s olevad numbrid ja väljastab ainult | |||
# unikaalsed numbrid | |||
fo = open("fail.txt", "r" ) | |||
fo2 = open("unikaalsed.txt", 'w') | |||
lines = fo.readlines() | |||
results = map(int, lines) | |||
results2 = list(set(results)) | |||
results2.sort() | |||
for item in results2: | |||
fo2.write("%s\n" % item) | |||
fo.close() | |||
fo2.close() | |||
</source> | </source> |
Revision as of 15:05, 9 April 2013
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Tere täpiline kommentaar
import sys
print sys.argv[0], sys.argv[1]
print 'Tere maailm!'
muutuja = 5
teine_muutuja = 2.0
print 'jagatis on', muutuja/teine_muutuja
print 'Palun sisesta midagi'
vastus = int(raw_input())
print 'sisestasid', vastus/2.0
if vastus%2 == 0:
print 'vastus on paarisarv'
else:
print 'vastus on paaritu arv'
for i in range(1,9999):
print 'i =',i
while True:
print "sisesta midagi"
a = raw_input()
if a == 'q':
print 'programm lõpetas töö'
sys.exit(0)
Pythoni programm, mis kontrollib, kas arv jagub 10-ga
#!/usr/bin/python
# -*- coding: utf-8 -*-
while True:
print 'Kirjuta mingi arv, mis jagub 10ga'
#raw_input annab stringi, see on vaja teha integer tüüpi
kasutajaarv = int(raw_input())
if kasutajaarv%10 == 0:
break
print 'Jagus! Programm lõpetab!'
Skript väljastab esmalt argumentide arvu ja argumendid (iga argument eraldi real)
#!/usr/bin/python
# -*- coding: utf-8 -*-
#loo skript, mis saab käsurealt 0..n argumenti
#skript väljastab esmalt argumentide arvu ja argumendid (iga argument eraldi real)
import sys
print 'argumentide arv:', len(sys.argv)
for i in sys.argv:
print "Argument: ", i
Pythoni programm, mis väljastab ainult paaritud argumendid
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Loo skript mis saab käsurealt 0..n argumenti
#väljastab kõik paaritud argumendid (iga argument eraldi real)
import sys
for i in sys.argv[1:]:
if int(i)%2 == 0:
pass
else:
print 'Paaritu argument: ', i
Pythoni programm, mis kirjutab faili, kus on 1000 rida ja igal real on juhuarv 1..1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
import random
fo = open("fail.txt", 'w')
for x in range(1, 1001):
ran=str(random.randrange(1,1000+1))
fo.write(ran+'\n')
fo.close()
Skript sorteerib fail.txt-s olevad numbrid ja väljastab uute faili
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Skript sorteerib fail.txt-s olevad numbrid ja väljastab
# uute faili
fo2 = open("fail.txt", "r" )
fo = open("sorteeritud.txt", 'w')
lines = fo2.readlines()
results = map(int, lines)
results.sort()
for item in results:
fo.write("%s\n" % item)
fo.close()
fo2.close()
Skript sorteerib fail.txt-s olevad numbrid ja väljastab ainult unikaalsed numbrid
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Skript sorteerib fail.txt-s olevad numbrid ja väljastab ainult
# unikaalsed numbrid
fo = open("fail.txt", "r" )
fo2 = open("unikaalsed.txt", 'w')
lines = fo.readlines()
results = map(int, lines)
results2 = list(set(results))
results2.sort()
for item in results2:
fo2.write("%s\n" % item)
fo.close()
fo2.close()