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