Chapter Quizzes - Part I
Chapter Quizzes - Part I
(USQ, Australia)
Dept. of Drilling & Production Engineering
Faculty of Geology & Petroleum Engineering
HCMUT, VIETNAM
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 2
Mathematics in Data Science
A. Linear Algebra
B. Multivariate Calculus
C. Fuzzy Logic
D. Deep Learning
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 3
Data Analytics vs Data Analysis
Quiz: Fill in the blanks with appropriate answers
• Objective ? ?
• Outcomes/Results ? ?
• Methodology ? ?
• Tools ? ?
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 4
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 5
Quiz: Which ones are correct?
1
if 5 > 2:
print("Five is greater than two!")
2
if 5 > 2:
print("Five is greater than two!")
3
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
4
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 6
Python Arithmetic Operators
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 7
Python Arithmetic Operators
Active learning with application in mind
Quiz 1 Quiz 2
x = 5 x = 5
y = 2 y = 2
print(x % y) print(x // y)
>>> >>>
??? ???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 8
Python Assignment Operators
Quiz 1 Quiz 2
x = 5 x = 5
x += 3 x -= 3
print(x) print(x)
>>> >>>
??? ???
Quiz 3 Quiz 4
x = 5 x = 5
x *= 3 x /= 2
print(x) print(x)
>>> >>>
??? ???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 9
Python Comparison Operators
Quiz 1 Quiz 2
x = 5 x = 5
y = 3 y = 3
print(x == y) print(x != y)
>>> >>>
??? ???
Quiz 5 Quiz 6
x = 5 x = 5
y = 3 y = 3
print(x >= y) print(x <= y)
>>> >>>
??? ???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 10
Python Logical Operators
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 11
Python Bitwise Operators
Quiz 1 Quiz 2
x = 5 # 0101 x = 5 # 0101
y = 3 # 0011 y = 3 # 0011
print(x & y) print(x | y)
>>> >>>
??? ???
Quiz 4 Quiz 3
x = 5 # 0101
x = 5 # 0101
y = 3 # 0011
print(~x)
print(x ^ y)
>>> >>>
??? ???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 12
Python Bitwise Operators
Exercise:
A= 1 0 0 1
B= 0 0 1 1
C= ? ? ? ?
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 13
Output Variables
Common Escape Sequences:
\n Newline
\t Tab
\\ Backslash
\’ Single quote
\’’ Double quote
\uXXXX Unicode character
Quiz 1
print(???)
>>>
Your text here with special characters: \n, \t, etc...
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 14
Output Variables (cont’d)
Quiz 2
name = "Alice"
age = 30
print(???)
>>>
Name: Alice & Age: 30
Quiz 3
name = "Alice"
age = 30
print(???)
>>>
Alice’s age is 30
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 15
Slicing Strings in Python
Quiz
print(str1[1:3])
print(str1[-3:-1])
>>>
???
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 16
Slicing Strings in Python
Quiz 1
b = "Hello, World!"
print(b[2:5])
print(b[:5])
print(b[2:])
print(b[-5:-2])
>>>
???
???
???
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 17
for Loop in Python
Fill in the blanks to complete the loop that prints the results below
Quiz 1:
>>>
1
4
9
16
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 18
Range with for Loops
Fill in the blanks to complete the loop that prints the results below
Quiz 2:
for i in range(__,__,__):
print(i)
>>>
0
-1
-2
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 19
while loop in Python
Quiz: Randomly guess a number until it matches a given one
import random as rd
given = 3
guess = rd.randint(0,9)
counter = 1
while ???:
print(f'not matched this time! guess= ???')
guess = ???
counter = ???
print(f"Bingo, matched after ??? trials (guess = ???)")
>>>
not matched this time! guess= 0
not matched this time! guess= 2
not matched this time! guess= 1
not matched this time! guess= 4
Bingo, matched after 5 trials! (guess = given = 3)
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 20
break statement in loops
Quiz
for i in range(10):
if i == 5:
break
print(i)
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 21
continue statement in loops
Quiz
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 22
Nested loops
Quiz
for i in range(5):
for j in range(i+1):
print("1", end="")
print()
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 23
List Slicing in Python
Quiz 1
fruits = ["apple", "banana", "orange", "grape", "kiwi"]
print(fruits[1:3])
print(fruits[2:])
print(fruits[:4])
print(fruits[::2])
>>>
???
???
???
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 24
Using List’s Methods
Quiz 1
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 25
Using List’s Methods
Quiz 1
>>>
["apple", "grape", "banana", "orange"]
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 26
Using List’s Methods
Quiz 1
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 27
Using List’s Methods
Quiz 1
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 28
Using List’s Methods
Quiz 1
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 29
Using List’s Methods
Quiz 1
>>>
2
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 30
Using Built-In Functions for Lists
Quiz 1
numbers = [5, 3, 7, 1]
# Print the numbers in the ascending order
sorted_numbers = ???
print(sorted_numbers)
>>>
[1,3,5,7]
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 31
Using Built-In Functions for Lists
Quiz 1
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 32
Using Built-In Functions for Lists
Quiz 1
numbers = [5, 3, 7, 1]
# print the location of the maximum number in the given list
print(???)
>>>
2
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 33
Using Built-In Functions for Lists
Quiz 1
numbers = [5, 3, 7, 1]
# print the sum of all numbers in the above list
print(???)
>>>
16
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 34
Using Built-In Functions for Lists
Quiz 1
numbers = [5, 3, 7, 1]
# print the list in ascending order
print(???)
>>>
[1, 3, 5, 7]
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 35
Built-In Functions for Lists
Quiz 1
numbers = [5, 3, 7, 1]
reversed_nums = list(???)
print(reversed_nums)
>>>
[1, 7, 3, 5]
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 36
Built-In Functions for Lists
Quiz 1
fruits = ["apple", "banana", "orange"]
# Print all items of the fruits list along with their ordinal
number
for ??? in ???:
print(???)
>>>
0 apple
1 banana
2 orange
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 37
Accessing Tuple Elements
Quiz 3
my_tuple = (5, 3, 2, 1, 8)
>>>
(5,3,2)
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 38
Concatenating Tuples
Quiz 1
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 39
Membership Testing for Tuples/Lists
Quiz 1
>>>
banana in fruits: True
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 40
Tuple’s/List’s Methods
Quiz 1
my_tuple = (1, 2, 3, 1, 1, 4)
# Count the number of 1’s in the given tuple
print(???)
>>>
3
Quiz 2
my_tuple = (10, 20, 30, 40, 30)
# Find the position of number 30 (first encountered)
in the given tuple
print(???)
>>>
2
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 41
Duplicate Items in a Set?
Quiz 1
this_set = {"apple", "banana", "cherry", "apple"}
print(this_set)
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 42
True, False vs 1, 0 in Sets
Quiz 1
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 43
Using Set’s Methods
Quiz 1
fruits = {"apple", "banana", "cherry"}
# Add "orange" to the fruits set
fruits._____
print(fruits)
>>>
{“apple”,”banana”,”cherry”,”orange”}
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 44
Using Set’s Methods
Quiz 1
fruits = {"apple", "banana", "cherry"}
# delete ‘banana’ from the fruits set
fruits._____
print(fruits)
>>>
{“apple”,”cherry”}
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 45
Using Set’s Methods
Quiz 1
set1 = {1, 2, 3}
set2 = {3, 4, 5}
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 46
Using Dictionary’s Methods
Dictionary - Quiz 1
my_dict = {
"name": "Bob",
"age": 30
}
# Adding a key:value as “job”:“Engineer”
# and modifying “age” to 32
my_dict.______({"age": 32, "job": "Engineer"})
print(my_dict)
>>>
{'name': 'Bob', 'age': 32, 'job': 'Engineer'}
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 47
Using Dictionary’s Methods
Dictionary - Quiz 2
my_dict = {
"name": "Bob",
"age": 25,
"email": "bob@example.com",
"city": "New York",
"nationality": "U.S"
}
# delete the entry “email”
my_dict._______
print(my_dict)
>>>
{'name':'Bob','age':25,'city':'New York','nationality':'U.S'}
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 48
Using Dictionary’s Methods
Print all key-value pairs
Dictionary - Quiz 3
my_dict = {
"name": "Bob",
"age": 25,
"city": "New York",
}
>>>
name: Bob
age: 25
city: New York
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 49
Using Dictionary’s Methods
Quiz 1
>>>
???
Quiz 2
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 50
Using Dictionary’s Methods
Quiz 1
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York’}
# Change the age to 25
??? = 25
print(my_dict)
>>>
{'name': 'Alice', 'age': 25, 'city': 'New York’}
Quiz 2
>>>
{'name':’Alice','age':30,'city':'New York,’nationality’:’U.S.’}
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 51
Using Dictionary’s Methods
Quiz
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 52
Using List Comprehension
Quiz 1
x = [1, 2, 3]
# print items in the list that is less than 4
lt_4 = [???]
print(lt_4)
>>>
[1,2,3]
Quiz 2
matrix = [[1, 2, 3], [3, 4, 5], [5, 6, 7]]
# print the diagonal of the given matrix
diagonal = [???]
print(diagonal)
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 53
Using Set Comprehension
Quiz 2
original_set = {1, 2, 3, 4, 5}
# calculate cumulative sum of the items in the set
cumsum_set = {???}
print(cumsum_set)
>>>
[1,3,6,10,15]
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 54
Python Functions
Quiz 1
def my_function(a, b=2):
return a * b
result = my_function(3)
print(result)
>>>
???
Quiz 2
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
results = factorial(5)
print(results)
>>>
???
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 55
Lambda Functions
Quiz 1
temp_c = [0,10,30,50]
# Define lambda function to convert C-degree to F-degree temp.
c2f = lambda ???
# Calling the lambda function
temp_f = ???
print(temp_f)
>>>
[32.0,50.0,86.0,140.0]
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 56
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 57
Creating and Manipulating Arrays in NumPy
Quizzes:
a) Create a NumPy array from the list [10, 20, 30, 40, 50].
b) Create a 4x4 array filled with zeros, and create a 3x3 array filled
with ones.
e) Given the 3x4 array from Quiz d), flatten it into a 1D array.
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 58
Creating and Manipulating Arrays in NumPy
Quizzes:
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 59
NumPy Array Operations
b) Given the exam scores [85, 92, 78, 95, 88, 72, 97, 65, 74, 83],
calculate the average score, the highest score, and the lowest
score?
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 60
NumPy Array Operations
Given a 2D array (3x4) representing matrix A (see below).
Calculate the sum of all elements in each columns and in each
rows in the matrix.
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 61
Indexing and Slicing in NumPy
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 62
Linear Algebra with NumPy
1. Create two matrices A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]].
Compute their matrix multiplication.
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 63
Exercises with NumPy
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 64
Pandas Series
Q1: Create a Pandas Series using the following data: [‘school',
‘college', ‘university’, ‘institute’]. Assign custom index labels as
follows: ['A', 'B', 'C', 'D'].
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 65
Pandas Series’ Methods
Quiz: Given a list of Celcius temperature data [0, 20, 30, 50]
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 66
Quiz: Creating a DataFrame from a
Dictionary
Quiz: Create a Pandas DataFrame from the given data.
Employee Data:
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 67
Quiz: Creating a DataFrame from a List
of Lists
Quiz: Create a Pandas DataFrame from the given data. Specify
appropriate column names: ['Product', 'Price', 'Category'].
Data:
['Pen', 2, 'Stationery']
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 68
Quiz: Creating a DataFrame from a
NumPy Array
Quiz: Create a Pandas DataFrame from the given data. Specify
appropriate column names: ['Celsius', 'City'].
Data:
[37, 'Chicago’]
[100, 'Houston']
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 69
Exercise: Creating Pandas DataFrame
1. Create a list of student IDs from 1 to 5.
4. Use the student IDs as the index of the DataFrame, and the
subjects as the column names.
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 70
Practical Exercise
The PVT fluid data from a gas well in the Anaconda Gas Field is given below:
10/26/2024 Dr. Mai Cao Lan, Dept. of Drilling & Production Engineering, GEOPET, HCMUT 71
Practical Exercise (cont’d)
The production flow rate of a gas well can be estimated using
well and reservoir data as follow:
qg
kh m p r m p w f
0.4 72 re
14 24 T ln s
rw
p 2p
m( p ) dp
0 Z
10/26/2024 Dr. Mai Cao Lan, Dept. of Drilling & Production Engineering, GEOPET, HCMUT 72
Practical Exercise (cont’d)
Trapezoidal Method
10/26/2024 Dr. Mai Cao Lan, Dept. of Drilling & Production Engineering, GEOPET, HCMUT 73
Practical Exercise (cont’d)
10/26/2024 Dr. Mai Cao Lân, Faculty of Geology & Petroleum Engineering, HCMUT, Vietnam 74
Matplotlib package
Exercise:
+ Function 1: 1 = sin( )
+ Function 2: 2 = cos( )
+ Function 3: 3 = 2
- Create a line plot for each function using a different color and label
each line accordingly. Add a title, and labels for the x-axis and y-axis.
Include a legend to distinguish between the lines.
10/26/2024 Dr. Mai Cao Lan - Faculty of Geology & Petroleum Engineering, HCMUT 75