COURSE 6 – PYTHON FOR DATA SCIENCE, AI & DEVELOPMENT
Module 4: Working with Data in Python
IBM AI DEVELOPER PROFESSIONAL CERTIFICATE
Complete Coursera Study Guide
Last updated:
TABLE OF CONTENT
INTRODUCTION – Working with Data in Python
This module provides a detailed introduction to working with data in Python. It begins with the fundamental skills of reading and writing files, which are essential for managing data input and output. As you continue through the module, you will explore the most powerful Python libraries designed to aid in data manipulation and mathematical operations.
These libraries will enable you to perform complex data analysis tasks efficiently and effectively. By the end of this module, you will have a solid understanding of how to handle data in Python, leveraging the best tools and techniques available.
Learning Objectives
- Explain how Pandas use data frames.
- Use Pandas library for data analysis.
- Read text files using Python libraries including “open” and “with”.
- Utilize NumPy to create one-dimensional and two-dimensional arrays.
- Write and save files in Python.
PRACTICE QUIZ 1
1. What are the most common modes used when opening a file?
- (a)ppend, (r)edline, (w)rite
- (a)ppend, (r)ead, (w)rite (CORRECT)
- (s)ave, (r)ead, (w)rite
- (a)ppend, (c)lose, (w)rite
2. What is the data attribute that will return the title of the file?
- File1.name (CORRECT)
- File1.mode
- File1.open()
- File1.close()
3. What is the command that tells Python to begin a new line?
- \b
- \q
- \e
- \n (CORRECT)
4. What attribute is used to input data into a file?
- File1.write() (CORRECT)
- File1.open()
- File1.read()
- File1.close()
5. What does the attribute name of the file object display ?
- The name of the file (CORRECT)
- The mode of the file
6. What is the advantage of using the with statement to open a file
- it automatically closes the file object (CORRECT)
- it saves a backup
7. Consider the file object File1 in write mode, what would the following line of code write to the
file:File1.write(“Hello\n world”)
- Hello\n world
- Hello
- world (CORRECT)
- Hello world
correct the \n represents a new line
PRACTICE QUIZ 2
1. What python object do you cast to a dataframe?
- set
- tuple
- dictionary (CORRECT)
2. How would you access the first-row and first column in the dataframe df?
- df.ix[0,0] (CORRECT)
- df.ix[0,1]
- df.ix[1,0]
3. What is the proper way to load a CSV file using pandas?
- pandas.from_csv(‘data.csv’)
- pandas.load_csv(‘data.csv’)
- pandas.read_csv(‘data.csv’) (CORRECT)
- pandas.import_csv(‘data.csv’)
4. Use this dataframe to answer the question.
How would you select the Genre disco? Select all that apply.
- df.iloc[6, ‘genre’]
- df.loc[6, 5]
- df.iloc[6, 4] (CORRECT)
- df.loc[‘Bee Gees’, ‘Genre’]
5. Use this dataframe to answer the question.
Which will NOT evaluate to 20.6? Select all that apply.
- df.iloc[4,5]
- df.iloc[6,5]
- df.loc[4,’Music Recording Sales’] (CORRECT)
- df.iloc[6, ‘Music Recording Sales (millions)’] (CORRECT)
6. Use this dataframe to answer the question.
How do we select Albums The Dark Side of the Moon to Their Greatest Hits
- (1971-1975)? Select all that apply.
- df.iloc[2:5, ‘Album’]
- df.loc[2:5, ‘Album’] (CORRECT)
- df.iloc[2:6, 1] (CORRECT)
- df.loc[2:5, 1]
PRACTICE QUIZ 3
1. What is the Python library used for scientific computing and is a basis for Pandas?
- Tkinter
- Requests
- Numpy (CORRECT)
- datetime
2. What attribute is used to retrieve the number of elements in an array?
- a.size (CORRECT)
- a.ndim
- a.shape
- a.dtype
3. How would you change the first element to “10” in this array c:array([100,1,2,3,0])?
- c[2]=10
- c[1]=10
- c[0]=10 (CORRECT)
- c[4]=10
4. What attribute is used to return the number of dimensions in an array?
- a.size
- a.shape
- a.ndim (CORRECT)
- a.dtype
5. What is the type of the following
array:a=np.array([0,1,7,3, 7])
- int
- numpy.ndarray (CORRECT)
6. Consider the following lines of code:
c=np.array([20,1,2,3,4])
c[0]=100
c[0]=2
what is the value of c[0]
- 100
- 20
- 2 (CORRECT)
7. consider the numpy array u how would you multiply each element in the numpy array by 2
- 2*u (CORRECT)
- [2,2]*u
8. How would you perform the dot product between the numpy arrays u and v
- np.dot(u,v) (CORRECT)
- u*v
9. What is the shape of the following array:
A=np.array([[1,0,1],[0,1,1]])
- (2,3) (CORRECT)
- (3,2)
- 6
correct, the first element of the tuple is the number of rows the second is the number of columns
10. Consider the following array:
A=np.array([[1,0,1],[2,2,2]])
what is the value in A[0,1]?
- 0 (CORRECT)
- 1
- 2
11. What is the result of the following operation:
Y=np.array([[2,1],[1,2]])
Z=2*Y
- array([[4, 2],
- [2, 4]]) (CORRECT)
- array([[2, 2],
- [2, 2]])
- array([[2, 1],
- [1, 2]])
correct, multiplying the array by two doubles each element
MODULE 4 GRADED QUIZ
1. What is the result of the following lines of code?
a=np.array([0,1])
b=np.array([1,0])
np.dot(a,b)
- 0 (CORRECT)
- 1
- array([1,1])
2. What is the value of Z after the following code is run?
X=np.array([[1,0],[0,1]])
Y=np.array([[0,1],[1,0]])
Z=X+Y
- array([[1,1],[1,1]]) (CORRECT)
- array([[1,0],[0,1]])
- array([[0,1],[1,1]])
correct, the ‘+’ corresponds to matrix addition
3. What values does the variable out take if the following lines of code are run?
X=np.array([[1,0,1],[2,2,2]])
out=X[0:2,2]
out
- array([1,0])
- array([1,2]) (CORRECT)
- array([1,1])
correct, the first index corresponds to the rows the second index corresponds to the columns
4. What do the following lines of code do?
with open(“Example1.txt”,”r”) as file1:
FileContent=file1.readlines()
print(FileContent)
- Read the file “Example1.txt” (CORRECT)
- Write to the file “Example1.txt”
- Append the file “Example1.txt”
Correct, the mode is set to r for read.
5. What do the following lines of code do?
with open(“Example.txt”,”w”) as writefile:
writefile.write(“This is line A\n”)
writefile.write(“This is line B\n”)
- Read the file “Example.txt”
- Write to the file “Example.txt” (CORRECT)
- Append the file “Example.txt”
6. What do the following lines of code do?
with open(“Example3.txt”,”w”) as file1:
file1.write(“This is line C\n”)
- Read the file “Example3.txt”.
- Append the file “Example3.txt”.
- error (CORRECT)
Correct. There is no indent.
7. Consider the dataframe df. How would you access the element in the 2nd row and 1st column?
- df.iloc[1,0] (CORRECT)
- df.iloc[2,1]
- df.iloc[0,1]
8. In the lab, you learned you can also obtain a series from a dataframe df, select the correct way to assign the column with the header Length to a pandas series to the variable x.
- x=df[‘Length’] (CORRECT)
- x=df[[‘Length’]]
- x=df.[[‘Length’]]
9. What is the result of the following lines of code?
a=np.array([-1,1])
b=np.array([1,1])
np.dot(a,b)
- array([0,2])
- 1
- 0 (CORRECT)
10. How do you perform matrix multiplication on the numpy arrays A and B ?
- A+B
- A*B
- np.dot(A,B) (CORRECT)
11. What values does the variable out take if the following lines of code are run?
X=np.array([[1,0,1],[2,2,2]])
out=X[0,1:3]
out
- array([0,1]) (CORRECT)
- array([1,0,1])
- array([2,2])
correct, the first index corresponds to the rows the second index corresponds to the columns
12. What is the value of Z after the following code is run?
X=np.array([[1,0],[0,1]])
Y=np.array([[2,2],[2,2]])
Z=np.dot(X,Y)
- array([[2,2],[2,2]]) (CORRECT)
- array([[2,0],[0,2] ])
- array([[3,2],[2,3]])
correct, the dot function corresponds to matrix multiplication
13. Consider the following text file: Example1.txt:
This is line 1
This is line 2
This is line 3
What is the output of the following lines of code?
with open(“Example1.txt”,”r”) as File1:
file_stuff=File1.readline ()
print(file_stuff)
- This is line 1 (CORRECT)
- This is line 1
- This is line 2
- This is line 3
- This is line 1
- This is line 2
14. What task do the following lines of code perform?
with open(‘Example2.txt’,’r’) as readfile:
with open(‘Example3.txt’,’w’) as writefile:
for line in readfile:
writefile.write(line)
- Check the mode of the open function for each file object.
- Copy the text from Example2.txt to Example3.txt. (CORRECT)
- Print out the content of Example2.txt.
15. What function would you use to load a csv file in Pandas?
- pd.read_csv (CORRECT)
- pd.read_excel
16. What is the value of Z after the following code is run?
X=np.array([[1,0],[0,1]])
Y=np.array([[2,1],[1,2]])
Z=np.dot(X,Y)
- array([[2,1],[1,2] ]) (CORRECT)
- array([[2,0],[1,0]])
- array([[3,1],[1,3] ])
correct, the dot function corresponds to matrix multiplication
17. Consider the following line of code:
with open(example1,”r”) as file1:
What mode is the file object in?
- read (CORRECT)
- write
- append
Correct, the mode is set to r for read.
18. What do the following lines of code do?
with open(“Example.txt”,”a”) as writefile:
writefile.write(“This is line A\n”)
writefile.write(“This is line B\n”)
- Read the file “Example.txt”
- Append the file “Example.txt” (CORRECT)
- Write to the file “Example.txt”
19. Consider the dataframe df. How would you access the element in the 1st row 3rd column
- df.iloc[2,0]
- df.iloc[1,3]
- df.iloc[0,2] (CORRECT)
20. What method gets the unique elements of the following: df[‘Length’] ?
- unique (CORRECT)
- head
21. What values does the variable out take if the following lines of code are run?
X=np.array([[1,0,1],[2,2,2]])
out=X[0:2,2]
out
- array([1,0])
- array([1,2]) (CORRECT)
- array([1,1])
correct, the first index corresponds to the rows the second index corresponds to the columns
22. Consider the following text file: Example1.txt:
This is line 1
This is line 2
This is line 3
What is the output of the following lines of code?
with open(“Example1.txt”,”r”) as file1:
FileContent=file1.read()
print(FileContent)
- This is line 1
- This is line 2
- This is line 3 (CORRECT)
- This is line 1
- This
CONCLUSION – Working with Data in Python
In conclusion, this module offers a thorough introduction to working with data in Python. You will start by mastering the essential skills of reading and writing files, which are crucial for data management.
As you progress, you will become familiar with the most powerful Python libraries for data manipulation and mathematical operations, enabling you to perform complex data analysis with ease. By the end of this module, you will be well-equipped with the knowledge and tools needed to handle and analyze data effectively in Python.
Quiztudy Top Courses
Popular in Coursera
- Google Advanced Data Analytics
- Google Cybersecurity Professional Certificate
- Meta Marketing Analytics Professional Certificate
- Google Digital Marketing & E-commerce Professional Certificate
- Google UX Design Professional Certificate
- Meta Social Media Marketing Professional Certificate
- Google Project Management Professional Certificate
- Meta Front-End Developer Professional Certificate
Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!

