Dear reader,
Thank you for visiting my page once more. Today I will focus on the pure basics of the Python programming language. I will explain the different types there are in Python and what they are used for. Until this point I realized that I have not actually tried to understand the language of Python. Most times I just picked a random topic and started to write about it without actually trying to understand what I am doing. This starts NOW.
In Python, everything is an object. Each object is classified into different kinds of things. The way these objects are classified is to call them different classes or data types.
The main, so-called, native datatypes that are preinstalled in Python will further be explained one after another (this is only Part I of this series):
- Strings
Strings (denoted as ‘str‘, sequence of letters) are surrounded by quotation marks (single ‘, double ” or triple ”’ quotes).
The reason for this is, that when e.g. printing a sentence using an apostrophe (as is “I’m smart”), Python would confuse the single quotes. Thus, you would surround the string with double quotes.
(The same works the other way around: When you want to e.g. quote someone, use ‘ single quotes. Additionally, use triple quotes when framing a longer string.)
- Numbers (integers, floats)
Numbers are, of course, also often used within programs. In Python numbers are either classified as ‘int’ or ‘float’.
– ‘int‘ meaning integer numbers (like 1,2,3..)
– ‘float‘ numbers with a decimal point (like 1.25)
Python actually lets you find out what type a certain object is using the ‘type’ function.
See below:
- Boolean (True/False)
Booleans are either True or False. There are two constants installed within Python which can be used to assign boolean values directly. In places like ‘if’-statements the Python program expects a reference to compare to a ‘boolean value’. In these places, called ‘boolean contexts’, any expression can be used and then Python determines its truth value. Each data types discussed in this article have different values that can be proven True or False.
I now add a list I value useful regarding the different operators (‘blocked objects’) installed within Python – True & False are two of them:
Continuing on Boolean types, I will explain it by using an if-scenario. See the following program in order for me to explain:
In this program I assigned the ‘size’ variable as an integer in line 6. Before, I asked the user to enter their name and a number of their choice. The program will then test if the number entered is positive or negative. It does so by using the if-operator. The ‘if’ part in line 8 checks, if the variable (size) is smaller than (<) Zero (0). If this holds true, I ask the program to display to the user that the number given must be negative. When this is not the case, the ‘else’ operator will display that given number must be positive.
This is the logic behind Booleans.
If there are any questions remaining. Let me know!!
Best,
James de Tec
Sources:
http://www.tutorialspoint.com/python/python_variable_types.htm