Quick start with Python
In this section you will write your first Python programs. Yes, we will do it right away, without theory or setting up environment (that will come later).
Our Python Project
This course is project based. We are building a simulation of Bank system and learning Python along the way. Isn't that cool? :) But for real, best learning is project based learning. This section describes what we are building within this course.
Introduction to Python (theory and environment)
This is a theoretical section. You can quickly read trough it or return back to it if you are tired of coding at any point.
Python Basics (variables, operators, input and output)
Before working on a more complex subjects we need to get our basics. Python variables, how to handle operators, strings, inputs and outputs.
Data structures in Python
Programming is all about managing data. To be able to manage data good, we need to know different ways to keep this data stored (data structures).
Code flow in Python
Programming is also abaut managing decisions. To be able to control our program, we need to learn code flow rules.
Functions in Python
Programming is all about reusing. We don't want to code same things over and over again. This is why we need functions.
Object Oriented programming in Python
Finally, it is time for us to make our banking software example. This part actually goes above "Python for Beginners", but still, try it. You might like it.
Debugging in Python
If we are to continue our programming career we will have bugs, lots of them. We need to learn how to find and fix them.
Logic blocks with Python Functions
Within the previous lessons, we made a simple program which checks if the number is negative or not.
- Now, there is a high probability that we will want to use this logic many times in our code.
- With our current approach, we would have to copy paste complete program every-time when we need it.
That wouldn’t make much sense now, would it.
This is why programming languages (including Python) have functions.
- function – a way to take a part of the program, and assign it to some common name ‘function name’.
- Then, when you need this logic, you simply call function name (and it will execute all the lines of code inside of the function).
- parameters – values which are somehow related to the function
- input parameters – values that function can accept.
- In general, functions can be without parameters, or they can accept one or more parameters.
- There is a different story about how many parameters a function should have, but that is more advanced topic.
- return parameter – value that will be returned by the function
- docblock – comment that is describing a function completely
1.4.1. Lets rewrite out previous code and make a function out of it
As we did so far please open https://www.onlinegdb.com/online_python_compiler and code this example.
# 1. We define our function, make a name for it and program the logic def is_integer_negative(x): """ Check if the integer is negative Keyword arguments: x - integer which needs to be checked """ if x < 0: print('The number you have entered is negative') elif x == 0: print('The number you have entered equals zero') else: print('The number you have entered is positive') # 2. We take the values from the console and store them to memory x = int(input("Please enter an integer: ")) # 3. We call our function with the values from memory (which we picked up from the console) is_integer_negative(x)
Once you retype it (don’t copy paste it), it should look like this in your editor.
Homework: There is an error within this screenshot, find it, comment below and earn Codies.
Explanation of the example
- Between lines 4-18 we have created our function.
When we want to make new function, we start a code line with ‘def’ keyword. It is short for a ‘define’. After def we usually write function name, followed by the ().
Inside of the () we can write input parameters. In our case we used (x), which means that function has one input parameter which is saved in memory under a label/variable name ‘x’.
Later when we call this function, line 24, we will use this name and store something in this variable.
- Function has a docblock between lines 5-10.
This part of the code is just a description of the function and it is not doing anything. It will not be executed.
It is used by the other developers, to quickly learn about your function if they use it.
Same goes for the other way, if you are using someones function then you will most likely read docblock (if there is one to read).
- At line 21, we are taking an input from console
This is something that we did multiple times so far.
- At the line 24, we are calling our function which is then executed.
Note: It is important to understand that function can be called multiple times.
So, we define functions once (lines 4-18) but we can call it lots of times (line 24 can be used lots of times, where instead of ‘x’ we can use some other value).
Look at this code example and try to understand will the result be.
# 1. We define our function, make a name for it and program the logic def is_integer_negative(x): """ Check if the integer is negative Keyword arguments: x - integer which needs to be checked """ if x < 0: print('The number you have entered is negative') elif x == 0: print('The number you have entered equals zero') else: print('The number you have entered is positive') # 2. We take the values from the console and store them to memory x = int(input("Please enter an integer: ")) # 3. We call our function with the values from memory (which we picked up from the console) is_integer_negative(x) # 4. Lets call our function few more times is_integer_negative(3) is_integer_negative(-2) is_integer_negative(x+1) is_integer_negative(x+x) is_integer_negative(x-x)
For example, if we enter integer 10, these will be the results:
Why did we get results like this?
- Line 27 – function is calling 3 which is positive
- Line 28 – function is calling -2 which is negative
- Line 29 – function is calling x+1 , which is calculated as 10+1 = 11 and 11 is positive
- Line 30 – function is calling x+x, which is calculated as 10+10 = 20 and 20 is positive
- Line 31 – function is calling x-x, which is calculated as 10-10 = 0 which equals zero
- What will the results be if we enter some other integer, for example -1 ?
- Will function at line 32 ever had any other result than ‘equals zero’ ?
- Can you change the name of this function, but make sure that program is still working?