16 Python Strings Loops Merged
16 Python Strings Loops Merged
16 Python Strings Loops Merged
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
- 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.
• 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
• 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.
• 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
• Example:
myText = “Python is easy to learn.“
• 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.
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:
• 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.
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.
• 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.
• 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.
• 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 = ["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.
- 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.
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
for x in fruitList:
print(x) # printing the List element from the current loop iteration
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")
- 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 = 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.
for x in fruitTuple: #iterates through the items and prints the values
print(x)
i = 0
• 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.
• 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.
# The code outside the function (specified after function definition, somewhere 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:
• Example – a function with a parameter that has a default value set in function definition:
def myCountry (country = “Canada"):
print("I am from " + country)
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.
- 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():
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.
# a function
def myFunction():
print(x) # x is accessible from inside the function
• Example:
total = 0 # Here total is a 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.
• 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.
- 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
• 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")
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.’)
try:
print(x) # The statement will raise an exception, since 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: ")
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.
"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:
• 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”
• 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
aFile = open(“demo.txt”)
content = aFile.readlines()
# 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.
"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")
• 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.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’)
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
dirPath = "/home/User/Documents/test"
os.rmdir(dirPath) #removes the test directory from the given path
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.
• 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
- 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!
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
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
$ 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’
() 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"
\s Returns a match where the string contains a 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
[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present
[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
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’
match() checks for a match only at the beginning of the string, while search() checks for a match anywhere in the string.
---------------------------------------------------------------------------------------------------------------------------------------------------------------
import re
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’
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.
fruit = 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.
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 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.
- 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)
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"}
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
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
- 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
myDict = {}
myDict2 = dict() #using the built-in dict() function
employee = {
"firstName":"Joe",
"lastName":"Black",
"employeeId":1
}
Ex:
employee = {
"firstName": "Joe",
"lastName": "Black",
"employeeId": 1,
"employeeId": 5 # will overwrite the value for ‘employeeId’ key; ie. will not add another employeeId key.
}
• 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"]
}
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.
• [] 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).
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)
• 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"]
• 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.
employeeCopy = employee.copy()
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