linux_course_doc/modules/qualifying/learning_python3.md

77 lines
4.9 KiB
Markdown
Raw Normal View History

2021-05-19 09:08:32 +02:00
# Learning Python3
## 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
* variables
* conditional logic
* functions
* loops
We've seen *four* of these topics before when we learned Bash scripting.
The concept behind these topics is the same in both Bash and Python3, it's just *how* you write them that is different.
This *how* is part of the **syntax** of the language.
**Objects** aren't implemented in Bash as it's not an OOP language but there are [ways](https://hipersayanx.blogspot.com/2012/12/object-oriented-programming-in-bash.html) of doing object orientated programming in Bash.
This is out of the scope of what we'll learn but I'm including the information to stress that OOP is more of a [concept](https://www.tutorialspoint.com/What-are-basic-Object-oriented-programming-concepts) than a feature of a particular 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)).
Throughout this course you have already learned **two** different syntaxes.
The first one you learned was **markdown** which you [master](https://www.markdownguide.org/basic-syntax) by now!
The second one was Bash which, in my [opinion](https://forums.freebsd.org/threads/sh-shell-scripts-have-an-ugly-syntax.76635/), is a pretty *ugly* syntax but very useful for quick and dirty scripts that modify a Linux system.
A nice [cheatsheet](https://devhints.io/bash) outlines quite a bit of the Bash syntax.
Python3 syntax shares similarities with Bash but is a lot more modern and designed to be human readable.
A shared syntax similarity for example is the use of `# I'm a comment` to add comments to your code.
A big difference is the use of *indented* blocks which are **mandatory** in Python3 and **optional** in Bash.
I find [this](https://medium.com/capital-one-tech/bashing-the-bash-replacing-shell-scripts-with-python-d8d201bc0989) blogpost a very good analysis of how to think Bash versus Python3.
### Tools
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?
This is one of the many **features** of an [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) and makes our life so much nicer when writing code.
We'll come back to these features in a bit.
In order to **run** Python3 code you need the Python3 interpreter.
Just as with Bash, in order to run a Bash script you need to have Bash installed.
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
Our text editor of choice, `vim-nox` comes with some of these features and we can add more with plugins so vim can [become](https://dev.to/shahinsha/how-to-make-vim-a-python-ide-best-ide-for-python-23e1) an IDE if we want it to.
To make the overall learning curve a bit less steep we'll start out with a more user friendly IDE, [pycharm](https://www.jetbrains.com/help/pycharm/installation-guide.html).