First program - Print, comments and data types

Let’s now write our first programs and learn a little about functions, comments and data types of Python.

Functions

First, just so that this doesn’t get in the way of understanding the content that will be shown next, let’s talk a little bit about functions. That’s because we will use some functions that are already defined by Python itself and it is good to know, at least superficially, what we are doing.

Functions are predefined blocks of code that can be reused. Functions can take parameters or not. Parameters are information, data, passed in to the function, that uses this data to make some calculation or transformation.

Functions can return a value or not.

The usual way to execute the code in a function is to call it through it’s name and parameters inside parenthesis. If the function doesn’t have any parameters, the parenthesis are still needed, but without anything inside them. The print function, a Python standard function, for example, takes what will be printed as a parameter.

The print function

We already saw a little bit of the print function, in the last chapter, about introduction. Basically, the print function “prints” in the screen, meaning that it will show in the screen, whatever is inside the parenthesis.

Note that the behavior of this function has changed from Python 2 to Python 3. In Python 3, print is exclusively a function, meaning that it can only be called as a function, with the parenthesis and the parameter inside of the parenthesis.

In Python 2, it was also possible to use it as a statement. You could call it without the parenthesis, passing what would be printed after a single space. This usually led to confusion and ambiguity, and thus, print as a statement doesn’t work anymore on Python 3. Be aware though, that some content or literature that you find might be using Python 2, and as such, might still use print as a statement, like in this example:

print "Hi, Python"

In Python 3, this doesn’t work anymore, and will return an error. So, again, print in Python 3, only works if it is used like this:

print("Hi, Python")

Comments

Another important thing that you will eventually see in other people code are comments. Comments are self-explanatory. They are annotations in the code that are not executed, and are generally used to explain, illustrate or provide additional information about the code. In Python, there are two ways to write comments in your code. The first one, single line comments, must be preceded by a #. Everything after the # is, then, considered as a commentary. Note that it doesn’t have to be in the beginning of your line of code. Check this example:

# Isto é um comentário e não será executado pelo Python
print("Hi Python")
print("Explaining comments") # isto também é um comentário e não será executado

If you run the code above, you will notice that only what is inside the print functions is actually printed.

Another possibility is to use multiline strings as comments. Multiline strings are defined between triple quotes ("""), as in the example below:

a = 10 + 5
"""
Now I'll write a long string just to show that
multiline strings can be used as comments
"""
print(a)

One thing to be aware is that, multiline strings, when located after the signature definition of a function, a class definition or in the beginning of a module, are treated as docstrings, which work as documentation for these elements. These, unlike comments preceded by #, are not ignored by Python’s internal parser, and can be accessed programatically. Normally, this isn’t cause for concern, but it’s important to be aware of this information.

Basic data types

Python allows you to work with different data types. Here they are:

  • Strings - strings are defined between double quotes (") or single quotes ('), and they basically represent text to be printed by a program or exported by it. Ex: “Hi Python”
  • Integer numbers - numbers without decimal places, are simply defined by the number itself. Ex: 5, 31, 150

Floating point numbers (float) - numbers with decimal places, defined by the number and the decimal places after the dot. Floats have precision issues, and as such, are not recommended for use cases where precision is important (such as dealing with money, for example). For these cases, the usage of Decimal objects, from the decimal module, is recommended. Ex: 5.67, 99.99

Boolean - used to represent the concepts of true or false. Booleans can only be one of two values, represented by True and False Ex: True, False

Lists - used to represent sequences of values, allowing the user to store multiple values in a single entity. List items are defined inside square brackets. Ex: [“Joseph”, “Caroline”, “Adam”]

Tuples - tuples are also used to store sequences of values. The difference to lists is that tuples, after being defined, are immutable. Elements in a tuple are defined inside parenthesis. Ex: (“Beatles”, “Queen”, “Rush”)

Set - sets are the third data type used to store sequences of values in a single variable. The unique thing about sets is that there are no duplicate values in it, with a set keeping only a single occurrence of a value. Its values are defined inside curly braces. Ex: {“Google”, “Apple”, “Microsoft”}

Dictionaries - Data structure used to store values in a key:value pair format. Ex: {“nome”: “Felipe”, “altura”: “174 cm”}

Below, we show how each of these data types can be defined and are shown in Python. The type function, as the name already shows, tells us the type of a certain value that is passed as a parameter:

>>> type("string example")
<class 'str'>
>>> type(3)
<class 'int'>
>>> type(4.44)
<class 'float'>
>>> type(True)
<class 'bool'>
>>> type(["Joseph", "Caroline", "Adam"])
<class 'list'>
>>> type(("Beatles", "Queen", "Rush"))
<class 'tuple'>
>>> type({"Google", "Apple", "Microsoft"})
<class 'set'>
>>> type({"name": "Felipe", "height": "174 cm"})
<class 'dict'>

Conclusion

With this, we finish this chapter. We saw a little bit of Python’s basic syntax, how to write comments in your code and the basic data types that Python allows you to use.