Basic data types in Python

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 its versatilityflexibility, and object-oriented features.

Python has a broad 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.

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 the examples I have shown below.


#In Jupyter notebook no need to write >>>
>>> 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 its value. 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 a string. Strings in Python are arrays of bytes representing Unicode characters.

To define a string, use single, double, and triple quotes 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"

Use the print () function to display a specific message to the screen or other standard output device. The print () converts the object to string datatype before printing.

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

There are different ways in which messages in python can be formatted. One such method is using curly braces. Use Curly brackets to format the print statements. The variable replaces Curly brackets by the value assigned to it.

#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, curly braces will replace the variable name and company in the present scenario.

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

You can assign variables inside curly brackets as well.

#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

To access characters in the string, use the indexing and slicing method. The Python string is a sequence 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., you can change elements of a list. A list 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 read the next article for other Basic data types in Python 👇👇👇. If you have any questions please comment.

Next:-Dictionary, Tuple, and Set in Python

3 thoughts on “Basic data types in Python”

Leave a Comment

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