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
Andrew Ng
Acknowledgments
Tommy Nelson Isabel Zaro Daniel Villarraga
DeepLearning.AI DeepLearning.AI DeepLearning.AI
Andrés Zarta Muhammad Mubashar Lesly Zerna
DeepLearning.AI DeepLearning.AI DeepLearning.AI
AI Python for Beginners
Module 1: Basics of AI Python Coding
AI Python for Beginners
Course Introduction
AI Python for Beginners
Lesson 1: What is
Computer Programming?
Programming helps the advancement of humanity
Programming helps the advancement of humanity
Programming: command machines to do your bidding!
A set of instructions to perform a task
Sum two numbers and display the result:
Fry the perfect egg:
# Values for num1 and num2
1.
num1 = 37
Crack an egg into a small bowl making num2 = 5
sure there are no shells on it. Set aside.
2. Add butter, or other fat, to a non-stick pan # Calculate the sum
on medium-high heat for a minute. sum = num1 + num2
3. Add egg into pan. Cook for three to four minutes. # Display the result
4. Serve. Add salt and pepper to your taste. print(sum)
Programming gives you an edge
• Automate repetitive tasks
• Analyze lots of data
• Build, improve and use AI models
Date Customer Amount
ID
06/05 CUST585 $ 12400
9
04/15 CUST572 $ 16100
9
04/12 CUST535 $ 5040
0
05/25 CUST683 $ 23200
9
... ... ...
... ... ...
... ... ...
05/10 CUST445 $ 1200
9
Programming + AI = superpowers!
Programming will let you unleash the true power of current AI tools
Why Python?
Programming Language Ratings in June 2024
Python
C++
C
Java
C#
Javascript
Go
SQL Python is currently the most popular
Visual Basic programming language.
Fortran
Delphi/Object Pascal
Swift
Assembly language • Supportive community.
MATLAB
PHP
• Chatbots like ChatGPT are reliable
Scratch
Rust
Ruby
Kotlin
COBOL
Others
Ratings (%)
Python is everywhere in AI
Self-driving cars Chatbots Smart Agriculture
Python is everywhere. Period.
AI Python for Beginners
Lesson 2: Writing Code with Chatbots
AI Python for Beginners
Lesson 3: Navigating the Learning
Platform
AI Python for
Beginners
Lesson 4: Running
your First Program
AI Python for Beginners
Lesson 5: How to Succeed in Coding
AI Python for
Beginners
Lesson 6: Data in
Python
Day-to-day data
Population density map of
South Korea
Weekly weather conditions in
Washington, DC (USA)
Prices of individual stocks with
daily highs and lows
(Image credits: South Korean Government, Weather Underground, pexels.com)
Data in Computer Programming
Text “a”, “egg”, “My favorite activity is hiking!”
Numbers 3.14, 525600, -42, 5.67e-34
Item Quantity Price per unit ($)
Tabular T-shirt 3 26
Data
Jeans 1 55
3 3 3 4 8 9
3 3 3 3 9 8
Images
2 2 4 2 8 9
1 0 1 0 7 5
2 2 3 3 1 1
1 1 2 2 1 1
Signal
Sound 1 4 3 31 15 6
Time
Text in Python: Strings
"Hello, World!"
Double quotation marks
• Strings in python are any combination of
text, number, or symbol characters enclosed
in double quotation marks
• Everything between the quotation marks is
part of the string, including white space
Text in Python: Strings
Strings
Used to represent text:
• "Hello, world"
• "My favorite drink is Earl Grey tea"
• "¯\_(ツ)_/¯"
• "2.99"
Multiline strings in Python
"""Hello, World!
It’s great to be here!"""
Triple quotes
Checking data type in python
print("Hello, World!")
Hello, World
type("Hello, World!")
str
print() and type() are functions
More about those in a later lesson…
Numbers in Python
Integers and floats
Used to represent numbers:
• Integers represent numbers with no decimal part
42
100
-9
0
• Floats have digits after the decimal place
3.14
2.99
-0.003
Using Python as a calculator
print(2 + 6) Addition
print(57 - 40) Subtraction
print(12 * 60) Multiplication
print(100 / 60) Division
Order of operations in Python
The same as in Math!
Celsius to Farenheit:
Then multiply!
Temp. in C = (Temp in F – 32 ) * 5/9
Do this first!
print(75 - 32 * 5 / 9)
Then subtract Python will do
result from 75 this step first!
print((75 – 32) * 5 / 9)
Wrap subtraction
in parentheses
AI Python for
Beginners
Lesson 7: Combining
Text and Calculations
Progress so far
Strings represent text
• "Hello, world"
Integers and floats represent numbers
• Integers: 42, 100, -9, 0
• Floats: 3.14, 2.99, -0.003
Display data to screen
• print("Hello, World!")
• print(100)
Use Python as a calculator
• print(3 * 4.5)
How do print statements work?
print("Isabel is 28 years old.")
print(f"Isabel is {28 / 7} dog years old.")
How Python carries out the
print() statement
print("Isabel is 28 years old.")
Isabel is 28 years old.
1. Tells Python to display to screen
2. Checks for opening and closing parentheses
3. Sees a quotation mark, checks for match to
close
4. Python displays everything between the quotes
to the screen exactly as written
How Python interprets f-strings
do the math 4.0
print(f"Isabel is {28 / 7} dog years old.")
1.Tells Python to display to screen
2.Checks for opening and closing parentheses
3.Sees the f character – knows this is a formatted string
4.Checks for opening and closing quotation marks
5.Checks for opening and closing curly braces
6.Carries out the calculation inside the curly
braces
How Python interprets f-strings
print(f"Isabel is {28 / 7} dog years old.")
Isabel is 4.0 dog years old.
AI Python for
Beginners
Lesson 8: Variables
Variables – what are they
age = 28
28 Value
Variable
name
Variables – what are they
age = 28
age = 5
28
Same
variable
name
Variables – what are they
age = 28
age = 5
5 New
value
Same
variable
name
age = 28 28
name = "Otto"
"Otto"
gnome_height = 12.7 12.7
What variables are for
1. Give a value a name to use
later
2. Use the same name to refer to
changing values
Score
0
Score
50
Score
150
print(score)
save_high_score(score)
Score
450
print(f"""Otto's age in dog years is {dog_age}.
So a dog that's about {dog_age} would
be the same age as Otto. Any dog born about
{dog_age} years ago would be in the
same stage of life as Otto.""")
3.0
print(f"""Otto's age in dog years is {dog_age}.
3.0
So a dog that's about {dog_age}
3.0 would
be the same age as Otto. Any dog born about
{dog_age}
3.0 years ago would be in the
same stage of life as Otto.""")
3.0
print(f"""Otto's age in dog years is {dog_age}.
3.0
So a dog that's about {dog_age}
3.0 would
be the same age as Otto. Any dog born about
{dog_age}
3.0 years ago would be in the
same stage of life as Otto.""")
Otto's age in dog years is 3.0. So a dog that's
about 3.0 would be the same age as Otto. Any dog
born about 3.0 years ago would be in the same stage
of life as Otto.
AI Python for
Beginners
Lesson 9: Building LLM
Prompts with Variables
AI Python for
Beginners
Lesson 10 – Functions:
Actions on Data
Functions make coding easier
Some functions allow you to perform actions on
data for specific tasks and provide (or
return) results
len("Hello World!")
round(42.17)
Other functions allow you to display information
print("Hello World!")
Function data, actions and
results
This function gets the number of
characters for the string you provide
Function Data or
name arguments
len("Hello World!")
Parentheses
The commonly used lingo for the data you
provide for a function is "argument."
Function data, actions and
results
This function gets the number of
characters for the string you provide
len("Hello World!")
12
result
The commonly used lingo for using a function is
"calling a function."
We also say the function returns its result.
Function data, actions and
results
This function displays what you provide
Name of function
Argument
print("Hello World!")
Hello World!
Display-only result
Assigning function results to
variables
You can save function results, which is useful for
functions that return values
Variable name
string_length = len("Hello World!")
Assignment Function call
Assigning function results to
variables
string_length = 12
12
print(string_length)
12