Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Programming course
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
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
courses
Programming course
Commits
9f6b7fb5
Commit
9f6b7fb5
authored
6 years ago
by
Mihai Lefter
Browse files
Options
Downloads
Patches
Plain Diff
Extend flow control
parent
47f6304b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
introduction/flow_control/flow_control.tex
+99
-4
99 additions, 4 deletions
introduction/flow_control/flow_control.tex
with
99 additions
and
4 deletions
introduction/flow_control/flow_control.tex
+
99
−
4
View file @
9f6b7fb5
...
...
@@ -289,14 +289,109 @@ e \end{lstlisting}}
\section
{
Functions
}
\subsection
{
Defining a function
}
\begin{pframe}
A function is a named sequence of statements that performs some piece of work.
Later on that function can be called by using its name.
\medskip
A function definition includes its name, arguments and body.
\begin{pythonfile}
{
functions.py
}
\begin{minted}
[linenos]
{
python
}
def add
_
two(number):
return number + 2
for i in range(5):
print add
_
two(i)
\end{minted}
\end{pythonfile}
\end{pframe}
\subsection
{
Keyword arguments
}
\begin{pframe}
Besides regular arguments, functions can have keyword arguments.
\begin{pythonfile}
{
functions
\_
keywords.py
}
\begin{minted}
[linenos]
{
python
}
def add
_
some
_
other
_
number(number, other
_
number=12):
return number + other
_
number
add
_
some
_
other
_
number(2, 6)
add
_
some
_
other
_
number(3, other
_
number=4)
add
_
some
_
other
_
number(5)
\end{minted}
\end{pythonfile}
\end{pframe}
\subsection
{
Functions are values
}
\begin{pframe}
We can pass functions around just like other values, and call them.
\begin{pythonfile}
{
function
\_
values.py
}
\begin{minted}
[linenos]
{
python
}
functions = [add
_
two, add
_
some
_
other
_
number]
for function in functions:
print function(7)
# Simple anonymous functions can be created with lambda.
functions.append(lambda x: x * 7)
for function in functions:
print function(4)
\end{minted}
\end{pythonfile}
\end{pframe}
\subsection
{
Docstrings
}
\begin{pframe}
Like many other definitions, functions can have docstrings.
\begin{itemize}
\item
Docstrings are regular string values which you start the definition
body with.
\item
You can access an object's docstring using help.
\end{itemize}
\begin{ipython}
\begin{pythonin}
{
python
}
help(map)
Help on class map in module builtins:
class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.
\end{pythonin}
\\
\begin{pythonin}
{
python
}
list(map(add
_
two, [1, 2, 3, 4]))
\end{pythonin}
\\
\begin{pythonout}
{
python
}
[3, 4, 5, 6]
\end{pythonout}
\end{ipython}
\end{pframe}
\section
{
Hands on!
}
\begin{pframe}
\vspace
{
-0.5cm
}
\begin{enumerate}
\item
\begin{enumerate}
[a]
\item
\end{enumerate}
\item
Write a Python function that returns the maximum of two numbers.
\item
Write a Python function that returns the maximum of three numbers.
Try to reuse the first maximum of two numbers function.
\item
Write a Python function that accepts a string as parameter.
Next, it calculates and prints the number of upper case letters and lower
case letters. Make us of the
\mintinline
{
python
}{
isupper
}
and
\mintinline
{
python
}{
islower
}
built in methods.
\end{enumerate}
\end{pframe}
...
...
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