Python: Tutorial in German for beginners
Related Videos: Learn Python - Full Course for Beginners [Tutorial] (May 2024).
In this Python tutorial, we will show you the most important commands you need to know in Python 3 for programming. It is aimed at beginners who have had little or no experience to date.
Python tutorial: print, sleep & input - the 3 basic commands
These three basic commands can almost always be used in Python. With them you can even easily create the first small programs. If you have not yet installed Python 3 on your computer, you can download it free of charge from our download area.
- The print command occurs in virtually every Python program. With this you can have the program output words, numbers or whole sentences. Always write "print" first (without quotation marks) and then write in brackets what should be output. You can either enter something directly (eg "print ('Test')") or integrate a variable: eg "print ('Hello' + x + '! Nice to meet you!')" In this case, the x is given a name replaced, which is stored in the variable x.
- The variables are very important in Python. You can either specify this directly in the code (eg "x = 3") or inquire with an input command: "x = input (" ... ")" You can, for example, formulate a question between the quotation marks, eg "What is the name of you?". The name is then automatically saved under the variable x.
- If you want the program to wait a short time before executing the next command, you need to include a sleep command in the code. To do this, you must first import the sleep module with the "from time import sleep" command and then write "sleep (1)" in the next line. Incidentally, the value in brackets stands for the seconds to be waited for.
Python tutorial: If-Then functions (conditions) in Python
You need conditions in almost every Python program. There are a few things to consider so that everything works properly.
- Always write "if" first and then the condition. For example, the whole thing could look like this: "if x == 0:" (without outer quotation marks). If the variable x is now exactly 0, the condition applies and the command that is subject to this condition is executed.
- If you want to use several conditions, use "if" for the first condition, but "elif" for all others.
- If none of the conditions apply, the commands that are under "else:" are executed.
- If you want to test whether a variable x has a specific numerical value, you can do so with "==" (without quotation marks). You can also test whether x is larger, smaller, larger-equal or smaller-equal to a specific value. You can do this with ">", "=" or "<=". Conversely, you can also use "! =" To test whether x is not 0, for example.
- However, conditions can not only relate to numbers, they can also work with letters or words. This is done with "is" or "is not". For example, the if-then function "if x is 'hello':" applies if the word "hello" was stored as variable x.
- If you want a command to run when multiple conditions apply or one of several possible conditions applies, you can add "and" or "or" to your code. For example, the condition "if x == 0 or x == 1:" applies if x is exactly 0 or 1.
Python 3: math operators
As with any programming language, you can do different things with Python.
- You can easily add, subtract, multiply and divide in Python with "+", "-", "*" or "/" (without quotation marks).
- If you want to potentiate two numbers (or variables), you can do this with "**".
- With "//" you get the result of a division without the rest. You only get the rest with "%". With the command "divmod (x, y)" the program outputs two values; the result of dividing x and y and the rest.
- With "int (x)" you can convert x into an integer or an integer. The opposite is "float (x)". Here, for example, a 2 becomes a floating point number, i.e. a 2.0.
- With "abs (x)" you get the amount of x. The amount of 2 is 2 and 2 is also 2.
More tips about Python in our CHIP guides
Tip: You can find further programming tutorials on our Python programming language topic page. There we show you, for example, how you can define functions in Python.