9120122, 859 PM
In [1]:
In [2]:
out(2]
In [3]:
out[3]
In [4]:
FOP Day 2- Jupyter Notebook
# Standard Data Types
The data stored in memory can be of many types. For example, a person's age is
stored
as a numeric value and his or her address is stored as alphanumeric
characters.
Python has various standard data types that are used to define the operations
possible on them
and the storage method for each of then.
Python has five standard data types-
Numbers
string
List
Tuple
Dictionary
‘Python Numbers
var2
10
# Python Lists
A list contains items separated by commas and enclosed within square brackets
(i).
To sone extent, lists are similar to arrays in C.
One of the differences between them is that all the items belonging to a list
can be of different data type.
The values stored in a list can be accessed using the slice operator ({_] and
) with
indexes starting at @ in the beginning of the list and working their way to
end -1.
The plus (+) sign is the list concatenation operator, and the asterisk (*) is
the repetition operator.
list = [ ‘abed", 786 , 2.23, ‘john’, 70.2 ]
tinylist = [123, ‘john"]
localhost 8889inotebooks/FDP Day 2ipynbt9120122, 859 PM
In [5]:
out [5]:
In [6]:
out[6]
In [7]:
In [8]:
In (]:
In [10]:
In [11]:
In [12]:
In [13]:
FOP Day 2- Jupyter Notebook
list # Prints complete List
['abed", 786, 2.23, ‘john’, 70.2]
tinylist
[123, john")
print (list[@]) # Prints first element of the List
abed
print (list[1:3]) # Prints elements starting from 2nd till 3rd
[786, 2.23]
# print (List[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints List two times
[123, "john", 123, ‘john']
print (list + tinylist) # Prints concatenated Lists
['abed', 786, 2.23, ‘john’, 78.2, 123, ‘john']
# Python Tuples
A tuple is another sequence data type that is similar to the list.
A tuple consists of a number of values separated by commas.
Unlike lists, however, tuples are enclosed within parenthesis.
The main difference between lists and tuples is- Lists are enclosed in
brackets ( [_] ) and
their elements and size can be changed, while tuples are enclosed in
parentheses ( ( ) )
and cannot be updated.
Tuples can be thought of as read-only lists.
tuple = ( ‘abed", 786 , 2.23,
tinytuple = (123, ‘john")
john", 78.2 )
print(tuple) # Prints complete tuple
(abed', 786, 2.23, ‘john’, 70.2)
localhost 8889inotebooks/FDP Day 2ipynbt9120122, 859 PM
In [14]:
out [14]:
In [15]:
In [16]:
In [17]:
In [18]:
In [19]:
In [20]:
In]:
FDP Day 2 -Jupyter Notebook
tinytuple
print (tuple[0]) # Prints first element of the tuple
abed
print (tuple[1:3]) # Prints elements starting from 2nd till 3rd
(786, 2.23)
print (tuple[2:]) # Prints elements starting from 3rd element
(2.23, "john", 70.2)
print (tinytuple * 2) # Prints tuple two times
(123, ‘John’, 123, "john*)
print (tuple + tinytuple) # Prints concatenated tuple
(‘abed", 786, 2.23, “John’, 70.2, 123, ‘john")
tuple[2] = 1000 # Invalid syntax with tuple
TypeError Traceback (nost recent call last)
in
=> 1 tuple[2] = 1000 # Invalid syntax with tuple
TypeError: ‘tuple’ object does not support item assignment
localhost 8889inotebooks/FDP Day 2.pynit
39