Basic Python
Osnove Pythona
Korisne informacije
list(xrange()) == range() --> U python3, range je xrange u python2 (nije lista već generator) Razlika između Tuple i Liste je u tome što pozicija vrednosti u tuple-u daje značenje, dok su liste samo uređene vrednosti. Tuple imaju strukturu, dok liste imaju redosled.
Osnovne operacije
Za stepenovanje broja koristite: 3**2 (ne 3^2) Ako uradite 2/3, vraća 1 jer delite dva cela broja (integers). Ako želite decimale, treba da delite float-ove (2.0/3.0). i >= j i <= j i == j i != j a and b a or b not a float(a) int(a) str(d) ord("A") = 65 chr(65) = 'A' hex(100) = '0x64' hex(100)[2:] = '64' isinstance(1, int) = True "a b".split(" ") = ['a', 'b'] " ".join(['a', 'b']) = "a b" "abcdef".startswith("ab") = True "abcdef".contains("abc") = True "abc\n".strip() = "abc" "apbc".replace("p","") = "abc" dir(str) = Lista svih dostupnih metoda help(str) = Definicija klase str "a".upper() = "A" "A".lower() = "a" "abc".capitalize() = "Abc" sum([1,2,3]) = 6 sorted([1,43,5,3,21,4])
Spajanje karaktera 3 * ’a’ = ‘aaa’ ‘a’ + ‘b’ = ‘ab’ ‘a’ + str(3) = ‘a3’ [1,2,3]+[4,5]=[1,2,3,4,5]
Delovi liste ‘abc’[0] = ‘a’ 'abc’[-1] = ‘c’ 'abc’[1:3] = ‘bc’ od [1] do [2] "qwertyuiop"[:-1] = 'qwertyuio'
Komentari # Komentar u jednoj liniji """ Komentar u više linija Još jedan """
Petlje
Tuple
t1 = (1, '2', 'tri') t2 = (5, 6) t3 = t1 + t2 = (1, '2', 'tri', 5, 6) (4,) = Singlton d = () prazan tuple d += (4,) --> Dodavanje u tuple NE MOŽE! --> t1[1] == 'Nova vrednost' list(t2) = [5, 6] --> Iz tuple u listu
Lista (niz)
d = [] prazan a = [1, 2, 3] b = [4, 5] a + b = [1, 2, 3, 4, 5] b.append(6) = [4, 5, 6] tuple(a) = (1, 2, 3) --> Iz liste u tuple
Rečnik
d = {} prazan monthNumbers={1:'Jan', 2: 'feb','feb':2}—> monthNumbers ->{1:'Jan', 2: 'feb','feb':2} monthNumbers[1] = 'Jan' monthNumbers['feb'] = 2 list(monthNumbers) = [1, 2, 'feb'] monthNumbers.values() = ['Jan', 'feb', 2] keys = [k for k in monthNumbers] a={'9':9} monthNumbers.update(a) = {'9':9, 1:'Jan', 2: 'feb','feb':2} mN = monthNumbers.copy() #Nezavisna kopija monthNumbers.get('ključ',0) #Provera da li ključ postoji, Vraća vrednost monthNumbers["ključ"] ili 0 ako ne postoji
Skup
U skupovima nema ponavljanja myset = set(['a', 'b']) = {'a', 'b'} myset.add('c') = {'a', 'b', 'c'} myset.add('a') = {'a', 'b', 'c'} #Bez ponavljanja myset.update([1, 2, 3]) = set(['a', 1, 2, 'b', 'c', 3]) myset.discard(10) #Ako je prisutno, ukloni, ako nije, ništa myset.remove(10) #Ako je prisutno, ukloni, ako nije, podiže izuzetak myset2 = set([1, 2, 3, 4]) myset.union(myset2) #Vrednosti u myset ILI myset2 myset.intersection(myset2) #Vrednosti u myset I myset2 myset.difference(myset2) #Vrednosti u myset ali ne i u myset2 myset.symmetric_difference(myset2) #Vrednosti koje nisu ni u myset NI myset2 (nisu u oba) myset.pop() #Dobijanje prvog elementa skupa i uklanjanje myset.intersection_update(myset2) #myset = Elementi u oba myset i myset2 myset.difference_update(myset2) #myset = Elementi u myset ali ne i u myset2 myset.symmetric_difference_update(myset2) #myset = Elementi koji nisu u oba
Klase
Metod u __It__ će biti korišćen od strane sortiranja za poređenje da li je objekat ove klase veći od drugog
map, zip, filter, lambda, sorted i jednolinijski izrazi
Map je kao: [f(x) for x in iterable] --> map(tutple,[a,b]) = [(1,2,3),(4,5)] m = map(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]) --> [False, False, True, False, False, True, False, False, True]
Zip se zaustavlja kada se kraći od foo ili bar zaustavi:
Lambda se koristi za definisanje funkcije (lambda x,y: x+y)(5,3) = 8 --> Koristite lambda kao jednostavnu funkciju sorted(range(-5,6), key=lambda x: x** 2) = [0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5] --> Koristite lambda za sortiranje liste m = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]) = [3, 6, 9] --> Koristite lambda za filtriranje reduce (lambda x,y: x*y, [1,2,3,4]) = 24
mult1 = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x%3 == 0 ]
Izuzeci
Assert()
Ako je uslov netačan, string će biti prikazan na ekranu
Generatori, yield
Generator umesto vraćanja nečega, "daje" nešto. Kada mu pristupite, "vratit će" prvu generisanu vrednost, zatim možete ponovo pristupiti i dobiti sledeću generisanu vrednost. Dakle, sve vrednosti nisu generisane istovremeno i mnogo memorije može biti ušteđeno koristeći ovo umesto liste sa svim vrednostima.
g = myGen(6) --> 6 next(g) --> 7 next(g) --> Greška
Redovni izrazi
import re re.search("\w","hola").group() = "h" re.findall("\w","hola") = ['h', 'o', 'l', 'a'] re.findall("\w+(la)","hola caracola") = ['la', 'la']
Posebna značenja: . --> Sve \w --> [a-zA-Z0-9_] \d --> Broj \s --> Beli znak[ \n\r\t\f] \S --> Znak koji nije beli znak ^ --> Počinje sa $ --> Završava se sa + --> Jedan ili više * --> 0 ili više ? --> 0 ili 1 pojavljivanje
Opcije: re.search(pat,str,re.IGNORECASE) IGNORECASE DOTALL --> Dozvoljava tački da se poklapa sa novim redom MULTILINE --> Dozvoljava ^ i $ da se poklapaju u različitim redovima
re.findall("<.*>", "<b>foo</b>and<i>so on</i>") = ['<b>foo</b>and<i>so on</i>'] re.findall("<.*?>", "<b>foo</b>and<i>so on</i>") = ['<b>', '</b>', '<i>', '</i>']
IterTools product from itertools import product --> Generiše kombinacije između 1 ili više lista, možda ponavljajući vrednosti, kartezijev proizvod (distributivno svojstvo) print list(product([1,2,3],[3,4])) = [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)] print list(product([1,2,3],repeat = 2)) = [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
permutations from itertools import permutations --> Generiše kombinacije svih karaktera na svakoj poziciji print list(permutations(['1','2','3'])) = [('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'),... Svaka moguća kombinacija print(list(permutations('123',2))) = [('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')] Svaka moguća kombinacija dužine 2
combinations from itertools import combinations --> Generiše sve moguće kombinacije bez ponavljanja karaktera (ako postoji "ab", ne generiše "ba") print(list(combinations('123',2))) --> [('1', '2'), ('1', '3'), ('2', '3')]
combinations_with_replacement from itertools import combinations_with_replacement --> Generiše sve moguće kombinacije od karaktera nadalje (na primer, 3. je mešan od 3. nadalje ali ne sa 2. ili 1.) print(list(combinations_with_replacement('1133',2))) = [('1', '1'), ('1', '1'), ('1', '3'), ('1', '3'), ('1', '1'), ('1', '3'), ('1', '3'), ('3', '3'), ('3', '3'), ('3', '3')]
Dekoratori
Dekorator koji meri vreme potrebno za izvršavanje funkcije (od ovde):
Ako ga pokrenete, videćete nešto slično sledećem:
Last updated