Datatypes and Variables in Python

2833
In this article, we will learn about:
  • Why we need data types
  • How data is stored in the Program
  • Different data types that are available in Python
  • Rules for declaring a variable in Python
  • How to define the variables of different data types in Python.  
  • How to assign values to multiple variables in one line 

Table of Contents

Why we need data types

The two major components of any programming code are Program logic and Data. We execute program logic to process input data to get the desired output.

To process the input data, we need to store the data. Data types help to set the boundary for the data being stored, it helps to differentiate raw data by classifying them into different types. It would make the life of a programmer easier if one knows the nature/format of data in prior. Based on the nature or type of data we design our program logic. In short, it is hard to imagine any program without data types.

How data is stored in the Program

Data is stored using variables in Python. We define variables based on the type of data we want to use for our program logic.

Data Types available in Python

Python programming language offers various built-in data types. Given below is the list of available data types with the category.

Data type CategoryData Type
Text Typestr
Numeric Typesint, float, complex
Sequence Typeslist, tuple, range
Mapping Typedict
Set Typesset, frozenset
Boolean Typebool
Binary Typesbytes, bytearray, memoryview

Rules for declaring a variable in Python

  • A variable name in Python can only begin with an underscore or a letter. You cannot begin the variable name with a number.
  • A variable name should only contain alphanumeric characters and underscores. (a-z, A-Z, 0-9, _ )
  • Variable names are Case-sensitive. City, city, CITY are three different variables.

Define the variables of different data types in Python.

A variable in python is defined or set when you assign a value to the variable. Like in other programming languages such as C or C++ we do not need to declare the variable in Python.

The Python code given below shows how to set the variable for different data types.

Code:

# Set String value (str data type) to a variable
a_str = "I am a string data type value"
print(a_str)

# Set Integer value (int data type) to a variable
a_int = 41
print(a_int)

# Set Float value (float data type) to a variable
a_float = 53.5
print(a_float)

# Set Complex value (complex data type) to a variable
a_complex = 5j
print(a_complex)

# Set List value (list data type) to a variable
a_list = ["Asia", "America", "Africa"]
print(a_list)

# Set Tuple value (tuple  data type) to a variable
a_tuple = ("Asia", "America", "Africa")
print(a_tuple)

# Set Range value (Range data type) to a variable
a_range = range(11)
print(a_range)

# Set Dictionary value (dict data type) to a variable
a_dict = {"Employee_id": 70923, "Employee_name": "Jonathan"}
print(a_dict)

# Set SET value (set data type) to a variable
a_set = {"Asia", "America", "Africa"}
print(a_set)

# Set frozenset value (frozenset data type) to a variable
a_frozenset = frozenset({"Asia", "America", "Africa"})
print(a_frozenset)

# Set Boolean value (bool data type) to a variable
a_bool = True
print(a_bool)

# Set Bytes value (bytes data type) to a variable
a_bytes = b"Hello"
print(a_bytes)

# Set Bytearray value (bytearray data type) to a variable
a_bytearray = bytearray(5)
print(a_bytearray)

# Set memoryview value (memoryview data type) to a variable
a_memoryview = memoryview(bytes(5))
print(a_memoryview)

# Use type() function to return class type of the argument(object) passed as parameter.
print(type(a_str))
print(type(a_int))
print(type(a_float))
print(type(a_complex))
print(type(a_list))
print(type(a_tuple))
print(type(a_range))
print(type(a_dict))
print(type(a_set))
print(type(a_frozenset))
print(type(a_bool))
print(type(a_bytes))
print(type(a_bytearray))
print(type(a_memoryview))

 

Output:

I am a string data type value
41
53.5
5j
['Asia', 'America', 'Africa']
('Asia', 'America', 'Africa')
range(0, 11)
{'Employee_id': 70923, 'Employee_name': 'Jonathan'}
{'Africa', 'America', 'Asia'}
frozenset({'Africa', 'America', 'Asia'})
True
b'Hello'
bytearray(b'\x00\x00\x00\x00\x00')
<memory at 0x00000271C43CDD08>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'list'>
<class 'tuple'>
<class 'range'>
<class 'dict'>
<class 'set'>
<class 'frozenset'>
<class 'bool'>
<class 'bytes'>
<class 'bytearray'>
<class 'memoryview'>

 

Assign values to multiple variables in one line 

Like other programming languages, Python also provides the feature to set values to multiple variables in a single line.

Code:

# Assigning Multiple values to Multiple Variables in single line
a_continent, b_continent, c_continent = "Asia", "America", "Africa"
print(a_continent)
print(b_continent)
print(c_continent)

# You can also assign same value to multiple variables in single line
a_continent = b_continent = c_continent = "Asia"
print(a_continent, b_continent, c_continent)

 

Output:

Asia
America
Africa
Asia Asia Asia