Data Types in Python | FACE Prep

As you know, variables are used to store values and they can store different types of values like integer values, float values, string values, etc. So, a data type is nothing but a text before the variable which defines the type of value stored in the variable.

Fortunately, since Python is a dynamically typed language, we need not define the type of the variable while declaring it. Rather, the interpreter will itself understand this based on what has been assigned to the variable. But whereas, in languages like C, C++, Java etc, we need to explicitly mention the data type while declaring a variable.

data types in python

How to Determine a Variable’s Data Type in Python 

As a programmer, if you are still curious to know the variable data type in Python, then you can use the type() inbuilt function to know this. The below code will make it clear for you.

a = 5
b = 'FACE Prep'
c = 96.23
d = 'A'
print (type(a))
print (type(b))
print (type(c))
print (type(d))
Output:
<type 'int'>
<type 'str'>
<type 'float'>
<type 'str'>

Also, let us now explore the different data types in Python.

Understanding Basic Data Types in Python

The standard data types available in Python are,

  1. Numbers
  2. List
  3. Tuple
  4. String
  5. Set
  6. Dictionary

Below is a brief introduction about each of these data types. However, you can also choose to explore in-depth to understand each of them better.

a) Numbers

Python supports 4 different numerical types and they are:

  • int (integers like 5, 9, 25, 894 etc)
  • long (long integers, which can be represented in octal and hexadecimal formats, like -0x19328L)
  • float (floating-point values like 8.72, 6.92, 10.00)
  • complex (complex numbers like 20 + 2j, 242j etc)

Syntax for Number Data Types in Python:

a = 10
print (type(a))

Output: <class ‘int’>

>> Know in detail about Numbers Data Type in Python

b) List

  • Lists are used to store values of different data types in sequential order. In simple, they are similar to arrays in C but unlike arrays, they store values of various data types
  • These values stored in a list are separated by commas , and enclosed within the square brackets []. So your list looks like this:  [1, ‘FACE Prep’, ‘Python’, 24]
  • Similar to an array, list index also starts from 0
  • There are several operators used to access or slice or modify data in a list.

Syntax for List Data Types in Python:

a = [1,'FACE Prep', 2.6, 2+7j]
print (type(a))

Output: <class ‘list’>

>> Know in detail about List Data Type in Python

c) Tuple

  • A Tuple is similar to that of a list. It also stores values of different data types.  However, the major difference is that we cannot change the size of the tuple once declared or change the value of items stored in a tuple. This means a tuple is immutable.
  • The values stored in the tuple are separated by commas , and enclosed within the round brackets ()
  • Tuples are used to protect data and they work faster than a list.

Syntax for Tuple Data Types in Python:

a = (2.36, 'Python FACE Prep', 9)
print(type(a))

Output: <class ‘tuple’>

Know in detail about Tuple Data Type in Python

d) String

  • A string is a collection of a sequence of characters
  • In python, we can use either single or double or triple quotes to represent a string
  • Operators like +, * are used with strings to perform string operations like addition, repetition etc.

Syntax for String Data Types in Python:

a = "I love Learning Python on FACE Prep"
print(type(a))

b = ‘I love Learning Python on FACE Prep’
print(type(b))

c = ”’I love Learning Python on FACE Prep”’
print(type(c))

Output: 
<class ‘str’>
<class ‘str’>
<class ‘str’>

>> Know in detail about String Data Type in Python

e) Set

  • A set is a collection of unique elements and hence a set eliminates duplicate values
  • The values in a set are separated by commas , and enclosed within the curly brackets {}

Syntax for Set Data Types in Python:

a = {5,2,3,1,4}
print(type(a))

Output: <class ‘set’>

Know in detail about Set Data Type in Python

f) Dictionary

  • Dictionary is a collection of unordered key-value pairs
  • It is used when we have to deal with a huge amount of data and to retrieve the data faster. However, we must know the key to retrieve the value.
  • Values are enclosed within curly brackets {} and each value is a pair in the form of key: value
  • Both key and value can be of any type
  • We can retrieve the value using the key, but we cannot do vice versa

Syntax for Dictionary Data Types in Python:

a = {34:'value','key':1}
print(type(a))

Output: <class ‘dict’>

Know in detail about Key Data Type in Python

Based on the type of input given by the user, the interpreter determines the data type of the variable and hence you need not worry about this. However, certain times, you might have to convert a variable from one data type to another and for this, you need to be aware of the concept of type conversion and type casting.