100% found this document useful (1 vote)
135 views

Data Types and Data Structures

This document discusses data types, data structures, and common operations in Python. It covers the main data types like numbers, strings, and Booleans. Common data structures in Python include lists, tuples, dictionaries, sets, and frozensets. Lists are mutable while tuples and frozensets are immutable. The document provides examples of built-in list methods like append(), pop(), insert(), extend(), and more. It also covers indexing, slicing, and other list operations in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
135 views

Data Types and Data Structures

This document discusses data types, data structures, and common operations in Python. It covers the main data types like numbers, strings, and Booleans. Common data structures in Python include lists, tuples, dictionaries, sets, and frozensets. Lists are mutable while tuples and frozensets are immutable. The document provides examples of built-in list methods like append(), pop(), insert(), extend(), and more. It also covers indexing, slicing, and other list operations in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Data Types and Data Structures:

A data type, in programming, is a classification that specifies which type of value a variable
has. When working with data, we need ways to store it in variables so we can manipulate it.

Three of the most common data types used in programming: Numbers, Strings and
Booleans. We assigned those data types to variables one-by-one, like so:

x = 3 # numbers
a = "Python” # strings
t = True # Boolean

Datatypes available in python:

Numbers (integer, float and complex numbers)


Strings ( single quote(‘’), double quotes(“”), triple single quotes(‘’’ ’’’), triple double quotes.
Boolean (True, False)

Data Structures are efficient ways to store data, so that we can access and
manipulate data accordingly.

Data structures in python:


1. List
2. Tuple
3. Dictionary
4. Set
5. Frozenset

Everything in Python is an object. And all objects in Python can be either mutable or
immutable.

Mutable: An object that can be changed after it is created during run-time. It means you can
change their content without changing their identity. Mutable objects in python are list,
dictionary and set.

Immutable: An object that can’t be changed after it is created is called an immutable object.
Immutable objects in python are int, float, complex, string, tuple and frozen set.

1. Python handles mutable and immutable objects differently.


2. Immutable are quicker to access than mutable objects.
3. Mutable objects are great to use when you need to change the size of the object,
example list, dict etc.
4. Immutables are used when you need to ensure that the object you made will always
stay the same.
5. Immutable objects are fundamentally expensive to “change”, because doing so
involves creating a copy. Changing mutable objects is cheap.
Numbers (Immutable):
Numbers are one of the most prominent Python data types. Unlike many languages which
have only integers and floats, Python introduces complex as a new type of numbers.

The numbers in Python are classified using the following keywords.


int, float, and complex.

Integers and floating points are separated by the presence or absence of a decimal point. 5
is integer whereas 5.0 is a floating point number.
Complex numbers are written in the form, x + yj, where x is the real part and y is the
imaginary part.

a = 1  int
b = 10.5  float
c = 3+4j  complex

While integers can be of any length, a floating point number is accurate only up to 15
decimal places (the 16th place is inaccurate).
Numbers we deal with everyday are decimal (base 10) number system. But computer
programmers (generally embedded programmer) need to work with binary (base 2),
hexadecimal (base 16) and octal (base 8) number systems.
In Python, we can represent these numbers by appropriately placing a prefix before that
number. Following table lists these prefix

Number System Prefix


Binary '0b' or '0B'
Octal '0o' or '0O'
Hexadecimal '0x' or '0X'

print(0b1101011)  Output: 107


print(0xFB + 0b10)  Output: 253 (251 + 2)
print(0o15)  Output: 13

List (Mutable):
 List is one of the most frequently used and very versatile datatype used in Python.
 It’s an ordered collection of heterogeneous elements enclosed using square brackets
[ ].
 It is a mutable type means we can add or remove elements from the list.
 It maintains insertion order and are similar to arrays.
 List elements can be accessed using indexing, which starts from 0.

Creating List:
Creating a list is as simple as putting different comma-separated values in square brackets.
a = [1, 2, 3, 4]
b = ['a', 'b', 'c', 'd']
c = ['one', 'two', 'three', 'four']

Accessing List Elements:


Elements from a list can be accessed using indexing. Python indexes from 0 rather than 1.

l = [1, 2, 3, 4]
l[0]  1
l[3]  4
l[5]  Throws an index error.
d = [1, 2, 'three', 'four']
d[1]  2
d[2]  ‘three’

Python supports two types of indexing, positive and negative indexing.

Positive 0 1 2 3 4 5 6 7
index

Values 9 14 12 19 16 18 24 15

Negative -8 -7 -6 -5 -4 -3 -2 -1
index

Indexing:

a = [9, 14, 12, 19, 16, 18, 24, 15]


a[2]  12
a[5]  18
a[-1]  15
a[-4]  16
a[8]  throws error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range.

Slicing:
To retrieve a portion or part of data from a given sequence or list. A sub-list created by
specifying start/end indexes. Lists will traverse through forward direction in case of slicing
with positive step. Traverse through backward direction in case of negative step.
Syntax:

list[start: end] # end is exclusive


list[start:] # to end of list
list[:end] # from start of list to exclusive end.
list[start: end: step] # every step'th value and default step is 1.

Examples:
a[:5]  [9, 14, 12, 19, 16] # from the beginning of list till 5th index
(excluding 5th index)
a[2:]  [12, 19, 16, 18, 24, 15] # from 2nd index to till the end.
a[2:6]  It will print elements from indexes 2 to 6(excluding 6) [12, 19,
16, 18] default step 1.
a[2:8:2]  It will print elements from indexes 2 to 8(excluding 8) [19,
18, 15] with step 2.
a[-1:-5]  [ ] # returns an empty list.
a[-5:-2]  [19, 16, 18].

Update list elements:

a[3] = 10  [9, 14, 12, 10, 16, 18, 24, 15]. It will update 3rd index
value 19 to 10.
a[3:6] = [20, 30]  [9, 14, 12, 20, 30, 24, 15]

How to reverse a list using slicing:

a[::-1] # here step is -1 which will read a list from backward direction.

Basic List Operations:


Lists respond to the + and * operators much like strings; they mean concatenation and
repetition here too, except that the result is a new list, not a string.

In fact, lists respond to all of the general sequence operations we used on strings in the prior
chapter.
Python Expression Results Description

len([1, 2, 3]) 3 Length

[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation

[1, 2, 3] * 2 [1, 2, 3, 1, 2, 3] Repetition

3 in [1, 2, 3] True Membership

for x in [1,2,3] : print (x,end = ' ') 123 Iteration

List Methods:
dir(list) will display all the methods we can perform on list object.

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',


'__delslice__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__',
'__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',
'__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append',
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

The methods or attributes which start with __ (double underscore) and endswith __ (double
underscore) are called as magic methods.

Method Description

append(object) Appends the object at the end of the list

clear() Removes all the elements from the list keeping the structure.

copy() Creates another copy of list.

index(object) Returns the index value of the object.

count(object) It returns the number of times an object is repeated in list.

pop()/pop(index) Removes the last object or the specified indexed object.


It displays the popped object.

insert(index, Insert an object at the given index.


object)

extend(sequence) It adds the sequence to existing list.

remove(object) It removes the object from the given List.

reverse() Reverse the position of all the elements of a list.

sort() It is used to sort the elements of the List.

append:
Appends adds its argument as a single element to the end of a list.

a = [ ]
a.append(5)  [5]
a.append(‘hello’)  [5, ‘hello’]
a.append([1,2,3])  [5, ‘hello’, [1,2,3]]

clear:
clear() will remove all items from the list. But the structure remains same.
a = ['one', 'two', 'three', 'four', 'five']
a.clear()  [ ]

copy: It will create another copy of list.

a = [1, 10, 5, 7]
b = a.copy()  [1, 10, 5, 7]

index(object):

data = [10,'abc','python',123.5]
data.index('python')  2
data.index('abc')  1

count(object): It gives the no. of occurrences of a given element.

data = [10, 'abc', 'python', 123.5, 100, 'rahul', 'b', 100]


data.count('abc')  1
data.count(100)  2

pop()/pop(int):

data = [10,'abc','python',123.5,786]
data.pop()  786 # removes last element from list
data.pop(1)  'abc' # removes element from the specified index.

insert(index, object):

data = ['abc',123,10.5,'a']
data.insert(2,'hello')  ['abc', 123, 'hello', 10.5, 'a']

extend(sequence):

Extend method can extend only iterables(list, tuple, string, dict)


Numbers are not iterables.

data1 = ['abc',123,10.5,'a']
data2 = ['ram',541]
data1.extend(data2)  ['abc', 123, 10.5, 'a', 'ram', 541]

remove(object):
Removes an element from the list, if the element doesn’t exist it throws an error.

data = ['abc', 123, 10.5, 'a', 'xyz']


data.remove(123)  ['abc', 10.5, 'a', 'xyz']

data.remove(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

reverse():
list1 = [10,20,30,40,50]
list1.reverse()  [50, 40, 30, 20, 10]

sort():

In Python2 sort will work on both homogeneous and heterogeneous elements.


In Python3 sort will work on homogeneous elements.

list1 = [10, 30, 20, 90, 60]


list1.sort()  [10, 20, 30, 60, 90] # default sort order is ascending
order.
list1.sort(reverse=True)  [90, 60, 30, 20, 10] # sorts in descending
order.

Accessing nested lists:

a = [1, 10, 2, [20, 6, 7], 8]


a[3]  [20, 6, 7]
a[3][2]  7
a[3] = 20  [1, 10, 2, 20, 8]

Tuple (Immutable):
 It’s an ordered collection of heterogeneous elements enclosed using parentheses ( ).
 It is an immutable type means we can’t add or remove elements in tuple.
 Tuple elements can be accessed using indexing, which starts from 0.
 Default return type in python is tuple.

Tuple is similar to list. Only the difference is that list is enclosed between square bracket,
tuple between parenthesis and List is mutable type whereas Tuple is an immutable type.

t = ( ) # empty tuple
t = 1, 2, 3, 4  (1, 2, 3, 4) # default type is tuple.
t = (1,) # one element tuple.

NOTE: If Parenthesis is not given with a sequence, it is by default treated as Tuple.

Accessing Tuple:

Tuple can be accessed in the same way as list using indexing and slicing.

data1=(1, 2, 3, 4)
data2=('x', 'y', 'z')
data1[0]  1
data1[0:2]  (1, 2)
data2[-3:-1]  (‘x’, ‘y’)
data1[0:]  (1, 2, 3, 4)
data2[:2]  (‘x’, ‘y’)

Advantages of tuples over lists:


1. Processing of Tuples are faster than Lists.
2. It makes the data safe as Tuples are immutable and hence cannot be changed.
3. Tuples are used for String formatting.
4. Tuples are used whenever you want to return multiple results from a function.

We can’t modify elements in tuple using assignment, due to its immutable nature. Only two
methods we can perform on tuple which are count and index.

dir(tuple):

['__add__', '__class__', '__contains__', '__delattr__', '__doc__',


'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

Adding tuples:

t1 = (1, 2, 3)
t2 = (7, 8, 9)
t1 + t2  (1, 2, 3, 7, 8, 9)

Strings (Immutable):
1. Strings are sequences of characters.

2. Python strings are "immutable" which means they cannot be changed after they are
created.

3. To create a string, put the sequence of characters inside either single quotes, double
quotes, or triple quotes and then assign it to a variable.

s = 'Hello World!'  single quote strings


s = "Hello World!"  double quote strings
s = ‘’’ Welcome to Dreamwin Technologies’’’  triple single quotes
s = """Sunday  triple double quotes.
Monday
Tuesday"""

Access characters in a string:


To access characters from String, use indexing or slicing. Strings supports both positive and
negative indexing. String index starts from 0.
Indexing:
To retrieve a single character from a string.
s[0]  ‘P’
s[3]  ‘H’
s[-1]  ‘N’
s[-4]  ‘T’

Slicing:
To retrieve a portion or part of data from a given sequence or string.
There can be many forms to slice a string. As string can be accessed or indexed from both
the direction and hence string can also be sliced from both the direction that is left and right.

Syntax:

<string_name>[start:end: step]  It will exclude end index and the default


step is 1.
<string_name>[:endIndex]
<string_name>[startIndex:]

Note: startIndex in string slice is inclusive whereas endIndex is exclusive.

data ="dreamwin"
data[0:6]  ‘dreamw’
data[2:7]  ‘eamwi’
data[:5]  ‘dream’
data[2:]  ‘eamwin’
data[-1: -6]  ‘ ’ # returns an empty string
data[-6: -1]  ‘eamwi’
data[-1:]  ‘n’
data[: -5]  ‘dre’
data[2:8:2]  ‘emi’
data[:8:2]  ‘demi’

Modifying/Deleting Python Strings.

Python Strings are by design immutable. It suggests that once a String binds to a variable; it
can’t be modified. If you want to update the String simply re-assign a new String value to the
same variable.

sample_str = 'Python String'


sample_str[2] = 'a'
# TypeError: 'str' object does not support item assignment
sample_str = 'Programming String'
print (sample_str)  ‘Programming String’

Similarly, we cannot modify the Strings by deleting some characters from it. Instead, we can
remove the Strings altogether by using ‘del’ command.

sample_str = "Python is the best scripting language."


del sample_str[1]
# TypeError: 'str' object doesn't support item deletion
del sample_str
print (sample_str)
# NameError: name 'sample_str' is not defined

Python has several built-in methods associated with the string data type. These methods let
us easily modify and manipulate strings.
List down all string methods using dir(str). Below are the methods we can perform on string
object.
['__add__', '__class__', '__contains__', '__delattr__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',
'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum',
'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric',
'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower',
'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith',
'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

String Methods:
Capitalize: Converts the first character of a string into Upper case.

s = "welcome to dreamwin"
s.capitalize()  'Welcome to dreamwin'

Casefold(): Returns casefolded string. Converts Uppercase to lowercase.

s='DreamWin Technologies'
s.casefold()  'dreamwin technologies'

Center: Returns a new copy of the string after centering it in a field of length width

s = 'dreamwin'
s.center(20)  ' dreamwin '
s.center(20, '*')  '******dreamwin******'

Rjust: Returns a new copy of the string justified to left in field of length width.

s = "Dreamwin"
s.rjust(10)  ' Dreamwin'
s.rjust(5)  'Dreamwin'
s.rjust(12, '*')  '****Dreamwin'
Ljust: Returns a new copy of the string justified to left in field of length width.

s = "Dreamwin"
s.ljust(10)  'Dreamwin '
s.ljust(5)  'Dreamwin'
s.ljust(12, '*')  'Dreamwin****'

Endswith: endswith() method returns True if a string ends with the specified prefix(string). If
not, it returns False

s = 'dreamwin'
s.endswith('n')  True
s.endswith('h')  False
s.endswith('win')  True

Expand tabs:

s = 'xyz\t12345\tabc'
s.expandtabs()  'xyz 12345 abc'
s.expandtabs(2)  xyz 12345 abc
s.expandtabs(3)  xyz 12345 abc
s.expandtabs(4)  xyz 12345 abc
s.expandtabs(5)  xyz 12345 abc

Startswith: startswith() method returns True if a string starts with the specified prefix(string).
If not, it returns False.

s = "dreamwin tech"
s.startswith('dream')  True
s.startswith('tech')  False

Count: Counts the occurrences of a substring.

s = "Python is awesome"
s.count('s')  2
s.count('w')  1
s.count('on')  1
s.count('x')  0

Find: finds the substring and returns the first occurrences index if exists. If not find return -1.

s = 'Python is awesome'
s.find('is')  7
s.find('e')  12
s.find('x')  -1

Rfind: finds the substring from the right side and returns the first occurrences index if exists.
If not find returns -1.
s = 'Python is awesome'
s.rfind('is')  7
s.rfind('e')  16
s.rfind('x')  -1

Index: Returns the index of a given substring.

s = 'Python is awesome'
s.index('is')  7
s.index('e')  12
s.index('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

Rindex: Returns the index of a given substring from the right side occurrence.

s = 'Python is awesome'
s.rindex('is')  7
s.index('e')  16
s.index('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

Format: The format() reads the type of arguments passed to it and formats it according to
the format codes defined in the string.

format() method takes any number of parameters. But, is divided into two types of
parameters:

 Positional parameters - list of parameters that can be accessed with index of


parameter inside curly braces {index}
 Keyword parameters - list of parameters of type key=value, that can be accessed
with key of parameter inside curly braces {key}

"There are {} people in {} classroom".format(5, 'python')


'There are 5 people in python classroom'

"{1} and {0} are two courses in dreamwin technologies".format('RPA',


'Python')
'Python and RPA are two courses in dreamwin technologies'
"{a} and {b} are two courses in dreamwin technologies".format(a='RPA',
b='Python')
'RPA and Python are two courses in dreamwin technologies'

"{a} and {} are two courses in dreamwin technologies".format('Python',


a='RPA')
'RPA and Python are two courses in dreamwin technologies'

Join:
The join() method provides a flexible way to concatenate string. It concatenates each
element of an iterable (such as list, string and tuple) to the string and returns the
concatenated string.
The join() method returns a string concatenated with the elements of an iterable. If the
iterable contains any non-string values, it raises a TypeError exception.

Syntax:

String.join(iterable) # accepts only one iterable argument.

Examples:

l = ['1', '2', '3', '4']


','.join(l)  1,2,3,4
''.join(l)  1234
t = ('1', '2', '3', '4')
','.join(t)  1,2,3,4
''.join(t)  1234

s1 = 'abc'
s2 = '123'
""" Each character of s2 is concatenated to the front of s1"""
s1.join(s2))  1abc2abc3abc
""" Each character of s1 is concatenated to the front of s2"""
s2.join(s1))  a123b123c123

Split: splits the given string based on delimiter. Default delimiter in whitespace.

s = 'hello welcome to python'


s.split()  ['hello', 'welcome', 'to', 'python']
s.split(maxsplit=2)  ['hello', 'welcome', 'to python']
s = 'hello welcome, to, python'
s.split(',')  ['hello welcome', ' to', ' python']

Rsplit: rsplit() method splits string from the right at the specified separator and returns a list
of strings.

s = 'hello welcome to python'


s.rsplit()  ['hello', 'welcome', 'to', 'python']

s = 'hello welcome, to, python'


s.rsplit(',')  ['hello welcome', ' to', ' python']
s.split(':')  ['hello welcome to python']

Strip: strip() method returns a copy of the string with both leading and trailing characters
removed (based on the string argument passed).
Lstrip: lstrip() method returns a copy of the string with leading characters removed (based on
the string argument passed).
Rstrip: rstrip() method returns a copy of the string with trailing characters removed (based on
the string argument passed).
Strip methods by default removes white spaces in a string, if you don’t provide any
arguments.
s = " hello welcome to python "
s.strip()  'hello welcome to python'
s.rstrip()  ' hello welcome to python'
s.lstrip()  'hello welcome to python '
s.strip(',')  ' hello welcome to python '

s = "Python is awesome"
s.strip('Py')  'thon is awesome'

Dictionary: (Mutable Type)


1. A dictionary is an unordered collection of key-value pairs.
2. It’s a mutable type. We can add elements and remove elements from dictionary.
3. It’s a built-in mapping type in Python where keys map to values. These key-value
pairs provide an intuitive way to store data.
4. Only immutable types (numbers, strings, tuples) can be used as dictionary keys.
Values can be of any type.
5. It is also known as Associative arrays or hash tables.
Why Need A Dictionary?
The dictionary solves the problem of efficiently storing a large data set. Python has made the
dictionary object highly optimized for retrieving data.
Creating a Dictionary
1. Python syntax for creating dictionaries use curly or flower braces { } where each item
appears as a pair of keys and values.
2. The key and the value is separated by a colon (:). This pair is known as item.
3. Items are separated from each other by a comma (,). Different items are enclosed
within a curly brace and this forms Dictionary.
4. Keys must be unique in dictionary while values may not be.
General syntax of dictionary is as follows:

d = {'name': 'dreamwin', 'place': 'bangalore', 'started': 'Dec 2017'}


type(d)  <class 'dict'>
d  {'started': 'Dec 2017', 'name': 'dreamwin', 'place': 'bangalore'}

Adding items into dictionary during run-time:

dt = { }
dt[‘lang’] = ‘Python’
dt[‘year’] = 1990
dt[‘author’] = ‘Guido Van Rossum’
print(dt)  {'author': 'Guido Van Rossum', 'year': 1990, 'lang':
'Python'}

Accessing Dictionaries Elements With Keys:


Dictionaries act like a database. Here, we don’t use a number to get a particular index value
as we do with a list. Instead, we replace it with a key and then use the key to fetch its value.
Consider dictionary d = {'name': 'dreamwin', 'place': 'bangalore', 'started': 'Dec 2017'}
>>> d['name']  'dreamwin'
>>> d['place']  'bangalore'
>>> d['started']  'Dec 2017'
>>> d['course']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'course'

Modifying a Dictionary (Add/Update/Delete):

Dictionary are mutable. We can add new items or change the value of existing items using
assignment operator.
If the key is already present, value gets updated, else a new key: value pair is added to the
dictionary.

Add or update items into dictionary:

>>> c = {'name': 'dreamwin tech', 'place': 'bangalore', 'start': 2017}


>>> c['area'] = 'marathahalli'
>>> c  {'name': 'dreamwin tech', 'area': 'marathahalli', 'place':
'bangalore', 'start': 2017}
>>> c['course'] = ['python', 'datascience']
>>> c
{'course': ['python', 'datascience'], 'name': 'dreamwin tech', 'area':
'marathahalli', 'place': 'bangalore', 'start': 2017}
>>> c['course'] = 'django'
>>> c
{'course': 'django', 'name': 'dreamwin tech', 'area': 'marathahalli',
'place': 'bangalore', 'start': 2017}

Delete or remove elements from dictionary:

1. We can remove a particular item in a dictionary by using the method pop(). This method
removes as item with the provided key and returns the value.
2. The method, popitem() can be used to remove and return an arbitrary item (key, value)
form the dictionary. All the items can be removed at once using the clear() method.
3. We can also use the del keyword to remove individual items or the entire dictionary itself.

>>> squares = {1:1, 2:4, 3:9, 4:16, 5:25}


>>> squares.pop(2)  4
>>> squares  {1: 1, 3: 9, 4: 16, 5: 25}
>>> squares.popitem()  (1, 1)
>>> squares  {3: 9, 4: 16, 5: 25}
>>> del squares[5]
>>> squares  {3: 9, 4: 16}
>>> del squares
>>> squares
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'squares' is not defined

Dictionary methods:
Dictionary methods can be listed using dir(dict):
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear',
'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

Clear: The clear() method removes all items from the dictionary.

>>> d = {'started': 'Dec 2017', 'name': 'dreamwin', 10: 'ten', 'place':


'bangalore', 'courses': ['Python', 'Datascience', 'Django']}
>>> d.clear()
>>> d  {}

You can also remove all elements from the dictionary by assigning empty dictionary { }.
However, there is a difference between calling clear() and assigning { } if there is another
variable referencing the dictionary.

>>> d = {10: "ten", 2: "twenty"}


>>> d1 = d # creating another reference
>>> d.clear()
>>> d  { }
>>> d1  { }
>>> d = {10: "ten", 2: "twenty"}
>>> d1 = d
>>> d = {}
>>> d  { }
>>> d1  {10: 'ten', 2: 'twenty'}

Copy: They copy() method returns a shallow copy of the dictionary. It doesn't modify the
original dictionary. It creates a new dictionary.

>>> d = {10: "ten", 20: "twenty", 30: 'thirty'}


>>> d1 = d.copy()
>>> d1  {10: 'ten', 20: 'twenty', 30: 'thirty'}

Fromkeys: The fromkeys() method creates a new dictionary from the given sequence of
elements with a value provided by the user.

>>> {}.fromkeys('python')
{'h': None, 't': None, 'p': None, 'o': None, 'n': None, 'y': None}
>>> {}.fromkeys('python', 10)
{'h': 10, 't': 10, 'p': 10, 'o': 10, 'n': 10, 'y': 10}

Get:
The get() method takes maximum of two parameters:
key - key to be searched in the dictionary
value (optional) - Value to be returned if the key is not found. The default value is None.
The get() method returns:
the value for the specified key if key is in dictionary.
None if the key is not found and value is not specified.
value if the key is not found and value is specified.

>>> d = {10: 'ten', 20: 'twenty', 30: 'thirty'}


>>> d.get(10)  'ten'
>>> d.get(40)  None
>>> d.get(40, 'forty')  'forty'

Keys: The keys() returns a view object that displays a list of all the keys.

>>> d = {'started': 'Dec 2017', 'name': 'dreamwin', 10: 'ten', 'place':


'bangalore', 'courses': ['Python', 'Datascience', 'Django']}
>>> d.keys()  dict_keys(['started', 'name', 10, 'place', 'courses'])

Values: The values() method returns a view object that displays a list of all values in a given
dictionary.

>>> d = {'started': 'Dec 2017', 'name': 'dreamwin', 10: 'ten', 'place':


'bangalore', 'courses': ['Python', 'Datascience', 'Django']}
>>> d.values()
dict_values(['Dec 2017', 'dreamwin', 'ten', 'bangalore', ['Python',
'Datascience', 'Django']])

Items: The items() method returns a view object that displays a list of a given dictionary's
(key, value) tuple pair.

>>> d = {'started': 'Dec 2017', 'name': 'dreamwin', 10: 'ten', 'place':


'bangalore', 'courses': ['Python', 'Datascience', 'Django']}
>>> d.items()
dict_items([('started', 'Dec 2017'), ('name', 'dreamwin'), (10, 'ten'),
('place', 'bangalore'), ('courses', ['Python', 'Datascience', 'Django'])])

Popitem: The popitem() returns and removes an arbitrary element (key, value) pair from the
dictionary.

>>> d = {'name': 'virat', 'age': 22, 'salary': 35000}


>>> d.popitem()  ('age', 22)
>>> d  {'name': 'virat', 'salary': 35000}

Pop: The pop() method removes and returns an element from a dictionary having the given
key.

>>> d = {'name': 'virat', 'age': 22, 'salary': 35000}


>>> d.pop('age')  22
>>> d
{'name': 'virat', 'salary': 35000}

Setdefault: The setdefault() method returns the value of a key (if the key is in dictionary). If
not, it inserts key with a value to the dictionary.
The setdefault() takes maximum of two parameters:
1. key - key to be searched in the dictionary.
2. default_value (optional) - key with a value default_value is inserted to the dictionary if
key is not in the dictionary.
3. If not provided, the default_value will be None.
>>> c = {'name': 'dreamwin tech', 'place': 'bangalore', 'start': 2017}
>>> c.setdefault('course')
>>> c  {'course': None, 'name': 'dreamwin tech', 'place':
'bangalore', 'start': 2017}
>>> c.setdefault('area', 'marathahalli')  'marathahalli'
>>> c  {'course': None, 'name': 'dreamwin tech', 'area':
'marathahalli', 'place': 'bangalore', 'start': 2017}

Update: The update() method updates the dictionary with the elements from the another
dictionary object or from an iterable of key/value pairs.
The update() method takes either a dictionary or an iterable object of key/value pairs
(generally tuples).
If update() is called without passing parameters, the dictionary remains unchanged.

>>> c = {'name': 'dreamwin tech', 'place': 'bangalore', 'start': 2017}


>>> c.update({'area': 'marathahalli'})
>>> c
{'name': 'dreamwin tech', 'area': 'marathahalli', 'place': 'bangalore',
'start': 2017}
>>> c.update(course = 'python')
>>> c
{'course': 'python', 'name': 'dreamwin tech', 'area': 'marathahalli',
'place': 'bangalore', 'start': 2017}

Sets: (Mutable Type)


1. A set is an unordered collection of items. Every element is unique (no duplicates) and
must be immutable (which cannot be changed).

2. Its definition starts with enclosing braces { } having its items separated by commas
inside.

3. However, the set itself is mutable. We can add or remove items from it.

4. Sets can be used to perform mathematical set operations like union, intersection,
symmetric difference etc.

Why Need A Set?

The set type has a significant advantage over a list. It implements a highly optimized method
that checks whether the container hosts a specific element or not. The mechanism used
here is based on a data structure known as a hash table.

Creating A Set:

To create a set, call the built-in set() function with a sequence or any iterable object.

>>> s = set("Python data types")


>>> type(s)  <class 'set'>
>>> s  {'e', 'y', 't', 'o', ' ', 'd', 's', 'P', 'p', 'n', 'h', 'a'}
Another simpler way is to specify the elements enclosed in curly braces {}.

>>> another_set = {'red', 'green', 'black'}


>>> type(another_set)  <class 'set'>
>>> another_set  {'red', 'green', 'black'}

Note: Creating an empty set is a bit tricky. Empty curly braces { } will make an empty
dictionary in Python. To make a set without any elements we use the set() function without
any argument.

>>> a = { }
>>> type(a)  <class 'dict'>
>>> s = set()
>>> type(s)  <class 'set'>

Add / Update a set in Python:

Sets are mutable. But since they are unordered, indexing have no meaning.
We cannot access or change an element of set using indexing or slicing. Set does not
support it.
We can add single element using the add() method and multiple elements using the update()
method.
The update() method can take tuples, lists, strings or other sets as its argument. In all cases,
duplicates are avoided.

>>> s = {1, 2, 'dreamwin'} # creating a set


>>> s  {1, 2, 'dreamwin'}
>>> s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing

# using add we can add only one element at a time.


>>> s.add(5)
>>> s.add('python')
>>> s  {1, 2, 'dreamwin', 5, 'python'}

# add multiple elements using update.


>>> s.update([7, 8, 9])
>>> s  {1, 2, 5, 7, 8, 9, 'dreamwin', 'python'}
>>> s.update([100, 20], ('one', 'two'), {30, 40})
>>> s  {1, 2, 100, 5, 7, 8, 9, 40, 'dreamwin', 'one', 20, 'two',
'python', 30}

Remove elements from set:

A particular item can be removed from set using methods, discard() and remove().
The only difference between the two is that, while using discard() if the item does not exist in
the set, it remains unchanged. But remove() will raise an error in such condition.
>>> my_set = set('dreamwin tech')
>>> my_set  {'d', 'm', 'a', 'c', 'h', 't', 'w', 'e', ' ', 'i', 'r',
'n'}
>>> my_set.discard('h')
>>> my_set  {'d', 'm', 'a', 'c', 't', 'w', 'e', ' ', 'i', 'r', 'n'}
>>> my_set.remove('i')
>>> my_set  {'d', 'm', 'a', 'c', 't', 'w', 'e', ' ', 'r', 'n'}
>>> my_set.discard(10)  None
>>> my_set.remove('p')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'p'

Similarly, we can remove and return an item using the pop() method.
Set being unordered, there is no way of determining which item will be popped. It is
completely arbitrary.
We can also remove all items from a set using clear().

>>> my_set = set('dreamwin tech')


>>> my_set.pop()  'd'
>>> my_set.pop()  'm'
# clears elements from set
>>> my_set.clear()
>>> my_set  set() # empty set

List all set operations using dir(set).

['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',


'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__',
'__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__',
'__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__',
'__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__',
'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection',
'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove',
'symmetric_difference', 'symmetric_difference_update', 'union', 'update']

Python Set Operations:

Sets can be used to carry out mathematical set operations like union, intersection, difference
and symmetric difference. We can do this with operators or methods.

Let us consider the following two sets for the following operations.

>>> A = {1, 2, 3, 4, 5}
>>> B = {4, 5, 6, 7, 8}

Union: Union of A and B is a set of all elements from both sets.Union is performed using |
operator. Same can be accomplished using the method union().

# use union on set A


>>> A.union(B)
{1, 2, 3, 4, 5, 6, 7, 8}
>>> A | B
{1, 2, 3, 4, 5, 6, 7, 8}
# use union function on B
>>> B.union(A)
{1, 2, 3, 4, 5, 6, 7, 8}

Intersection:
Intersection of A and B is a set of elements that are common in both sets. Intersection is
performed using & operator. Same can be accomplished using the method intersection().

# use intersection function on A


>>> A.intersection(B)
{4, 5}
>>> A & B
{4, 5}
# use intersection function on B
>>> B.intersection(A)
{4, 5}

Difference:

Difference of A and B (A - B) is a set of elements that are only in A but not in B. Similarly, B -
A is a set of element in B but not in A.
Difference is performed using - operator. Same can be accomplished using the method
difference().

# use difference function on A


>>> A.difference(B)
{1, 2, 3}
>>> A – B
{1, 2, 3}
# use - operator on B
>>> B – A
{8, 6, 7}
# use difference function on B
>>> B.difference(A)
{8, 6, 7}

Symmetric Difference:

Symmetric Difference of A and B is a set of elements in both A and B except those that are
common in both.
Symmetric difference is performed using ^ operator. Same can be accomplished using the
method symmetric_difference().

# use symmetric_difference function on A


>>> A.symmetric_difference(B)
{1, 2, 3, 6, 7, 8}
>>> A ^ B
{1, 2, 3, 6, 7, 8}
# use symmetric_difference function on B
>>> B.symmetric_difference(A)
{1, 2, 3, 6, 7, 8}

Difference_update:
The difference_update() updates the set calling difference_update() method with the
difference of sets.
Symmetric_difference_update:
The symmetric_difference_update() method updates the set calling the
symmetric_difference_update() with the symmetric difference of sets.
isdisjoint(): Return True if two sets have a null intersection
issubset(): Return True if another set contains this set
issuperset(): Return True if this set contains another set

FrozenSet: (Immutable)
Frozenset is a new class that has the characteristics of a set, but its elements cannot be
changed once assigned. While tuples are immutable lists, frozensets are immutable sets.
Sets being mutable are unhashable, so they can't be used as dictionary keys. On the other
hand, frozensets are hashable and can be used as keys to a dictionary.
Frozensets can be created using the function frozenset().
This datatype supports methods like copy(), difference(), intersection(), isdisjoint(),
issubset(), issuperset(), symmetric_difference() and union(). Being immutable it does not
have method that add or remove elements.

Interview Questions:
Datatypes in Python?
1. Numbers (int, float, complex)
2. String
Data structures in python?
1. List
2. Tuple
3. Dictionary
4. Set
5. Frozenset

Mutable vs Immutable?
A mutable object can change its state or contents during run-time and immutable objects
cannot.

Mutable objects: list, dict, set, byte array


Immutable objects: int, float, complex, string, tuple, frozen set [note: immutable version of
set], bytes

1. Python handles mutable and immutable objects differently.


2. Immutable are quicker to access than mutable objects.
3. Mutable objects are great to use when you need to change the size of the object,
example list, dict etc.. Immutables are used when you need to ensure that the object
you made will always stay the same.
4. Immutable objects are fundamentally expensive to “change”, because doing so
involves creating a copy. Changing mutable objects is cheap.

List vs Tuple?
List is a collection of heterogeneous elements and enclosed using square brackets []. It’s a
mutable type.
Tuple is a collection of heterogeneous elements and enclosed using parentheses (). It’s an
immutable type.
When do you use list vs tuple?

List and Tuple are both ordered containers.


Tuple: If you want an ordered container of constant elements use tuple as tuples are
immutable objects.
List: If you want an ordered container of arbitrary data or elements use lists. Lists are
mutable objects.

The main difference between list and tuple is you can change the list but you cannot change
the tuple. Tuple can be used as keys in mapping where list is not.

What is used to represent Strings in Python? Is double quotes used for String representation
or single quotes used for String representation in Python?

Using Single Quotes (‘ ’)

You can specify strings using single quotes such as ‘Hello welcome to python’. All white
space i.e. spaces and tabs are preserved as-is.

Using Double Quotes (“ ”)

Strings in double quotes work exactly the same way as strings in single quotes.
Eg:
S = “What’s your name”

Using Triple Quotes ()

You can specify multi-line strings using triple quotes. You can use single quotes and double
quotes freely within the triple quotes. An example is
Triple single quotes are used to define multi-line strings or multi-line comments.

’’’This is a multi-line string.


This is the first line.
This is the second line.’’’

Triple double quotes are used to define doc strings.


What is slicing in Python?

Slicing in Python is a mechanism to select a range of items from Sequence types like
strings, list, tuple, etc.
Example of slicing:

>>> l=[1,2,3,4,5]
>>> l[1:3]
[2, 3]
>>> l[1:-2]
[2, 3]
>>> l[-3:-1] # negative indexes in slicing
[3, 4]

>>> s="Hello World"


>>> s[1:3]
'el'
>>> s[:-5]
'Hello '
>>> s[-5:]
'World'

What is a negative index in python?

Python list items can be accessed with positive or negative numbers (also known as index).
For instance our list is of size n, then for positive index 0 is the first index, 1 second, last
index will be n-1. For negative index, -n is the first index, -(n-1) second ... A negative index
accesses elements from the end of the list counting backwards.
An example to show negative index in python.

>>> a= [1, 2, 3]
>>> print a[-3]
1
>>> print a[-2]
2
>>> print a[-1]
3

What is the difference Between List And Tuple?

List Tuple
List objects are mutable objects Tuple objects are immutable Objects
Applying iterations on list objects takes Applying iterations on tuple Objects
longer time takes less time
If the frequent operation is insertion or If the frequent operation is retrieval of
deletion of the elements then it is the elements then it is recommended
recommended to use list to use tuple
Tuple can be used as a key for the
List can’t be used as a ‘key’ for the
dictionary if the tuple is storing only
dictionary
immutable elements

What is the Dictionary?


Dictionary objects can be created by using curly braces {} or by calling dictionary function
Dictionary objects are mutable objects
Dictionary represents key value base
Each key value pair of Dictionary is known as a item
Dictionary keys must be immutable
Dictionary values can be mutable or immutable
Duplicate keys are not allowed but values can be duplicate
Insertion order is not preserved
Heterogeneous keys and heterogeneous values are allowed.

d = {}
d[‘name’] = ‘Python’
d[‘year’] = 1990

What does this code output?

def f(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)
f(2)
f(3,[3,2,1])
f(3)
Answer
[0, 1]
[3, 2, 1, 0, 1, 4]
[0, 1, 0, 1, 4]
Explain set?
Set is an unordered collection of unique and immutable or hash able elements. It’s a mutable
type.
S = {}
s.add(10)  {10}

set is used to remove duplicates from an iterable. One can perform mathematical set
operations like union, intersection, difference..
Explain list methods append vs extend?
Append and extend are list methods, which are used to add elements at the end of the list.
Append: using append we can add any elements at the end of the list.
l=[]
l.append(10)  [10]
l.append(‘hello’)  [10, ‘hello’]
l.append([1, 2, 3])  [10, ‘hello’, [1, 2, 3]]
Extend: using extend we can add only iterables into a list. i.e except integers we can add
any type.
l.append(‘hell’)  [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
l.append([1,2,3])  [1, 2, 3]
Difference between sort and sorted?
Sort is a list method, which will does the in-place sorting. It means it changes the original list
object.
Sorted is a built-in function, upon sort it creates new object. It will not modify the original list
object.

Explain split() and join() function.


Python’s split() function helps to split a string into list of substrings.
join() function does exactly the opposite. Given a list of values you can make a string out of it
based on the given delimiter.
a = ‘hello welcome to python’
a.split()  [‘hello’, ‘welcome’, ‘to’, ‘python’]
‘ ’.join(a)  ‘hello welcome to python’

What is the difference between Xrange and Range?

range() and xrange() are two functions that could be used to iterate a certain number of
times in for loops in Python. In Python 3, there is no xrange , but the range function behaves
like xrange If you want to write code that will run on both Python 2 and Python 3, you should
use range().
range() – This returns a list of numbers created using range() function.
xrange() – This function returns the generator object that can be used to display numbers
only by looping. Only particular range is displayed on demand and hence called “lazy
evaluation“.
Both are implemented in different ways and have different characteristics associated with
them. The points of comparisons are:
 Return Type
 Memory
 Operation Usage
 Speed

range() returns – the list as return type.


xrange() returns – xrange() object.

You might also like