2. Getting started
It is customary for language tutorials to start with a plain "Hello World" and as such an adherent to tradition that I am, I will do no less:
[1]:
print("Hello world")
Hello world
This should simply output:
Hello world
It is a very simple example where we pass the string "Hello world" to the function print which, well, print stuff (for those who don’t know, a string is sequence of characters, that is, a bunch of text).
We could make this a little more interesting, though:
[2]:
somevar = "Hello world"
print(somevar)
Hello world
Which gets us the same output, but now, we first “store" the string inside the variable somevar, and then ask the function to print it for us.
Now let’s add some comments to our script:
[3]:
#This is a Hello world in Python
somevar = "Hello world"
print(somevar)
Hello world
Just like bash scripting, everything that goes after # will be ignored by the Python interpreter. Now, lets try doing some math with integers:
[4]:
someint = 12
someotherint = 23
somemath = someint + someotherint
print("Result:", somemath)
Result: 35
Which should simply output:
Result: 35
Note that the print function allows us to pass multiple arguments to it, and automatically concatenate them for us.
Now look at this other example using decimal numbers (called floats on Python).
[5]:
somefloat = 3.43
someotherfloat = 1.4
amultiplication = somefloat * someotherfloat
print("Result:", amultiplication)
Result: 4.802
Which should output:
[6]:
Result: 4.802
Let’s also try something more “complicated":
[7]:
somefloat = 3.43
someotherfloat = 1.4
amultiplication = (3.24 + somefloat) * someotherfloat
print("Result:", amultiplication)
Result: 9.338
Which should output:
[8]:
Result: 9.338
Note that, as your High School teacher always said, multiplication takes precedence over division:
[9]:
somefloat = 3.43
someotherfloat = 1.4
amultiplication = 3.24 + somefloat * someotherfloat
print("Result:", amultiplication)
Result: 8.042
Which should output:
[10]:
Result: 8.042
2.1. Print and interactive mode
At this point, you might have already noticed that while using the interactive mode of Python (or IPython), you don’t need to use print function to see the actual value of a variable, all you need to do is type this variable:
[11]:
something = "lol man, print is useless"
something
[11]:
'lol man, print is useless'
So from now on, we will not be using the print function on all the example to avoid getting you bored. But just remember this is only valid for interactive Python sessions (you must use print function if you run a Python script stored in a file, for example).
Notice however, one thing about IPython: if you paste multiple lines of code there, like this one:
[13]:
something = "that's something"
somethingelse = "that's not the same"
something
somethingelse
[13]:
"that's not the same"
You’ll only get you the output of the last line, like this:
In [30]: something = "that's something"
...: somethingelse = "that's not the same"
...: something
...: somethingelse
...:
Out[30]: "that's not the same"
Whereas in a vanilla Python interactive terminal you’d get:
>>> something = "that's something"
>>> somethingelse = "that's not the same"
>>> something
"that's something"
>>> somethingelse
"that's not the same"
2.2. Exercises for delving deeper
You may now want to use the function type to check the type of those variables, like this:
[16]:
somefloat = 3.43
someint = 1.4
somestring = "ske"
someotherstring = "2321"
type(somefloat)
type(someint)
type(somestring)
type(someotherstring)
type(someint + somefloat)
[16]:
float
Also note that none of these will work:
"2321" + 32
"2323.1" + 32
"2323" + 32.1
"2323.4" + 32.1
So, don’t try to add a string to an integer.