From 8b24ba95775d94b66e19ab8c7921e10961377a0c Mon Sep 17 00:00:00 2001 From: waldek Date: Wed, 27 Oct 2021 13:43:29 +0200 Subject: [PATCH] starts completing doc and adds gitignore --- .gitignore | 3 ++ learning_python3.md | 120 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..733a58d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pdf +*.docx +.~lock* diff --git a/learning_python3.md b/learning_python3.md index d78831a..bc00c37 100644 --- a/learning_python3.md +++ b/learning_python3.md @@ -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: -* objects -* variables +* objects or **OOP** (Object Orientated Programming) + * abstraction + * encapsulation + * inheritance + * polymorphism +* variables (which are **not** *boxes* in python3) * conditional logic * functions * loops @@ -71,7 +75,33 @@ TODO # 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 + +int main() { + printf("Hello World!"); + return 0; +} +``` + +```javascript +alert( 'Hello, world!' ); +``` ## How to execute @@ -80,16 +110,96 @@ TODO helloworld ## 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 +```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 -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 +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 TODO say hello plus ask for age