Basic data types in Python

Python data type
pythoncodingwithanish

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

Number

#In Jupyter notebook no need to write >>>
>>> 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 its value. 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 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.

>>>name='John'
>>>company='Tcs'
>>>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,
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

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

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:]
name='slicing'
#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])
>>>sii

#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])
>>>gii

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.

>>>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 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 *