All You Need To Know About Variables And Data Types

Gopal Khadka
3 min readAug 23, 2023

--

Data types are the classification of various data. They tell us what kind of operations can be done on them. Since Python is an object-oriented language, everything is just class. So data types are also class and variables are instances of those classes.

The built-in function type() helps us find out the type of any data.

print(type(5)) # result: int

Variables in Python

They are like containers that hold different type of data value like integer, string, list, etc. They used letters, numbers or underscore to name themselves. However, a number can’t be the first character of the variable name but the other two can.

name= "henry"
age= 29
_name= "Harry"
1name = "Sam" ## gives error

Constants in Python

As the name suggests, they hold values that can’t be changed during the execution of the whole program.

GAME_FPS= 60
GAME_FPS= 120 ## gives error

Numeric Data Types in Python

There are three types of numeric data type in python, i.e.

  1. Integer (int): It is a data type that can hold only integer value of any number.
int x = 5.5
print(x) #result : 5

2. Floating number (float): It is a data type that can hold floating numbers .

float x = 5.5
print(x) #result : 5.5

3. Complex number (complex): It is a data type that can hold both imaginary and real numbers. For example: 2 + 4j where j stands for imaginary part.

x = -5 + 3j
print(type(x)) #result : <class 'complex'>

Boolean Data Types in Python

This data type is used to represent on/off, 0/1 or True/False values. It is used in all programming languages to satisfy the required condition.

isRaining=True
isSunny=False
## Or we can write this:
isSunny= not isRaining ## result: False

Here, we used logical operators on one value to get the value of another one. The long list of operators will be discussed in future blogs.

Sequence Data Types in Python

Sequence data types are those data types whose inner value can be accessed using indexing and can be looped, which will be discussed later on.

  1. String (str): It is a data type that can hold any characters, words or even paragraphs. It is also kind of an array used in C language.
str greeting="Hello user !!"
print(greeting[0]) ## result: H
print(greeting[1]) ## result: e

From the above example, we can say that str data type can hold any characters or sequence of characters. Its inner value can be accessed using indexing. For example: greeting[0] gives “H” because H is the first character of the string.

Note: The indexing ranges from 0 to n-1 in most of the programming language.

2. List : It is the data type that can hold multiple other data types of same or different kind. It is generally used to group related information. Indexing also works in this.

students=["harry","jenny","sam","richard"]
printf(students[0]) ## result: harry
## we can change inner values like this:
students[0] ="henry"
printf(students[0]) ## result: henry

3. Tuples: It is the data type that works similar to the list, but its inner values can be accessed but not changeable. It is used to store constant values that need not be changed during execution of the whole program.

geometry = (500,600)
printf(geometry(0)) ## result: 500
geometry(0) = 100 ## gives error

Set data type in Python

Set as we learned in the school is a collection of unordered unique elements, i.e. items can not be repeated. Operations like union, intersection, difference can be done in this data type.

my_set = {1, 2, 2, 3, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}

This data automatically removes duplicate values if added.

Dictionary Type In Python

This data works similar to widely used JSON object. It contains pairs of key and value. Values can be repeated, but keys can’t be. Each key is unique in the dictionary object.

my_dict={"key1":"value1","key2":"value2"}
person={"name":"Henry","age":20}

Look carefully that the values inside the key or value is actually just another data type like integer, float, string, sequence data types etc.

None Type in Python

Like void in C language, there is a built-in None data type in Python that indicated absence of any value. It is used when initializing variable or return the result of invalid value.

result = None

This concludes the basic fundamental concepts discussion of the data types in Python. Did I miss something?

--

--

Gopal Khadka
Gopal Khadka

Written by Gopal Khadka

Aspiring coder and writer, passionate about crafting compelling stories through code and words. Merging creativity and technology to connect with others.