0% found this document useful (0 votes)
18 views

Unit 2

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unit 2

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

UNIT – 2

LIST
List is a collection of ordered items.

Create a Python List

A list is created in Python by placing items inside [], separated by commas .

For example,

# A list with 3 integers

numbers = [1, 2, 5]

print(numbers)

Output: [1, 2, 5]

Here, we have created a list named numbers with 3 integer items.

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 = []

# list with mixed data types


my_list = [1, "Hello", 3.4]

Access Python List Elements

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,

languages = ["Python", "Swift", "C++"]

# access item at index 0

print(languages[0]) # Python

# access item at index 2

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.

Negative Indexing in Python

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

languages = ["Python", "Swift", "C++"]

# access item at index 0

print(languages[-1]) # C++

# access item at index 2

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,

# List slicing in Python

my_list = ['p','r','o','g','r','a','m','i','z']

# items from index 2 to index 4

print(my_list[2:5])

# items from index 5 to end

print(my_list[5:])

# items beginning to end

print(my_list[:])

Output

['o', 'g', 'r']


['a', 'm', 'i', 'z']
['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']

Here,

 my_list[2:5] returns a list with items from index 2 to index 4.


 my_list[5:] returns a list with items from index 5 to the end.
 my_list[:] returns all list items
Add Elements to a Python List

Python List provides different methods to add items to a list.

1. Using append()

The append() method adds an item at the end of the list.

For example,

numbers = [21, 34, 54, 12]

print("Before Append:", numbers)

# using append method

numbers.append(32)

print("After Append:", numbers)

Output

Before Append: [21, 34, 54, 12]


After Append: [21, 34, 54, 12, 32]

Here, append() adds 32 at the end of the array.

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)

# join two lists


prime_numbers.extend(even_numbers)

print("List after append:", prime_numbers)

Output

List1: [2, 3, 5]
List2: [4, 6, 8]
List after append: [2, 3, 5, 4, 6, 8]

Change List Items


Python lists are mutable. Meaning lists are changeable. And, we can change items of a list by
assigning new values using = operator. For example,

languages = ['Python', 'Swift', 'C++']

# changing the third item to 'C'


languages[2] = 'C'

print(languages) # ['Python', 'Swift', 'C']

Remove an Item From a List

1. Using del()

In Python, the del statement is used to remove one or more items from a list.

For example,

languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the second item


del languages[1]
print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the last item


del languages[-1]
print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust']

# delete first two items


del languages[0 : 2] # ['C', 'Java', 'Rust']
print(languages)

2. Using remove()

The remove() method is use to delete a list item.

For example,

languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']

# remove 'Python' from the list


languages.remove('Python')

print(languages) # ['Swift', 'C++', 'C', 'Java', 'Rust', 'R']

Python List Methods

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

insert() inserts an item at the specified index

remove() removes item present at the given index

pop() returns and removes item present at the given index

clear() removes all items from the list

index() returns the index of the first matched item

count() returns the count of the specified item in the list

sort() sort the list in ascending/descending order

reverse() reverses the item of the list

copy() returns the shallow copy of the list

Iterating through a List

The for loop is used to iterate over the elements of a list.

For example,

languages = ['Python', 'Swift', 'C++']

# iterating through the list


for language in languages:
print(language)

Output

Python
Swift
C++

Check if an Item Exists in the Python List

We use the in keyword to check if an item exists in the list or not. For example,

languages = ['Python', 'Swift', 'C++']

print('C' in languages) # False


print('Python' in languages) # True

Here,

 'C' is not present in languages, 'C' in languages evaluates to False.


 'Python' is present in languages, 'Python' in languages evaluates to True.

Python List Length

In Python, we use the len() function to find the number of elements present in a list. For
example,

languages = ['Python', 'Swift', 'C++']

print("List: ", languages)

print("Total Elements: ", len(languages)) #3

Output

List: ['Python', 'Swift', 'C++']


Total Elements: 3

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.).

# Different types of tuples

# Empty tuple
my_tuple = ()
print(my_tuple)

# Tuple having integers


my_tuple = (1, 2, 3)
print(my_tuple)
# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
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.

As mentioned earlier, we can also create tuples without using parentheses:

my_tuple = 1, 2, 3
my_tuple = 1, "Hello", 3.4

Create a Python Tuple With one Element

In Python, creating a tuple with one element is a bit tricky. Having one element within
parentheses is not enough.

We will need a trailing comma to indicate that it is a tuple,

var1 = ("Hello") # string


var2 = ("Hello",) # tuple

We can use the type() function to know which class a variable or a value belongs to.

var1 = ("hello")
print(type(var1)) # <class 'str'>

# Creating a tuple having one element


var2 = ("hello",)
print(type(var2)) # <class 'tuple'>

# 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'>

Access Python Tuple Elements

Like a list, each element of a tuple is represented by index numbers (0, 1, ...) where the first
element is at index 0.

We use the index number to access tuple elements. For example,

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.

# accessing tuple elements using indexing


letters = ("p", "r", "o", "g", "r", "a", "m", "i", "z")

print(letters[0]) # prints "p"


print(letters[5]) # prints "a"

In the above example,

 letters[0] - accesses the first element


 letters[5] - accesses the sixth element

2. Negative Indexing

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. For example,

# accessing tuple elements using negative indexing


letters = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(letters[-1]) # prints 'z'
print(letters[-3]) # prints 'm'

In the above example,

 letters[-1] - accesses last element


 letters[-3] - accesses third last element

3. Slicing

We can access a range of items in a tuple by using the slicing operator colon :.

# accessing tuple elements using slicing


my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

# elements 2nd to 4th index


print(my_tuple[1:4]) # prints ('r', 'o', 'g')
# elements beginning to 2nd
print(my_tuple[:-7]) # prints ('p', 'r')

# elements 8th to end


print(my_tuple[7:]) # prints ('i', 'z')

# elements beginning to end


print(my_tuple[:]) # Prints ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

Output

('r', 'o', 'g')


('p', 'r')
('i', 'z')
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

Here,

 my_tuple[1:4] returns a tuple with elements from index 1 to index 3.


 my_tuple[:-7] returns a tuple with elements from beginning to index 2.
 my_tuple[7:] returns a tuple with elements from index 7 to the end.
 my_tuple[:] returns all tuple items.

Python Tuple Methods

In Python ,methods that add items or remove items are not available with tuple. Only the
following two methods are available.

Some examples of Python tuple methods:

my_tuple = ('a', 'p', 'p', 'l', 'e',)


print(my_tuple.count('p')) # prints 2
print(my_tuple.index('l')) # prints 3

Here,

 my_tuple.count('p') - counts total number of 'p' in my_tuple


 my_tuple.index('l') - returns the first occurrence of 'l' in my_tuple

Iterating through a Tuple in Python

The for loop to iterate over the elements of a tuple. For example,

languages = ('Python', 'Swift', 'C++')

# iterating through the tuple


for language in languages:
print(language)

Output

Python
Swift
C++

Advantages of Tuple over List in Python

Since tuples are quite similar to lists, both of them are used in similar situations.

However, there are certain advantages of implementing a tuple over a list:

 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.

Create a Set in Python


In Python, we create sets by placing all the elements inside curly braces {}, separated by
comma.

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.

Let's see an example,

# create a set of integer type


student_id = {112, 114, 116, 118, 115}
print('Student ID:', student_id)

# create a set of string type


vowel_letters = {'a', 'e', 'i', 'o', 'u'}
print('Vowel Letters:', vowel_letters)

# create a set of mixed data types


mixed_set = {'Hello', 101, -2, 'Bye'}
print('Set of mixed data types:', mixed_set)

Output

Student ID: {112, 114, 115, 116, 118}


Vowel Letters: {'u', 'a', 'e', 'i', 'o'}
Set of mixed data types: {'Hello', 'Bye', 101, -2}

In the above example, we have created different types of sets by placing all the elements
inside the curly braces {}.

Create an Empty Set in Python

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,

# create an empty set


empty_set = set()

# create an empty dictionary


empty_dictionary = { }

# check data type of empty_set


print('Data type of empty_set:', type(empty_set))

# check data type of dictionary_set


print('Data type of empty_dictionary', type(empty_dictionary))
Output

Data type of empty_set: <class 'set'>


Data type of empty_dictionary <class 'dict'>

Here,

 empty_set - an empty set created using set()


 empty_dictionary - an empty dictionary created using {}

Finally we have used the type() function to know which class empty_set and
empty_dictionary belong to.

Duplicate Items in a Set

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.

Add and Update Set Items in Python

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.

Add Items to a Set in Python

In Python, we use the add() method to add an item to a set. For example,

numbers = {21, 34, 54, 12}

print('Initial Set:',numbers)

# using add() method


numbers.add(32)

print('Updated Set:', numbers)

Output

Initial Set: {34, 12, 21, 54}


Updated Set: {32, 34, 12, 21, 54}

In the above example, we have created a set named numbers. Notice the line,
numbers.add(32)

Here, add() adds 32 to our set.

Update Python Set

The update() method is used to update the set with items other collection types (lists, tuples,
sets, etc). For example,

companies = {'Lacoste', 'Ralph Lauren'}


tech_companies = ['apple', 'google', 'apple']

companies.update(tech_companies)

print(companies)

# Output: {'google', 'apple', 'Lacoste', 'Ralph Lauren'}

Here, all the unique elements of tech_companies are added to the companies set.

Remove an Element from a Set

We use the discard() method to remove the specified element from a set. For example,

languages = {'Swift', 'Java', 'Python'}

print('Initial Set:',languages)

# remove 'Java' from a set


removedValue = languages.discard('Java')

print('Set after remove():', languages)

Output

Initial Set: {'Python', 'Swift', 'Java'}


Set after remove(): {'Python', 'Swift'}

Here, we have used the discard() method to remove 'Java' from the languages set.

Built-in Functions with Set


Built-in functions like all(), any(), enumerate(), len(), max(), min(), sorted(), sum() etc. are
commonly used with sets to perform different tasks.

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.

len() Returns the length (the number of items) in the set.

max() Returns the largest item in the set.

min() Returns the smallest item in the set.

sorted() Returns a new sorted list from elements in the set(does not sort the set itself).

sum() Returns the sum of all elements in the set.

Iterate Over a Set in Python

fruits = {"Apple", "Peach", "Mango"}

# for loop to access each fruits


for fruit in fruits:
print(fruit)

Output

Mango
Peach
Apple

Find Number of Set Elements

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)

# find number of elements


print('Total Elements:', len(even_numbers))
Output

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 Operations

Python Set provides different built-in methods to perform mathematical set operations like
union, intersection, subtraction, and symmetric difference.

Union of Two Sets

The union of two sets A and B include all the elements of set A and B.

Set Union in Python

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}

# perform union operation using |


print('Union using |:', A | B)

# perform union operation using union()


print('Union using union():', A.union(B))

Output
Union using |: {0, 1, 2, 3, 4, 5}
Union using union(): {0, 1, 2, 3, 4, 5}

Note: A|B and union() is equivalent to A ⋃ B set operation.

Set Intersection

The intersection of two sets A and B include the common elements between set A and B.

Set Intersection in Python

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}

# perform intersection operation using &


print('Intersection using &:', A & B)

# perform intersection operation using intersection()


print('Intersection using intersection():', A.intersection(B))

Output

Intersection using &: {1, 3}


Intersection using intersection(): {1, 3}

Note: A&B and intersection() is equivalent to A ⋂ B set operation.


Difference between Two Sets

The difference between two sets A and B include elements of set A that are not present on set
B.

Set Difference in Python

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}

# perform difference operation using &


print('Difference using &:', A - B)

# perform difference operation using difference()


print('Difference using difference():', A.difference(B))

Output

Difference using &: {3, 5}


Difference using difference(): {3, 5}

Note: A - B and A.difference(B) is equivalent to A - B set operation.


Set Symmetric Difference

The symmetric difference between two sets A and B includes all elements of A and B without
the common elements.

Set Symmetric Difference in Python

In Python, we use the ^ operator or the symmetric_difference() method to perform symmetric


difference between two sets. For example,

# first set
A = {2, 3, 5}

# second set
B = {1, 2, 6}

# perform difference operation using &


print('using ^:', A ^ B)

# using symmetric_difference()
print('using symmetric_difference():', A.symmetric_difference(B))

Output

using ^: {1, 3, 5, 6}
using symmetric_difference(): {1, 3, 5, 6}

Check if two sets are equal

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}

# perform difference operation using &


if A == B:
print('Set A and Set B are equal')
else:
print('Set A and Set B are not equal')

Output

Set A and Set B are equal

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.

Other Python Set Methods

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

add() Adds an element to the set

clear() Removes all elements from the set

copy() Returns a copy of the set

difference() Returns the difference of two or more sets as a new set

difference_update() Removes all elements of another set from this set

Removes an element from the set if it is a member. (Do


discard()
nothing if the element is not in set)

intersection() Returns the intersection of two sets as a new set

intersection_update() Updates the set with the intersection of itself and another

isdisjoint() Returns True if two sets have a null intersection

issubset() Returns True if another set contains this set


issuperset() Returns True if this set contains another set

Removes and returns an arbitrary set element. Raises


pop()
KeyError if the set is empty

Removes an element from the set. If the element is not a


remove()
member, raises a KeyError

symmetric_difference() Returns the symmetric difference of two sets as a new set

Updates a set with the symmetric difference of itself and


symmetric_difference_update()
another

union() Returns the union of sets in a new set

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

Create a dictionary in Python


Here's how we can create a dictionary in Python.

capital_city = {"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}


print(capital_city)
Run Code

Output
{'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}

In the above example, we have created a dictionary named capital_city. Here,

Keys are "Nepal", "Italy", "England"

Values are "Kathmandu", "Rome", "London"

Here, keys and values both are of string type. We can also have keys and values of different
data types.

Example 1: Python Dictionary

# dictionary with keys and values of different data types


numbers = {1: "One", 2: "Two", 3: "Three"}
print(numbers)

Output

[3: "Three", 1: "One", 2: "Two"]

In the above example, we have created a dictionary named numbers. Here, keys are of
integer type and values are of string type.

Add Elements to a Python Dictionary


We can add elements to a dictionary using the name of the dictionary with []. For example,

capital_city = {"Nepal": "Kathmandu", "England": "London"}


print("Initial Dictionary: ",capital_city)

capital_city["Japan"] = "Tokyo"

print("Updated Dictionary: ",capital_city)

Output

Initial Dictionary: {'Nepal': 'Kathmandu', 'England': 'London'}


Updated Dictionary: {'Nepal': 'Kathmandu', 'England': 'London', 'Japan': 'Tokyo'}

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.

Change Value of Dictionary


We can also use [] to change the value associated with a particular key. For example,

student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}


print("Initial Dictionary: ", student_id)

student_id[112] = "Stan"

print("Updated Dictionary: ", student_id)

Output

Initial Dictionary: {111: 'Eric', 112: 'Kyle', 113: 'Butters'}


Updated Dictionary: {111: 'Eric', 112: 'Stan', 113: 'Butters'}

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,

student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}

print(student_id[111]) # prints Eric


print(student_id[113]) # prints Butters

Here, we have used the keys to access their corresponding values.


If we try to access the value of a key that doesn't exist, we'll get an error. For example,

student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}


print(student_id[211])

# Output: KeyError: 211


.
Removing elements from Dictionary
We use the del statement to remove an element from the dictionary. For example,

student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}

print("Initial Dictionary: ", student_id)

del student_id[111]

print("Updated Dictionary ", student_id)


Output

Initial Dictionary: {111: 'Eric', 112: 'Kyle', 113: 'Butters'}


Updated Dictionary {112: 'Kyle', 113: 'Butters'}

Here, we have created a dictionary named student_id.

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,

student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}

# delete student_id dictionary


del student_id

print(student_id)

# Output: NameError: name 'student_id' is not defined


We are getting an error message because we have deleted the student_id dictionary
and student_id doesn't exist anymore.

Python Dictionary Methods


Methods that are available with a dictionary are tabulated below. Some of them have already
been used in the above examples.

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.

len() Return the length (the number of items) in the dictionary.

sorted() Return a new sorted list of keys in the dictionary.

clear() Removes all items from the dictionary.

keys() Returns a new object of the dictionary's keys.

values() Returns a new object of the dictionary's values

Dictionary Membership Test

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

print(2 not in squares) # prints True

# membership tests for key only not value


print(49 in squares) # prints false

Output

True
True
False

Iterating Through a Dictionary

We can iterate through each key in a dictionary using a for loop.


# Iterating through a Dictionary
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
for i in squares:
print(squares[i])
Output

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,

# create a string using double quotes


string1 = "Python programming"

# create a string using single quotes


string1 = 'Python programming'

Here, we have created a string variable named string1. The variable is initialized with the
string Python Programming.
Example: Python String

# create string type variables

name = "Python"
print(name)

message = "I love Python."


print(message)

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.

Access String Characters in Python


We can access the characters in a string in three ways.
Indexing: One way is to treat strings as a list and use index values. For example,

greet = 'hello'

# access 1st index element


print(greet[1]) # "e"

Negative Indexing: Similar to a list, Python allows negative inexing for its strings. For
example,

greet = 'hello'

# access 4th last element


print(greet[-4]) # "e"
Slicing: Access a range of characters in a string by using the slicing operator colon :. For
example,

greet = 'Hello'

# access character from 1st index to 3rd index


print(greet[1:4]) # "ell"

If we try to access an index out of the range or use numbers other than an integer, we will get
errors.

Python Strings are immutable


In Python, strings are immutable. That means the characters of a string cannot be changed.
For example,

message = 'Hola Amigos'


message[0] = 'H'
print(message)

Output

TypeError: 'str' object does not support item assignment

However, we can assign the variable name to a new string. For example,

message = 'Hola Amigos'

# assign new string to message variable


message = 'Hello Friends'

prints(message); # prints "Hello Friends"

Python Multiline String


We can also create a multiline string in Python. For this, we use triple double quotes """ or
triple single quotes '''. For example,
# multiline string
message = """
Never gonna give you up
Never gonna let you down
"""

print(message)

Output

Never gonna give you up


Never gonna let you down

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.

1. Compare Two Strings


We use the == operator to compare two strings. If two strings are equal, the operator
returns True. Otherwise, it returns False. For example,

str1 = "Hello, world!"


str2 = "I love Python."
str3 = "Hello, world!"

# compare str1 and str2


print(str1 == str2)

# compare str1 and str3


print(str1 == str3)

Output

False
True
In the above example,
str1 and str2 are not equal. Hence, the result is False.

str1 and str3 are equal. Hence, the result is True.


2. Join Two or More Strings
In Python, we can join (concatenate) two or more strings using the + operator.

greet = "Hello, "


name = "Jack"

# using + operator
result = greet + name
print(result)

# Output: Hello, Jack

In the above example, we have used the + operator to join two strings: greet and name.

Iterate Through a Python String


We can iterate through a string using a for loop. For example,

greet = 'Hello'

# iterating through greet string


for letter in greet:
print(letter)

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'

# count length of greet string


print(len(greet))

# Output: 5

String Membership Test


We can test if a substring exists within a string or not, using the keyword in.

print('a' in 'program') # True


print('at' not in 'battle') False

Methods of Python String


Besides those mentioned above, there are various string methos present in Python. Here are
some of those methods:

Methods Description

upper() converts the string to uppercase

lower() converts the string to lowercase

partition() returns a tuple

replace() replaces substring inside

find() returns the index of first occurrence of substring


rstrip() removes trailing characters

split() splits string from left

startswith() checks if string starts with the specified string

isnumeric() checks numeric characters

index() returns index of substring

Escape Sequences in Python


The escape sequence is used to escape some of the characters present inside a string.
Suppose we need to include both double quote and single quote inside a string,

example = "He said, "What's there?""

print(example) # throws error

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.

To solve this issue, we use the escape character \ in Python.


# escape double quotes
example = "He said, \"What's there?\""

# escape single quotes


example = 'He said, "What\'s there?"'

print(example)

# Output: He said, "What's there?"

Here is a list of all the escape sequences supported by Python.


Escape Sequence Description
\\ Backslash

\' Single quote

\" Double quote

\a ASCII Bell

\b ASCII Backspace

\f ASCII Formfeed

\n ASCII Linefeed

\r ASCII Carriage Return

\t ASCII Horizontal Tab

\v ASCII Vertical Tab

\ooo Character with octal value ooo

\xHH Character with hexadecimal value HH

Python String Formatting (f-Strings)


Python f-Strings make it really easy to print values and variables. For example,

name = 'Cathy'
country = 'UK'

print(f'{name} is from {country}')


Output

Cathy is from UK

Here, f'{name} is from {country}' is an f-string.


This new formatting syntax is powerful and easy to use. From now on, we will use f-Strings
to print strings and variables.

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

Python has a module named re to work with RegEx. Here's an example:

import re

pattern = '^a...s$'

test_string = 'abyss'

result = re.match(pattern, test_string)

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.

Expression String Matched?

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

acde 2 matches (contains 4 characters)

^ - Caret
The caret symbol ^ is used to check if a string starts with a certain character.

Expression String Matched?

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.

Expression String Matched?

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.

Expression String Matched?

mn 1 match

man 1 match

ma*n maaan 1 match

main No match (a is not followed by n)

woman 1 match

+ - Plus
The plus symbol + matches one or more occurrences of the pattern left to it.

Expression String Matched?

ma+n mn No match (no a character)

man 1 match

maaan 1 match
Expression String Matched?

main No match (a is not followed by n)

woman 1 match

? - Question Mark
The question mark symbol ? matches zero or one occurrence of the pattern left to it.

Expression String Matched?

mn 1 match

man 1 match

ma?n maaan No match (more than one a character)

main No match (a is not followed by n)

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.

Expression String Matched?

a{2,3} abc dat No match

abc daat 1 match (at daat)

aabc daaat 2 matches (at aabc and daaat)


Expression String Matched?

aabc daaaat 2 matches (at aabc and daaaat)

Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits but not more
than 4 digits

Expression String Matched?

ab123csde 1 match (match at ab123csde)

[0-9]{2,4} 12 and 345673 3 matches (12, 3456, 73)

1 and 2 No match

| - Alternation
Vertical bar | is used for alternation (or operator).

Expression String Matched?

cde No match

a|b ade 1 match (match at ade)

acdbea 3 matches (at acdbea)

Here, a|b match any string that contains either a or b


() - Group
Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string that
matches either a or b or c followed by xz

Expression String Matched?

(a|b|c)xz ab xz No match
Expression String Matched?

abxz 1 match (match at abxz)

axz cabxz 2 matches (at axzbc cabxz)

\ - 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.

Expression String Matched?

the sun Match


\Athe
In the sun No match

\b - Matches if the specified characters are at the beginning or end of a word.

Expression String Matched?

\bfoo football Match

a football Match
Expression String Matched?

afootball No match

the foo Match

foo\b the afoo test Match

the afootest No match

\B - Opposite of \b. Matches if the specified characters are not at the beginning or end of a
word.

Expression String Matched?

football No match

\Bfoo a football No match

afootball Match

the foo No match

foo\B the afoo test No match

the afootest Match

\d - Matches any decimal digit. Equivalent to [0-9]

Expression String Matched?

\d 12abc3 3 matches (at 12abc3)


Expression String Matched?

Python No match

\D - Matches any non-decimal digit. Equivalent to [^0-9]

Expression String Matched?

1ab34"50 3 matches (at 1ab34"50)


\D
1345 No match

\s - Matches where a string contains any whitespace character. Equivalent to [ \t\n\r\f\v].

Expression String Matched?

Python RegEx 1 match


\s
PythonRegEx No match

\S - Matches where a string contains any non-whitespace character. Equivalent to [^ \t\n\r\f\


v].

Expression String Matched?

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.

Expression String Matched?

12&": ;c 3 matches (at 12&": ;c)


\w
%"> ! No match

\W - Matches any non-alphanumeric character. Equivalent to [^a-zA-Z0-9_]

Expression String Matched?

1a2%c 1 match (at 1a2%c)


\W
Python No match

\Z - Matches if the specified characters are at the end of a string.

Expression String Matched?

I like Python 1 match

Python\Z I like Python Programming No match

Python is fun. No match

.
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

string = 'hello 12 hi 89. Howdy 34'


pattern = '\d+'

result = re.findall(pattern, string)


print(result)

# Output: ['12', '89', '34']

If the pattern is not found, re.findall() returns an empty list.


re.split()
The re.split method splits the string where there is a match and returns a list of strings where
the splits have occurred.
import re
string = 'Twelve:12 Eighty nine:89.'
pattern = '\d+'
result = re.split(pattern, string)
print(result)

# Output: ['Twelve:', ' Eighty nine:', '.']


If the pattern is not found, re.split() returns a list containing the original string.
You can pass maxsplit argument to the re.split() method. It's the maximum number of splits
that will occur.
import re
string = 'Twelve:12 Eighty nine:89 Nine:9.'
pattern = '\d+'
# maxsplit = 1
# split only at the first occurrence
result = re.split(pattern, string, 1)
print(result)

# Output: ['Twelve:', ' Eighty nine:89 Nine:9.']


By the way, the default value of maxsplit is 0; meaning all possible splits.
re.sub()
The syntax of re.sub() is:
re.sub(pattern, replace, string)
The method returns a string where matched occurrences are replaced with the content
of replace variable.

# Program to remove all whitespaces


import re
# multiline string
string = 'abc 12\
de 23 \n f45 6'

# matches all whitespace characters


pattern = '\s+'
# empty string
replace = ''
new_string = re.sub(pattern, replace, string)
print(new_string)

# 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'

# matches all whitespace characters


pattern = '\s+'
replace = ''

new_string = re.sub(r'\s+', replace, string, 1)


print(new_string)

# 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.

# Program to remove all whitespaces


import re
# multiline string
string = 'abc 12\
de 23 \n f45 6'
# matches all whitespace characters
pattern = '\s+'
# empty string
replace = ''
new_string = re.subn(pattern, replace, string)
print(new_string)
# Output: ('abc12de23f456', 4)

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.

match = re.search(pattern, str)

import re

string = "Python is fun"


# check if 'Python' is at the beginning
match = re.search('\APython', string)
if match:
print("pattern found inside the string")
else:
print("pattern not found")

# Output: pattern found inside the string

Here, match contains a match object.


Match object
The group() method returns the part of the string where there is a match.

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')

match.start(), match.end() and match.span()

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'

Using r prefix before RegEx


When r or R prefix is used before a regular expression, it means raw string. For example, '\
n' is a new line whereas r'\n' means two characters: a backslash \ followed by n.
Backlash \ is used to escape various characters including all metacharacters. However,
using r prefix makes \ treat as a normal character.

Example 7: Raw string using r prefix

import re

string = '\n and \r are escape sequences.'

result = re.findall(r'[\n\r]', string)


print(result)

# Output: ['\n', '\r']

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.

# Python Module addition

def add(a, b):

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

The value of pi is 3.141592653589793

Python import with Renaming

In Python, we can also import a module by renaming it. For example,

# import module by renaming it


import math as m

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.

Python from...import statement

We can import specific names from a module without importing the module as a whole. For
example,

# import only pi from math module


from math import pi

print(pi)

# Output: 3.141592653589793

Here, we imported only the pi attribute from the math module.

Import all names

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 *

print("The value of pi is", pi)


Here, we have imported all the definitions from the math module. This includes all names
visible in our scope except those beginning with an underscore(private definitions).

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.

The dir() built-in function

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()

['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']

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.

Package Model Structure in Python Programming

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)

Import Without Package Prefix


If this construct seems lengthy, we can import the module without the package prefix as
follows:
from Game.Level import start

We can now call the function simply as follows:

start.select_difficulty(2)

Import Required Functionality Only


Another way of importing just the required function (or class or variable) from a module
within a package would be as follows:

from Game.Level.start import select_difficulty

Now we can directly call this function.

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.

You might also like