# Python - Data Types

Python

Share In this tutorial we will learn about Python data types.

Following are the list of data types we use in Python.

Everything in Python is an object. So, data types are the classes and variables are the objects.

## The `type` function

We can know the type of a variable using the `type()` function.

Now, lets talk about the Python data types.

## Number

There are three types of numbers in Python and they are listed below.

## Integer Number

These are the numbers without any decimal part. Example of integer number: 1, 2, 3, -10, 0, etc.

In the following example we are creating a variable and assigning value 10.

``````# integer variable x
x = 10

# type of x
print("Type of x =", type(x), "Value of x =", x)
``````

The above code will print the following output.

``````Type of x = <class 'int'> Value of x = 10
``````

## Floating Point Number

These are the numbers with decimal part. Example: 3.14.

In the following example we are creating a variable and assigning floating point value 22/7.

``````# float variable x
x = 22/7

# type of x
print("Type of x =", type(x), "Value of x =", x)
``````

The above code will print the following output.

``````Type of x = <class 'float'> Value of x = 3.142857142857143
``````

## Complex Number

Complex numbers are written in the form, `x + yj`, where `x` represents the real part and `y` represents the imaginary part of the number.

In the following example we are creating a variable and assigning complex number 1 + 2j.

``````# complex variable x
x = 1 + 2j

# type of x
print("Type of x =", type(x), "Value of x =", x)
``````

The above code will print the following output.

``````Type of x = <class 'complex'> Value of x = (1+2j)
``````

## Boolean

Boolean data type can take two values `True` and `False`.

We use boolean to represent logical ideas. For example, the statement "Is it raining now?" can have two values "Yes" or "No" and we represent "Yes" or positive outcome using "True" and "No" or negative outcome using "False".

In the following example we are creating two boolean variables.

``````# boolean variables
isGameOver = True
isUserActive = False

# type
print("Type of isGameOver =", type(isGameOver), "Value of isGameOver =", isGameOver)
print("Type of isUserActive =", type(isUserActive), "Value of isUserActive =", isUserActive)
``````

We will get the following output for the above program.

``````Type of isGameOver = <class 'bool'> Value of isGameOver = True
Type of isUserActive = <class 'bool'> Value of isUserActive = False
``````

## String

A string is a sequence of unicode characters.

We use single quote `'` and double quotes `"` to represent a string value in Python.

For multiline string values we use triple quotes `'''`.

In the following example we are creating variables and assigning string values.

``````# string
str1 = 'Hello World'

str2 = "This is string."

str3 = '''This is a
multiline string.'''

# type
print("Type of str1 =", type(str1), "Value of str1 =", str1)
print("Type of str2 =", type(str2), "Value of str2 =", str2)
print("Type of str3 =", type(str3), "Value of str3 =", str3)
``````

We will get the following output.

``````Type of str1 = <class 'str'> Value of str1 = Hello World
Type of str2 = <class 'str'> Value of str2 = This is string.
Type of str3 = <class 'str'> Value of str3 = This is a
multiline string.
``````

## List

A list in Python is an ordered sequence of items. The items can be of multiple types and can repeat.

We create lists using square `[ ]` brackets and separate the list items using comma `,`.

In the following example we are creating a list of numbers.

``````# list
myList = [1, 3.14, 1+2j]

# type
print("Type of myList =", type(myList), "Value of myList =", myList)
``````

The above code will give us the following output.

``````Type of myList = <class 'list'> Value of myList = [1, 3.14, (1+2j)]
``````

## Tuple

Tuples are immutable ordered sequence of items similar to a List.

Once a tuple is created it can't be modified.

We use parenthesis `( )` to create tuple in Python and the items are separated by comma `,`.

``````# tuple
myTuple = ('Hello World', 1, 3.14, 10+20j)

# type
print("Type of myTuple =", type(myTuple), "Value of myTuple =", myTuple)
``````

The above code will give us the following output.

``````Type of myTuple = <class 'tuple'> Value of myTuple = ('Hello World', 1, 3.14, (10+20j))
``````

## Set

A set is an unordered unique collection of items.

We create a set in Python using curly `{ }` brackets and separate the items using comma `,`.

In the following example we are creating a set.

``````# set
mySet = {1, 3.14, 1-2j}

# type
print("Type of mySet =", type(mySet), "Value of mySet =", mySet)
``````

We will get the following output.

``````Type of mySet = <class 'set'> Value of mySet = {1, 3.14, (1-2j)}
``````

## Dictionary

A dictionary in Python is an unordered collection of key-value pairs.

We use curly `{ }` brackets to create dictionary and separate key from value using colon `:`. Each pair of the dictionary is separated by comma `,`.

In the following example we are creating a dictionary.

``````# dictionary
myDictionary = {
'name': 'Yusuf Shakeel',
'score': 9.1,
'isOnline': False
}

# type
print("Type of myDictionary =", type(myDictionary), "Value of myDictionary =", myDictionary)
``````

We will get the following output.

``````Type of myDictionary = <class 'dict'> Value of myDictionary = {'name': 'Yusuf Shakeel', 'score': 9.1, 'isOnline': False}
``````
Share