some cleanup, added exercises and tree gif
This commit is contained in:
parent
0f96156f97
commit
f0795fcf99
Binary file not shown.
After Width: | Height: | Size: 22 MiB |
|
@ -127,18 +127,37 @@ The most basic printing can be done by calling the `print` function.
|
||||||
In python a call is symbolized by the `()`.
|
In python a call is symbolized by the `()`.
|
||||||
In practice this becomes as follows.
|
In practice this becomes as follows.
|
||||||
|
|
||||||
```python3
|
```python
|
||||||
print("hello world")
|
print("hello world")
|
||||||
print("my name is Wouter")
|
print("my name is Wouter")
|
||||||
print("I'm", 35, "years old")
|
print("I'm", 35, "years old")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`print` is a **built-in** function.
|
||||||
|
We can *prove* this in a shell, plus we can read it's [docstring](https://docs.python.org/3/glossary.html#term-docstring).
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> print
|
||||||
|
<built-in function print>
|
||||||
|
>>> print(print.__doc__)
|
||||||
|
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
|
||||||
|
|
||||||
|
Prints the values to a stream, or to sys.stdout by default.
|
||||||
|
Optional keyword arguments:
|
||||||
|
file: a file-like object (stream); defaults to the current sys.stdout.
|
||||||
|
sep: string inserted between values, default a space.
|
||||||
|
end: string appended after the last value, default a newline.
|
||||||
|
flush: whether to forcibly flush the stream.
|
||||||
|
>>>
|
||||||
|
```
|
||||||
|
|
||||||
🏃 Try it
|
🏃 Try it
|
||||||
---
|
---
|
||||||
|
|
||||||
Try printing different lines and with combinations of different object types such as `int`, `float` and `str`.
|
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?
|
What happens if you *add* (`+`) values to one another?
|
||||||
What about subtracting?
|
What about subtracting?
|
||||||
|
And multiplication and division?
|
||||||
|
|
||||||
We can also print the objects referenced by variables.
|
We can also print the objects referenced by variables.
|
||||||
A simple example:
|
A simple example:
|
||||||
|
@ -150,7 +169,7 @@ age = "35"
|
||||||
print("Hello, my name is", name, "and I'm", age, "years old.")
|
print("Hello, my name is", name, "and I'm", age, "years old.")
|
||||||
```
|
```
|
||||||
|
|
||||||
While it works perfectly well it's not super *readable*.
|
While it works perfectly fine it's not super *readable*.
|
||||||
We can improve the readability by using either string replacement or string formatting.
|
We can improve the readability by using either string replacement or string formatting.
|
||||||
My personal preference is string formatting.
|
My personal preference is string formatting.
|
||||||
|
|
||||||
|
@ -187,7 +206,7 @@ print("Hello, my name is {} and I'm {} years old.".format(name, age))
|
||||||
|
|
||||||
The first **built-in function** we saw is `print` which can be used to signal messages to the user.
|
The first **built-in 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?
|
But how can we **get** some information from the user?
|
||||||
This is done with the `input` function.
|
This is done with the built-in `input` function.
|
||||||
If we open up a python shell we can observe it's behaviour.
|
If we open up a python shell we can observe it's behaviour.
|
||||||
|
|
||||||
```python3
|
```python3
|
||||||
|
@ -203,6 +222,23 @@ The [prompt](https://en.wikipedia.org/wiki/Command-line_interface#Command_prompt
|
||||||
This is one of the slight nuances between running scripts and using the shell.
|
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.
|
The shell is more *verbose* and will explicitly tell you what a function returns, unless it doesn't return anything.
|
||||||
|
|
||||||
|
`input` is, like `print`, a **built-in** function.
|
||||||
|
We can *prove* this in a shell, plus we can read it's [docstring](https://docs.python.org/3/glossary.html#term-docstring) just as with `print`.
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> input
|
||||||
|
<built-in function input>
|
||||||
|
>>> print(input.__doc__)
|
||||||
|
Read a string from standard input. The trailing newline is stripped.
|
||||||
|
|
||||||
|
The prompt string, if given, is printed to standard output without a
|
||||||
|
trailing newline before reading input.
|
||||||
|
|
||||||
|
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
|
||||||
|
On *nix systems, readline is used if available.
|
||||||
|
>>>
|
||||||
|
```
|
||||||
|
|
||||||
## Some functions are blocking
|
## Some functions are blocking
|
||||||
|
|
||||||
When we call `print` the function is executed immediately and the python interpreter continues with the next line.
|
When we call `print` the function is executed immediately and the python interpreter continues with the next line.
|
||||||
|
@ -235,6 +271,8 @@ Can you tell me why I did that?**
|
||||||
|
|
||||||
Try playing around with the `input` function and incorporate the different ways to print with it.
|
Try playing around with the `input` function and incorporate the different ways to print with it.
|
||||||
Ask multiple questions and combine the answers to print a sensible message on one line.
|
Ask multiple questions and combine the answers to print a sensible message on one line.
|
||||||
|
Can you create a simple calculator?
|
||||||
|
If not, can you explain me *why*?
|
||||||
|
|
||||||
## Functions can take arguments
|
## Functions can take arguments
|
||||||
|
|
||||||
|
@ -380,6 +418,12 @@ True
|
||||||
|
|
||||||
⛑ **Remember CTRL-q opens the documentation in Pycharm and don't forget to actually use it!**
|
⛑ **Remember CTRL-q opens the documentation in Pycharm and don't forget to actually use it!**
|
||||||
|
|
||||||
|
🏃 Try it
|
||||||
|
---
|
||||||
|
|
||||||
|
Can you code ma little calculator *now*?
|
||||||
|
What about one that takes floating point numbers?
|
||||||
|
|
||||||
## Some links to read up on
|
## Some links to read up on
|
||||||
|
|
||||||
* [realpython](https://realpython.com/python-conditional-statements/) conditional logic
|
* [realpython](https://realpython.com/python-conditional-statements/) conditional logic
|
||||||
|
@ -409,6 +453,7 @@ I can't understand you...
|
||||||
|
|
||||||
If you want to make the program a bit more complex, try adding the reverse as in Fahrenheit to Celsius.
|
If you want to make the program a bit more complex, try adding the reverse as in Fahrenheit to Celsius.
|
||||||
Your first question to the user could then be *in which direction do you want to convert?*.
|
Your first question to the user could then be *in which direction do you want to convert?*.
|
||||||
|
If you want to add an other feature, I suggest you try to make the converter work for `int` and for `float` objects.
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Spoiler warning</summary>
|
<summary>Spoiler warning</summary>
|
||||||
|
@ -471,6 +516,10 @@ else:
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
|
||||||
|
## Some links to read up on
|
||||||
|
|
||||||
|
* how to to [str.isfloat()](https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python)?
|
||||||
|
|
||||||
# A text based adventure game
|
# A text based adventure game
|
||||||
|
|
||||||
We can use conditional logic to create quite elaborate decision processes.
|
We can use conditional logic to create quite elaborate decision processes.
|
||||||
|
@ -532,6 +581,7 @@ else:
|
||||||
|
|
||||||
I urge you to read up on some [best practices](https://www.tutorialdocs.com/article/python-conditional-statements-tips.html) for `if` statements.
|
I urge you to read up on some [best practices](https://www.tutorialdocs.com/article/python-conditional-statements-tips.html) for `if` statements.
|
||||||
We will not improve on this particular example but I do advise you to create a similar style game during one of the workshops once we have learned some new tricks.
|
We will not improve on this particular example but I do advise you to create a similar style game during one of the workshops once we have learned some new tricks.
|
||||||
|
If you need some inspiration you could have a look [here](https://github.com/vamshi-krishna-prime/python-text-game).
|
||||||
|
|
||||||
# Creating your own functions
|
# Creating your own functions
|
||||||
|
|
||||||
|
@ -553,6 +603,8 @@ def first_function():
|
||||||
print("Hear me roar!")
|
print("Hear me roar!")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
⛑ **Do not fight the indentation, it's part of the syntax of python!**
|
||||||
|
|
||||||
If you type *only* the code above in a new script, and run it, you won't see much.
|
If you type *only* the code above in a new script, and run it, you won't see much.
|
||||||
This is because you only **created** the function.
|
This is because you only **created** the function.
|
||||||
To use it you need to **call** it.
|
To use it you need to **call** it.
|
||||||
|
@ -574,6 +626,8 @@ For now you should not overthink the structure of you scripts.
|
||||||
As long as they work you should be happy.
|
As long as they work you should be happy.
|
||||||
We'll dive into the proper *anatomy* of a program real soon.
|
We'll dive into the proper *anatomy* of a program real soon.
|
||||||
|
|
||||||
|
⛑ **Remember you can't use functions before defining them! Learn to read the error messages. Also, if it does not autocomplete, it *probably* won't work...**
|
||||||
|
|
||||||
## Functions that *do* something
|
## Functions that *do* something
|
||||||
|
|
||||||
The first function I showed you above performs a series of actions each time it is called.
|
The first function I showed you above performs a series of actions each time it is called.
|
||||||
|
@ -1157,7 +1211,7 @@ print("after the loop, counter: {}".format(counter))
|
||||||
```
|
```
|
||||||
|
|
||||||
The `while True` condition is *always* `True` so the loop will **never** exit!
|
The `while True` condition is *always* `True` so the loop will **never** exit!
|
||||||
This is what we call an infinite loop.
|
This is what we call an **infinite loop**.
|
||||||
The `break` keyword was added to the language so we can *break out* of a loop.
|
The `break` keyword was added to the language so we can *break out* of a loop.
|
||||||
The logic is as follows.
|
The logic is as follows.
|
||||||
|
|
||||||
|
@ -1510,6 +1564,52 @@ The third letter by ROT2 so will become `n`.
|
||||||
Now, the fourth letter will be offset again by `a` so ROT0 and will become `l`.
|
Now, the fourth letter will be offset again by `a` so ROT0 and will become `l`.
|
||||||
And so on...
|
And so on...
|
||||||
|
|
||||||
|
# Coding challenge - Christmas Tree
|
||||||
|
|
||||||
|
Can you make me a program that draws various size Christmas trees?
|
||||||
|
Like the code block below.
|
||||||
|
|
||||||
|
```
|
||||||
|
#
|
||||||
|
###
|
||||||
|
#####
|
||||||
|
#######
|
||||||
|
#########
|
||||||
|
###########
|
||||||
|
#############
|
||||||
|
###############
|
||||||
|
#################
|
||||||
|
###
|
||||||
|
###
|
||||||
|
|
||||||
|
#
|
||||||
|
###
|
||||||
|
#####
|
||||||
|
#######
|
||||||
|
###
|
||||||
|
###
|
||||||
|
```
|
||||||
|
|
||||||
|
Can you add in some balls as below?
|
||||||
|
|
||||||
|
```
|
||||||
|
#
|
||||||
|
##°
|
||||||
|
#°###
|
||||||
|
#°###°#
|
||||||
|
###°#####
|
||||||
|
°##°##°#°##
|
||||||
|
#°###°°°#####
|
||||||
|
#°############°
|
||||||
|
#####°###°#°####°
|
||||||
|
###
|
||||||
|
###
|
||||||
|
```
|
||||||
|
|
||||||
|
What about this *super funky* gif...
|
||||||
|
|
||||||
|
![tree](./assets/tree.gif)
|
||||||
|
|
||||||
# List comprehension
|
# List comprehension
|
||||||
|
|
||||||
This is a bit of an advanced topic but I'm putting it here to show you a very unique and powerful feature of python.
|
This is a bit of an advanced topic but I'm putting it here to show you a very unique and powerful feature of python.
|
||||||
|
|
Loading…
Reference in New Issue