An introductory course to the Python 3 programming language, with a curriculum aligned to the Certified Associate in Python Programming (PCAP) examination syllabus (PCAP-31-02).
https://knowledgebase.hyperlearning.ai/courses/introduction-to-python
In this module we will cover advanced operations with strings and lists, namely:
# Include a Unicode character in a Python 3 string literal
hospital_in_french = 'hôpital'
hospital_in_japanese = '病院'
hospital_in_arabic = 'مستشفى'
print(f'The name for Hospital in French is: {hospital_in_french}')
print(f'The name for Hospital in Japanese is: {hospital_in_japanese}')
print(f'The name for Hospital in Arabic is: {hospital_in_arabic}')
# Include a Unicode character in a Python 3 identifier
日本の人口 = 126_500_000
print(f'The population of Japan is: {日本の人口}')
# Usage of the encode() string method
directions = 'The name of the hospital is Charité - Universitätsmedizin Berlin'
print(directions.encode(encoding="ascii", errors="backslashreplace"))
print(directions.encode(encoding="ascii", errors="ignore"))
print(directions.encode(encoding="ascii", errors="namereplace"))
print(directions.encode(encoding="ascii", errors="replace"))
print(directions.encode(encoding="ascii", errors="xmlcharrefreplace"))
# Usage of the ascii() function
names = ['Quddus', 'Gößmann', 'José']
print(ascii(names))
# Usage of the ord() function
dragon = "竜"
print(f'The Unicode code point value for {dragon} is {ord(dragon)}')
# Usage of escape characters
print('Escaping a \' single quote character')
print("Escaping a \" double quote character")
print('Escaping a \\ backslash character')
print('Escaping a \
newline character')
print('Escaping an \a ASCII bell character')
print('Escaping an \b ASCII backspace character')
print('Escaping an \f ASCII formfeed character')
print('Escaping an \n ASCII linefeed character')
print('Escaping an \r ASCII carriage return character')
print('Escaping an \t ASCII horizontal tab character')
print('Escaping an \v ASCII vertical tab character')
# Usage of escape sequences
print('Escaping the \xf6 German umlaut o character')
print('Escaping the \N{MOUSE} Unicode mouse character')
print('Escaping the \u2708 Unicode airplane character')
# Try to change a character in a string
test_string = 'Hello World'
print(test_string[6])
test_string[6] = "Q"
# Apply the id() function to strings
my_first_string = 'abracadabra'
my_second_string = 'abracadabra'
print(f'id(my_first_string) = {id(my_first_string)}')
print(f'id(my_second_string) = {id(my_second_string)}')
# Apply the id() function to each character in a string
for idx in range(0, len(my_first_string)):
print(f'{my_first_string[idx]} = {id(my_first_string[idx])}')
# Strings are immutable, but variables can be changed to point to different things
my_string = 'I am a Data Scientist'
print(my_string)
my_string = 'I am a Software Engineer'
print(my_string)
my_multiline_string = '''Line 1\tEOL
Line 2\t\tEOL
Line 3 EOL'''
print(my_multiline_string)
# Represent the German umlaut using two different code point sequences
umlaut_sequence_1 = '\u00F6'
umlaut_sequence_2 = '\u006F\u0308'
print(f'{umlaut_sequence_1} has length {len(umlaut_sequence_1)}')
print(f'{umlaut_sequence_2} has length {len(umlaut_sequence_2)}')
# Compare strings that contain different code point sequences
import unicodedata
print(umlaut_sequence_1 == umlaut_sequence_2)
print(unicodedata.normalize('NFD', umlaut_sequence_1) == unicodedata.normalize('NFD', umlaut_sequence_2))
# Create a string
my_string = 'abracadabra'
# Extract characters of a string that have even indexes using the stride size argument
print(my_string[::2])
# Use a negative stride size argument to extract in reverse order
print(my_string[::-2])
# Extract a defined subset of characters with a stride size of 2 within that subset
print(my_string[4:10:2])
# Extract characters with even indexes starting from the 5th character
print(my_string[4::2])
# Create two variables to point to the same string
my_first_string = 'abracadabra'
my_second_string = 'abracadabra'
print(f'id(my_first_string) = {id(my_first_string)}')
print(f'id(my_second_string) = {id(my_second_string)}')
# Create a new variable bound to a subset of a string using slice notation
my_third_string = my_first_string[4:]
print(f'my_third_string = \'{my_third_string}\'')
print(f'id(my_third_string) = {id(my_third_string)}')
# isupper()
print('MY NAME IS JILLUR'.isupper())
# islower()
print('my name is jillur Q'.islower())
# isalpha()
print('learning python'.isalpha())
# isalnum()
print('learning python 3'.isalnum())
# isdecimal()
print('01092020'.isdecimal())
# isspace()
print(' \t\n\t\v '.isspace())
# istitle()
print('Jillur Quddus'.istitle())
# capitalize()
print('capitalize me'.capitalize())
# len()
print(len('abracadabra'))
# chr()
print(chr(9786))
# ord()
print(ord('愛'))
# Create a list containing the first ten square numbers
squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(squares)
# Access elements in a list by their index numbers
print(squares[0])
print(squares[3])
print(squares[-1])
print(squares[-4])
# Extract elements from a list that have even indexes using the stride size argument
print(squares[::2])
# Use a negative stride size argument to extract elements in reverse order
print(squares[::-2])
# Extract a defined subset of elements with a stride size of 2 within that subset
print(squares[4:8:2])
# Extract elements with even indexes starting from the 5th element
print(squares[4::2])
# Define a list of numbers
my_numbers = [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
print(f'My list of numbers (first 10 triangle numbers): {my_numbers}')
# Substitute a subset of the list with new elements
my_numbers[1:3] = [4, 9]
print(f'My list of numbers (substitutions): {my_numbers}')
# Replace every Nth element with a new value, where N = 3
my_numbers[::3] = [1, 16, 49, 100]
print(f'My list of numbers (n-th replacements): {my_numbers}')
# Replace and resize a subset of the list
my_numbers[4:] = [25, 36, 49, 64]
print(f'My list of numbers (first 8 square numbers): {my_numbers}')
# Delete every Nth element starting from the 2nd element and where N = 2
del my_numbers[1::2]
print(f'My list of numbers (every other square number): {my_numbers}')
# Define a list of phrases
my_phrases = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
print(f'Original list of phrases: {my_phrases}')
# Create a shallow copy of the original list using slice notation, plus append a new element
my_shallow_phrases = my_phrases[:2] + [['j', 'k', 'l']]
print(f'Shallow copy of my phrases: {my_shallow_phrases}')
# Alternatively you may use the list() function to create a shallow copy
my_other_shallow_phrases = list(my_phrases) + [['j', 'k', 'l']]
print(f'Another shallow copy of my phrases: {my_other_shallow_phrases}')
# Make a change to the original list of phrases only
my_phrases[0][0] = 'ALPHA'
my_phrases[0][1] = 'BETA'
my_phrases[0][2] = 'GAMMA'
print(f'\nUpdated original list of phrases: {my_phrases}')
# Examine the shallow copies of the original list
print(f'Shallow copy of my phrases: {my_shallow_phrases}')
print(f'Another shallow copy of my phrases: {my_other_shallow_phrases}')
# Create a deep copy of the original list
import copy
my_phrases = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
my_deep_phrases = copy.deepcopy(my_phrases)
print(f'Original list of phrases: {my_phrases}')
print(f'Deep copy of my phrases: {my_deep_phrases}')
# Make a change to the original list of phrases only
my_phrases[0][0] = 'ALPHA'
my_phrases[0][1] = 'BETA'
my_phrases[0][2] = 'GAMMA'
print(f'\nUpdated original list of phrases: {my_phrases}')
# Examine the deep copy of the original list
print(f'Deep copy of my phrases: {my_deep_phrases}')
# Create a list
my_diatomic_list = [0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4]
# Iterate the list using a while loop
i = 0
while i < len(my_diatomic_list):
print(f'Diatomic Sequence - Element #{i + 1}: {my_diatomic_list[i]}')
i += 1
# Iterate the list using a for loop with the range function
for x in range(len(my_diatomic_list)):
print(f'Diatomic Sequence - Element #{x + 1}: {my_diatomic_list[x]}')
# Iterate the list using a for loop with the in operator
for elem in my_diatomic_list:
print(elem)
# Iterate the list using a for loop with the in operator and enumerate function
for idx, elem in enumerate(my_diatomic_list):
print(f'Diatomic Sequence - Element #{idx + 1}: {elem}')
# Test whether an element exists in a list
squares = [1, 4, 9, 16, 25]
x = 16
if x in squares:
print(f"{x} exists in our list of square numbers")
else:
print(f"{x} does not exist in our list of square numbers")
# Test whether an element is NOT in a list
y = 169
if y not in squares:
print(f"{y} does not exist in our list of square numbers")
else:
print(f"{y} exists in our list of square numbers")
# Create a list of square numbers using list comprehension
squares = [num * num for num in range(1, 9)]
print(squares)
# Create a list formed of characters from a string
letters_in_abibliophobia = [character for character in 'abibliophobia']
print(letters_in_abibliophobia)
# Create a list of all the prime numbers between 1 and 100
def isPrime(num):
if num <= 1 or num % 1 > 0:
return False
for i in range(2, num//2):
if num % i == 0:
return False
return True
prime_numbers = [i for i in range(1, 101) if isPrime(i)]
print(prime_numbers)
# Create a list of all the even numbers between 1 and 100, and replace the odd numbers with 0
even_numbers_zeroed_odd = [i if i % 2 == 0 else 0 for i in range(1, 101)]
print(even_numbers_zeroed_odd)
# Create a list of square numbers
squares = [1, 4, 9, 16]
more_squares = [25, 36, 49, 64, 81, 100]
# list.append(element)
squares.append(25)
print(squares)
# list.extend(newlist)
del more_squares[0]
squares.extend(more_squares)
print(squares)
# list.insert(index, element)
squares.insert(0, 0)
print(squares)
# list.remove(element)
squares.remove(0)
print(squares)
# list.pop([index])
squares.pop()
print(squares)
# list.clear()
squares.clear()
print(squares)
# Populate the list of square numbers again
squares.extend([1, 4, 9, 16, 25, 36, 49, 64, 81, 100])
# list.index(element[, start[, end]])
print(squares.index(64))
# list.count(element)
print(squares.count(169))
# list.sort()
squares.sort()
print(squares)
# list.reverse()
squares.reverse()
print(squares)
# list.copy()
squares_copy = squares.copy()
print(squares_copy)
# Create a list of numbers
my_numbers = [34, 13, 101, 4, 10, 24, 7]
# Create a list of letters
my_letters = ['j', 'z', 'e', 'f', 'x']
# len()
print(len(my_numbers))
# sorted()
print(sorted(my_numbers))
print(sorted(my_letters))
# max()
print(max(my_numbers))
print(max(my_letters))
# min()
print(min(my_numbers))
print(min(my_letters))
# Create a two-dimensional array
my_2d_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in my_2d_array:
for elem in row:
print(elem, end=' ')
print()
# Create a two-dimensional array with list comprehension
number_cols = 3
number_rows = 3
my_2d_array = [[0 for i in range(number_cols)] for j in range(number_rows)]
for row in my_2d_array:
for elem in row:
print(elem, end=' ')
print()
# Access elements using square brackets and indices
my_2d_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(my_2d_array[0][0])
print(my_2d_array[1][1])
print(my_2d_array[2][2])
# Create a two-dimensional array
my_2d_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Update a specific element in our two-dimensional array
my_2d_array[1][1] = 0
# Insert a new row in our two-dimensional array at a given row number using the insert method
my_2d_array.insert(2, [9, 9, 9])
# Append a new row to our two-dimensional array using the append method
my_2d_array.append([7, 4, 7])
# Append elements to an existing row in our two-dimensional array at a given row number using the extend method
my_2d_array[0].extend([0, 0])
my_2d_array[1].extend([0, 0])
my_2d_array[2].extend([0, 0])
my_2d_array[3].extend([0, 0])
my_2d_array[4].extend([0, 0])
# Reverse the order of the elements in a given row in our two-dimensional array using the reverse method
my_2d_array[2].reverse()
# Delete a row at a given row number from our two-dimensional array
del my_2d_array[3]
# Display out final two-dimensional array
for row in my_2d_array:
for elem in row:
print(elem, end=' ')
print()
for row in my_2d_array:
for elem in row:
print(elem, end=' ')
print()
import pprint
# Create a three-dimensional array of size 3 x 3 x 3
my_3d_array = [[[x + y + z for x in range(3)] for y in range(3)] for z in range(3)]
pprint.pprint(my_3d_array)
# Access a list element in our three-dimensional array
print(my_3d_array[1][2])
# Access a specific element in our three-dimensional array
print(my_3d_array[2][0][1])
# Compare two given strings using comparison operators
print("Hello" > "Hello")
print("Hello" < "Hello")
print("abracadabra" > "Hello")
print("abracadabra" > "Hello")
print('a' < 'b' < 'c')
# Compare two given lists using comparison operators
print([1, 2, 3] > [1, 2, 3])
print([1, 2, 3] < [1, 2, 3])
print([1, 2, 3] > [1, 2, 4])
print([1, 2, 3] < [1, 2, 4])