Getting Started with Python

Data Types in Python

Python Data Types

Python data types identify the type of data that a variable can store. Besides, a variable is basically an object or an element that we store in the memory. This variable stores different types of values. Hence, the data type decides the type of a value. Every value in python belongs to a different data type. Moreover, there are various types of data types in python.

python data types

Data Types in Python

These are as follows:

  • Number: int, float, complex
  • Boolean
  • Sequence: string, list, tuple
  • Set
  • None
  • Mapping: dictionary

Let us study them one by one in detail.

Python Number

We store numeric values in this data type. Furthermore, it has three different types as follows:

int

It stores all the integer numbers. Moreover, these numbers can be positive or negative. For example, 1, -99, 10, 25, etc.

float

This data type stores the real or floating type numbers. This means that it stores all the numbers having decimal values. Moreover, they can be negative or positive. For example, 4.0, -2.89, 3.45, etc.

complex

This data type stores complex type numbers. Moreover, such numbers have two parts, real and imaginary. For example, 2+4i, 6-8i, etc.

Also, we can use the ‘type()’ function to find out the type of a particular value or variable. For example,

                    
>>> num=10

>>> type (num)

<class 'int'>

>>> num=111.23

>>> type (num)

<class 'float'>

>>> num= 10 + i55

>>> type (num)

<class 'complex'>

                

Besides, variables that hold these simple data types can hold only a single value at a time. Therefore, they are not useful for storing things like months, names of employees in a company, etc. Hence, python consists of data types like lists, sets, tuples, etc for storing such values.

Python Boolean

Boolean is a sub-data type of number’s integer data type. Furthermore, we declare it using the keyword ‘bool’. Moreover, it has only two constant values which are ‘True’ or ‘False’. Besides, these values should start with a capital ‘T’ and a capital ‘F’ respectively, otherwise, the interpreter gives an error. Moreover, boolean True values are denoted by non-zero, non-null, or 1. Similarly, boolean False values are denoted by 0.

Python Sequence

It is a type that stores more than one item at a time. Therefore, we can say that it is a collection of items. Moreover, to access these items each item has a particular index number. There are three types of sequence data types namely, strings, lists, and tuples.

Python Pytho Strings

It is a group of characters. These characters can be digits, alphabets, or special symbols. Moreover, they can be with or without spaces. We can represent a string using single quotes, double quotes, or, triple quotes. Hence, these quotes mark the beginning and end of a string and are not a part of the string. Besides, numeric operations are not valid on a string. Examples of Strings are as follows:

                    

>>> str='hello'

>>> str2=''hello''

>>> str3='231'

Python Lists

It is a collection of different types of data. Besides, we can represent it using square brackets []. We sequentially store the items and separate them using commas. Examples are as follows:

                    

# creating a list

>>> list1 = [ 1, 33, 'hello', 45.3 ]

# printing list items

>>> print (list1)

[ 1, 33, 'hello', 45.3 ]

Python Tuples

It is also a sequence of characters separated by commas. Moreover, we declare a tuple using round brackets (). Besides, we cannot change a tuple once we create it. Examples are as follows:

                    

# creating a tuple

>>> tuple1 = ( 1, 33, 'hello', 45.3 )

# printing tuple items

>>> print (tuple1)

( 1, 33, 'hello', 45.3 )

Browse more Topics Under Getting Started with Python

Python Sets

This is also a collection of data items separated by commas, like the lists and tuples. But, the set is an unordered collection. Moreover, after creation, we cannot make changes to a set. Besides, it cannot contain duplicate values. We can represent a set using curly brackets {}. Examples are as follows:

                    

# creating a set

>>> set1 = { 1, 33, 'hello', 45.3 }

# printing set items

>>> print (set1)

{ 1, 33, 'hello', 45.3 }

# printing set with duplicate values

>>> set2 = { 1, 33, 1, 1, 2, 33 }

# printing tuple items, duplicate values are not printed

>>> print (set2)

{ 1, 33, 2 }

None

This is a special data type in python. Furthermore, it represents missing value in a situation or in other words, the absence of a value. Besides, it does not mean that the value is null, 0, or false. Moreover, it does not support any special operation. The example is as follows:

                    

>>> var = None

# printing var

>>> print (var)

None

Mapping

This is an unordered data type. There is one type of mapping data type in python that is dictionary.

Python Dictionaries

We can represent a python dictionary using curly brackets {}. Moreover, it is a collection of data items in the form of key-value pairs. We write s key-value pair using a colon: between them for separation. Besides, this data type provides faster access. We can access any data item by mentioning the key in square brackets []. Moreover, the ‘key’ is usually a string value and the ‘value’ part can be of any type. Examples of dictionary are as follows:

                    

# creating a dicitionary

>>> dict1 = { 'Name':'ana', 'Age':22, 'Gender':'Female }

# printing dicitionary items

>>> print (dict1)

{ 'Name':'ana', 'Age':22, 'Gender':'Female }

# printing a particular dicitionary item

>>> print (dict1['Gender'])

Female

FAQs on Python Data Types

Q1. What is a data type?

A1. Data types in python identify the type of data that a variable can store.

Q2. What is a variable in python?

A2. A variable is basically an object or an element that we store in the memory. This variable stores different types of values.

Q3. What are the different data types in python?

A3. Different data types in Python are as follows:

  • Number: int, float, complex
  • Boolean
  • Sequence: string, list, tuple
  • Set
  • None
  • Mapping: dictionary

Q4. State true or false.

‘We can change a value in tuple.’

A4. False, we cannot change a value in a tuple once we create it.

Q5. State the type representing int, float, and, complex.

A5. In python, the number data type represents int, float, and complex data types.

Q6. We use which brackets to declare the following data types:

a. lists

b. tuples

c. sets

d. dictionaries?

A6. The respective brackets denoting the following data types are as follows:

a. Square Brackets []

b. Round Brackets ()

c. Curly Brackets {}

d. Curly Brackets {}

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Browse

Getting Started with Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Browse

Getting Started with Python

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.