Remember you solved for 'x' back in school days? They were called variables. Well, variables in programming are somewhat similar too.
Variables are a storage location that also has a name associated with it. Names that are cute and sweet. Now these names help us in labelling and improving code readability. You may ask, what does that mean Aamina? I'm here to tell you😌. It means you understand why the variable was created, just by looking at its name. This makes it easier for multiple developers to work on the same project. A common way of thinking about it is that variables act like named containers.
Now that we have a basic idea of what variables are, let us move on to data types. Before breaking down data types, what is data? Data is what we store in these containers/variables. We can store data, fruits, vegetables, and spices in variables(I will argue that was a good joke hehe). A data type is simply the type of data or the type of fruit that we have stored in these containers/variables. Variables can store different types of data. There is no issue with storing a carrot or an int number in variables.
Normally while programming, you need to declare or tell the compiler what kind of data type it is dealing with, but in Python you don't need to. Yeah, Python will figure it out for itself (such a good child🥺). Therefore, Python is dynamically typed. It also means that the type of data in a variable could change and it is not fixed.
Now let's see how to declare a variable and assign a value to it.
# Assigning an integer value to a variable called age
age = 25
print("Age:", age)
Here we stored an int type of data in the variable age. Similarly, we could have stored other kinds of data types as well ( can't store a carrot unfortunately😔).
Let's see the different types of Data types that are available in Python.
Data Type | Description
--------------|---------------------------------------------------
int | Integer (whole numbers)
float | Floating-point numbers (decimal numbers)
str | String (text)
bool | Boolean (True or False)
list | List (ordered, mutable sequence)
tuple | Tuple (ordered, immutable sequence)
dict | Dictionary (key-value pairs)
set | Set (unordered collection of unique elements
int: This is pretty self-explanatory. They are whole numbers that are to be stored.
# Example of an integer
age = 25
print("Age:", age)
float: These are decimal numbers.
# Example of a floating-point number
height = 5.9
print("Height:", height)
str: These are strings. Strings are simply text or a collection of characters.
# Example of a string
name = "John"
print("Name:", name)
bool: These are data types that hold values that are either true or false.
# Example of a boolean where we check if someone is a student
is_student = True
print("Is student?", is_student)
list: A list is a sequence of elements that can be changed later on(mutable). You could store different types of elements in lists.
# Example of a list
numbers = [1, 2, 3, 4, 5]
print("Numbers:", numbers)
tuple: similar to a list but can't be changed(immutable).
# Example of a tuple
coordinates = (3.5, 7.2)
print("Coordinates:", coordinates)
dict: It is a collection of key and value pairs. Somewhat like in a dictionary, a word and a meaning, here keys have unique values associated with them. We will learn about them in detail later.
# Example of a dictionary
person = {'name': 'John', 'age': 25, 'city': 'New York'}
print("Person Information:", person)
set: Represents an unordered collection of unique elements. Sets are enclosed in curly braces.
# Example of a set
unique_numbers = {1, 2, 3, 4, 5}
print("Unique Numbers:", unique_numbers)
We have looked at the basics of Python data types here. There is always more to learn but this would be enough to get us started. We will be looking at basic operations in the next article. I hope you guys enjoyed this one.
Will we ever be able to store carrots in variables or variables in carrots? Will have to wait I guess.👽