Python data type

Basic data types in Python are Integer, Float, Complex number, Boolean, String, List, Tuple, and Dictionary.

Python is one of the most preferred programming languages ​​because of the versatility, flexibility, and object-oriented features.

Python has wide spectrum of uses in

  • Data science
  • Scientific and mathematical computing
  • Web development
  • Finance and trading
  • System automation and administration
  • Computer graphics
  • Basic game development
  • Security and penetration testing
  • General and application-specific scripting
  • Mapping and geography (GIS software)

This tutorial takes you through the basics of python language, which will help you kick-start your python journey.

It is recommended that you follow the following steps to learn better.

Step 1:-download python distribution Anaconda from

and install.

Next:-Run Anaconda

Basic data types in Python

Next:-Launch Jupyter notebook.

Basic data types in Python

Create a new notebook for python 3

Jupyter notebook for python

Start coding in the cell, use Alt+Enter to execute

Basic data types in Python

You can copy, paste, and execute from examples I have shown below.


>>> 1
>>> 22
#type() function tells datatype
>>> type(1)
<class 'int'>


>>> 1.0
>>> 22.1
>>> type(22.4)
<class 'float'>

Arithmetic Operations

>>> 2*4
#mod/Modulo in python,it returns remainder
##mod operation to get remainder while division,percentage sign is used
>>> 5%2
>>> 2%2
>>> 5/2

We do not need to specify the variable type in python, the variable takes type based on the value assigned to it. A variable name should not start with a number and special characters.

>>> x=5
>>> y=10
>>> x+y
#starting variable name with a special character or #number will throw an error
File "<ipython-input-2-4ea1d34784a1>", line 1
SyntaxError: invalid syntax


Python does not have a character data type, Even one character is string.strings in Python are arrays of bytes representing Unicode characters.

Single quotes and double quotes both can be used to define string based on situation and requirement.

>>>'I love doing python'
'I love doing python'
>>>"I love doing python"
'I love doing python'
#now if you want to use a single quote like #don't#can't then you have to use double quote otherwise
#it will give an error
>>>'don't worry be happy'
File "<ipython-input-13-5ad6caae9a1e>", line 3
    'I can't go there'
SyntaxError: invalid syntax
#using double quote will solve the issue
>>>"don't worry be happy"
"don't worry be happy"

Print() function

The print function display messages to screen or any other output device.Print() converts the object to string datatype before displaying.

#function to display output, result in python 
>>>print('Python is the best language')
Python is the best language

Print statements can be formatted to display output by integrating them with variables.

Curly brackets are used to format the print statements. Curly brackets are replaced by the variable value assigned in format function in an orderly fashion.

#how to use the print format in python
>>>print('My name is {} and I work in {}'.format('John','Tcs'))
My name is John and I work in Tcs

In place of John and Tcs, we could also use variables, it will replace {} with variables value sequentially.

For example in the present scenario first, curly braces will be replaced by the variable name and second will be replaced by the variable company.

>>>print('My name is {} and I work in {}'.format(name,company))
My name is John and I work in Tcs

In print format, curly brackets are replaced by variables inside format() in an orderly fashion.

In order to overcome this problem variables can be assigned inside curly brackets.

#name_copy and cmp_copy are just two variables
>>>print('My name is {name_copy} and I work in {cmp_copy}'.format(name_copy=name,
>>>print('My name is {name_copy} and I work in {cmp_copy} Michal also works in {cmp_copy} '.
#we can assign variables in any order and any number of times

Accessing characters in Python

The indexing and slicing method can be used to access characters in the string.

The Python string is a sequence made up of one or more individual characters that could be letters, numbers, whitespace characters, or symbols.

The indexing starts at 0.

#'+' can be used to concatenate
#using \n for line change while printing output
#using print formatting, we learned it before
>>>print("different letters in python is \n{}\n{}\n{}\n{}\n{}\n{}".format(name[0],name[1],
different letters in python are 
#we can also move backward using 
#negative indexing but it starts 
#with -1 rather than 0
#for example
#slicing string i.e displaying more than 
#one elements of string in python
#for example name[x:y] will display elements of #variable name from index x to y-1
#for example name[-x:-y] will display elements of #variable name from index -x to -y-1
#will display from index 0 to 2
#will display all elements
#will display all elements
#will display till index 4
#will display from index 2 to 4
#please try as many slicing as you can
#please try to answer the result of the 
#below statements without executing.
#general format of slicing is name(start, stop, step)
#start-index you want to start with
#stop-index -1 where you want to stop
#step- index you want to jump

print (name[0:6:2])

#this will print each 2nd index from index 0 to 5.

#steps can be forward or backward, positive no will take forward #steps and negative no will take backward steps

print (name[-1:0:-2])

List in python

The list is one of the four collection types in python others are Dictionary, Tuple, and Set.

It is mutable i.e its value can be changed, it can take any data type.

It is declared in a square bracket separated by a comma.

[1, 2, 3, 4]
['John', 'Smith', 'clark', 'david']
#indexing in list
#same as a string as we have learned before#examples
#will display values at index 0
#will display value at index 2
#slicing is also same as string
#will display elements of list_string from index 0 to 1
['John', 'Smith']
#as list is mutable/changeable therefore we can change the value of the element in the list
#smith will be replaced by johnson
['John', 'Smith', 'clark', 'david']
#using append function to add more elements to list
['John', 'Smith', 'clark', 'david']
#nested list is also possible
#for example
['john', 'clark', 'david', [2.0, 3.5, 6.9]]
[2.0, 3.5, 6.9]

Please find below all examples I have solved in Jupyter notebook👇👇👇

Please read the next article for other Basic data types in Python 👇👇👇. If you have any question please comment.

Next:-Dictionary,Tuple and Set in Python


IT Analyst at Tata Consultancy Services. Follow @aniluvall

4 thoughts on “Basic data types in Python”

Leave a Reply

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