The Basic Data Types In Python

The ABC and 123 of Python

The Basic Data Types In Python

There’s this popular joke about programming where they say: 2 + 2 = 22. Weird but it’s true. '2' + '2' = '22' I’m going to pretend I’m your professor and we’ll go ahead and prove that but you’ll never understand till you know the data types in python.

No one might ever explain it to you this way and it might sound weird but data types are like the basic tools for command construction, moreover you can't issue a command in English if you don't know the English alphabets. We want to command python to do magic for us but python’s type of English is kind of Gothic, so we have to learn how to construct the commands properly with the data types (I want to make a joke on the Winter Soldier but you already get it, don’t ask).

Just like every language has it’s alphabets and arithmetic, python has its. Python's arithmetic is just a little strict. Python divided them into two, namely, Integers and Float.

Integers

Quite similar to what you do in mathematics, integers (in python) are numbers with no decimal parts. Numbers like 23, 564, 8, -7, -45 etc.

Declaring an integer in Python is very simple. In the form: variableName = initial value

Don’t get confused. What I did up there was create a variable called variableName and assign the value initial value to it. initial value should be the integer in this case. If you don’t still understand, you might want to check out my article on variables here .

Examples:

Age = 30
MobileNumber = 23480879811
print (Age + Age)

Output:

60

That wasn't at all hard, I declared a variable named Age and another named MobileNumber, assigning values to them. On the third line of the example, I used a print() function to add the Age to itself. Just in case that part got you confused, check out my article that explains what the print() function does and how it works, here .

Floats

Still on arithmetic, floats (in python) are numbers that have decimal parts. Numbers like 45.1234, 0.01, 100.401 etc.

Quick Question:

Are 20.0, 4.0, 879.0 etc floats?

You might want to reread the definition, of course these numbers are still equal to their representations in integers but they have decimal parts and are therefore considered to be floats by python (Quite strict right? I know).

Declaring a float is just similar to declaring an integer: variableName = initial value

Examples:

userHeight = 1.85
userWeight = 56.8
print (userHeight + userWeight)

Output:

 58.65

Strings

Now down to the reason why 1 + 1 = 11 and 2 + 2 = 22 can be possible in python; strings. Strings aren’t strictly arithmetic, they are the alphabet-like data type in python, now you see why working with them doesn’t really give us correct answers (or so you think). Strings refer to texts. Yeah, texts like “I am a boy”, “You are a programmer” or “Bukola” and “94”.

To declare a string, you can either use

variableName = ‘initial value’ (single quotes)

or

variableName = “initial value” (double quotes)

Examples:

HumanMale = “Zahid” 
HumanFemale = ‘Adeola’ 
AverageAge = ‘26.6

You saw that last example right, integers and floats if wrapped with the quotes are no longer considered whatever they are, they become strings.

Concatenation

Now here’s the magic you’ve been waiting for…

Just like integers and floats (arithmetic) can be added together in python, strings too can be added together. The process where strings are added together is known as concatenation.

Input:

FullName = “Benjamin ” + “Olapade” 
print (FullName)

Output:

Benjamin Olapade

2.jpg “Benjamin ”, a string was added to “Olapade”, another string. What if we added two number-like strings?

Input:

Experiment = '2' + '2'
print (Experiment)

Output:

'22'

6.jpg Proven! 2 + 2 = 22. When strings are added together, python just put them beside each other, like a continuation of text. That’s how concatenation works. I might have already proven what you were waiting for but there are a few things you might still want to catch.

Concatenation is not only done with the + sign, it can also be done with , but , is mainly used for concatenation inside a print() function and is used when adding two different data types.

Built-In String Functions

Python possesses some cool built-in functions to manipulate strings. Don’t get confused, a function is just a block of reusable code that can perform a certain task.

Maybe I’ll write a cheat book to access all of python’s built-in string functions and more later but an example of these functions are the .upper() and .lower() functions. We use the .upper() fuction to capitalize all letters in a string while .lower() does the opposite.

Input:

print ("Benjamin".upper()) 
print ("PYTHON".lower())

Output:

BENJAMIN 
python

Type Casting

Rounding up on the basic data types in python, it’ll be pointless not to mention type casting.

Sometimes while we write our program, it becomes necessary for us to convert one data type to another, such as converting a float to a string. That is known as type casting.

Similar to the three basic data types in python, there are three built-in functions that allows us to do type casting in python: int() , float() , and str() .

The int() function in Python takes in a float or an appropriate string and converts it to an integer. To change a float to an integer, we can type int(5.712987). We’ll get 5 as the result (anything after the decimal point is removed). To change a string to an integer, we can type int ("4") and we’ll get 4. However, we cannot type int ("Hello") or int ("4.22321"). We’ll get an error in both cases.

The float() function takes in an integer or an appropriate string and changes it to a float. For instance, if we type float(2) or float("2"), we’ll get 2.0. If we type float("2.09109"), we’ll get 2.09109 which is a float and not a string since the quotation marks are removed.

The str() function on the other hand converts an integer or a float to a string. For instance, if we type str(2.1), we’ll get "2.1".

These functions become important as we get deeper in python.

The word “basic” has been highlighted a lot in "basic data types" because the data types mentioned in this article isn’t all the data types, they are only the basic ones.

Congratulations! You made it to the end of this article, I'm hoping you learnt a thing or two. If you made it to this point, I’ll say you really wanted to learn and that’s commendable, I hope you did. If you have any questions, you can just drop them as comments or contact me directly here, eager to help.