Variables in Python and how to use them
Welcome back to the Python Tutorial.
Today we're gonna take a look at variables.
Well, some of you might ask: What the hell are variables while others will ask How can I declare them?
I want to cover all those Questions.
Firstly, what are variables?
Well, a computer needs to store given values somewhere in his memory. You can compare it with your brain memorizing an information. A variable is changeable, meaning you can overwrite it at any given time.
Secondly, how can i use variables?
You may know some syntax like int i =0
from other programming languages.
Good News: It's much easier in Python. You neither have to declare type of the variable (whether it is a number, a char or a string) nor do you have to declare it explicitly.
Let's look at the Syntax:
i = 0
Very easy, isn't it? Python evaluates you variable to determine what type it is. So you can use the very same syntax for i = 0
and i = 'a'
or even i = "Test"
!
Did you ever ask yourself why Python is a so-called interactive programming language? Well, it has a console in which you can test and debug live! We will use this to test out our variable syntax.
On your PC, open a terminal window and type in python
. Press Enter. You should now get a console prompt.
Now we try our variables. Enter i = 0
. If you enter i
afterwards, you will see the Computer stored your entrance and you will get 0 as Output. If we now try to do i = "Test"
and we do i
again we will see that the Output is now Test.
We just changed a variable without even changing its Type! For a programming language, such functions are very useful.
In the next parts we will deal with loops and functions. Afterwards, we will write our own little calculator.
Thank you for reading! If you have any Questions, write it in the commentaries or PM me.