Data
types: In Python there are following data types mainly
used,
- Numbers
- Strings
- Lists
- Tuples
- Dictionaries
1. Numbers:
· Number data type contains the numeric
value in it. Number data type created when you assign a numeric value to them.
· Python supports three types of numerical
data types:
o
Int
o
Float
o
Complex numbers
For
example:
Integer
declaration, var1 = 5
Float
declaration, var2 = 9.22
Complex
declaration, var3 = 3.14j
In Jupyter Notebook
type following to study the number data type. To get output for given statement
type Shift + Enter tab.
2. Strings:
· In Python string can be represented in
quotation mark with contiguous set of characters.
· For example:
str1 = ‘Hello World!!!’
print (str)
· In Jupyter Notebook type following to
study the string data type. To get output for given statement type Shift + Enter
tab.
3. Lists:
· In Python, Lists are most flexible data
types that contain items separated by commas and enclosed within the square
bracket like [ ].
· It looks like array but only difference
is to store the different data types together.
· For example:
list1 = ['dnyanesh', 11, 19.5,
'bhavesh']
list2 = [111, 'santosh']
print (list1) # It prints complete list
print (list1[0]) #
It prints 1st element of the list
print (list1[1:3]) # It prints elements starting from 2nd
to 3rd element
print (list1[2:]) # It prints elements starting from 3rd
element
print (list2 * 2) # It prints list two times
print (list1 + list2) # It prints concatenated lists
This
produce following result:
4. Tuples:
· In Python, tuple is look like similar to
the list. In tuple values are separated using commas and enclosed within the
parenthesis like ( ).
· The main distinction between lists and
tuples is- Lists are enclosed within square brackets [ ] and their values and
size can be changed, while tuples are enclosed within parenthesis like ( ) and
their values and size cannot be modified.
· Thus we can say that tuples are
read-only list data type.
· For example:
tuple1
= ('dnyanesh', 11, 19.5, 'bhavesh')
tuple2 = (111, 'santosh')
print (tuple1) # It prints complete tuple
print (tuple1[0]) #
It prints 1st element of the tuple
print (tuple1[1:3]) # It prints elements starting from 2nd to 3rd ele.
print (tuple1[2:]) # It prints elements starting from 3rd element
print (list2 * 2) #
It prints list two times
print (tuple1 + tuple2) # It prints concatenated lists
This produce following
result in Anaconda Jupyter tool:
In following ways
tuple shows error message when modification done at tuple value:
tuple1 = ('dnyanesh',
11, 19.5, 'bhavesh')
lis1t = ['dnyanesh', 11, 19.5, 'bhavesh']
tuple[2] = 10 # It is invalid syntax in tuples
list[2] = 10 # It is valid syntax with lists
lis1t = ['dnyanesh', 11, 19.5, 'bhavesh']
tuple[2] = 10 # It is invalid syntax in tuples
list[2] = 10 # It is valid syntax with lists
5.
Dictionaries:
·
In
Python, dictionaries are work like hash table or an associative array which
consists of key with value pairs.
·
Dictionaries
can store any python data type in it, but generally it use string and number.
·
The
vales are stored in dictionaries using curly bracket i.e. { } and accessed
using square bracket i.e. [ ].
·
For
example:
d = {'key1':'item1','key2':'item2'}
d['key1']
This produce following
result in Anaconda Jupyter tool:
No comments:
Post a Comment