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.
Breaking and bug-fixing in Python
In this lesson we will learn how to break our program.
More importantly, we will learn how to recognize what is broken and how to fix it.
This is a serious matter.
If you continue your goal of becoming a developer you will break programs a lot. You will break them while you learn, while you work as a junior developer, while you work as a senior developer. You will break them in testing environment, in production and in your dreams as well.
Yup, simply put, but let us use a proper terminology here:
- Bug – an error in code
- Debugging – process of finding a bug (there are different approaches here, more on that later)
- Error log – list of the error messages
- Stack trace – Message which is returned by compiler when program is stopped because of error
- Syntax bug – an error which was introduced because you coded something wrong (i.e. you entered IFF instead of IF)
- Semantic bug – and error in logic. Meaning, program is coded correctly, but logic is not good, so the end result is not good as well.
- Syntax bugs are easier to find and fix, because they will stop your program from execution.
- Semantic bugs are harder to find, because everything works but the logic is wrong.
- At the beginning of your journey, you will make lots of syntax bugs (which is ok).
- In time, syntax bugs will go away, and they will be replaced with semantic bugs.
But let’s back to what we were doing, breaking our first program.
Just as a reminder, at the previous lesson, we made a program which is determining if number is bigger or smaller than zero.
x = int(input("Please enter an integer: ")) 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')
Then, we executed this program in https://www.onlinegdb.com/online_python_compiler and everything worked properly.
Now, let’s introduce some syntax bugs and see what will happen.
First bug: At the line 5, we want to change == with = to make it look like ‘elif x = 0:’
- In programming there is a difference between == and =.
- Two equal signs check if the items are the same, example x == 5 (this checks IF x is equal 5)
- One equal sign assigns value to a variable, example x = 5 (this means that 5 is assigned to x)
- Essentially, this will be a bug, because in IF-ELIF-ELSE structure we need checks ‘==’ and not assignment ‘=’
This is a message we will get after executing ‘buggy example’. Python compiler will tell us that there is a “SyntaxError” and point out to the problematic line. However, compiler will not tell us how to resolve it. In our case, it is simple. Since we introduced a bug intentionally, we can simply solve it by using ‘==’ instead of ‘=’.
Whole point of this example is to intentionally break something, so we can see what will the compiler complain about. Later, when we make a real mistake (this one or similar) error messages will not be so strange for us.
Second bug: At the line 3, we will change ‘if x < 0:’ with ‘if x < 10:’
- This is not a syntax bug, because syntax will still be correct, program will work.
- This is a semantic bug, because first IF will not calculate things correctly.
- Result will be that numbers smaller than 10 will be recognized as negative (i.e. 5 will be reported as negative).
- As you can see, this is a logical bug, which is easy to detect here, but somewhere else (in bigger code) it would cause issues.
If you run this example, result will be like this
That is it for this lesson. We broke our program two times and fixed it.
First time it was a syntax bug and second time it was a semantic bug.
There will be a dedicated lessons on debugging, but for now this will be sufficient.
- Play with this code, break it intentionally.
- Change some lines and observe what will be reported as error in console.
- Try to make semantic bug as well, not only the syntax ones.