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.
Lets write our first Python program
There is one big problem with programming courses for beginners. They guide you trough the long introductions, history facts, lots of theory.
Usually, most people simply quit before getting to the “good stuff”.
Let us try a bit different approach.
Lets write our first Python program
Here is a list of things that you need to know for now:
- There is a lot of programming theory around Python and you will learn it in time.
- Python has 2 major versions (Python 2x and Python 3x) and you will learn the differences in time.
- You will have to setup development environment for Python.
- You have fantastic Python documentation at https://docs.python.org/3/tutorial if you prefer book like learning.
For now, let us postpone most of the standard steps and simply write and run our first Python program.
Task: Write a program which reads input number and tells us if that number is positive (bigger than 0) or negative (less than 0).
- There are online Python compilers, which allows you to write Python code in your browser and execute it. Yes, you don’t have to setup anything at this moment. We can simply get started.
- Try not to do copy paste, because you want to get used to typing, also known as coding… 😉
- Python cares about indents (tabs). When typing examples, pay attention to this.
Open Python Compiler at this link: https://www.onlinegdb.com/online_python_compiler
Once you open this link, type in the solution as provided here.
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')
Final solution should look like this:
Once you successfully typed the solution in Python compiler, click Run.
Program will ask you to enter some number (for example 20). After doing this and clicking enter, program will let you know if the number is positive or negative.
Yay! You just made your first Python program!
(rainbow unicorns all around, feel that heat of instant gratification, etc, etc.)
Now, what does this program actually do?
For now, just notify couple of things:
- input() – this is a Python function that is used to get the input from the console. Meaning, it allows someone to type in something and send it to your program. Later, program decides what to do with this.
- int() – function which tells program to treat some information as integer (otherwise program wouldn’t know how to treat something)
- int(input(“Please enter an integer: “)) – get something from console and treat it as an integer
- x = – here ‘x’ represents a ‘variable’. Variables are used to store some information in memory and store it under some name (variable name)
- x = int(input(“Please enter an integer: “)) – get something from console, treat it as an integer and store it under a name ‘x’
- if x < 0: – In most of the programming languages IF is a standard way to make a condition. If some condition is good, do some logic..
- elif – If the first condition is not good, check some other condition.
- else – If none of the previous conditions were good, do this anyway.
if x < 0: print('The number you have entered is negative')
This part of the code says: If the value stored in ‘x’ is smaller then 0, then print out “The number you have entered is negative” message in the console.
elif x == 0: print('The number you have entered equals zero')
This part of the code says: If the first condition is not good, and the X is equal to 0, then print out the message in the console.
else: print('The number you have entered is positive')
The last part of the code says: If any of the previous conditions were not good (so, X is not smaller than 0 and it is not 0), then print out “The number you have entered is bigger then zero”, which is the only possible option.
That is it. You coded your first Python program.