Basic data types in Python

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 https://www.anaconda.com/products/individual

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.

Number

>>> 1
1

>>> 22
22

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

Float

>>> 1.0
1.0
>>> 22.1
22.1

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

Arithmetic Operations

#addition
1+1
2

#multiplication

>>> 2*4
8

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

#division
>>> 5/2
2.5

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
15

#starting variable name with a special character or #number will throw an error

>>>$var='john'
File "<ipython-input-2-4ea1d34784a1>", line 1
    $var='john'
    ^
SyntaxError: invalid syntax

String

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.

>>>name='John'
>>>company='Tcs'

>>>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,
cmp_copy=company))

#or

>>>print('My name is {name_copy} and I work in {cmp_copy} Michal also works in {cmp_copy} '.
      format(cmp_copy=company,name_copy=name))

#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.

name='Python'

>>>print(name[0]+name[1]+name[2]+name[3]+
name[4]+name[5])

Python

#'+' 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],
name[2],name[3],name[4],name[5]))

different letters in python are 
P
y
t
h
o
n

#we can also move backward using 
#negative indexing but it starts 
#with -1 rather than 0
#for example
>>>name[-1]
'n'
>>>name[-2]
'o'

#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

>>>name[0:3]
#will display from index 0 to 2
'Pyt'
>>>name[:]
#will display all elements
'Python'
>>>name[0:]
#will display all elements
'Python'
>>>name[:5]
#will display till index 4
'Pytho'
>>>name[2:5]
#will display from index 2 to 4
'tho'

>>>name[-3:-1]
'ho'

#please try as many slicing as you can
#please try to answer the result of the 
#below statements without executing.

name[-1]
name[-5:-1]
name[:4]
name[3:]

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.

>>>list_num=[1,2,3,4]
>>>list_string=['John','Smith','clark','david']

>>>print(list_num)
>>>print(list_string)

[1, 2, 3, 4]
['John', 'Smith', 'clark', 'david']

#indexing in list
#same as a string as we have learned before#examples

>>>list_num[0]
#will display values at index 0
1

>>>list_string[2]
#will display value at index 2
'clark'

#slicing is also same as string
>>>list_string[0:2]
#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

>>>list_string[1]='johnson'
#smith will be replaced by johnson
['John', 'Smith', 'clark', 'david']

#using append function to add more elements to list
>>>list_string.append('smith')
>>>list_string
['John', 'Smith', 'clark', 'david']

#nested list is also possible
#for example

>>>nested_list=[1,2,3,['john','clark','david',[2.0,3.5,6.9]]]
>>>nested_list[3]
['john', 'clark', 'david', [2.0, 3.5, 6.9]]

>>>nested_list[3][3]
[2.0, 3.5, 6.9]

>>>nested_list[3][3][2]
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

4 thoughts on “Basic data types in Python

Leave a Reply

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