python_introduction/learning_python3.md

343 lines
8.8 KiB
Markdown
Raw Normal View History

2021-10-25 09:24:01 +02:00
# What we'll learn
You'll learn **three** things at the same time so don't get discouraged if it feels a bit much at the start.
Everybody's issues will be in these three different domains and at the beginning it can be difficult to differentiate between them.
Keep this in mind, everybody has to go through this stage and the *click* comes at different times for different people but everybody clicks at some point!
The three new things you'll learn:
1. the **concepts** of programming, most notably Object Orientated Programming (OOP)
2. the **syntax** of one particular language, in our case Python3
3. the **tools** needed to start programming in our language of choice
Within each of these topics there are *subtopics* but there are not bottomless!
Below is a small overview of how I would subdivide them.
## Concepts
The subtopics behind the concept of programming can be sliced (in no particular order) as follows:
* objects or **OOP** (Object Orientated Programming)
* abstraction
* encapsulation
* inheritance
* polymorphism
* variables (which are **not** *boxes* in python3)
2021-10-25 09:24:01 +02:00
* conditional logic
* functions
* loops
The concept behind these topics are the same in most languages, it's just *how* you write them that is different.
This *how* is part of the **syntax** of the language.
## Syntax
> In computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions in that language.
> This applies both to programming languages, where the document represents source code, and to markup languages, where the document represents data.
> The syntax of a language defines its surface form.[1] Text-based computer languages are based on sequences of characters,
The quote above is taken shamelessly from [wikipedia](https://en.wikipedia.org/wiki/Syntax_(programming_languages)).
## Tools
### Writing code
Scripts are text files, plain and simple.
So in order to **write** a Python3 script all we need is a text editor.
Nano, vim, notepad++ all do a good job of editing plain text files but some make it *easier* than others.
You've noticed that vim colors the code of a shell script no?
One of the many features of an [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) is *syntax highlighting*.
It colors things such as [keywords](https://realpython.com/python-keywords/) which makes our life so much nicer when writing code.
We'll come back to these features in a bit.
### Running code
In order to **run** Python3 code you need the Python3 interpreter.
This is because when you **execute** your script, the interpreter will **read and execute** each line of the text file **line by line**.
Most people who want to write and run Python3 code, or any language for that matter, will install an Integrated Development Environment to do so.
There are no *rules* as to what has to be included for a program to qualify as an IDE but in my opinion they should include:
* syntax highlighting
* autocomplete
* *goto* commands such as goto definition, goto declaration, goto references
* automatic *pair* opening and closing
* builtin help navigation
There is a plethora of IDE's available and you can't really make a *wrong* choice here, but to make the overall learning curve a bit less steep we'll start out with a user friendly IDE, [pycharm](https://www.jetbrains.com/help/pycharm/installation-guide.html).
# The python3 shell
TODO animated overview of the shell and the world of OOP
# Installing pycharm
TODO
# Your first project
In almost any language you'll find a *helloworld* program.
It serves to illustrate a *very* basic working script or program to showcase the syntax.
In python a helloworld is done as such.
```python3
print("Hello World!")
```
Just for reference below are a few helloworld programs in different languages.
First `c#` then `c` and last but not least `javascript`.
```c#
Console.WriteLine("Hello World!");
```
```c
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
```
```javascript
alert( 'Hello, world!' );
```
2021-10-25 09:24:01 +02:00
2021-10-26 14:31:10 +02:00
## How to execute
* within pycharm
* from the command line
2021-10-25 09:24:01 +02:00
## Simple printing
The most basic printing can be done by calling the `print` function.
In python a call is symbolized by the `()`.
In practice this becomes as follows.
```python3
print("hello world")
print("my name is Wouter")
print("I'm", 35, "years old")
```
__🏃 Try it__
Try printing different lines and with combinations of different object types such as `int`, `float` and `str`.
What happens if you *add* (`+`) values to one another?
We can also print the objects referenced by variables.
A simple example:
```python3
name = "Wouter"
age = "35"
print("Hello, my name is", name, "and I'm", age, "years old.")
```
While it works perfectly well it's not super *readable*.
We can improve the readability by using either string replacement or string formatting.
My personal preference is string formatting.
__🏃 Try it__
Have a look at both ways illustrated below and try them out.
2021-10-25 09:24:01 +02:00
## String replacement
```python3
name = "Wouter"
age = "35"
print("Hello, my name is {name} and I'm {age} years old.")
```
## String formatting
```python3
name = "Wouter"
age = "35"
print("Hello, my name is {} and I'm {} years old.".format(name, age))
```
## Some links to read up
* [realpython string formatting](https://realpython.com/python-string-formatting/)
2021-10-25 09:24:01 +02:00
# Taking input
The first **builtin function** we saw is `print` which can be used to signal messages to the user.
But how can we **get** some information from the user?
This is done with the `input` function.
If we open up a python shell we can observe it's behaviour.
```python3
>>> input()
hello world
'hello world'
>>>
```
It seems to echo back what we type on the empty line.
If we take this idea and add it to a script the behaviour changes slightly.
The [prompt](https://en.wikipedia.org/wiki/Command-line_interface#Command_prompt) appears but when we hit `enter` the text is not printed.
This is one of the slight nuances between running scripts and using the shell.
The shell is more *verbose* and will explicitly tell you what a function returns, unless it doesn't return anything.
2021-10-25 09:24:01 +02:00
## Functions can return something
So, functions can **return** something but how can we *use* the returned objects?
This is where **variables** come in handy.
```python3
print("What is your name? ")
answer = input()
print("Well hello", answer, "!")
```
## Functions can take arguments
2021-10-25 09:24:01 +02:00
# Taking input and evaluation
TODO say hello plus ask for age
2021-10-26 14:31:10 +02:00
* slightly insist on *blocking* nature of `input()`
2021-10-25 09:24:01 +02:00
## Conditional logic
2021-10-26 14:31:10 +02:00
* introduction to `if`, `elif` and `else`
2021-10-25 09:24:01 +02:00
## Class string methods
# Coding challenge - Celsius to Fahrenheit converter
2021-10-26 21:52:08 +02:00
# A text based adventure game
TODO mini text based adventure game to point out the complexity of conditional logic and flow control
2021-10-25 09:24:01 +02:00
# Creating your own functions
2021-10-26 14:31:10 +02:00
## Functions that *do* something
2021-10-26 21:52:08 +02:00
TODO recipe with variable flavor (chocolate, vanilla, ...)
2021-10-25 09:24:01 +02:00
TODO pretty_print
2021-10-26 14:31:10 +02:00
## Variable scope
2021-10-25 09:24:01 +02:00
## Functions that *return* something
2021-10-26 14:31:10 +02:00
TODO basic math functions
# Using the standard library
TODO import random exercise (digital dice)
TODO import datetime exercise (will it be Sunday)
2021-10-26 21:52:08 +02:00
TODO simple ROT13 cryptography with multiple libs
2021-10-26 14:31:10 +02:00
# Coding challenge - Memento Mori calculator
2021-10-25 09:24:01 +02:00
# Writing your first library
2021-10-26 14:31:10 +02:00
TODO import pretty print and digital dice
2021-10-25 09:24:01 +02:00
## What are libraries?
## How do we write libraries?
## What is `__name__ == "__main__"`?
2021-10-26 14:31:10 +02:00
## Anatomy of a program
TODO imports, functions, execution
# While loop
TODO guess the number exercise
# Lists
## Creating lists
## Picking elements
## Slicing lists
# For loop
TODO say hello to my friends exercise
# Coding challenge - Cheerleader chant
TODO nested for loop exercise
# Handling files
## Reading from a file
## Writing to a file
## csv, JSON and yaml
2021-10-26 21:52:08 +02:00
## pickle
2021-10-26 14:31:10 +02:00
# Coding challenge - Login generator
TODO write a login generator as a library with a cli as program
BONUS argparse, save to file, read from file
2021-10-26 21:52:08 +02:00
# Dictionaries as data containers
TODO adapt the login generator to output a dict
2021-10-26 14:31:10 +02:00
# Creating our own classes
## Class examples
TODO simple animal or vehicle exercise
2021-10-26 21:52:08 +02:00
TODO task manager
2021-10-26 14:31:10 +02:00
## Class inheritance
2021-10-26 21:52:08 +02:00
TODO shapes and surfaces
2021-10-26 14:31:10 +02:00
TODO superhero game
## Improve the login generator
TODO convert the login generator to a class
# Infinite programs
* insist on the nature of scripts we did up until now
## Logic breakdown of a simple game
TODO hangman exercise
## Trivial pursuit multiple choice game
TODO [db](https://opentdb.com/api_config.php)
2021-10-26 21:52:08 +02:00
### Introduction to the `requests` library
2021-10-26 14:31:10 +02:00
## Threading
TODO add a countdown timer to the multiple choice game
# GUI programming
## wxpython helloworld
2021-10-25 09:24:01 +02:00
2021-10-26 14:31:10 +02:00
## wxpython guess the number
2021-10-25 09:24:01 +02:00
2021-10-26 21:52:08 +02:00
## MVC design pattern
2021-10-25 09:24:01 +02:00
2021-10-26 14:31:10 +02:00
# Coding challenge - Login generator with GUI
2021-10-26 21:52:08 +02:00
# Coding challenge - Trivial pursuit with GUI
# Introduction to the `logging` library
2021-10-26 14:31:10 +02:00