starts completing doc and adds gitignore
This commit is contained in:
parent
33163ad17b
commit
8b24ba9577
|
@ -0,0 +1,3 @@
|
||||||
|
*.pdf
|
||||||
|
*.docx
|
||||||
|
.~lock*
|
|
@ -16,8 +16,12 @@ Below is a small overview of how I would subdivide them.
|
||||||
|
|
||||||
The subtopics behind the concept of programming can be sliced (in no particular order) as follows:
|
The subtopics behind the concept of programming can be sliced (in no particular order) as follows:
|
||||||
|
|
||||||
* objects
|
* objects or **OOP** (Object Orientated Programming)
|
||||||
* variables
|
* abstraction
|
||||||
|
* encapsulation
|
||||||
|
* inheritance
|
||||||
|
* polymorphism
|
||||||
|
* variables (which are **not** *boxes* in python3)
|
||||||
* conditional logic
|
* conditional logic
|
||||||
* functions
|
* functions
|
||||||
* loops
|
* loops
|
||||||
|
@ -71,7 +75,33 @@ TODO
|
||||||
|
|
||||||
# Your first project
|
# Your first project
|
||||||
|
|
||||||
TODO helloworld
|
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!' );
|
||||||
|
```
|
||||||
|
|
||||||
## How to execute
|
## How to execute
|
||||||
|
|
||||||
|
@ -80,16 +110,96 @@ TODO helloworld
|
||||||
|
|
||||||
## Simple printing
|
## Simple printing
|
||||||
|
|
||||||
## String formatting
|
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.
|
||||||
|
|
||||||
|
|
||||||
## String replacement
|
## 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/)
|
||||||
|
|
||||||
# Taking input
|
# Taking input
|
||||||
|
|
||||||
TODO say hello program
|
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.
|
||||||
|
|
||||||
## Functions can return something
|
## 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
|
||||||
|
|
||||||
# Taking input and evaluation
|
# Taking input and evaluation
|
||||||
|
|
||||||
TODO say hello plus ask for age
|
TODO say hello plus ask for age
|
||||||
|
|
Loading…
Reference in New Issue