16 Python Strings Loops Merged

Download as pdf or txt
Download as pdf or txt
You are on page 1of 160

IT Automation

INFO8025
Conestoga College
IT AUTOMATION – INFO 8025
Topic 14 – Strings; Loops
Objectives
• String Manipulation
• Loops in Python
- while loop
- for loop
- range() function
• Hands-on Time
Strings are Arrays
• Strings are arrays, sequence of characters.
• To access elements of a string we use the zero-based index in square brackets

a = "Hello, World!"
print(a[1]) #accessing and printing out the element found at the 2 nd
position

Output: e

• To get the length of a string:


- len() function

- Example:
myText = "Hello, World!“
strLength = len(myText) # 13 would be saved to strLength variable
String Methods
There is a number of built-in methods that can be called with strings.

• strip() - removes any whitespace from the beginning and the end of a string.

a = " Hello, World! “


a = a.strip()
print(a)  #Output: Hello, World!
• lstrip() - removes any whitespace from the left side (beginning) of a string
• rstrip() - removes any whitespace from the right side (end) of a string

• lower() - returns a string in lower case.


a = " Hello, World! "
print( a.lower() )  #Output: hello, world!

• upper() - returns a string in upper case.


a = "Hello, World!"
print(a.upper()) #Output: HELLO, WORLD!
String Methods, cont’d
• replace() - replaces all occurrences of a substring found in a string, with another string
a = "Hello, World!"
print(a.replace(“W”, “J”)) #Output: Hello, Jorld!

• split() - splits the string into substrings (ie. multiple strings) if it finds instances of the separator:
a = "Hello, World!"
print(a.split(","))  # Output is a List object: ['Hello', ' World!’]

• All methods that return a string, return a new string object - ie. they do not change the original string.
a = "Hello, World!“
b = a.replace(“W”, “J”)
print(a) #The original string remained the same. Output: Hello, World!
print(b) #The new value; Output: Hello, Jorld!

- To retain the effects of a method we need to save its return value to a variable.
- Can use the original variable or create a new one. Will depend on whether we need to refer to the initial value
later on in code.
Additional String Methods
text = “python is Easy to Learn.“

• capitalize() Converts the first character of the first word to upper case and the rest of the characters to lower
text.capitalize() # returns “Python is easy to learn.”
• title() Converts the first character of each word to upper case
text.title() # returns “Python Is Easy To Learn.”
• startswith() Returns True if the string starts with the specified value
text.startswith(‘python is Easy’) # returns True
• endswith() Returns True if the string ends with the specified value
text.endswith(‘to Learn.’) # returns True
• count() Returns the number of times a specified value occurs in a string
text.count(‘y’) # returns 2
• find() Searches the string for a specified value and returns the position of where it was first found;
returns -1 if the value is not found in the string
text.find(‘y’) # returns 1
• rfind() Searches the string for a specified value and returns the last position of where it was found
text.rfind(‘y’) # returns 13
Additional String Methods, cont’d
text1 = “Python is easy 2 learn.“
• isspace() Returns True if all characters in the string are whitespaces
• isalpha() Returns True if all characters in the string are letters
• isalnum() Returns True if all characters in the string are alphanumeric

text1.isalnum() # returns False, because of the whitespaces and the dot

• isdecimal() Returns True if all characters in the string are decimals - supports only numbers 0-9
• isdigit() Returns True if all characters in the string are digits - supports numbers 0-9, subscripts, superscripts
• isnumeric() Returns True if all characters in the string are numeric - supports numbers 0-9, subscripts,
superscripts, fractions, Roman numerals
• None of the previous three methods will recognize a decimal number as a number. One way to check if a value is a
decimal number, is to find a decimal point (count()), get rid of it (replace() ), and then use one of the three methods
above:
text2 = 123.45
text2.isdecimal() # returns False
text2 = text2.replace(‘.’,’’)
text2.isdecimal() # returns True
Comparing Strings
• The two basic string comparison operators are == and !=
They work with strings the same way as with integer and float values:
- The == operator returns True if there is an exact match, otherwise False will be returned.
- The != operator returns True if there is no match and otherwise returns False.

• Comparing the string with == and != is case sensitive.


a = ‘hello’
b = ‘Hello’
if (a == b): # this will return False
# do something…

• To do a case-insensitive comparison, we can convert both strings to uppercase or to lowercase letters and then
compare them
if ( a.lower() == b.lower() ): # this will return True
# do something….
Checking String Content
• To check if a string is present in another string, we can use the keywords in and not in

• The value returned is True or False.

• Example:
myText = “Python is easy to learn.“

isFound = “eas" in myText #Checks if “eas” is contained in myText


print(isFound) #Output: True

isFound = “eas“ not in myText #Checks if “eas” is not contained in myText


print(isFound) #Output: False
String Concatenation & Escape Character
• To concatenate, or combine, two strings we can use the + operator
a = "Hello"
b = "World"
c = a + " " + b #Combines the two strings and also adds a space in between
print(c) #Output: Hello World

• The escape character \ (backslash) is used before double or single quotes that we want to include in a
string.

text = "Odessa – the so-called "Pearl of the Black Sea"." #this would result in an error.
text = "Odessa – the so-called \"Pearl of the Black Sea\"." #this would work.

print(text) #Output: Odessa – the so-called "Pearl of the Black Sea".


String concatenation, cont’d
• We use + operator to concatenate strings

• We cannot use + to concatenate strings and numbers in the same way:


age = 70
text = "I am " + age + “ years old” #this will result in an error
print(text)

• Option 1: Cast the number to string


age = 70
text = "I am " + str(age) + “ years old” #this will work
print(text)

• Option 2: use the format() method (see the next slide)


String format() method
• format() method takes in parameters we pass to it and places them in the string on which the method is called
wherever the placeholders {} in the string are:
age = 70
text = "I am {} years old." # The string we want to format; it has a {} – a placeholder
print( text.format(age) ) # calling the string format() method with the parameter
• The number of parameters with format() method is unlimited; they are each placed into its respective placeholder.
quantity = 2
itemNo = 123
itemPrice = 19.98

order = "I would like to order {} pieces of item {} at a price of ${} per item.“ # the string being
formatted
print( order.format(quantity, itemNo, itemPrice) )

• To be sure the arguments are placed in the correct placeholders, we have the option to use the zero-based index
referencing the position of the parameter to use with a placeholder:

order = "I would like to order {2} pieces of item {0} at a price of ${1} per item."
print( order.format(itemNo, itemPrice, quantity) )
Loops
• Python supports two types of loops: while and for
While Loop
• We use a while loop when we want to execute a set of statements as long as the specified condition is true.
- Example:

#Printing i as long as it is less than 6


i = 1 # i is initialized to the value of 1
while i < 6: # the condition
print(i) # this runs as long the condition specified evaluates to true
   i += 1 # incrementing i, or else the loop would continue forever
# the code in the loop will print out the values 1 through 5

• break statement – used to stop the loop - even while the condition still evaluates to true
i = 1
while i < 6:

print(i)
if i == 3:
     break # Exit the loop when i is 3; Only the numbers 1, 2 and 3 will be printed out.  

i += 1
While Loop
• continue statement - used to stop the current iteration, and continue with the next
# note that the increment logically needs to come before the continue statement
otherwise once i hits 3 the increment won't happen anymore because of the continue statement; i would continue to be 3
forever.
i = 0
while i < 6:
i += 1
if i == 3:
     continue # if i is 3 do nothing but continue to the next iteration;
   print(i) # will print out 1, 2, 4, 5

• else statement - used to run a block of code once more, after the condition stops evaluating to true
i = 1
while i < 6:
print(i) # will print out numbers 1 through 5
   i += 1
else:
   print("i is no longer less than 6") # Print the message once the condition evaluates to
false
For Loop
• Less like the For loop in PowerShell (and other languages) and more like foreach loop
• Used for iterating over a sequence of values (a list, a tuple, a dictionary, a set, or a string)
- It is used for executing one or more statements, once for each item
- We will start by looking at how it works with strings, as each string is an array (a sequence of characters)

• Example:
myText = “College”
for x in myText: # Looping through the letters in the word “College"
   print(x) # Printing each letter of the word - one letter per iteration

• break statement - used to stop the loop before it has looped through all the items
for x in “College":
if x == “l":
     break # Exit the loop when x is “l“; note that here the break comes before the print
   print(x)    # Printing the letter from the current itteration; only the letters ‘C’ and ‘o’ will be printed
For Loop
• continue statement – stops the current iteration of the loop, and continues with the next

for x in "College":
if x == "o":
     continue # Do not print o
   print(x) # printing the current letter
# all the letters but ‘o’ will be printed

• else statement - specifies a block of code to be executed once the loop is finished.
Note: If the loop is stopped with a break statement, the else does not run.

for x in “College": #Looping through the letters in the word “College"


   print(x) # printing the current letter; all letters will be printed
else:
print(“All done!”)
For Loop
Example: continue, break, else in a loop:

for x in "College": #Looping through the letters in the word “College"


if x == 'l':
continue # skip the current iteration
elif x == 'g':
break # breaks out of the loop; prevents the loop’s else from running

print(x) # printing the current letter; letters 'l', 'g', and 2nd 'e' will not be printed
else: # the loop’s else statement
print("All done!“) # Print the message once the loop went through all the elements.
# Will not run due to the break statement

- The output:
C
o
e
For loop with range() function
• The range() function generates a sequence of numbers
• By default, the sequence starts from 0, increments by 1, and ends at the specified number - 1.
numSequence = range(6) # will generate a sequence of numbers from 0 to 5

• range() function can be used in a for loop when we want to execute some code a specified number of times.

for x in range(6): # range(6) generates numbers 0 - 5


print(x) # printing numbers from 0 to 5, one number per each loop iteration

• We can specify a starting value other than 0, by adding a parameter in the 1st position: 
for x in range(2, 6): # range(2, 6) generates numbers 2 to 5
print(x) # printing numbers from 2 to 5, one number per each loop iteration

• We can specify an increment other than 0, by adding a parameter in the 3rd position: 
for x in range(2, 15, 3): # generated numbers start at 2 and go to the maximum of 14, with an increment of 3
print(x) # printing numbers 2, 5, 8, 11, 14, one number per each loop iterations
Troubleshooting your Code
• There are syntax and logical errors.
• After running your code check the PROBLEMS tab in the Terminal for a list of syntax errors.
• If your code runs without reporting any errors back to you, but still the results are not what you expect
them to be, check your code for possible logical errors.
• If you can’t figure out what is causing the issue in your script, take a step back: start checking the code
section by section.
Do this by commenting out some of the code and leaving only the sections you want to check
first, to see if they work. If they do, start adding back in some of the remaining code, by
uncommenting it. After confirming that that code works as expected, uncomment some
more code and see if it runs without issues. Continue the process until you are able to find
and fix the issue.
Questions?
References
• https://www.w3schools.com/python/python_strings.asp
• https://www.w3schools.com/python/python_while_loops.asp
• https://www.w3schools.com/python/python_for_loops.asp
IT Automation
INFO8025
Conestoga College
IT AUTOMATION – INFO 8025
Topic 17 – Lists & Tuples
Objectives
• Collection data types:
- Lists
- Tuples
• Hands-on Time
Collections
• Python has 4 built-in collection data types that are used for storing multiple items into a single variable:
- List
- Tuple
- Set
- Dictionary

• Each has different qualities and usage: ordered/unordered, changeable/unchangeable, allows/does not
allow duplicate elements.

- List is a collection which is ordered and changeable. Allows duplicate members.


- Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
- Set is a collection which is unordered and unindexed. No duplicate members.
- Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
Lists
• Lists are created using square brackets:
- Ex: myList = [] #initializing an empty list
- Ex: fruit = ["apple", "banana", "cherry"]

• List are ordered, indexed, changeable, and allow duplicate values.

• Ordered
- the items have a defined order, and that order does not change: if adding new items to a list,
they will be placed at the end
• Indexed
- First item has index [0], the second item has index [1], and so on.
• Changeable
- we can change, add, and remove items in a list after it has been created
• Allows Duplicates
- Since lists are indexed, lists can have items with the same value:
- Ex: fruit = ["apple", "banana", "cherry", "apple", "cherry"]
List Items Data Types
• List Items can be of any data type.
• Examples:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
list4 = ["abc", 34, True, 40, "male"]
List Length
• To determine how many items a list has, we use the len() function.
• Example:
fruitList = ["apple", "banana", "cherry"]
print(len(fruitList)) #Output: 3
Accessing List Items
• Accessing list items by using the zero-based index:
fruitList = ["apple", "banana", "cherry“, “lemon", “watermelon", "cherry"]
print(fruitList[1]) # Prints out the second item in the list
print(fruitList[-1]) # Negative indexing means start from the end; prints out the last item
print(fruitList[2:5]) # The range: will print out the items located at the position 3, 4, and 5. The
# search starts at index 2 (included) and ends at index 5 (not included).

- When specifying a range, the return value will be a new List containing the specified items.

• Checking if an item exists in a list:


- Use the in keyword - returns True or False
- Example:
fruitList = ["apple", "banana", "cherry"]
if "apple" in  fruitList :
   print("'apple' is in the fruit list")

• Can use the not in keyword to check if an item does not exist in a list.
Modifying Items in a List
• To modify a value of a list item, make use of the index number:

fruitList = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]

fruitList[1] = "blackcurrant“ # Changes “banana” to “blackcurrant”

fruitList[1:3] = ["blackcurrant", "watermelon"] # Using a range in the index – 1 included, 3 is


not;
# Changes "banana", "cherry“ to
# "blackcurrant", "watermelon”
Adding List Items
• To insert a new item we use the insert() method. It inserts an item at the specified, indexed, position.

fruitList = ["apple", "banana", "cherry"]
fruitList.insert(2, "watermelon") # inserts the item in the third position (after “banana”)

• To add a new item to the end of the list, we use the append() method.

frutiList.append("orange") # inserts the item at the end to the end


Joining Lists
• There are a couple ways to join two or more lists in Python.

• By using the + operator


nonTropical = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
fruit = nonTropical + tropical #List contains: [‘apple‘, ‘banana‘, ‘cherry‘,
‘mango‘, ‘pineapple‘, ‘papaya‘]

• By using the extend() method


fruit = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
fruit.extend(tropical)
print(fruit) # List contains:['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya’]

- extend() method can be used to add any other type of a collection to a list as well: a tuple, set, or a
dictionary. - extend() can join two objects only
Sorting a List
• The sort() method will sort a list.

- ascending is the default sorting order

fruit = ["orange", "mango", "kiwi", "pineapple", "banana"] # List with string values


fruit.sort()

numbers = [100, 50, 65, 82, 23] # List with numeric values


numbers.sort()

- To sort descending, use the parameter reverse = True

fruit = ["orange", "mango", "kiwi", "pineapple", "banana"]
fruit.sort(reverse = True)

• The reverse() method reverses the current order of the elements (regardless of the alphabet)
fruit = ["banana", "Orange", "Kiwi", "cherry"]
fruit.reverse() # will be reversed to ['cherry', 'Kiwi', 'Orange', 'banana']
Removing List Items
• remove() method removes an item by value.
fruitList = ["apple", "banana", "cherry“, “apple”]
fruitList.remove("apple") # using the value of the element; only the first occurrence is removed

• pop() method and del keyword both remove the item by index.
fruitList.pop(1) # Removes 2nd item
del fruitList[1] # Removes 2nd item
del fruitList[0:2] # Removes a range of elements: 1st and 2nd element

- Without an index, the pop() method removes the last item


fruitList.pop() # removes “apple” at the end

• The clear() method empties the list


fruitList.clear() # The List object itself is not deleted

• del keyword can also delete the list completely


del fruitList # Deletes the whole List object
Looping through a List
• We can process values saved in a List by using a for loop or a while loop.

• Ex. fruitList = ["apple", "banana", "cherry"]

- Using a for loop:

for x in fruitList:
print(x) # printing the List element from the current loop iteration

- Using a while loop:

i=0 # initializing the variable that will be used as a counter in the loop
while i < len(fruitList):
print(fruitList[i]) # using i as the List index, to reference the List element in the current
iteration
i+=1 # incrementing the loop counter by 1
Additional List Methods
• count() method return the number of times a specified value appears in the list

Ex:
fruit = [‘cherry’, 'apple', 'banana', 'cherry’]
x = fruit.count("cherry") # Returns the # of times “cherry” appears in fruit: 2

• index() method returns the position (index) of the first occurrence of the specified value

Ex:
fruit = ['apple', 'banana', 'cherry’, ‘lemon’, ‘cherry’]
x = fruit.index("cherry") # Returns the position of the 1st ‘cherry’ value: 2
Tuples
• A tuple is a collection which is ordered,  unchangeable, and allows duplicate values.
• In Python tuples are written with round brackets.
- Ex: fruit = ("apple", "banana", "cherry")

• Accessing items in a Tuple: by using the zero-based index


- Ex:
fruit = ("apple", "banana", "cherry")
print(fruit[1]) - prints out banana

- Negative indexing means starting from the end; index -1 references the last item
-Can specify a range of indexes by specifying where to start and where to end the range.
- When specifying a range, the return value will be a new tuple with the specified items
- Ex:
fruitTuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruitTuple[2:5]) # will start at index 2 (included) and end at index 5 (not included).
Tuple
• Checking if an item exists in a tuple:
- Use the in keyword; it returns True or False

fruitTuple = ("apple", "banana", "cherry“)
if "apple" in  fruitTuple :
   print("'apple' is in the fruit tuple")

• Can use the not in keyword to check if an item does not exist in a tuple.
Tuple Items Data Types
• Tuple Items can be of any data type.
• Examples:
tuple1 = ("apple", "banana", "cherry“)
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
tuple4 = ("abc", 34, True, 40, "male“)
Tuples are unchangeable (immutable)
• This means that we cannot change its existing values, add new values, remove values.
• A workaround: can convert a tuple into a list, change the list, and convert the list back into a tuple

x = ("apple", "banana", "cherry") # starting with a tuple

y = list(x) # converting the tuple to a list amd saving it to a new variable


y[1] = "kiwi“ # changing the list (the element at the 2nd position

x = tuple(y) # converting the list back to the tuple, and reassigning it to the original value.
Tuple Length
• To determine how many items a tuple has, we use the len() function.
• Example:
fruitTuple = ("apple", "banana", "cherry“)
print(len(fruitTuple)) #Output: 3
Joining Tuples
• To join two or more tuples we can use the + operator:

nonTropical = ("apple", "banana", "cherry“)
tropical = ("pineapple", "papaya“)
fruit = nonTropical + tropical # The tuple contains: (‘apple‘, ‘banana‘, ‘cherry‘,  ‘pineapple‘, ‘papaya‘)
Additional Tuple Methods
• count() method returns the number of times a specified value appears in the tuple

Ex:
fruit = (‘cherry’, 'apple', 'banana', 'cherry’)
x = fruit.count("cherry") # Returns the # of times “cherry” appears in fruit: 2

• index() method returns the position (index) of the first occurrence of an element with the specified value

Ex:
fruits = ('apple', 'banana', 'cherry’, lemon, ‘cherry’)
x = fruits.index("cherry") # Returns the position of the 1st ‘cherry’ value: 2
Looping through a Tuple
• To loop through a tuple we can use a for loop or a while loop.

• Ex. fruitTuple = ("apple", "banana", "cherry“)

- Using a for loop:

for x in fruitTuple: #iterates through the items and prints the values
print(x)

• Using a while loop:

i = 0

while i < len(fruitTuple):


print(fruitTuple[i])
i = i + 1
Lists vs. Tuples
• How do we decide which data type to use - a list or a tuple? – The answer will depend on the task at
hand.
• In general, lists are used more often, mostly because they are dynamic in nature, while tuples are
unchangeable and, therefore, fixed in size.
• Tuples are faster than lists when being processed in a loop.
• If you know that you are to work with a constant set of data, which you know you will not need to
change, you may opt to go with a tuple, for performance reasons.
String join() method
• The join() string method provides an easy way to join (concatenate) elements of an iterable object into one string.
• It works on strings, lists, tuples, sets, and dictionaries.
• It joins each element of an iterable by a string separator - the string on which the join() method is called, and returns
the concatenated string.
• For the method to work all elements of the iterable object need to be strings.

• Example using join() with a list:


numList = ['1', '2', '3', '4’]
separator = ‘, ‘
print(numList) #Output: ['1', '2', '3', '4’]
print(separator.join(numList)) #Output is: 1, 2, 3, 4 - as one string

• Example using join() with a tuple:


numTuple = ('1', '2', '3', ‘4’)
separator = ', ‘
print(separator.join(numTuple))
Questions?
References
• https://www.w3schools.com/python/python_lists.asp
• https://www.w3schools.com/python/python_lists_access.asp
• https://www.w3schools.com/python/python_lists_change.asp
• https://www.w3schools.com/python/python_lists_add.asp
• https://www.w3schools.com/python/python_lists_remove.asp
• https://www.w3schools.com/python/python_lists_loop.asp
• https://www.w3schools.com/python/python_lists_sort.asp
• https://www.w3schools.com/python/python_lists_copy.asp
• https://www.w3schools.com/python/python_lists_join.asp
• https://www.w3schools.com/python/python_lists_methods.asp
• https://www.w3schools.com/python/exercise.asp?filename=exercise_lists1
• https://www.programiz.com/python-programming/methods/string/join
• https://www.w3schools.com/python/ref_string_join.asp
IT Automation
INFO8025
Conestoga College
IT AUTOMATION – INFO 8025
Topic 18 – Functions
Objectives
• Functions
- Defining and calling a function in Python
- Function parameters
- Positional and keyword arguments
- Returning a value
• Variable Scope
• Using a flag variable
• The map() function
• Hands-on Time
Functions
• A function is a named block of code which only runs when it is called.
• When called, the function performs an action and either returns a value or not as its result
• Function can be defined to accept parameters, or not. We then use parameters to pass data to the
function.
• Benefits of using functions:
- code reuse:
- saves on coding time,
- avoids code duplication – which in turn helps with code maintenance, as the code is modified in
one place only: in the function
- breaking up a program into smaller units (functions) aids structural organization of the code
- makes the program easier to read, understand and maintain.
- easier debugging (unit by unit, logically)
Defining and Calling a Function
• A function is defined using the def keyword
def greeting():
   print(“Hello World!")

• Use a descriptive name when naming a function. We will also use camelCase for function names.

def addTwoNumbers():

• To call a function in your script, use the function name followed by parenthesis.

- For ex, to call the function defined above:


greeting() Output: Hello World!

• A function must be defined before it is called – if not, the code returns an error
Function Parameters (Arguments)
• Function parameters are used for passing data into functions.
• A parameter is the variable listed inside the parentheses in the function definition.
def addTwoNumbers(num1, num2): #the function accepts 2 parameters: num1 and num2

• “Parameter” vs “Argument”:
- The terms are used for the same thing: information that is passed to/accepted by a function.
- parameter is used when we talk about the function itself, to refer to the variable listed inside the
parentheses in the function definition
- argument refers to the value that is sent to the function when it is called.

• When calling a function, the arguments are specified after the function name, inside parentheses, and
are separated by a comma.

def greeting(firstName): #the function is defined to accept one parameter, firstName


   print(“Hello,” + firstName + “!") #the parameter is then used in the code inside the function

greeting(“Ali”) #Calling the function and passing to it an argument for firstName


#Output: Hello, Ali!
Function Parameters (Arguments)
• Function parameters can be of any data type (eg. string, number, Boolean, list, dictionary etc.)

• Example - a function accepting a List parameter:

def listFood (fruit): #declaring a function that accepts one parameter, fruit


  
for aFruit in fruit: #looping through the values in fruit variable (ie. the parameter)
     print(aFruit)

# The code outside the function (specified after function definition, somewhere later in the script):

fruitList = ["apple", "banana", "cherry"] #declaring a List


listFood(fruitList) #calling the function and passing to it the List as the argument
Positional and Keyword Arguments
• Positional arguments: the arguments need to be passed in the correct positional order, as listed in
function definition.

def printMe (name, age):


print (“My name is”, name, “and I am”, age, “old.”)

printMe(“John”, 15) # calling the function later in the script

• Keyword arguments:
- When calling the function, the arguments are identified by name.
- The order does not need to be followed because the names are used to match the values with
parameters.
- Example, calling printMe() function using the keyword arguments:

printMe(age=15, name=“John”) #parameters are provided in different order


Default Parameter Value
• When defining a function, we can set a default value for one or more of its parameters
• When we call the function without specifying an argument for a parameter with a default value, it will
use the parameter’s default value in the code block.
• If an argument is passed when calling the function, that value will replace the default parameter value.

• Example – a function with a parameter that has a default value set in function definition:
def myCountry (country = “Canada"):
print("I am from " + country)

myCountry () #Calling the function without an argument; Output: I am from Canada


myCountry (“Australia") #Calling the function with an argument; Output: I am from Australia
Function Return Value
• To have a function return a value, we use the return statement. A function can but does not have to
return a value.
• Ex. 1:
def addTwoNumbers (x, y): # declaring a function
   return x + y # the function will return the value resulting from the expression x+y

# outside the function:


print(addTwoNumbers(3,5)) # calling the function, passing two arguments to it, and printing the
# result
• Ex. 2:
def addTwoNumbers (x, y):
sum = x + y # or, can save the result to a variable
return sum # and return the variable

# outside the function:


result = addTwoNumbers(3,5) # calling the function, passing two arguments to it, and
# storing the value returned by the function into a variable

print(“The sum of the numbers is “ + str(result)) # Printing the result is handled outside the function
Scope of Variables
• All variables in a script may not be accessible in all locations in the script.

• Scope of a variable determines the portion of the program in which the variable can be accessed.

• Scope of a variable will depend on where in the script the variable has been declared.

• There are two basic variable scopes in Python:

- Global variables
- Local variables
Local Variables
• A variable created inside a function is said to have a local scope (ie. it belongs to the local scope of that
function) and can only be accessed inside that function.

def myFunction():

x = 300 # x is a local variable as here it is defined inside a function


print(x)

print(x) # this would result in an error, since x is local to the function and as such is not accessible
# from outside the function
Global Variables
• A variable created in the main body (ie. outside any function) of a Python script is a global variable as it
belongs to the global scope.

• Global variables are available from within any scope, global and local: they can be accessed throughout the
program body and by all functions.

# outside the function


x = 300 # x is a global variable, as it is created in the script outside function

# a function
def myFunction():
   print(x) # x is accessible from inside the function

# outside the function:


myFunction() # calling the function executes the function code: value of x is printed to the console
print(x) # x is also accessible from the main body of the program (outside the function)
Naming Variables & Scope
• If a script creates a same name to create one variable inside a function and another variable outside any
functions – these two will be treated as two separate variables - one available in the global scope (outside the
function) and one available in the local scope (inside the function).

• Example:
total = 0 # Here total is a global variable

def sum(arg1, arg2): # Function definition


total = arg1 + arg2 # Here total is a local variable. The value stored is 30
print ("Inside the function: the local total : ", total) # Prints the local variable’s value

sum(10, 20) # Calling the function; prints the local variable


print ("Outside the function: the global total : ", total) # print() running outside the function;
prints the # value of the global variable

# Output:
Inside the function: the local total : 30
Outside the function: the global total : 0
Returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)

map() function
• map() function applies a function that we specify to each item in a given iterable (eg. list, tuple etc.)
• map() returns a list object, with the list of the results after applying the given function
• Syntax:
map(fun, iter) fun - a function to which map function passes each element of the given iterable
iter - an iterable which is to be mapped
• Example: using map() function to convert all values in a List to Strings, so that we then use the join() method to
concatenate the values in the List as one string.
myList = [1, 2, 3, 4] # a List with int values
myStrList = map(str, myList) # returns a List object, where each List item is casted to a str value
separator = “, “ # the str value to be used when calling the join() method, below
print(separator.join(myStrList)) # would return an error if we did not convert the values to str first

# Or, the code from thee previous three lines, all covered in one line:
print(", ".join(map(str, myList)))
A flag Variable
• A flag or a control variable is a variable we use in our script to control the flow of our code.
• A flag is not a special type of variable, but a logical concept.
- The concept is that the variable records the occurrence of an event: it is set with one value if the event happened,
and with a different value if the event did not happen.

• Usually, it is a Boolean variable that is used as a flag variable.


- We define the variable to have a value of either True or False, until the condition we want to check becomes true, in
which case we change the flag variable's value to the opposite value (True to False, and vice versa.

• For example, we can use a flag variable in a loop, to check a certain condition while the loop progresses. Then
in the code we use the value of the variable to further build our logic in the code.

• Example: A program that prompts user to enter a word between 6 and 12 characters long, accepts the value and checks


if it’s valid. If it is not, the question is repeated until the user provides a valid value. The script makes use of a flag variable
in the loop, by using it in the loop condition and by changing its value from False to True once the user provides a valid
value – which in turn will stop the loop. See flag-variable.py
Questions?
References
• https://www.geeksforgeeks.org/what-is-the-difference-between-pythons-module-package-
and-library/
• https://www.w3schools.com/python/python_functions.asp
• https://www.geeksforgeeks.org/python-map-function/
IT Automation
INFO8025
Conestoga College
IT AUTOMATION – INFO 8025
Topic 19: Error Handling in Python
Objectives
• Error Handling in Python
• Modules, Packages, Libraries & PIP
Exception (Error) Handling
• Logical errors: the code runs, and runs without errors, however logically it does not do what you expect
it to do.
- Ex: instead dividing, it multiplies 2 numbers, or it divides by a wrong number.
• Syntax errors: the code does not run, due to issues with the syntax used in the code.
- Ex: forgetting ‘:’ at the end of an if statement.
• Exceptions: when an error occurs while a program is running, we say that an exception is raised.
- Ex: using int() function to convert a string value that is not a number
• When an exception is raised Python will normally stop and generate an error message.
• We can add code to our program that will handle exceptions.
• The benefits:
- Preventing the program from stopping uncontrollably when an exception is raised; it gives us more control
over how our code runs, and when it will/should or should not stop

- Ability to display an informational and meaningful message about the problem to the user, vs. the program
stopping with the default built-in error message, which is most often not easy to understand
Exception Handling with a try…except block
• For exception handling we use a try…except block in our code:

- The try block lets us test a block of code for errors. This is where we specify the code we want to run
initially.
- The except block lets us catch and handle any errors resulting from trying to run the code in the try block.

• Example (try-except):
try:
   print(x) # The statement will raise an exception, since x is not defined.

except: # Since the try block raises an error, the except block will be
print("An error has occurred") # executed; it ‘catches’ and ‘handles’ the exception

print(“bye”) # this line will run (ie. the program won't stop due to the error, since


# it has been handled with try/except)
Exception Handling: the finally block
• The finally block lets us execute some additional code, regardless of whether or not the try block raises
an error

• It is often used to clean up resources, for instance to close a previously opened file.

• Example, try-except-finally:

try:
f = open("myFile.txt")
f.write("Just some text")

except:          # will run if for ex. the file is not writable, in which case an exception is raised


print("Something went wrong when attempting to write to the file")

finally:
f.close()      # closing the file; will execute no matter whether an exception is raised or not.
Exception Handling: the else block
• The else keyword can be used to define some additional code that will be executed only if no errors
were raised. It lets us run some code conditionally – ie. only if no exceptions came out of the code that
ran prior to it.

• Example, try-except-else:

try:
    age=int(input('Enter your age: ’)) # will cause an error if user enters a string, for ex.

except:
    print ('You have entered an invalid value.’)

else: # executed only if no errors were raised in try block –     if age <= 21: #


ie. only if user entered a valid, numeric, value for age
        print('You are not allowed to enter, you are too young.’)
    else:
        print('Welcome, you are old enough.')
Multiple except blocks
• There are different types of built-in exceptions
• Can code multiple excepts in one try/except block– for handling different types of exceptions.
• The last specified except is a generic one, used to catch any remaining exceptions not caught by one of the
previous except statements:

try:
   print(x) # The statement will raise an exception, since x is not defined.

except NameError: # handles the built-in NameError exception


print("Variable x is not defined")

except: # handles any exception type, will catch any errors that are not of
print(“Something else went wrong”) # NameError type
Throwing an Exception
• We can choose to programmatically throw our own exception depending on whether a condition does or
does not occur.
- For ex: to communicate to the user that a different input value was expected.
• To throw (raise) an exception, raise keyword is used.
• When throwing an exception, we also get to define the error message that gets displayed with the error
• Throwing an exception stops the program – unless handled by a try….except block.
• Can raise a generic Exception
Ex:
x = input("Please enter an integer number greater than zero: ")

if int(x) < 0:
raise Exception("Sorry, no numbers below zero...") # throw a generic Exception
# if the user enters a negative number

print("bye!")        #This line does not execute, as throwing the exception stops the program
Throwing an Exception, cont’d
• Can also throw (or raise) one of the built-in exceptions

Ex.
x = “hi”
if not type(x) is int: # checking if the value in x is an integer value
  raise TypeError("Only integers are allowed") # throws a TypeError if the value in x is a non-integer
value
print("bye!")        # This line does not execute, as throwing
# the exception stops the program.
Catching (handling) Our Exceptions
• Any exceptions we throw, we can further handle programmatically within a try except block – the same way
we handle any Python errors.
• Ex: see throwing-exception3.py

try:
print("Enter an integer number greater than zero: ")

    x = int(input()) # when used with input(), int() function will return a ValueError exception 


                   # if the value provided is a non-integer value (a string, or a decimal)
      if x < 0: 
raise Exception()    # throw a generic exception in case when user enters a negative number

except:
     print("\nThe program is expecting an integer number greater than 0.") # will catch either of the two exceptions

print("Bye!") # This line will run, as the exceptions are handled in try...except
                  # - ie. the program does not stop due to an exception.
Modules, Packages, Libraries & PIP
• A script is a single file of Python code that is meant to be executed as the 'main' program.
• A module is a simple Python file, with .py extension, that contains collections of functions and global variables. It is an executable
file. It is meant to be imported, for code reuse.
• A package in Python is a simple directory containing modules (Python (.py) files). Packages are used to help organize modules. In
addition to modules, a Package contains an additional file, named __init__.py file. It tells the interpreter that the directory is a
Package – as opposed to a regular directory containing a bunch of Python files.
• A library is a collection of modules and submodules with related functionality.

The scope of a library can vary a lot.


- For instance, the Python’s Standard Library is vast (has quite a few submodules: http://docs.python.org/2/library/).
- There are a lot of single-purpose libraries as well.
• PIP is a package manager and installer for Python. With Python version 3.4 or later, PIP is included by default.
Questions?
References
• https://www.tutorialspoint.com/python/python_functions.htm
• https://www.w3schools.com/python/python_scope.asp
• https://www.w3schools.com/python/python_try_except.asp
• https://www.w3schools.com/python/python_pip.asp
IT Automation
INFO8025
Conestoga College
IT AUTOMATION – INFO 8025
Topic 20: File & Directory
Management in Python
Objectives
• Working with Files (opening, reading, writing, creating, deleting)
• Managing Files and Directories
- Using OS module functions
- Using OS Path module functions and methods
• Hands on Time
File and Directory Management: Using Python Modules
• One of the tasks often performed by a system administrator is the management of files, directories and data. For
completing these types of tasks, we make use of different Python modules.
• A module is a simple Python file (with a .py extension) that contains collections of functions. The point is that it offers
code that we can easily reuse in our scripts.
• The following are some of the Python modules commonly used for data, file and directory-related tasks:
- The os module provides the functions used when performing operations on individual files and directories, mostly.
- The os path module contains some useful functions that have to do with path names. It is a submodule of os path
module. Its functions are used for different purposes such as merging and retrieving path names.
- The shutil module provides functions for some high-level operations on files and collections of files. For ex. functions
for copying and moving
• To call (use) the functions defined in a module, we code an import statement that imports (makes available) that
module in our script.
import os
import shutil
• An import statement needs to come before the call to a function provided by the module. The practice is to include any
import statements at the top of the script.
Opening Files
• open() function is used for opening files

- It takes two parameters: file name and mode


aFile = open("demo.txt", "rt")

- There are four different modes for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file.

- Also, can specify if the file should be handled as binary or text mode:

"t" - Text - Text mode; default value


"b" - Binary - Binary mode (eg. images)
Opening Files, cont’d
• To open a file for reading it is enough to specify the name of the file:
aFile = open("demo.txt") # the file is located in the current working directory
aFile = open("demo.txt", "rt") # The same as above. "read" and “text” are default values

• If the file is located in a location other than the current working directory, you would specify the
absolute file path:
aFile = open("D:\\myfiles\demo.txt")

• To avoid getting an error when opening a file from an existing path on Windows (as Windows is using
‘\’ in paths, and ‘\’ is an escape character in Python), do one of the following:

a) use ‘r’ in front of the file path (‘r’ stands for ‘row string’; this effectively doubles the backslashes)
filePath = r”c:\\user\new\files-python”

b) or, use forward instead of backward slashes in the file path


filePath = “c://user/files-python”
Reading Files
• open() function returns a File object, which has different methods that we can use.
• read() method: for reading the content of the file. Without any parameters it returns the whole text.
aFile = open("demo.txt", "r")
print(aFile.read()) # read() method returns all of the file content

• Reading only parts of a file with read() method: can specify the number of characters you want to
return:
print(aFile.read(11)) # reads the first 11 characters from the file

• readline() method: for reading one line of text from a file:


print(aFile.readline()) # Reads one line of text from a file
print(aFile.readline()) # This will read the next line from the same file

• By using a loop can read the whole file line by line:


for aLine in aFile:
print(aLine) # reading & printing one line at a time
Reading Files, cont’d
• readlines() method reads lines of content into a stream
- we can save the content read into a variable and then access specific lines using a zero-based index.

aFile = open(“demo.txt”)
content = aFile.readlines()

print(content[1]) # read and print 2nd line

# using a range as index; note: the range-end number is not included. Returns a List object.
print(content[1:3]) # read and print 2 and 3 line;

aFile.close()
Closing Files
• Once we are done with a file, we need to close it.
• close() method is used to close a file.

aFile = open("demo.txt") # opening file for reading


print(aFile.read()) # reading and printing all of the file content to the console
aFile.close() # closing the file
Writing to a File
• To write to a file, we need to first open the file in the write mode, by adding either “a” or “w” as a
parameter to the open() function.

"a" - Append - will append to the end of the file


"w" - Write - will overwrite any existing content

• And then we use write() method to add content to the file.


• Example - Appending text:
aFile = open("demo.txt", "a")
aFile.write(“Appending this text…")
aFile.close()

• Example – Writing (overwriting) text:


aFile = open("demo.txt", “w")
aFile.write(“Overwriting any existing content with this text.")
aFile.close()
Using with() function with open() for reading and writing
• Why use with() function when opening files?
- Auto-cleanup: file is automatically closed, even if an exception is raised along the way
- Provides for cleaner syntax and fewer lines of code

• Using with() function when opening file for reading:

with open("demo.txt") as aFile: Vs: aFile = open("demo.txt")


fileContent = aFile.read() fileContent = aFile.read()
aFile.close()

• Using with() function when opening file for writing:

with open("demo.txt", "w") as aFile: Vs: aFile = open("demo.txt", "a")


aFile.write("Hello World!") aFile.write(“Hello World!“)
aFile.close()
Creating a New File
• To create a new file in Python, we use the open() function, with one of the following parameters:

"x" - Create
"a" - Append
"w" - Write

aNewFile = open(“myFile.txt", “x") # Creates a new file; returns an error if the file exists
aNewFile = open(“myFile.txt", “a") # Creates a new file if it doesn’t exist already
aNewFile = open(“myFile.txt", “w") # Creates a new file if it doesn’t exist already
Deleting a File
• To delete a file, we need to import the OS module, and call its os.remove() function
import os
os.remove("demoFile.txt")

• If the file does not exist, os.remove will result in an error.

• To check if a file exists firsts (and avoid the error), use os.path.exists() method

import os

if os.path.exists("demoFile.txt"):
os.remove("demoFile.txt")
Managing Files and Directories
• The os module provides some functions we can use to perform operations on individual files and
directories, mostly. Below are some of the commonly used os module functions.

• Start by importing the os module, so that you can use its functions for managing files and directories
that come with the module in your script:
import os # import statements should be coded at the top of your script

• os.getcwd() function for getting the current working directory:


os.getcwd()

• os.chdir() function for changing the working directory:


os.chdir('/home/student/work’)

• os.listdir(path) function – returns a list of names of files and directories found in a directory
dirContents = os.listdir() # if path is omitted returned is a list of names of files and folders in
for x in dirContents: # the current directory; We can further process it in a loop as needed
print(x)
Managing Files and Directories
• os.rename() function – renames a file or a directory.
os.rename(‘dirOrFileName', 'newDirOrFileName’)

• os.mkdir() method – creates a directory; Doesn’t return any value.

- If the specified directory already exists, an error will be returned


- If the specified path is invalid, will result in an error as well

parentDir = "/home/User/Documents"
newDirectory = "python"
newDirPath = os.path.join(parentDir, newDirectory) #os.path.join() merges the two parts to get the full
path
os.mkdir(newDirPath) #creates the directory python in the specified path

• os.makedirs() method is used to create a directory recursively


path = "/home/User/Documents/a/b/c"
os.makedirs(path) # will create directory c, but also any other non-existing directories in
Managing Files and Directories
• os.rmdir() method – used to remove an empty directory.
• If the specified path is not an empty directory, it will result in an error.

dirPath = "/home/User/Documents/test"
os.rmdir(dirPath) #removes the test directory from the given path

• To remove a non-empty directory can use shutil.rmtree() method:

import shutil
shutil.rmtree(dirPath)
OS Path Module: Functions and Methods
• The os path module contains functions that have to do with path names. We use them for merging and
retrieving and extracting parts of path names, for example.

• Import os module before calling the functions in os path module in your scripts:

import os

• os.path.basename(path) - returns the file name only, from the given path

fileName = os.path.basename("/baz/foo.txt")
print(fileName) #Output: foo.txt

• os.path.dirname(path) - returns the directory name(s) part, from the given path

dirName = os.path.dirname("/baz/test/foo.txt")
print(dirName) #Output: /baz/test
OS Path Module: Functions and Methods
• os.path.isdir(path) - checks whether the path provided is an existing directory or not; returns True or False

isDir = os.path.isdir("C:\\Users")
print(isDir) #Output: True if directory exists, False if it does not

• os.path.isfile(path) - check whether the path is an existing file or not; returns True or False

isFile = os.path.isfile("C:\\Users\foo.csv")
print(isFile) #Output: True if file exists, False if it does not

• os.path.exists() method - checks whether the specified path exists or not. Returns True or False.

- It is used as long as you don’t care to know whether the path points to a file or directory. If you do, use isdir()
or isfile() instead.

path = ‘\home\User\Desktop\file.txt'

pathExists = os.path.exists(path)
print(pathExists) #Output: True or False
OS Path Module: Functions and Methods
• os.path.join() method is used to join one or more components of a path intelligently.
• The method concatenates the path components passed in to it as arguments, in the specified order, and with exactly one
directory separator (‘/’ or ‘\’ – depending on the operating system on which it runs) following each non-empty string
argument - with the exception of the last path component as no separator is added at the end.

path = “\home"
fullPath = os.path.join(path, "User\Desktop", "file.txt") # fullPath = “\home\User\Desktop\file.txt”

• If the last path component to be joined is an empty string, then a directory separator (‘\’) is put at the end as well.

fullPath = os.path.join(path, "User\Public\", "Documents", "") # fullPath = “\home\User\Public\Documents\”


• If a path component represents an absolute path (ie. starts with a slash), then all previous components joined are
discarded and joining continues from the absolute path component.
path = "User\Documents"
fullPath = os.path.join(path, “\home", "file.txt") # fullPath = “\home\file.txt”

• The method is often used in scripts, in combination with other functions for managing files and directories:
- it’s OS independent
- provides more code clarity than when using string concatenation = makes it clear the code is about joining paths.
Questions?
References
• https://www.geeksforgeeks.org/file-handling-python/?ref=lbp
• https://docs.python.org/3/library/shutil.html
• https://www.geeksforgeeks.org/os-path-module-python/?ref=lbp
• https://www.geeksforgeeks.org/python-os-path-exists-method/?ref=lbp
• https://www.geeksforgeeks.org/python-check-if-a-file-or-directory-exists-2/?ref=lbp
• https://www.geeksforgeeks.org/python-os-makedirs-method/?ref=lbp
• https://www.geeksforgeeks.org/python-os-mkdir-method/?ref=lbp
• https://www.geeksforgeeks.org/python-os-rmdir-method/?ref=lbp
IT Automation
INFO8025
Conestoga College
IT AUTOMATION – INFO 8025
Topic 21: Regular Expressions
Objectives
• Regular Expressions

- What regular expressions are


- Building regular expressions in Python

• Functions that work with regular expressions (re Module)

- findall()
- search()
- match()
- split()
- sub()
Regular Expressions
• Regular Expressions are like a specialised language that can be used for string manipulation.
• Regular Expressions are used for performing different types of text search and text search and
replace operations.
• A Regular Expression (also called RegEx or RE) is a sequence of characters that defines a string
pattern.
- So, a RegEx allows us to specify a generalized pattern to match when searching some text.
• The string pattern is expressed as a combination of ordinary alphanumeric characters,
special characters known as metacharacters, and parenthesis. The pattern is then used to
match it against text strings. The matching is either successful or not (‘match’ or ‘no match’) 

Example: a RegEx can be used to express a pattern for any arbitrary phone number. Then, we can use a search
function that will scan a document and look for pattern matches in the text – based on the matches found we would
be able to extract all phone numbers contained in the document.
• String manipulation is a common requirement in programming which is why many languages, Python included, include
Regular Expressions as a built-in language component.
Regular Expressions
• Example Regular Expression:

^a...s$ - the pattern translates to: “any five-letter string starting with ’a’ and ending with ’s’ “
- Then we can use the pattern defined by the RegEx to try to match it against a string:
String
‘absence’ - no match
‘alias’ - match
‘abyss’ - match
‘Alias’ - no match (capital ‘A’)
‘An abacus’ - no match
Regular Expressions, cont’d
• Example - using a string method vs. a regex function (re: the benefits of using a RegEx)

#Task: Clean the string so that there is only one space between each two words

import re

a = "Hello-----Again---World!“

a = a.replace("-", " "))   #Uses a fixed matching string: “-” (exactly one dash), and replaces each
# occurrence with a blank space
# a will contain Hello Again World!

a = re.sub("-+", " ", a) # “-+” translates to “one or more ‘-’ “


# will replace each group of one or more ‘-’ with exactly one blank space
# a will contain “Hello Again World!”
re Module
• Defining a pattern using a Regular Expression is just a first step. We need to use some kind of a function that
will match the pattern against a string – that will do the search or search and replace operation on the string.
• Re module in Python offers a set of functions that work with Regular Expressions.
• To use the functions, import the module into your scripts: import re
• Example: using re.match() function to search for a pattern within a given string.

import re

pattern = '^a...s$’ # A RegEx defining the pattern to match: a five-letter string starting with ’a’ and ending with ’s’
testString = 'abyss’ # The string we are matching the pattern against

result = re.match(pattern, testString) # using re.match() function to match the pattern against testString.
# returns a Match object if the search is successful. If not, it returns None.
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
re Module, cont’d
• There are a few other functions available in the re module, that work with RegEx:

Function Description

findall Returns a List containing all matches


(finds all occurrences of a pattern in a given string and returns a list of all matching substrings)

search Returns a Match object if there is a match anywhere in the string. If not, it returns None

match Returns a Match object if there is a match at the beginning of the string. If not, it returns
None

split Returns a List where the string has been split at each match

Source:
sub https://www.w3schools.com/python/python_regex.asp
Replaces one or many matches with a string

• We will first learn about Regular Expressions, how to build them, and then come back to
the functions.
Regular Expressions
• When creating search patterns using Regular Expressions, we use:
- Metacharacters
- Special sequences
- Sets
MetaCharacters - characters with a special meaning
Meta Description Example Match
Character RE

[] Square brackets specify a set of characters you wish to match. "[a-d]“ same as [abcd]; the string contains any of the letters in the range
Can use ^ (caret symbol) inside the brackets to invert the character set. “[abcd]” the string contains a, b, c, or d
See the Sets slide for more details.
“[^0-9]” the string contains any character except digits

\ Signals a special sequence – see the Special Sequences slide for details "\d“
(any digit)

. A period matches any single character (except newline '\n') “.." ‘a’ – no match; ‘ac’ – 1 match; ‘acd’ – 1 match, ‘acde’ – 2 matches

^ Starts with "^hello" ‘hello world’ – match; ‘hey… hello!’ – no match

$ Ends with "world$" ‘hello world’ – match; ‘The world is mine!’ - no match

* The star symbol matches zero or more occurrences of the pattern on "aix*" The string contains ai followed by 0 or more x’s
the left. ‘ai’ – match ‘ix’ – no match (missing ‘a’)
‘aix’ – match
‘aixxxxx’ – match

+ One or more occurrences "aix+" The string contains ai followed by 1 or more x’s

{} Exactly the specified number of occurrences "aix{2}" The string contains ‘ai’ followed by exactly two occurrences of ‘x’

| Either or “day|night" The string contains either day or night

() Grouping sub-patterns - Placing a part of a reg. ex in brackets groups it “(ab)+” The string contains one or more occurrences of ‘ab’
and we can then apply a quantifier to that entire group inside the brackets

Source: https://www.w3schools.com/python/python_regex.asp
Special Sequences
• A special sequence is a \ (a backslash) followed by one of the characters in the list below, and it has a special
meaning:
Character Description RE Example

\A Returns a match if the specified characters are at the beginning of the STRING "\AThe"

\b Returns a match where the specified characters are at the beginning (or at the end) of a WORD r"\bain"
(the "r" in the beginning is making sure that the string is being treated as a "raw string“: it removes the special meaning from ‘\’) r"ain\b"

\B Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a WORD r"\Bain"
(the "r" in the beginning is making sure that the string is being treated as a "raw string") r"ain\B"

\d Returns a match where the string contains digits (numbers from 0-9) "\d"

\D Returns a match at every non-digit character "\D"

\s Returns a match where the string contains a white space character "\s"

\S Returns a match at every non-white-space character "\S"

\w Returns a match where the string contains any “word characters” (characters from a to Z, digits from 0-9, and the underscore _ "\w"
character)

\W Return a match at every NON word character (eg. a blank space, !, ?, etc) "\W"

\Z Returns a match if the specified characters are at the end of the STRING "Spain\Z"

Source: https://www.w3schools.com/python/python_regex.asp
Sets
• A set is a set of characters inside a pair of square brackets [] that has a special meaning:

Set Description

[arn] Returns a match where one of the specified characters (a, r, or n) are present

[a-n] Returns a match for any lower case character, between (alphabetically) a and n

[^arn] Returns a match for any character EXCEPT a, r, and n

[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present

[0-9] Returns a match for any digit between (and including) 0 and 9

[1-5][2-9] Returns a match for any two-digit number in the range 12 to 59

[a-zA-Z] Returns a match for any alphabetical character between a and z, lower case OR upper case

[+] In sets, +, *, ., |, (), $,{} have no special meaning, so [+] means: return a match for any + character in the
string

Source: https://www.w3schools.com/python/python_regex.asp
re.findall() function
• Finds all occurrences of a specified value in a given string.
• Returns a list containing all matches. If no matches are found, an empty list is returned.
• A regular expression can be used to specify a pattern as a search value.
• Ex. 1:
import re

txt = "The essential is invisible to the eye“


matches = re.findall(“is", txt)

• Ex. 2: Or could use a RegEx:


import re

txt = "The essential is invisible to the eye“


re.findall(“\bis", txt) # Loking for ‘is’ at the beginning of a word

if matches != []: # Checking if any matches are found by checking whether the list returned is empty or not
print(len(matches), "match(es) are found.")
else: # the list is empty, ie. no matches are found
print(“No matches found.")
re.search() function
• Searches the string for an occurrence of a specified string value.
• If there is a match, the function returns a Match object.
- If there is more than one match, still the first occurrence of the match will be returned, in the Match object.
• If no match is found, value None is returned.
• A regular expression can be used to specify a pattern as a search value.
---------------------------------------------------------------------------------------------------------------------------------------------------------------
import re
txt = "The essential is invisible to the eye.“
pattern = r"\s" # pattern defined by the RegEx is ‘a white space character’

x = re.search(pattern, txt) # Searching for the first occurrence of a white space in the string in txt

if x != None:                      # checking if a match is found, by checking if a Match object is returned or not


     print("There is at least one white-space character in the given string")
re.match() function
• Will search the regular expression pattern and return the first occurrence.
• If there is a match, the function returns a Match object.
• If no match is found, value None is returned.
• match() vs search():

match() checks for a match only at the beginning of the string, while search() checks for a match anywhere in the string.
---------------------------------------------------------------------------------------------------------------------------------------------------------------
import re

pattern = '^a...s'          # pattern: any 5-letter string starting with ’a’ and that has an ‘s’ as its 5th letter                 


txt = 'abyss'       # The string within which we are searching for the pattern

result = re.match(pattern, txt)                    

if result: # checking if a match is found, by checking if a Match object is returned or not
     print("Match found")
else:
     print("Match not found")   
The Match object
• The Match object is an object containing information about the search performed by the function and the result returned
• The object has properties and methods we can use to retrieve the information contained in the object.
- span() method returns a tuple containing the (zero-based) start and end positions of the match
- start() method returns the start position of the match
- end() method returns the end position + 1 of the match
- string property returns the string passed into the function
- group() method returns the part of the string where there was a match
----------------------------------------------------------------------------------------------------------------------------------------------------------------
import re

txt = "The Essential is Invisible to The Eye."
pattern = r"\bE\w+"                  # pattern: any WORD that STARTS with an UPPER case "E" (\bE) and contains 
                                     # ONE OR MORE alphanumeric characters or an underscore (\w+)

result = re.search(pattern, txt)    # searching for a match in the given string

if result != None:                   # if a match is found (ie. a Match object has been returned by the function)
    
     print("start and end position of the first match occurrence:",result.span())  
     print("The string passed into the function:", result.string)   
     print("Part of the string where there was a match:", result.group()) 
re.split() function
• Used to split a string by a specified delimiter. Returns a List object with the resulting substrings as its items.
• Delimiter is often defined by using a regular expression.
• Can control the number of splits by specifying the maxsplit parameter.
• re.split() vs. the string split() method: split() is faster than re.split(). re.split() is used when the delimiter is not constant, not
fixed - when the use of a regular expression is required. If it’s fixed, split() is sufficient.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
import re
txt = "The  essential is invisible to the eye.“
delimiter = r“\s+” # pattern defined by the RegEx: ‘one or more white spaces’

#Ex 1: Splitting the string at each match:


substrings = re.split(delimiter, txt)

if len(substrings) > 1:              # checking if the original string was split - ie. if a match was found


print("The original string was split into the following substrings by using white space(s) as a delimiter:“, substrings)
----------------------------------------------
#Ex. 2: Splitting the string once only, at the first match:
substrings2 = re.split(delimiter, txt, 1)

if len(substrings2) > 1:  
print("The string is split, however only at the first occurrence of the delimiter:", substrings2)
re.sub() function
• Used to replace one or many matches of a pattern with a specified string.
• Can control the number of replacements to be made by specifying the count parameter.
• Returns a new string, the one resulting from the replacements made on the original string.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Example: using re.sub to remove the extra white spaces in the given string

import re
txt = "The           Essential is invisible  to the            eye."

#1: Replace every occurrence of one or more white-space characters with exactly one white space:

modifiedTxt = re.sub(r"\s+", " ", txt)
print("modifiedTxt:",modifiedTxt) # modifiedTxt will store “The Essential is invisible to the eye.”

#2: Using the count parameter to replace only the first 2 occurrences of one or more white-space characters

modifiedTxt2 = re.sub(r"\s+", " ", txt, 2)
print("modifiedTxt2:",modifiedTxt2) # modifiedTxt2 will store “The Essential is invisible to the            eye.”
Questions?
References
• https://www.w3schools.com/python/python_regex.asp
• https://www.programiz.com/python-programming/regex
• https://stackoverflow.com/questions/7501609/python-re-split-vs-split
• https://blog.finxter.com/python-re-asterisk
IT Automation
INFO8025
Conestoga College
IT AUTOMATION – INFO 8025
Topic 22: Sets & Dictionaries
Objectives
• Sets
• Dictionaries
• Hands-on Time
Collections
• Python has 4 built-in collection data types that are used for storing multiple items into a single variable.

• Each has different qualities and usage:

- List is a collection which is ordered and changeable. Allows duplicate members.


- Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
- Set is a collection which is unordered, unindexed and unchangeable. No duplicate members.
- Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
Sets
• Sets are used to store multiple items in a single variable.
• They are written with curly braces.
• Initializing an empty set:

fruit = set()

• Creating a non-empty set:

fruit = {"apple", "banana", "cherry“}

• A set is a collection which is unordered and unindexed, unchangeable and does not allow duplicate values.
• Unordered - means that the items in a set do not have a defined order. They can appear in a different order every
time we use them and so they cannot be referred to by index or key.
• Unchangeable – means that once a set is created, we cannot change its items; however, we can add new items.
• Not allowing duplicate values – means that a set cannot have two items with the same value. Duplicate values are
ignored.
Data Types of Set Items
• Set items can be of any data type that is immutable (where value cannot be changed): a string, boolean, numeric
data types. A list cannot be added to a set, since it’s mutable.

[When we ‘assign’ a new value to an int data type variable, we are not changing the value of the int object.
Instead, we are creating a new object and pointing the variable to this new object.]

• Ex:
set1 = {"apple", "banana", "cherry“}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
set4 = {"abc", 34, True, 40, "male“}
Accessing Items in a Set
• Set items are unordered – for this reason individual items can’t be accessed by referring to an index or a key.
• Can loop through the set items using a for loop.
• Can check if a specified value is present in a set, by using the in keyword.

• Example, using for loop:

fruit = {"apple", "banana", "cherry“}


for x in fruit: #iterates through the items and prints the values
# do something with x ...
print(x)

• Example, using in keyword:

if “cherry" in fruit:
# do something…
Adding Items to a Set
• Sets are unchangeable, in the sense that once a set is created, we cannot make changes to its individual items.

• We can add a new item to an existing set, by using add() method.

fruit = {"apple", "banana", "cherry“}


fruit.add(“kiwi”)

• We can also add all items from another set into an existing set, using the update() method.
- can use this method to add individual items from any other iterable object, such as a list, tuple, or dictionary.

fruit = {"apple", "banana", "cherry“}
tropical = ["pineapple", "papaya“] #a list
fruit.update(tropical) # The updated set: {‘apple‘, ‘banana‘, ‘cherry‘,  ‘pineapple‘, ‘papaya‘}
Removing Set Items
• remove() and discard() methods each removes one, specified item in a set.

fruit = {"apple", "banana", "cherry“}
fruit.remove(“apple”)
fruit.discard(“cherry”)

- If the item does not exist, remove() will raise an exception, while discard() will not.

• pop() method removes the last item, but since sets are unordered, we don’t not know ahead of time what item
would be removed. The return value of the pop() method is the removed item.

• clear() method empties the set.


fruit.clear()

• del keyword deletes the Set object.


del fruit
Joining Sets
• Multiple sets can be joined into one.
• There are several options when joining two or more sets in Python:

- joining by keeping all items from the sets (a union of the sets; see Fig. 1)
- joining by keeping only duplicates (an intersection of the sets; ie. elements found in all sets ; see Fig. 2)
- joining by keeping all but duplicates (a symmetric difference of two sets; elements from the sets that are not found
in, not common to all sets; see Fig. 3)
- joining by keeping only the items from one set that are not found in other set(s) (see Fig. 4)

Venn diagrams:

Fig.1: A union of two sets Fig. 2: An intersection of Fig. 3: A symmetric Fig. 4: Relative complement
two sets difference of two sets of A (left) in B (right)
Joining Sets by keeping ALL items (a union)
• update() method inserts all items from one set into another

nonTropical = {"apple", "banana", "cherry“}
tropical = {“pineapple", "papaya“}
Venn diagram:
# Set tropical contains all the items from both sets combined A union of two sets
tropical.update(nonTropical)

• union() method - returns a new set containing all items from both sets.

# The new set contains all the items from both sets combined
fruit = nonTropical.union(tropical)

- union() can be used for joining more than two sets.

vegetable = {“carrot", “cabbage", “potato“}
fruitAndVegetable = vegetable.union(nonTropical).union(tropical)

• Both union() and update() will exclude any duplicate items, ie. duplicates will appear only once, as sets do not
support duplicate values.
Joining Sets by keeping ONLY Duplicates
• intersection_update() method - will keep only the items that are present in both sets .

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
Venn diagram:
x.intersection_update(y) #the existing set x will have one item only, ‘apple’
An intersection of two sets

• intersection() method will return a new set, that only contains the items that are present in both sets.

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = {"apple“, “lemon"}

a = x.intersection(y) #the new set a will have one item, ‘apple’


- intersection() can be used for joining more than two sets.
a = x.intersection(y).intersection(z) #the new set a will have one item, ‘apple’
Joining Sets by keeping ALL BUT Duplicates
• symmetric_difference_update() method will keep only the elements
that are not present in both sets.

x = {"apple", "banana", "cherry"} Venn diagram: A symmetric


y = {"google", "microsoft", "apple"} difference of two sets

x.symmetric_difference_update(y)
print (x) #the existing set, x, will have all the items from two the sets except ‘apple’

• symmetric_difference() method will return a new set, that contains only the elements that are not present in both
sets. Can be used for joining more than two sets.

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.symmetric_difference (y)
print (z) #the new set z will have all the items from the two sets except ‘apple’
Joining Sets by keeping only the items from a set that are not found in the
other set
• difference() method - returns a new set containing the items from one set that are not found in other set(s)

z = x.difference(y) # z contains the items from the set x that are not found in the set y

- difference() can be used for joining more than two sets.

Relative complement
of A (left) in B (right)
Additional Set Methods
• len() method – returns length of a set
len(x) # returns the number of items in the set x

• copy() method - returns a copy of the set.


a = x.copy() # y contains a copy of the set x

- Why make a copy? – Sometimes we will need to make changes to a set in our program, but we also need to
continue to work with the original set – in which case we need to preserve the values in the original set, by making a
copy of it and changing the copy only.

- Re: copying a set: simply assigning a set variable to a new variable does not work:
a = x

- a will only be a reference to x, and so any changes made to the set x will automatically also be made in
y; therefore, this is not a way to keep a copy of the original set.
Questions?
Hands-on Time
• The file with the exercises is found on eConestoga.
• Exercises 1 - 3
Dictionaries
• Dictionaries are used to store data in key:value pairs.
• Each item is a key:value pair, and items are separated by a comma
• Written with curly braces

• Initializing an empty dictionary:

myDict = {}
myDict2 = dict() #using the built-in dict() function

• Creating a non-empty dictionary:

employee = {
"firstName":"Joe",
"lastName":"Black",
"employeeId":1
}

print(employee) #Output: {'firstName':'Joe', 'lastName':'Black', 'employeeId':1}


Dictionaries
• A dictionary is a collection which is unordered, changeable and does not allow duplicates.
• Unordered: the items do not have a defined order - you cannot refer to an item by using an index.
• Changeable (mutable): we can change, and also add and remove a dictionary items after the dictionary has been
created
• Does not allow duplicates: cannot have two items with the same key. Note that values can repeat.

Ex:
employee = {
"firstName": "Joe",
"lastName": "Black",
"employeeId": 1,
"employeeId": 5 # will overwrite the value for ‘employeeId’ key; ie. will not add another employeeId key.
}

print(employee) # Output: {'firstName': 'Joe', 'lastName': 'Black', 'employeeId': 5}


Dictionary Items Data Types
• Dictionary items can be of any data type.

• Example: a dictionary with items of string, int, boolean, and list data types
employee = {
"firstName": "Joe",
"lastName": "Black",
"employeeId":1,
“fullTime": True,
“courses": ["OOP", “Java", “JavaScript"]
}

• To determine number of items in a dictionary we can use the len() function.

len(employee) #returns 5
Accessing Dictionary Items
• We can access a value of an item in a dictionary by referring to its key using square brackets.

print (employee[“firstName"]) #Output: Joe

• get() method will give the same result:

x = employee.get("firstName") # The value of x: Joe

• [] vs. get(): If we use the square brackets [], an error is raised in case a key is not found in the dictionary. The get()
method returns None if the key is not found (ie. no exception is raised).

• keys() method returns a list of all the keys in a dictionary.


x = employee.keys()

• values() method returns a list of all the values in a dictionary


y = employee.values()

• in keyword can be used to determine if a specified key is present in a dictionary.

if “employeeId" in employee:
  # do something …
Changing Dictionary Items
• To change a value of a dictionary item, we use the [] syntax to refer to the item’s key:

employee = {
"firstName": "Joe",
"lastName": "Black"
}

employee[“firstName”] = “Jane”

• Or, can use the update() method - Note: The argument passed to the method must be a dictionary.

employee.update( {"firstName": ”Jane”} )
Adding Dictionary Items
• Adding an item to the dictionary is done by adding a new key and assigning a value to it

employee = {
"firstName": "Joe",
"lastName": "Black"
}
employee["employeeId"] = 1

• update() method
- argument to the method is passed as a dictionary
- updates the existing item(s) in a dictionary – however, if the specified items (keys) do not exist, a new item(s) will
be added.

employee.update( {“fullTime": True} ) # Adds a new element, since ‘fullTime’ key does not exist

- update() effectively joins one dictionary to another. The items from the second dictionary are joined to the first
dictionary. Duplicate items (ie. duplicate keys) are removed: the later-specified value is kept.
Joining dictionaries with update(), example
dict1 = {'a': 10, 'b': 8}
dict2 = {'d': 6, 'c': 4}

dict2.update(dict1)

print (dict2) #Output: {'d': 6, 'c': 4, 'a': 10, 'b': 8}


Removing Dictionary Items
There are several ways to remove items from a dictionary.

• pop() method removes the one item with the specified key
employee.pop(“fullTime")

• del keyword uses the index syntax to remove the item with the specified key
del employee["fullTime"]

• popitem() method removes the last inserted item

• clear() method empties the dictionary


employee.clear()

• The del keyword can also delete the dictionary completely


del employee
Looping through a Dictionary
• To loop through a dictionary, we can use a for loop.
• Ex, using a loop to print all keys in the dictionary, one by one:
for x in employee:
   print(x)

• Ex, using a loop to print all values in the dictionary:


for x in employee:
print(employee[x]) # using item’s key to get to its value

• Ex, using the values() method in a loop to return values of the dictionary
for x in employee.values():
print(x)

• Ex, using use the keys() method in a loop to return the keys of the dictionary
for x in employee.keys():
print(x)

• Ex, using a loop to iterate through both keys and values, by using the items() method:
for x, y in employee.items():
print(x, y) # x refers to keys, y to values
Copying Dictionaries
• There are ways to make a copy of a dictionary.

• Simply assigning the dictionary variable to a new variable would not work:
employeeCopy = employee

- employeeCopy will only be a reference to employee, and changes made to employee will automatically also be
made in employeeCopy; ie. this is not a way to keep a real copy of the original dictionary.

1) Using the copy() method:

employeeCopy = employee.copy()

2) Use the built-in function dict():

employeeCopy = dict(employee)
Questions?
Hands-on Time
• The same file, exercises 4 - 7
References
• https://www.programiz.com/python-programming/set
• https://www.programiz.com/python-programming/dictionary
• https://www.w3schools.com/python/python_sets.asp
• https://www.w3schools.com/python/python_sets_access.asp
• https://www.w3schools.com/python/python_sets_add.asp
• https://www.w3schools.com/python/python_sets_remove.asp
• https://www.w3schools.com/python/python_sets_loop.asp
• https://www.w3schools.com/python/python_sets_join.asp
• https://www.w3schools.com/python/python_sets_methods.asp
• https://www.w3schools.com/python/python_dictionaries.asp
• https://www.w3schools.com/python/python_dictionaries_access.asp
• https://www.w3schools.com/python/python_dictionaries_change.asp
• https://www.w3schools.com/python/python_dictionaries_add.asp
• https://www.w3schools.com/python/python_dictionaries_remove.asp
• https://www.w3schools.com/python/python_dictionaries_loop.asp
• https://www.w3schools.com/python/python_dictionaries_copy.asp

You might also like