How to create different data type variables in Python

Data Types in Python?

Python has several built-in data types. The most commonly used data types are:

Numeric Types: int for whole number, float for number with decimal, and complex for number with a real part and an imaginary part

Sequence Types: str for text, list for an ordered, mutable sequence of elements, and tuple for an ordered, immutable sequence of elements.

Set Types: set is used for unordered collection of unique elements.

Mapping Type: dict for collection of key-value pairs

Boolean Type: bool for 'True' or 'False'

None Type: None for the absence of a value or a null value

Declaring variable of different data types in Python

int: Declare a variable name my_integer and assign a whole number '99' using assign operator'=' as shown in the following example.

Example of int data type


Output of the above example

    99 is an integer number

float: Declare a variable name my_float and assign a number with decimal point '23.59' using assign operator'=' as shown in the following example.

Example of float data type


Output of the above example

    23.59 is a float number

complex: Declare a variable name my_complex and assign a complex number with real part (5) and imaginary part (2j) '5 + 2j' using assign operator'=' as shown in the following example.

Example of complex data type


Output of the above example

    5 + 2j is a complex number

str: Declare a variable name my_string and assign text 'Welcome to Python' using assign operator'=' as shown in the following example.

Example of str data type


Output of the above example

    Welcome to Python is an example of string data.

list: Declare a variable name fruit_list and assign ["Apple", "Banana", "Orange"] using assign operator'=' as shown in the following example.

Example of list data type


Output of the above example

    ['Apple', 'Banana', 'Orange'] is an example of list.

tuple: Declare a variable name my_tuple and assign (6, "Hello", 3.14, True) using assign operator'=' as shown in the following example.

Example of tuple data type


Output of the above example

    (6, 'Hello', 3.14, True) is an example of tuple.

set: Declare a variable name my_set and assign {7, 5, 4, 9} using assign operator'=' as shown in the following example.

Example of set data type


Output of the above example

    {9, 4, 5, 7} is an example of set.

dict: Declare a variable name student and assign { "name": "John", "age": 30, "grade": "A+", "course": "Python" } using assign operator'=' as shown in the following example.

Example of dict data type


Output of the above example

    {'name': 'John', 'age': 30, 'grade': 'A+', 'course': 'Python'} is an example of dict.

bool: Declare two variable name 'x' and 'y' and assign value 'True' and 'False' respectively using assign operator'=' as shown in the following example.

Example of bool data type


Output of the above example

    True
    False

Live Code Playground

In the following Python code editor, you can practice the above code by declaring more variables of different data types. Click on the 'Execute Code' button to execute the code; the executed output will be displayed in the following Python Code Output frame. Practice until you become comfortable and proficient with your code.

Python logo Python Online Editor
Python Code Output