Programming: Issue #6 [Tutorial][Python]: Basic Python Variables

python-logo

I’ve realised many people doesn’t have a clear idea on how python works, either for having experience in other languages and currently adapting to python or due having no experience at all. So I’ve decided to make a rework on the blog structure and create more sections.

First of all: Python is a “duck-typed” programming language, which means it doesn’t require to give an explicit type to a variable when it is created. Instead, Python assigns a type to that variable internally.

It’s not the same writting this:

var = 10

Than this:

var = 10.

In the first case we’re implementing a variable that contains an integer. In the second one, the variable contains a float.

Variable types can be checked using the built-in function type():

>>> type(var1)

There is a list of Python basic types:

– String (str):
Contains an array of characters.

>>> var1 = “Random text 12345 inside var1”

– Integer (int):
Variable for integers. Python has no limits on integer sizes.

>>> var2 = 1234567890133713370987654321

– Float:
Contains a decimal number.

>>> var3 = 13.37

– Complex:
A complex number in the “real+complex” form. Note Python uses “a+bj” notation.

>>> var4 = 42+7j

– List:
A mutable list of elements. Elements on the same list do not require to have the same types. Note it can contain no elements.

>>> var5 = [123, “hello”, 1.234, 3+3j]

>>> var6 = []

– Tuple:
Inmutable tuple of elements. Can hold different tipes of elements but the structure can’t change unless overwritten.

>>> var7 = (1234, “Hello World”)

– Dictionary(dict):
A mutable array that holds variables by pairs. Each entry can be seen as a tuple holding a key and an answer. When accesing the dictionary with a given key, the dictionary searches that key entry and returns it’s related ‘answer’. Both values on each pair can have different types between themselves and other pairs. Dictionaries have no strict placement order, meaning adding new pairs doesn’t place them at the end of the dictionary.

>>> var8 = {“ok”: 1234, “nook”: “it’s not ok”}

>>> var8[“ok”]
1234
>>> var8[“nook”]
“it’s not ok”

– Boolean(bool):
A variable that can hold a True/False value.

>>> var9 = True

>>> var10 = False

This is all as far as it goes concerning basic variable types. I don’t see how this can be improved as going deeper in one type requires more than a post to do it right, so I guess I’ll make separate sections for more complex types, aka Tuples, Lists and Dictionaries.

This entry was posted in Programming and tagged , , , , , , , , . Bookmark the permalink.

Leave a comment