Python Data Types: A Beginner’s Guide to Variables

Python, with its simplicity and versatility, has earned the spot of one of the most popular programming languages available. And like any programming language, Python has its own set of data types which can resemble or differ other programming languages. In this article, we will be talking about the different data types and its uses that a Python programmer might use.

What are Data Types?

In programming languages, data types are what programmers use to define what kind of value a variable will store. This will determine the use of the variable whether it will store a sentence, a number, or even a group of sentences or numbers. Data Types are very important in programming if a programmer even wants to create a meaningful program, they will need to use Data Types. As an analogy, Data Types are basically the programmer’s guns in which for each ammo(data) must be inputted inside the correct gun(Data Type)

Python’s Basic Built-In Data Types

As mentioned before, programming languages has the same or different collection of Data Types. Since we are talking about Python, we will be talking about the Data Types used in Python.

Integer (int)

The int Data Type is one of the most used Data Type for storing numerical values. It is useful for numerous reasons. It can be used for storing input made by the user, a counter for keeping track of the number of loops, or for mathematical operations. There are many numerical Data Types in Python, but int is the most basic as well as the most used. However, take note that the int Data Type can only store whole numbers and cannot store values with decimal values. The Data Type “double” is used for values with decimal values.

Examples of declaring an int variable:

x = 5
y = int(4)

String

Another most used Data Type that programmers use is the string. String is used to store any numerical or non-numerical value in the form of a text. In most applications today, mostly any form of text that we see is in the form of a string. The string Data Type can be used for storing important data like email addresses, passwords, descriptions, anything that will contain text, string will be important.

Examples of declaring a string variable:

name = “Parodez”
houseNumber = str(5)

List

A slightly more complicated Data Type is a list. A list is a Data Type in which not only stores one singular data but a collection of data. This can be a predefined list or an empty list to fill in later. It can store different types of data and is not limited to only a single Data Type.

Examples of decalring a list variable:

countries = [“Japan”, “China”, “Singapore”]
sentence = [1, “Hello”, ” “, “world”]

Tuple

Tuples are a bit similar to lists but slightly different. Compared to lists, tuples are immutable, meaning that once a tuple is defined, there is no way you can modify it whatsoever. Lists however can be modified even after the initial declaration. An example is that if you declared an empty tuple, there is no way that you can add data to the existing tuple.

Examples of declaring a tuple variable:

year = (“1991”, “1992”, “1993”)
odd = (1, 3, 5, 7, 9)

Dictionary

And last is the dictionary. A dictionary Data Type is a type of unordered sequence of variables that follows a key-value system. In each data in the dictionary there will be two types of data, the key and the value. The key is like the ID of the pair inside the dictionary. There can be no duplicate keys. Value on the other hand is the actual content of the pair as it will hold the important information that is related to the key.

Examples of declaring a dictionary variable:

employee = {1:”Owner”, 2:”Manager”, 3:”Supervisor”}
oddEven = {1:”odd”, 2:”even”, 3:”odd”, 4:”even”}

Operation with Variables

Now that we know some of the basic Data Types, we can continue on how to manipulate some of these Data Types.

Mathematical Operations

We can not only store numerical data into variables but we can actually manipulate them as well. It is as simple as just using simple addition and subtraction using substitution. For example, if youwe declared x = 10 and y = 5 and we want to create a new variable as the sum of x and y, we can declare z by doing:

z = x + y

The program will then take the values of x and y and add them together, resulting in a new value, z which will contain the sum of x and y which is 15.

The same concept can be used for other operations such as subtraction, multiplication, division, and other mathematical operation that a programmer will use in their program.

String Manipulation

When we declared a string variable, we assigned a text value to that variable, but what if we wanted to add another text to the string? We can do that by using concatenation! Concatenation is when you want to combine two strings to form a new string value. There are other ways to manipulate a string value but we will focus on the most simple one, which is concatenation.

For example, if we declared strings x = “Hello” and y = “world” and we would like to join it together or concatenate them rather into the variable z then we can use the same method as the example above with the addition operation. We do that that by doing:

z = x + y

Notice how similar string manipulation is to math operations. However, we come with the problem that the literal values of x and y will be combined which will make the value of z = “Helloworld”. Now, how do we add a space in between x and y? We just add it of course by doing:

z = x + ” ” + y

And now the value of z should be z = “Hello world”. Concatenating strings does not only have to include string variables, we can also add actual strings themselves. This also applies to mathematical operations as mentioned above. We can add other numbers that is not assigned to a variable.

Conclusion

As we’ve delved into Python data types and variables, we’ve uncovered the essential tools that every Python programmer needs to wield effectively. Data types, the foundation of programming, are akin to a programmer’s arsenal, determining the nature of data storage and use. From integers for numerical values to strings for text, lists and tuples for collections, and dictionaries for key-value pairs, Python offers a rich array of data types to suit a variety of needs.

Parodez

A Software Engineer that delves into multiple types of programming and development.

Recent Posts