Learning Python: Variables and primitive data types




Hey and welcome to my first blog post. In this one I'll introduce you to the programming language Python, but before we get into it, I'll want to give you a general idea of what programming is.


What is programming?

Programming is nothing else then giving your computer a set of instructions. Actually we're all doing it in some kind everyday without even knowing. Opening your Web browser or using a calculator, somehow you want your computer to execute these tasks. So you must be able to communicate in a certain language with your computer, but they can just handle 0's and 1's (Bits). So how is it even possible to understand a simple command like "5 + 5"? How should they know what the number 5 is or what a "+" is? The answer is, everything is encoded in a sequence of bits. For example "5" could be represented as "101" and let's say "+" is encoded as "000111", then "101 000111 101" is what the computer sees. For humans it's almost impossible to read machine code, but computers give you a result in a split second. So programming languages are not more than a translation from machine code to human readable code, which we can faster interact with. The process of translating a programming language into machine code is called compiling.
Okay I think, this gave you a good overview about programming in general. Let's dive right into Python.


Variables in Python

We all know variables from math. They work literally like there, but with one exception. Let's say we are having x = 3 and y = 5. In Python we could say x = y, but is this a mathematical correct equation? No it's not, x ≠ y. What we do in Python is, we assign values to the variable and we are not trying to solve an equation. Let's see this in code. I am using Python 3 for this tutorial, just go to https://www.python.org/ for the installation and follow the guide.

x = 3
y = 5
print(x)
x = y
print(x)

>>> 3
5

In the first 2 lines we are initilizing  the variables x and y. The next line shows a function called print. I'll explain functions at some other point, but print is a python built-in function, which takes an argument and shows it output on the console. In this case 3 was printed to the console. Next we assign y to x, that's why the next output is 5.


Integers and floats in Python

In the example above the values of the variables are numbers, but we can assign different types of data to the variables: 
In this blog we'll discuss the primitive data types. So first we start off with numbers, which integers and floats belong to:
  • Integers: like the whole numbers in math (e.g. 2, 3, 100, -5, ...)
  • Floats: all decimal numbers, fractional numbers (e.g. 0.2, 3.14, -3.0)
With integers and floats we can do all arithmetic operations (+, - , /, *, etc.):

x = 3
y = 5

a = x + y                  # a = 8
b = x - y                  # b = -2
c = x * y                  # c = 15
d = x / y                  # d = 0.6
e = (x + y) * x - 3        # e = 21 (follows calculation rules)
f = 5 % 3                  # f = 2 (modulo)
g = 2**3                   # g = 8 (2^3)

x = x + 1                  # incrementing x by 1 (x = 4)
x += 1                     # same as x = x + 1, but shorter syntax
x -= 2                     # decrementing x by 2

Don't wonder about the hashtags. I am using them to comment my code and the machine ignores everything, what follows behind them. Okay so I just showed you some basic operations you can do in python, well there are lots more, but we can't cover them all, but this should give you a good intuition about it.


Strings in Python

So next up we have strings. They are simply a concatenation of characters in quotation marks (e.g "Hello World!"). The computer doesn't interpret strings, he just takes them how they are:

name = "Max"
animal = "dog!"

x = 5 + 5                        # x = 10 
y = "5 + 5"                      # y = "5 + 5" (strings are not interpreted)

conc_name = name + animal        # conc_name = "Maxdog!" 
                                 # (concatenation of 2 strings)

sentence = name + " has" + " a " + animal   # spaces count as characters
print(sentence)

>>> Max has a dog!

This is what you can do basically with strings. Again there is much more you can do with strings, but you need functions for it and we haven't talked about them yet. 
Lastly there are bools or booleans:


Booleans in Python

Okay so now we know we can do arithmetic with numbers, work with strings, but what is this bool? Bools have the property of being either True or False respectively 1 or 0 (Boolean algebra). It's like telling your computer a statement and he replies with "yes" or "no" (For example: Germany is a continent. ==> False). We can also link together multiple statements with logic operators (AND, OR, NOT). But how can we do statements in python?  We use the '=='-operator for testing equality and of course there is <, >, <=, >=, just like comparisons in math: 


x = 3
y = 5

bool1 = x == y                          # bool1 = False (checks if x equals y)
bool2 = 6 != 32                         # bool2 = True (checks if x not equals y)
bool3 = not (2 == 9)                    # bool3 = True

bool4 = 4 <= 5                          # bool4 = True (checks if 
                                        #               4 is less or equal to 5)

bool5 = 5 > 4                           # bool5 = True

bool6 = True and False                  # bool6 = False
bool7 = True or False                   # bool7 = True
bool8 = not True                        # bool8 = False

bool9 = True and bool1 or bool3         # bool9 = False
bool9 = (True and bool1) or bool3       # bool9 = False 

bool10 = 4 + 2 == 6                     # bool10 = True

You have to see 'and' like a multiplication and 'or' as an addition, then you see why bool9 resolves to False. We can also test the truth value of equations, just look at bool10. Check out the truth table below to understand how the logic operators work:


Source: https://upload.wikimedia.org/wikipedia/commons/4/4a/Truth_table_for_AND%2C_OR%2C_and_NOT.png


That's it for this post. I hope this was helpful and you learned something from it. Follow me on social media :)


Comments