Python Variables and Data Types


Now coming to the next topic, today we are going to discuss something called as variables.
To understand clearly about variables, let us consider an example. In our college, during any exam registration or anything just like how we give our name in the name column, or our roll number in the roll number column — variables are used to store a value.
Example :
roll = 11
name = "prajitha"
There are different types of values that are present — for example, our name is considered to be a string since it is a word, our roll number will be an int since it consists of integer values and so on. All these types like string, integer are called as data types.
Data Types in Python:
int – integer values
float – decimal values
str – short for string, words or sentences or even letters
bool – True, False etc
Apart from these, there are a few more important data types in Python which we will learn in upcoming articles:
list – collection of multiple values in square brackets
tuple – similar to list but immutable (cannot change)
dict – key-value pairs used to store structured data
set – collection of unique values
We do not need to use data types in Python, we can just give a value to a variable like:
variable_name = variable_value
But there are some rules to name a variable:
Variable names consist of only capital or small case letters, numbers, and underscore (_) only.
The starting letter of a variable must be a letter or an underscore, it should not start with a number.
There should not be any spaces while writing the variables.
Variable names are case-sensitive.
Examples:
first name ❌ first_name ✅
1st ❌ first ✅
_value ✅ myvar, MYVAR, MyVar, myVar — all these four variable names are distinct.
While giving the values to the variables we can assign only a single value to a variable name.
And also while giving a string value or a character value, we have to give them in single or double quotes.
name = "Prajitha" or name = 'Prajitha'
letter = "A" or letter = 'A'
letter = A ❌ invalid
To assign an integer or a float value or any other values to a variable we do not need to specify the datatype. We can just assign them directly.
a = 10
b = 1.5
After assigning a value to a variable, we can use a function in Python that tells us the datatype of that variable’s value.
The function that is used is called as type()
.
Examples:
group = "BSc"
print(type(group))
# output - <class 'str'>
roll = 11
print(type(roll))
# output - <class 'int'>
grade = "8.5"
print(type(grade))
# output - <class 'str'>
In the third example if you observe, even if we have given 8.5
as value, it showed that the datatype is str
, because we have given 8.5
in double quotes. So anything that is mentioned inside single or double quotes is considered as string.
We can also convert the datatype of one variable into another datatype like converting a variable of datatype int
to a datatype str
. This process of converting is called as type casting.
It is very easy to perform type casting in Python.
Examples:
value_before = 5.2
print(type(value_before))
# output - <class 'float'>
value_after = int(value_before)
print(value_after)
# output - 5
print(type(value_after))
# output - <class 'int'>
value_before = "123"
print(type(value_before))
# output - <class 'str'>
value_after = int(value_before)
print(type(value_after))
# output - <class 'int'>
s = "abc"
int(s)
# output - ❌ It will show error as we are not performing type casting correctly.
In upcoming articles, we will cover:
How to use
list
,tuple
,dict
, andset
in real lifeHow to work with multiple variables at once
Advanced type conversions and error handling
Summary:
Variables are used to store a value in Python.
Data types tell us what kind of value the variable holds.
Python allows us to assign values without specifying the data type.
Use
type()
to check the data type.Use type casting to convert one data type into another.
Follow naming rules while creating variables.
Subscribe to my newsletter
Read articles from Tammana Prajitha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
