This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Boolovské hodnoty True a False sa používajú hlavne na testovanie a môžete ich kombinovať spojkami or, and alebo not | |
| not (10 < 20 and 30 > 20) # = False | |
| # okrem <, > môžete používať napr. porovnania <=, >=, == (má rovnakú hodnotu), != (nemá rovnakú hodnotu) | |
| # prázdnu hodnotu môžete otestovať operátorom is (resp. is not), ktorá porovnáva identitu objektov, napr.: | |
| answer = False | |
| answer is not None # = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # s číslami môžete používať bežné operácie + - * / | |
| (50 - 5.0 * 6) / 4 # = 5.0 | |
| # Python 2 zachová pri delení celých čísel iba celú časť (Python 3 vypočíta desatinný výsledok) | |
| 17 / 3 # = 5 pre Python 2 | |
| # ak je aspoň jedno číslo desatinné, vypočíta sa desatinný výsledok | |
| 17 / 3.0 # = 5.666666666666667 - pre kompatibilitu pridajte .0 aj pre celé čísla | |
| 17 // 3.0 # = 5.0 - // vypočíta iba celú časť delenia aj pre desatinné čísla | |
| 17 % 3 # = 2 - % vypočíta celočíselný zvyšok po delení | |
| 3 ** 2 # = 9 - ** vypočíta mocninu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ohraničujúce znaky môžete do reťazca vložiť cez postupnosť \' resp. \" ak je reťazec ohraničený úvodzovkami | |
| # podobne môžete do reťazca vložiť znak pre nový riadok \n, tabulátor \t alebo samotný znak \ ako \\ | |
| s = '"Isn\'t," she said.' | |
| print s | |
| # môžete definovať aj prázdny reťazec | |
| s = "" | |
| # reťazce ohraničené úvodzovkami alebo apostrofmi musia byť zapísané na jednom riadku | |
| # ak chcete zadať viacriadkový reťazec bez zápisu \n, môžete reťazec ohraničiť troma úvodzovkami |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # volanie viacerých metód môžete zreťaziť | |
| print " Python \t".strip().upper().replace("PY", "py") # = pyTHON | |
| # pri vyhľadávaní podreťazcov môžete zadať aj rozsah s negatívnymi indexami | |
| "one two one".find("one", -5) # = 8 - index druhého výskytu, vyhľadávaný text musí byť medzi poslednými 5 znakmi | |
| # metóda format automaticky prevedie atribúty na reťazce, takže namiesto: | |
| # str(1) + "two" + str(False) | |
| # používajte volanie metódy: | |
| print "{0}{1}{2}".format(1, "two", False) # číslo v {} označuje index parametra, ktorý sa dosadí namiesto formátovacieho poľa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # zoznamy sa definujú v hranatých zátvorkách [] a jednotlivé prvky sú oddelené , | |
| numbers = ["zero", 1, 2, 3] # zoznam môže obsahovať prvky rôznych typov | |
| # indexovanie je podobné ako pri reťazcoch, záporné indexy označujú prvky od konca zoznamu | |
| print numbers[0] # = ‘zero’ | |
| print numbers[-3] # = 1 | |
| numbers[0] = 0 # na rozdiel od reťazcov, prvky môžete meniť | |
| # aj pri zoznamoch môžete používať + na spájanie a * na opakovanie | |
| numbers = numbers + [4, 6] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # n-tice sa definujú ohraničením prvkov do zátvoriek () oddelených , | |
| t = (1, "two", True) # n-tice môžu obsahovať hodnoty rôznych typov | |
| # zoznam môžete previesť na n-ticu funkciou tuple | |
| t = tuple([1, "two", True]) | |
| # indexovanie je rovnaké ako pri zoznamoch alebo reťazcoch | |
| t[0:2] # = (1, "two") - rozsah vráti n-ticu | |
| # na rozdiel od zoznamov, prvky nemožno meniť (zápis t[index] = hodnota skončí chybou TypeError) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| basket = ["apple", "orange", "apple"] | |
| fruits = set(basket) | |
| print fruits # vypíše set([‘orange’,’apple’]) bez duplicitnej hodnoty ‘apple’ | |
| # nie je definované v akom poradí sú hodnoty množiny usporiadané! | |
| # množiny sú objekty s metódami, napr.: | |
| fruits.add("banana") # metóda add pridá ďalšiu hodnotu do množiny (ak tam už nie je) | |
| fruits.remove("orange") # remove odstráni hodnotu z množiny |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # mapy sa definujú zápisom dvojíc kľúč : hodnota oddelených , medzi zátvorky {} | |
| tel = {"jack": 1234, "john": 2345} | |
| # k hodnotám sa pristupuje indexovaním podľa kľúča | |
| # na rozdiel od zoznamov alebo n-tíc, okrem čísel môžete ako kľúč použiť napr. reťazce, alebo nemenné objekty | |
| tel["jack"] # = 1234 | |
| # ak sa pokúsite pristupovať k neexistujúcemu kľúču, príkaz skončí chybou KeyError | |
| # ak chcete otestovať či daný kľúč v mape existuje, použite operátor in | |
| "jack" in tel # = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # z modulu ‘datetime’ naimportujeme typy pre reprezentáciu času a dátumu ‘time’ a ‘datetime’ | |
| from datetime import time | |
| from datetime import datetime # ‘datetime’ je názov modulu aj typu | |
| # objekt pre reprezentáciu času vytvoríte funkciou time(hodina, minúta, sekunda, mikrosekunda) | |
| t = time(12) # čas 12:00:00 – ak vynecháte niektoré atribúty, budú doplnené 0 hodnotou | |
| # okrem metód môžu mať objekty definované aj dátové atribúty | |
| # pre typ ‘time’ sú definované atribúty hour, minute, second a microsecond | |
| print "{0:02d}:{1:02d}".format(t.hour, t.minute) # vypíše 12:00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| number = 42 | |
| if number < 0: | |
| print "negative value" # chyba! vnorené príkazy musia byť odsadené na novom riadku aspoň jednou medzerou alebo tabulátorom |
OlderNewer