Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Programování 1 pro matematiky
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Mareš
Programování 1 pro matematiky
Commits
1ab11651
Commit
1ab11651
authored
5 years ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
Knihovna: WIP
parent
42a0d950
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
12-knihovna/12-knihovna.tex
+240
-0
240 additions, 0 deletions
12-knihovna/12-knihovna.tex
12-knihovna/Makefile
+3
-0
3 additions, 0 deletions
12-knihovna/Makefile
slidemac.tex
+2
-0
2 additions, 0 deletions
slidemac.tex
with
245 additions
and
0 deletions
12-knihovna/12-knihovna.tex
0 → 100644
+
240
−
0
View file @
1ab11651
\documentclass
{
beamer
}
\usepackage
[utf8]
{
inputenc
}
\usepackage
[czech]
{
babel
}
\usepackage
{
palatino
}
\usepackage
{
verbatim
}
\usetheme
{
Warsaw
}
\title
{
Programování 1: Standardní knihovna
}
\author
[Martin Mareš]
{
Martin Mareš
\\\texttt
{
mj@ucw.cz
}}
\institute
{
Katedra Aplikované Matematiky
\\
MFF UK Praha
}
\date
{
2019
}
\begin{document}
\setbeamertemplate
{
navigation symbols
}{}
\setbeamertemplate
{
footline
}{}
\setbeamerfont
{
title page
}{
family=
\rmfamily
}
\shorthandoff
{
"
}
\begin{frame}
\titlepage
\end{frame}
\input
../slidemac.tex
% ----------------------------------------------------------------------
\begin{frame}
{
Moduly a importování
}
Programy mohou být členěné na
{
\bf
moduly
}
.
\medskip
Provedeme-li:
\medskip
\py
{
import math
}{}
načte se soubor
{
\tt
math.py
}
(hledá se v~aktuálním adresáři a pak v~knihovnách).
Potom píšeme:
\medskip
\py
{
math.sin(1)
}{}
Formálně:
{
\bf
math
}
je objekt, jako jeho atributy vidíme funkce a~proměnné
definované uvnitř modulu.
\medskip
Alternativně:
\medskip
\py
{
from math import sin
\\
from sys import *
}{}
navíc zkopíruje do aktuálního modulu definice z~importovaného modulu
(takže pak
{
\bf
sin
}
znamená totéž co
{
\bf
math.sin
}
apod.).
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Už jsme potkali
\dots
}
\begin{itemize}
\item
{
\bf
math
}
-- matematické funkce a konstanty
\medskip
\item
{
\bf
operator
}
-- funkční podoba operátorů (třeba
{
\bf
add
}
pro
$
+
$
)
\medskip
\item
{
\bf
collections
}
-- typ
{
\bf
defaultdict
}
\begin{itemize}
\item
také tu je
{
\bf
deque
}
(double-ended queue) -- seznam s~rychlým přidáváním a~odebíráním
na obou koncích
\end{itemize}
\end{itemize}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Binární řetězce
}
Typ
{
\bf
bytes
}
obsahuje neměnnou posloupnost bajtů (8-bitových hodnot).
~
Literály:
{
\tt
b'Brum'
}
,
{
\tt
b'
\bs
{}
x62
\bs
{}
x72
\bs
{}
x75
\bs
{}
x6d'
}
~
\py
{
%
"číslo".encode('utf-8')
}{
%
b'
\bs
{}
xc4
\bs
{}
x8d
\bs
{}
xc3
\bs
{}
xadslo'
}
\py
{
%
b'
\bs
{}
xc4
\bs
{}
x8d
\bs
{}
xc3
\bs
{}
xadslo'.decode('utf-8')
}{
%
'číslo'
}
\py
{
%
bytes([1, 2, 3])
}{
%
b'
\bs
{}
x01
\bs
{}
x02
\bs
{}
x03'
}
Při práci s~binárními soubory čteme/zapisujeme bytes.
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Binární seznamy
}
Typ
{
\bf
bytearray:
}
\medskip
\begin{itemize}
\item
jako
{
\tt
bytes
}
, ale lze modifikovat
\item
seznam bajtů (prostorově efektivnější než běžný seznam)
\item
{
\tt
bytearray(n)
}
-- seznam
$
n
$
nul
\item
{
\tt
bytearray([1,2,3])
}
\item
{
\tt
bytearray.decode('řetězec', 'utf-8')
}
\end{itemize}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Homogenní pole
}
\py
{
%
import array
\\
a = array.array('i', [1, 2])
\\
a.itemsize
}{
%
4
}
\medskip
{
\bf
'i'
}
je kód typu položek, například:
\medskip
\begin{itemize}
\item
{
\bf
i
}
-- integer: aspoň 32-bitové číslo se znaménkem
\item
{
\bf
I
}
-- totéž bez znaménka
\item
{
\bf
b
}
-- bajt se znaménkem
\item
{
\bf
q
}
-- alespoň 64-bitové číslo se znaménkem
\item
{
\bf
f
}
-- float: aspoň 32-bitové desetinné číslo
\item
{
\bf
d
}
-- double: aspoň 64-bitové desetinné číslo
\end{itemize}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Zlomky
}
\py
{
%
from fractions import Fraction
\\
Fraction(1, 2) + Fraction(1, 3)
}{
%
Fraction(5, 6)
}
\py
{
%
Fraction(1/3)
}{
%
Fraction(6004799503160661, 18014398509481984)
}
\py
{
%
Fraction(1/3).limit
\_
denominator(100000)
}{
%
Fraction(1, 3)
}
\py
{
%
Fraction("1/3")
}{
%
Fraction(1, 3)
}
\py
{
%
print(Fraction(1, 3))
}{
%
'1/3'
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Pseudonáhodný generátor
}
\py
{
%
import random
\\
random.random()
}{
%
0.28947857702914326
\cmt
{
(z~intervalu
$
[
0
,
1
]
$
)
}
}
\py
{
%
random.uniform(0, 1000)
}{
%
50.64122748168531
\cmt
{
(z~intervalu
$
[
a,b
]
$
)
}
}
\py
{
%
random.randrange(0, 1000)
}{
%
524
\cmt
{
(celé číslo od
$
a
$
do~
$
b
-
1
$
)
}
}
\py
{
%
random.randrange(1000)
}{
%
451
\cmt
{
(stejně jako u~range jde dolní mez vynechat)
}
}
\py
{
%
random.seed(12345)
\\
random.random()
}{
%
0.41661987254534116
\cmt
{
(vyjde vždy stejně)
}
}
\end{frame}
% ----------------------------------------------------------------------
\end{document}
This diff is collapsed.
Click to expand it.
12-knihovna/Makefile
0 → 100644
+
3
−
0
View file @
1ab11651
SLIDES
=
12-knihovna.pdf
include
../Makerules
This diff is collapsed.
Click to expand it.
slidemac.tex
+
2
−
0
View file @
1ab11651
...
...
@@ -15,3 +15,5 @@
}
\def\cmt
#1
{
\quad\hbox
{
\color
{
black
}
\rm
#1
}}
\def\bs
{
\char
92
\relax
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment