0% found this document useful (0 votes)
47 views25 pages

Ap4b M3

The document provides educational materials on using Python for data handling, including reading and writing files, working with CSV files, and creating reusable functions. It covers practical examples such as loading text files, extracting information from journal entries, and managing itineraries. The content is distributed under a Creative Commons License for non-commercial educational use.

Uploaded by

singhlakshdeep26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views25 pages

Ap4b M3

The document provides educational materials on using Python for data handling, including reading and writing files, working with CSV files, and creating reusable functions. It covers practical examples such as loading text files, extracting information from journal entries, and managing itineraries. The content is distributed under a Creative Commons License for non-commercial educational use.

Uploaded by

singhlakshdeep26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Copyright Notice

These slides are distributed under the Creative Commons License.

DeepLearning.AI makes these slides available for educational purposes. You may not use or distribute
these slides for commercial purposes. You may make copies of these slides and use or distribute them for
educational purposes as long as you cite DeepLearning.AI as the source of the slides.

For the rest of the details of the license, see https://creativecommons.org/licenses/by-sa/2.0/legalcode


AI Python for Beginners

Module 3: Working with


Your Own Data and
Documents in Python
AI Python for Beginners

Lesson 1: Using Files in


Python
Loading .txt files and reading
their contents

Opens the .txt file in reading mode ("r") and assigns it


to the variable f
f = open("email.txt", "r")

Reads the all of the text inside the file in f and stores
it in a string variable email

email = f.read()

Closes the file stored in f

f.close()
AI Python for Beginners

Lesson 2: Loading and


Using your Own Data
Your data

Documents

To do lists

Emails

Spreadsheets
Folder structure

Lesson_0 Lesson_1

Lesson_2

Developers tend to use the word directory rather than folder


Folder structure

Lesson_2.ipynb
helper_functions.py
email.txt
recipe.txt
Folder structure

Lesson_2.ipynb
Jupyter notebooks
are files with .ipynb
helper_functions.py extension
email.txt
Python files have .py
recipe.txt extension

The folder in which Python looks for files is called the current
working directory
AI Python for Beginners

Lesson 3: Reading Journals


from
Food Critics
Text data

Emails, journal entries, social media posts, and other


text can vary significantly in style and formatting.

Food critic journal entry

My first destination was


The Test Kitchen, a restaurant that has
earned its place among the world's
best. Situated in the trendy Woodstock
area, this dining spot is celebrated for
its innovative dishes. I was particularly
taken by their signature dish, the
"Pickled Fish Tacos." Useful information
within the text:
• Restaurant
name
• Name of dish
AI Python for Everyone

Lesson 4: Extracting Restaurant


Information from
Journal Entries
Extracting information from
text

Food critic journal entry

My first destination was


The Test Kitchen, a restaurant that has Useful
earned its place among the world's information
best. Situated in the trendy Woodstock within the text:
area, this dining spot is celebrated for • Restaurant
its innovative dishes. I was particularly name
taken by their signature dish, the • Name of dish
"Pickled Fish Tacos."

Get the important


information

Restaurant Dish
The Test Kitchen Pickled Fish Tacos
Saving files

Opens the file in writing mode ("w") and assigns it


to the variable f
f = open("highlighted_text.html", "w")

Stores the value of the variable html_response in the


file

f.write(html_response)

Closes the file stored in f

f.close()
AI Python for Beginners

Lesson 5: Vacation
Planning
Using CSV Files
CSV files

Used to store data that looks like tables


Arrival Departure City Country
July-01 July-08 New York USA
July-09 July-16 Rio de Janeiro Brazil
July-17 July-24 Cape Town South Africa
July-25 August-01 Istanbul Turkey
August-02 August-09 Paris France
August-10 August-17 Tokyo Japan
August-18 August-25 Sydney Australia

Rows are different lines, and columns are separated by


commas
Arrival,Departure,City,Country
July-01,July-08,New York,USA
July-09,July-16,Rio de Janeiro,Brazil
July-17,July-24,Cape Town,South Africa
July-25,August-01,Istanbul,Turkey
August-02,August-09,Paris,France
August-10,August-17,Tokyo,Japan
August-18,August-25,Sydney,Australia
Using CSV files in Python

Opens the file in reading


mode
f = open("itinerary.csv", "r")

csv_reader = csv.DictReader(f) Reads the file


itinerary = [] contents and
for row in csv_reader: assigns them to a
itinerary.append(row) variable

f.close()
Closes the file
Using CSV files in Python

f = open("itinerary.csv", mode='r')

csv_reader = csv.DictReader(f)
itinerary = []
for row in csv_reader:
itinerary.append(row)

f.close()
Using CSV files in Python

Tells Python the file is a csv so


that it reads it using dictionaries
f for
= each
open("itinerary.csv",
row mode='r')

csv_reader = csv.DictReader(f)
itinerary = [] Creates an empty list to store the data
for row in csv_reader:
itinerary.append(row)

Iterates
f.close()over each row in the csv file and
appends its contents to the empty list
AI Python for Beginners

Lesson 6: Turning Code


Blocks into Reusable
Functions
What is a function?

A reusable set of commands to perform a


specific task
Parts of a function

Definition Function
Parentheses Parameters
statement name

def print_journal(file): Colon


Block of f = open(file, "r")
commands
journal = f.read()
Indented
f.close()
print(journal)
Returning

Definition Function
Parentheses Parameters
statement name

def read_journal(file): Colon


Block of f = open(file, "r")
commands
journal = f.read()
Indented
f.close()
return journal
Return Value to return
statement
Without functions
vs with function

fahrenheit = 72 def fahrenheit_to_celsius(fahrenheit):


celsius = (fahrenheit - 32) * 5 / 9 celsius = (fahrenheit - 32) * 5 / 9
print(f"Equivalent to {celsius}°C") print(f"Equivalent to {celsius}°C")

fahrenheit = 68 fahrenheit_to_celsius(72)
celsius = (fahrenheit - 32) * 5 / 9 fahrenheit_to_celsius(68)
print(f"Equivalent to {celsius}°C") fahrenheit_to_celsius(76)
fahrenheit_to_celsius(71)
fahrenheit = 76
celsius = (fahrenheit - 32) * 5 / 9 Functions let you avoid writing
print(f"Equivalent to {celsius}°C") blocks of commands
repeatedly. It also makes your
fahrenheit = 71 programs easier to read
celsius = (fahrenheit - 32) * 5 / 9
print(f"Equivalent to {celsius}°C") and understand.
AI Python for Beginners

Lesson 7: Detailed
Itineraries for Multiple
Cities

You might also like