Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • viteks/nsi-lectures
1 result
Show changes
Commits on Source (3)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Úvod do Pythonu # [Primitivní datové typy](#primitivni-datove-typy)
## Primitivní datové typy
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# celá čísla - int # celá čísla - int
a = 5 a = 5
print(a, "je typ", type(a)) print(a, "je typ", type(a))
``` ```
%% Output %% Output
5 je typ <class 'int'> 5 je typ <class 'int'>
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# reálná čísla - float # reálná čísla - float
a = 2.0 a = 2.0
print(a, "je typ", type(a)) print(a, "je typ", type(a))
``` ```
%% Output %% Output
2.0 je typ <class 'float'> 2.0 je typ <class 'float'>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Strukturované datové typy ## Strukturované datové typy
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# seznam hodnot stejných nebo různých datových typů - list # seznam hodnot stejných nebo různých datových typů - list
a = [1, 2.2, 'python'] a = [1, 2.2, 'python']
print(a, "je typ", type(a)) print(a, "je typ", type(a))
print(a[1]) print(a[1])
``` ```
%% Output %% Output
[1, 2.2, 'python'] je typ <class 'list'> [1, 2.2, 'python'] je typ <class 'list'>
2.2 2.2
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# uspořádaná n-tice - tuple # uspořádaná n-tice - tuple
a = (10 / 3, 10 // 3) a = (10 / 3, 10 // 3)
print(a, "je typ", type(a)) print(a, "je typ", type(a))
``` ```
%% Output %% Output
(3.3333333333333335, 3) je typ <class 'tuple'> (3.3333333333333335, 3) je typ <class 'tuple'>
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# slovník - dict # slovník - dict
a = {"brand": "Ford", "model": "Mustang", "year": 1964} a = {"brand": "Ford", "model": "Mustang", "year": 1964}
print(a, "je typ", type(a)) print(a, "je typ", type(a))
``` ```
%% Output %% Output
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964} je typ <class 'dict'> {'brand': 'Ford', 'model': 'Mustang', 'year': 1964} je typ <class 'dict'>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Výrazy a operátory ## Výrazy a operátory
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
x = 13 x = 13
y = x % 4 # modulo y = x % 4 # modulo
y = y + 1 y = y + 1
y += 1 y += 1
a = (x==3) and (y==2) a = (x==3) and (y==2)
b = a or not a b = a or not a
s = "petr" s = "petr"
t = "klic" t = "klic"
u = s + t u = s + t
u *= 2 u *= 2
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Vícenásobná přiřazení ## Vícenásobná přiřazení
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
x, y, z = "Orange", "Banana", "Cherry" x, y, z = "Orange", "Banana", "Cherry"
x = y = z = "Orange" x = y = z = "Orange"
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Explicitní konverze ## Explicitní konverze
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
x = int(1) # x -> 1 x = int(1) # x -> 1
y = int(2.8) # y -> 2 y = int(2.8) # y -> 2
z = int("3") # z -> 3 z = int("3") # z -> 3
x = float(1) # x -> 1.0 x = float(1) # x -> 1.0
y = float(2.8) # y -> 2.8 y = float(2.8) # y -> 2.8
z = float("3") # z -> 3.0 z = float("3") # z -> 3.0
w = float("4.2") # w -> 4.2 w = float("4.2") # w -> 4.2
x = str("s1") # x -> 's1' x = str("s1") # x -> 's1'
y = str(2) # y -> '2' y = str(2) # y -> '2'
z = str(3.0) # z -> '3.0' z = str(3.0) # z -> '3.0'
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Standardní výstup ## Standardní výstup
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# výpis hodnoty proměnné s automatickým odřádkováním # výpis hodnoty proměnné s automatickým odřádkováním
x = 10 x = 10
print(x) print(x)
``` ```
%% Output %% Output
10 10
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# výpis s doprovodnou informací # výpis s doprovodnou informací
print('x = ', x) print('x = ', x)
``` ```
%% Output %% Output
x = 10 x = 10
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# formátováni pomocí modifikatorů, Python 2.x # formátováni pomocí modifikatorů, Python 2.x
print('x = %d' % x) print('x = %d' % x)
``` ```
%% Output %% Output
x = 10 x = 10
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# f-funkce, Python 3.6 a novejsi # f-funkce, Python 3.6 a novejsi
print(f'x = {x}') print(f'x = {x}')
``` ```
%% Output %% Output
x = 10 x = 10
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Výpis více proměnných ### Výpis více proměnných
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
x = 10; y = 11; z = 12 x = 10; y = 11; z = 12
print(x, y, z) # hodnoty automaticky oddeleny ' ' print(x, y, z) # hodnoty automaticky oddeleny ' '
print(x, y, z, sep=';') # potlaceni separatoru '' print(x, y, z, sep=';') # potlaceni separatoru ''
print(x, y, z, sep='\n') # odradkovani separatorem print(x, y, z, sep='\n') # odradkovani separatorem
print('x={}, y={}'.format(x,y)) print('x={}, y={}'.format(x,y))
print('x={1}, y={0}'.format(x,z)) print('x={1}, y={0}'.format(x,z))
``` ```
%% Output %% Output
10 11 12 10 11 12
10;11;12 10;11;12
10 10
11 11
12 12
x=10, y=11 x=10, y=11
x=12, y=10 x=12, y=10
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# kombinace pozicniho argumentu a klicoveho slova # kombinace pozicniho argumentu a klicoveho slova
print('Na hristi jsou {0}, {1}, a {other}.' print('Na hristi jsou {0}, {1}, a {other}.'
.format('Cesi', 'Slovaci', other ='rozhodci')) .format('Cesi', 'Slovaci', other ='rozhodci'))
``` ```
%% Output %% Output
Na hristi jsou Cesi, Slovaci, a rozhodci. Na hristi jsou Cesi, Slovaci, a rozhodci.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# formatovani cisel # formatovani cisel
print('A :{0:2d}, B :{1:8.2f}' print('A :{0:2d}, B :{1:8.2f}'
.format(12, 00.546)) .format(12, 00.546))
``` ```
%% Output %% Output
A :12, B : 0.55 A :12, B : 0.55
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# zmena pozicniho argumentu # zmena pozicniho argumentu
print('B: {1:3d}, A: {0:7.2f}'.format(47.42, 11)) print('B: {1:3d}, A: {0:7.2f}'.format(47.42, 11))
print('A: {a:5d}, B: {p:8.2f}' print('A: {a:5d}, B: {p:8.2f}'
.format(a = 453, p = 59.058)) .format(a = 453, p = 59.058))
``` ```
%% Output %% Output
B: 11, A: 47.42 B: 11, A: 47.42
A: 453, B: 59.06 A: 453, B: 59.06
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Podmíněný příkaz ## Podmíněný příkaz
if *condition*: if *condition*:
*statement* *statement*
*statement* *statement*
... ...
elif *condition*: # 0 or more elif clauses elif *condition*: # 0 or more elif clauses
*statement* *statement*
*statement* *statement*
... ...
else: # optional else: # optional
*statement* *statement*
*statement* *statement*
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dnes = "Pondeli" dnes = "Pondeli"
kopr = "N" kopr = "N"
prednaska = "Pondeli" prednaska = "Pondeli"
if dnes == "Nedele": if dnes == "Nedele":
if kopr == "Y": if kopr == "Y":
print("Jdi na prochazku") print("Jdi na prochazku")
else: else:
print("Zacni pripravovat prednasku na NSI!") print("Zacni pripravovat prednasku na NSI!")
elif dnes == "Ctvrtek": elif dnes == "Ctvrtek":
print("Cviko z NSI!") print("Cviko z NSI!")
elif dnes == prednaska: elif dnes == prednaska:
print("Jdi na prednasku!") print("Jdi na prednasku!")
elif dnes == "Utery" or dnes == "Streda" or dnes == "Patek" or \ elif dnes == "Utery" or dnes == "Streda" or dnes == "Patek" or \
dnes == "Sobota": dnes == "Sobota":
print("Nemas NSI") print("Nemas NSI")
else: else:
print("Co ze je za den?!") print("Co ze je za den?!")
``` ```
%% Output %% Output
Jdi na prednasku! Jdi na prednasku!
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Cyklus ## Cyklus
### while ### while
while *condition*: while *condition*:
*statement* *statement*
*statement* *statement*
Příklad: kolikrát se 0 objeví v celém čísle? Příklad: kolikrát se 0 objeví v celém čísle?
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
num = 2**100 num = 2**100
print(num) print(num)
``` ```
%% Output %% Output
1267650600228229401496703205376 1267650600228229401496703205376
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
count = 0 count = 0
while num > 0: #what if we changed to >=0? while num > 0: #what if we changed to >=0?
if num % 10 == 0: if num % 10 == 0:
count = count + 1 count = count + 1
num = num // 10 num = num // 10
print(count) print(count)
``` ```
%% Output %% Output
6 6
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## For ## For
for *variable* in *iterable*: for *variable* in *iterable*:
*statement* *statement*
*statement* *statement*
Příklad: ten samý problém, ale s využitím datového typu **str** Příklad: ten samý problém, ale s využitím datového typu **str**
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
num = 2**100 num = 2**100
count = 0 count = 0
for digit in str(num): for digit in str(num):
#print(digit, type(digit)) #print(digit, type(digit))
if digit == "0": if digit == "0":
count = count + 1 count = count + 1
print(count) print(count)
``` ```
%% Output %% Output
6 6
......