This time we'll look at variables in the Python language. A variable is a place you fill with one or several values later on.
Variables are very useful when you're arranging logic and manipulating data. Used correctly, they also make the code you write easier to read.
Creating a Variable in Python
Python is so simple that it has no special rule for creating a variable at all. A variable appears automatically the moment you give a name a value.
>>> a = 90 # variable a holds 90
>>> b = "Invasikode" # variable b holds "Invasikode"
>>> print(a)
90
>>> print(b)
Invasikode
In the example above you create 2 variables, a and b, with different values. The equals sign is probably obvious by now: you put the value on the right into the letter on the left.
Now one more example. What do you think the output is? Guess first, don't run it, it's more fun that way, hehe...
>>> a = 10+5
>>> a = "Invasikode"
>>> print(a)
As you can see, that code puts 2 different values into one variable, a. In Python the equals sign (=) automatically rewrites (overwrites) the value and the data type that were there before.
The output should be clear by now...
>>> a = 10+5
>>> a = "Invasikode"
>>> print(a)
Invasikode
Yay... congratulations if you guessed Invasikode, you got it right.
Calling a Variable in Python
Calling a Python variable is easy and direct. You write the variable's name.
>>> a = 9
>>> b = 90
>>> c = a + b # same as 9 + 90
>>> print(c)
99
Variable c in that example holds the sum of variables a and b. So we get 90+9 = 99.
One more thing. You've actually been calling variables with the print command all along. If the content inside print isn't wrapped in quotation marks, it's read as a variable name.
>>> nama = Budi
>>> print(nama)
Budi
>>> print("nama")
nama
You can see the difference.
Variable Naming Style in Python
In Python a variable name has to start with a letter (lowercase or capital) or an underscore (_). If a variable name starts with anything else, you get an error.
Python is also known as a strongly-dynamic-case sensitive typed language. What does that even mean? Let's take it one piece at a time.
Strongly Typed
Strongly typed means Python won't accept an operation between 2 or more different data types. Here's an example.
>>> a = 12+3 # valid, both are numbers
>>> a = 12+"tiga" # invalid, there is a number and a letter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
See that? If you fill a variable with a different data type, you get an error.
Dynamically Typed
As we covered earlier, when you assign a value to a variable, that value and its data type get rewritten (overwrited).
Not every programming language has this feature. Python is one of the languages that does. The reason is that running Python code never goes through a compile step, so a data type can change at runtime.
>>> a = "Makan Nasi"
>>> print(a)
Makan Nasi
>>> a = 90+9
>>> print(a)
99
Even though variable a gets 2 values of different types, the code stays valid.
Case Sensitive
Case sensitive means 2 variables with the same name but different capitalization count as 2 different variables.
>>> nama = "Adi"
>>> Nama = "Budi"
>>> NAMA = "Citra"
>>> print(nama, Nama, NAMA)
Adi Budi Citra
The example makes it obvious: you have 3 variables that all read as "nama", but each one is written differently. When printed, every value that comes out is different.
Right, the print command can print several values at once, by separating them with a comma.
Wrapping Up
That's it for this one. Come back to this piece later if you forget how to write a variable, hehe...
Hope it helps... Next time we'll go further into data types in Python.
See ya...