Unit 2
Unit 2
LIST
List is a collection of ordered items.
For example,
numbers = [1, 2, 5]
print(numbers)
Output: [1, 2, 5]
A list can have any number of items and they may be of different types (integer, float, string,
etc.). For example,
# empty list
my_list = []
In Python, each item in a list is associated with a number. The number is known as a list
index.
We can access elements of an array using the index number (0, 1, 2 …).
For example,
print(languages[0]) # Python
print(languages[2]) # C++
In the above example, we have created a list named languages.
Here, we can see each list item is associated with the index number. And, we have used the
index number to access the items.
Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2
to the second last item and so on.
Example
print(languages[-1]) # C++
print(languages[-3]) # Python
Slicing of a Python List
In Python it is possible to access a section of items from the list using the slicing operator :,
not just a single item.
For example,
my_list = ['p','r','o','g','r','a','m','i','z']
print(my_list[2:5])
print(my_list[5:])
print(my_list[:])
Output
Here,
1. Using append()
For example,
numbers.append(32)
Output
2. Using extend()
The extend() method to add all items of one list to another. For example,
prime_numbers = [2, 3, 5]
print("List1:", prime_numbers)
even_numbers = [4, 6, 8]
print("List2:", even_numbers)
Output
List1: [2, 3, 5]
List2: [4, 6, 8]
List after append: [2, 3, 5, 4, 6, 8]
1. Using del()
In Python, the del statement is used to remove one or more items from a list.
For example,
2. Using remove()
For example,
Python has many useful list methods that makes it really easy to work with lists.
Method Description
append() add an item to the end of the list
extend() add items of lists and other iterables to the end of the list
For example,
Output
Python
Swift
C++
We use the in keyword to check if an item exists in the list or not. For example,
Here,
In Python, we use the len() function to find the number of elements present in a list. For
example,
Output
TUPLE
A tuple in Python is similar to a list. The difference between the two is that we cannot change
the elements of a tuple once it is assigned whereas we can change the elements of a list.
Creating a Tuple
A tuple is created by placing all the items (elements) inside parentheses (), separated by
commas. The parentheses are optional, however, it is a good practice to use them.
A tuple can have any number of items and they may be of different types (integer, float, list,
string, etc.).
# Empty tuple
my_tuple = ()
print(my_tuple)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
Run Code
Output
()
(1, 2, 3)
(1, 'Hello', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))
In the above example, we have created different types of tuples and stored different data
items inside them.
my_tuple = 1, 2, 3
my_tuple = 1, "Hello", 3.4
In Python, creating a tuple with one element is a bit tricky. Having one element within
parentheses is not enough.
We can use the type() function to know which class a variable or a value belongs to.
var1 = ("hello")
print(type(var1)) # <class 'str'>
# Parentheses is optional
var3 = "hello",
print(type(var3)) # <class 'tuple'>
Run Code
Here,
("hello") is a string so type() returns str as class of var1 i.e. <class 'str'>
("hello",) and "hello", both are tuples so type() returns tuple as class of var1 i.e. <class
'tuple'>
Like a list, each element of a tuple is represented by index numbers (0, 1, ...) where the first
element is at index 0.
1. Indexing
We can use the index operator [] to access an item in a tuple, where the index starts from 0.
So, a tuple having 6 elements will have indices from 0 to 5. Trying to access an index outside
of the tuple index range( 6,7,... in this example) will raise an IndexError.
The index must be an integer, so we cannot use float or other types. This will result in
TypeError.
Likewise, nested tuples are accessed using nested indexing, as shown in the example below.
2. Negative Indexing
The index of -1 refers to the last item, -2 to the second last item and so on. For example,
3. Slicing
We can access a range of items in a tuple by using the slicing operator colon :.
Output
Here,
In Python ,methods that add items or remove items are not available with tuple. Only the
following two methods are available.
Here,
The for loop to iterate over the elements of a tuple. For example,
Output
Python
Swift
C++
Since tuples are quite similar to lists, both of them are used in similar situations.
We generally use tuples for heterogeneous (different) data types and lists for
homogeneous (similar) data types.
Since tuples are immutable, iterating through a tuple is faster than with a list. So there
is a slight performance boost.
Tuples that contain immutable elements can be used as a key for a dictionary. With
lists, this is not possible.
If you have data that doesn't change, implementing it as tuple will guarantee that it
remains write-protected.
SETS
A set is a collection of unique data. That is, elements of a set cannot be duplicate.
A set can have any number of items and they may be of different types (integer, float, tuple,
string etc.). But a set cannot have mutable elements like list or dictionaries as its elements.
Output
In the above example, we have created different types of sets by placing all the elements
inside the curly braces {}.
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. For
example,
Here,
Finally we have used the type() function to know which class empty_set and
empty_dictionary belong to.
Let's see what will happen if we try to include duplicate items in a set.
numbers = {2, 4, 6, 6, 2, 8}
print(numbers) # {8, 2, 4, 6}
Here, we can see there are no duplicate items in the set as a set cannot contain duplicates.
Sets are mutable. However, since they are unordered, indexing has no meaning.
We cannot access or change an element of a set using indexing or slicing. Set data type does
not support it.
In Python, we use the add() method to add an item to a set. For example,
print('Initial Set:',numbers)
Output
In the above example, we have created a set named numbers. Notice the line,
numbers.add(32)
The update() method is used to update the set with items other collection types (lists, tuples,
sets, etc). For example,
companies.update(tech_companies)
print(companies)
Here, all the unique elements of tech_companies are added to the companies set.
We use the discard() method to remove the specified element from a set. For example,
print('Initial Set:',languages)
Output
Here, we have used the discard() method to remove 'Java' from the languages set.
Function Description
all() Returns True if all elements of the set are true (or if the set is empty).
any() Returns True if any element of the set is true. If the set is empty, returns False.
Returns an enumerate object. It contains the index and value for all the items of
enumerate()
the set as a pair.
sorted() Returns a new sorted list from elements in the set(does not sort the set itself).
Output
Mango
Peach
Apple
We can use the len() method to find the number of elements present in a Set. For example,
even_numbers = {2,4,6,8}
print('Set:',even_numbers)
Set: {8, 2, 4, 6}
Total Elements: 4
Here, we have used the len() method to find the number of elements present in a Set.
Python Set provides different built-in methods to perform mathematical set operations like
union, intersection, subtraction, and symmetric difference.
The union of two sets A and B include all the elements of set A and B.
We use the | operator or the union() method to perform the set union operation. For example,
# first set
A = {1, 3, 5}
# second set
B = {0, 2, 4}
Output
Union using |: {0, 1, 2, 3, 4, 5}
Union using union(): {0, 1, 2, 3, 4, 5}
Set Intersection
The intersection of two sets A and B include the common elements between set A and B.
In Python, we use the & operator or the intersection() method to perform the set intersection
operation. For example,
# first set
A = {1, 3, 5}
# second set
B = {1, 2, 3}
Output
The difference between two sets A and B include elements of set A that are not present on set
B.
We use the - operator or the difference() method to perform the difference between two sets.
For example,
# first set
A = {2, 3, 5}
# second set
B = {1, 2, 6}
Output
The symmetric difference between two sets A and B includes all elements of A and B without
the common elements.
# first set
A = {2, 3, 5}
# second set
B = {1, 2, 6}
# using symmetric_difference()
print('using symmetric_difference():', A.symmetric_difference(B))
Output
using ^: {1, 3, 5, 6}
using symmetric_difference(): {1, 3, 5, 6}
We can use the == operator to check whether two sets are equal or not. For example,
# first set
A = {1, 3, 5}
# second set
B = {3, 5, 1}
Output
In the above example, A and B have the same elements, so the condition
if A == B
evaluates to True. Hence, the statement print('Set A and Set B are equal') inside the if is
executed.
There are many set methods, some of which we have already used above. Here is a list of all
the methods that are available with the set objects:
Method Description
intersection_update() Updates the set with the intersection of itself and another
update() Updates the set with the union of itself and others
Python Dictionary
Python dictionary is an ordered collection (starting from Python 3.7) of items. It stores
elements in key/value pairs. Here, keys are unique identifiers that are associated with
each value.
Let's see an example,
If we want to store information about countries and their capitals, we can create a dictionary
with country names as keys and capitals as values.
Keys Values
Nepal Kathmandu
Italy Rome
England London
Output
{'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}
Here, keys and values both are of string type. We can also have keys and values of different
data types.
Output
In the above example, we have created a dictionary named numbers. Here, keys are of
integer type and values are of string type.
capital_city["Japan"] = "Tokyo"
Output
In the above example, we have created a dictionary named capital_city. Notice the line,
capital_city["Japan"] = "Tokyo"
Here, we have added a new element to capital_city with key: Japan and value: Tokyo.
student_id[112] = "Stan"
Output
In the above example, we have created a dictionary named student_id. Initially, the value
associated with the key 112 is "Kyle". Now, notice the line,
student_id[112] = "Stan"
Here, we have changed the value associated with the key 112 to "Stan".
Accessing Elements from Dictionary
In Python, we use the keys to access their corresponding values. For example,
del student_id[111]
del student_id[111]
The del statement removes the element associated with the key 111.
We can also delete the whole dictionary using the del statement,
print(student_id)
Function Description
Return True if all keys of the dictionary are True (or if the dictionary is
all()
empty).
Return True if any key of the dictionary is true. If the dictionary is empty,
any()
return False.
We can test if a key is in a dictionary or not using the keyword in. Notice that the
membership test is only for the keys and not for the values.
# Membership Test for Dictionary Keys
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
# Output: True
print(1 in squares) # prints True
Output
True
True
False
1
9
25
49
81
Here, we have iterated through each key in the squares dictionary using the for loop.
Python Strings
In computer programming, a string is a sequence of characters. For example, "hello" is a
string containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.
We use single quotes or double quotes to represent a string in Python. For example,
Here, we have created a string variable named string1. The variable is initialized with the
string Python Programming.
Example: Python String
name = "Python"
print(name)
Output
Python
I love Python.
In the above example, we have created string-type variables: name and message with
values "Python" and "I love Python" respectively.
Here, we have used double quotes to represent strings but we can use single quotes too.
greet = 'hello'
Negative Indexing: Similar to a list, Python allows negative inexing for its strings. For
example,
greet = 'hello'
greet = 'Hello'
If we try to access an index out of the range or use numbers other than an integer, we will get
errors.
Output
However, we can assign the variable name to a new string. For example,
print(message)
Output
In the above example, anything inside the enclosing triple-quotes is one multiline string.
Python String Operations
There are many operations that can be performed with strings which makes it one of the most
used data types in Python.
Output
False
True
In the above example,
str1 and str2 are not equal. Hence, the result is False.
# using + operator
result = greet + name
print(result)
In the above example, we have used the + operator to join two strings: greet and name.
greet = 'Hello'
Output
H
e
l
l
o
Python String Length
In Python, we use the len() method to find the length of a string. For example,
greet = 'Hello'
# Output: 5
Methods Description
Since strings are represented by single or double quotes, the compiler will treat "He said, " as
the string. Hence, the above code will cause an error.
print(example)
\a ASCII Bell
\b ASCII Backspace
\f ASCII Formfeed
\n ASCII Linefeed
name = 'Cathy'
country = 'UK'
Cathy is from UK
REGULAR EXPRESSION
A Regular Expression (RegEx) is a sequence of characters that defines a search pattern.
For example,
^a . . . s$
The pattern is: any five letter string starting with a and ending with s
import re
pattern = '^a...s$'
test_string = 'abyss'
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
Here, we used re.match() function to search pattern within the test_string. The method
returns a match object if the search is successful. If not, it returns None.
Specify Pattern Using RegEx
To specify regular expressions, metacharacters are used. In the above example, ^ and $ are
metacharacters.
MetaCharacters
Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's
a list of metacharacters:
[] . ^ $ * + ? {} () \ |
[] - Square brackets
Square brackets specifies a set of characters you wish to match.
a 1 match
ac 2 matches
[abc]
Hey Jude No match
abc de ca 5 matches
Here, [abc] will match if the string you are trying to match contains any of the a, b or c.
You can also specify a range of characters using - inside square brackets.
[a-e] is the same as [abcde].
[1-4] is the same as [1234].
[0-39] is the same as [01239].
You can complement (invert) the character set by using caret ^ symbol at the start of a
square-bracket.
[^abc] means any character except a or b or c.
[^0-9] means any non-digit character.
. - Period
A period matches any single character (except newline '\n').
Expression String Matched?
a No match
ac 1 match
..
acd 1 match
^ - Caret
The caret symbol ^ is used to check if a string starts with a certain character.
a 1 match
^a abc 1 match
bac No match
abc 1 match
^ab
acb No match (starts with a but not followed by b)
$ - Dollar
The dollar symbol $ is used to check if a string ends with a certain character.
a$ a 1 match
Expression String Matched?
formula 1 match
cab No match
* - Star
The star symbol * matches zero or more occurrences of the pattern left to it.
mn 1 match
man 1 match
woman 1 match
+ - Plus
The plus symbol + matches one or more occurrences of the pattern left to it.
man 1 match
maaan 1 match
Expression String Matched?
woman 1 match
? - Question Mark
The question mark symbol ? matches zero or one occurrence of the pattern left to it.
mn 1 match
man 1 match
woman 1 match
{} - Braces
Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left
to it.
Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits but not more
than 4 digits
1 and 2 No match
| - Alternation
Vertical bar | is used for alternation (or operator).
cde No match
(a|b|c)xz ab xz No match
Expression String Matched?
\ - Backslash
Backlash \ is used to escape various characters including all metacharacters. For example,
\$a match if a string contains $ followed by a. Here, $ is not interpreted by a RegEx engine in
a special way.
If you are unsure if a character has special meaning or not, you can put \ in front of it. This
makes sure the character is not treated in a special way.
Special Sequences
Special sequences make commonly used patterns easier to write. Here's a list of special
sequences:
\A - Matches if the specified characters are at the start of a string.
a football Match
Expression String Matched?
afootball No match
\B - Opposite of \b. Matches if the specified characters are not at the beginning or end of a
word.
football No match
afootball Match
Python No match
ab 2 matches (at a b)
\S
No match
\w - Matches any alphanumeric character (digits and alphabets). Equivalent to [a-zA-Z0-9_].
By the way, underscore _ is also considered an alphanumeric character.
.
Python RegEx
Python has a module named re to work with regular expressions. To use it, we need to import
the module.
import re
The module defines several functions and constants to work with RegEx.
re.findall()
The re.findall() method returns a list of strings containing all matches.
import re
# Output: abc12de23f456
If the pattern is not found, re.sub() returns the original string.
You can pass count as a fourth parameter to the re.sub() method. If omited, it results to 0.
This will replace all occurrences.
import re
# multiline string
string = 'abc 12\
de 23 \n f45 6'
# Output:
# abc12de 23
# f45 6
re.subn()
The re.subn() is similar to re.sub() except it returns a tuple of 2 items containing the new
string and the number of substitutions made.
re.search()
The re.search() method takes two arguments: a pattern and a string. The method looks for the
first location where the RegEx pattern produces a match with the string.
If the search is successful, re.search() returns a match object; if not, it returns None.
import re
import re
string = '39801 356, 2102 1111'
# Three digit number followed by space followed by two digit number
pattern = '(\d{3}) (\d{2})'
# match variable contains a Match object.
match = re.search(pattern, string)
if match:
print(match.group())
else:
print("pattern not found")
# Output: 801 35
Here, match variable contains a match object.
Our pattern (\d{3}) (\d{2}) has two subgroups (\d{3}) and (\d{2}). You can get the part of
the string of these parenthesized subgroups. Here's how:
>>> match.group(1)
'801'
>>> match.group(2)
'35'
>>> match.group(1, 2)
('801', '35')
>>> match.groups()
('801', '35')
The start() function returns the index of the start of the matched substring.
Similarly, end() returns the end index of the matched substring.
>>> match.start()
2
>>> match.end()
8
The span() function returns a tuple containing start and end index of the matched part.
>>> match.span()
(2, 8)
match.re and match.string
The re attribute of a matched object returns a regular expression object.
Similarly, string attribute returns the passed string.
>>> match.re
re.compile('(\\d{3}) (\\d{2})')
>>> match.string
'39801 356, 2102 1111'
import re
Modules
As our program grows bigger, it may contain many lines of code. Instead of putting
everything in a single file, we can use modules to separate codes in separate files as per their
functionality. This makes our code organized and easier to maintain.
Module is a file that contains code to perform a specific task. A module may contain
variables, functions, classes etc.
Custom Moules
Let us create a module. Type the following and save it as example.py.
result = a + b
return result
Here, we have defined a function add() inside a module named example. The function takes
in two numbers and returns their sum.
Import modules in Python
We can import the definitions inside a module to another module or the interactive interpreter
in Python.
We use the import keyword to do this. To import our previously defined module example,
we type the following in the Python prompt.
import example
This does not import the names of the functions defined in example directly in the current
symbol table. It only imports the module name example there.
Using the module name we can access the function using the dot . operator. For example:
example.add(4,5) # returns 9
Standard modules can be imported the same way as we import our user-defined modules.
Import Python Standard Library Modules
The Python standard library contains well over 200 modules. We can import a module
according to our needs.
Suppose we want to get the value of pi, first we import the math module and use math.pi. For
example,
# import standard math module
import math
# use math.pi to get value of pi
print("The value of pi is", math.pi)
Output
print(m.pi)
# Output: 3.141592653589793
Here, We have renamed the math module as m. This can save us typing time in some cases.
Note that the name math is not recognized in our scope. Hence, math.pi is invalid,
and m.pi is the correct implementation.
We can import specific names from a module without importing the module as a whole. For
example,
print(pi)
# Output: 3.141592653589793
In Python, we can import all names(definitions) from a module using the following construct:
# import all names from the standard module math
from math import *
Importing everything with the asterisk (*) symbol is not a good programming practice. This
can lead to duplicate definitions for an identifier. It also hampers the readability of our code.
In Python, we can use the dir() function to list all the function names in a module.
For example, earlier we have defined a function add() in the module example.
We can use dir in example module in the following way:
dir(example)
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__initializing__',
'__loader__',
'__name__',
'__package__',
'add']
Here, we can see a sorted list of names (along with add). All other names that begin with an
underscore are default Python attributes associated with the module (not user-defined).
For example, the __name__ attribute contains the name of the module.
import example
example.__name__
# Output: 'example'
All the names defined in our current namespace can be found out using the dir() function
without any arguments.
a=1
b = "hello"
import math
dir()
PACKAGE
A package is a container that contains various functions to perform specific tasks. For
example, the math package includes the sqrt() function to perform the square root of a
number.
While working on big projects, we have to deal with a large amount of code, and writing
everything together in the same file will make our code look messy. Instead, we can separate
our code into multiple files by keeping the related code together in packages.
Suppose we are developing a game. One possible organization of packages and modules
could be as shown in the figure below.
Game Package Model Structure
Importing module from a package
In Python, we can import modules from packages using the dot (.) operator.
For example, if we want to import the start module in the above example, it can be done as
follows:
import Game.Level.start
Now, if this module contains a function named select_difficulty(), we must use the full name
to reference it.
Game.Level.start.select_difficulty(2)
start.select_difficulty(2)
select_difficulty(2)
Although easier, this method is not recommended. Using the full namespace avoids confusion
and prevents two same identifier names from colliding.
While importing packages, Python looks in the list of directories defined in sys.path, similar
as for module search path.