SlideShare una empresa de Scribd logo
1 de 28
Unit 3: Python
Lesson 4: Functions
January 5, 2014
Lesson 4: Functions
Introduction to
Programming
Lesson 1

Designing a
Game
Lesson 8

Sorting and
Searching
Lesson 9

Hardware &
Software
Lesson 2

Working with
Files
Lesson 7

Advanced
Algorithms
Lesson 10

Boolean Logic
Lesson 3

Loops
Lesson 6

Navigating the
Web (?)
Lesson 11

Functions
Lesson 4

Data Types
Lesson 5

Putting It All
Together
Lesson 12
2
Recap from last time (I)
• Boolean Logic is a phrase used to describe the way computers think
only in TRUE and FALSE
• AND, NOT, and OR can be used to combine statements together,
but their meanings are a little different from their English meanings

• Remember that OR means one, or the other, or both!

or
Fish only

or
Chips only

Fish and Chips

3
Recap from last time (II)
• IF statements allow a computer to perform differently when in
different situations
• Add ELSE to decide what will happen when the IF statement is
FALSE

• Add ELIF when you have more than two cases to choose from

Manchester United wins!

Liverpool wins!

Penalty kicks
4
Functions are important to understand
• Computers often use functions to do all kinds of actions, whether it’s
adding up numbers or even just showing us text on the screen
• To understand how we can program computers to do things for us,
we first need to understand more about functions

5
A function is like a vending machine
• A function is a reusable section of code that performs a specific task
• Like a vending machine, a function is programmed to accept
something from you (money) and give you something back in return
(a fizzy drink)

6
Any task can be made into a function (I)
• When programming, you can write functions to do just about
anything
• For example, you could write a function that will accept two numbers
and add them together for you
Function

7
Any task can be made into a function (II)
• When programming, you can write functions to do just about
anything
• For example, you could write a function that will accept two numbers
and add them together for you
Function

• Or you could write a function to accept a calendar date and figure
out its day of the week
Function
8
Functions don’t need to accept anything (I)
• Sometimes, functions will perform tasks that don’t require any input
from you
• For example, you could have a function that adds all the numbers
from 1 to 100 and returns the result
Function

5050

9
Functions don’t need to accept anything (II)
• Sometimes, functions will perform tasks that don’t require any input
from you
• For example, you could have a function that adds all the numbers
from 1 to 100 and returns the result
Function

5050

• You could even have a function that visits The Telegraph’s website
and returns the top headline
Function

“Drivers face 60mph speed limit on
motorways due to EU pollution rules”
10
Writing functions in Python is easy!
• For a function that will say “Happy Birthday!”, you just need to write:
Don’t forget the colon!

def birthday():
print “Happy Birthday!”

11
Run this function by typing birthday()
• For a function that will say “Happy Birthday!”, you just need to write:
Don’t forget the colon!

def birthday():
print “Happy Birthday!”
birthday()
• Now that the function is written, every time we run birthday(), , the
computer will wish us Happy Birthday!
Happy

Function

Happy Birthday!

12
Use words inside the parentheses to give the
function an input
• We can rewrite our function to say any phrase by adding an input:
def parrot(phrase):
print phrase
Any inputs go here

13
Include the input when running the function
• We can rewrite our function to say any phrase by adding an input:
def parrot(phrase):
print phrase
Any inputs go here

parrot(“Happy New Year!”) , the computer will wish us
• Now if we run birthday(),
Happy New Year!

“Happy New Year!”

Function

Happy New Year!

14
Exercise: Design a Dice Game (I)
• For this exercise, we’ll write a function of our own to create a simple
dice game in Python
• First, open your internet browser, go to labs.codecademy.com, and
click on the button for Python

15
Exercise: Design a Dice Game (II)
• Once the page finishes loading, you should see white space on the
left. This is where we’ll be writing the code for our function
• The black space on the left is where we’ll run our code to start a
game of dice!

We’ll start by
writing code here
We’ll go here
later to play dice

16
Exercise: Design a Dice Game (III)
• On the left side, let’s write the code shown below
Don’t forget the colon

Remember to tab these lines over

17
Exercise: Design a Dice Game (IV)
• If you look closely, you’ll see some familiar print statements and an
IF statement. Let’s go through this line-by-line

18
Exercise: Design a Dice Game (V)

Pick a random number from 1 to 6

19
Exercise: Design a Dice Game (VI)

Pick a random number from 1 to 6
Tell us what we rolled

20
Exercise: Design a Dice Game (VII)

Pick a random number from 1 to 6
Tell us what we rolled

Pick another number from 1 to 6
and tell us what you rolled

21
Exercise: Design a Dice Game (VIII)

Pick a random number from 1 to 6
Tell us what we rolled

Pick another number from 1 to 6
and tell us what you rolled
If our roll was larger, tell us we won!

22
Exercise: Design a Dice Game (IX)

Pick a random number from 1 to 6
Tell us what we rolled

Pick another number from 1 to 6
and tell us what you rolled
If our roll was larger, tell us we won

If your roll was larger, tell us we lost

23
Exercise: Design a Dice Game (X)

Pick a random number from 1 to 6
Tell us what we rolled

Pick another number from 1 to 6
and tell us what you rolled
If our roll was larger, tell us we won

If your roll was larger, tell us we lost
If the rolls were the same, tell us we tied
24
Exercise: Design a Dice Game (V)
• Now click

and wait for a few seconds

• Once a second yellow arrow appears on the right side, type
to play the game!

25
Summary (I)
• A function is a reusable section of code that performs a specific task
• Think of functions like vending machines – they can accept an input
and return an output
• Sometimes functions don’t even need an input to return an output

Input money

Output a fizzy drink

26
Summary (II)
• The syntax for writing a function in Python looks like this:
def parrot(phrase):
print phrase
Any inputs go here

• You run this function by typing its name: parrot(“Happy New Year!”)
Happy New Year!

27
What to do on your own
1. Go to URL to complete the Codecademy course online

2. Do the practice set on the material learned

1. Take the follow-up quiz to test your understanding

28

Más contenido relacionado

La actualidad más candente

PART 1 - Python Tutorial | Variables and Data Types in Python
PART 1 - Python Tutorial | Variables and Data Types in PythonPART 1 - Python Tutorial | Variables and Data Types in Python
PART 1 - Python Tutorial | Variables and Data Types in PythonShivam Mitra
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingAkhil Kaushik
 
rohit sharma - dev ops virtual assistant - automate devops stuffs using nlp a...
rohit sharma - dev ops virtual assistant - automate devops stuffs using nlp a...rohit sharma - dev ops virtual assistant - automate devops stuffs using nlp a...
rohit sharma - dev ops virtual assistant - automate devops stuffs using nlp a...Dariia Seimova
 
20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshopamptiny
 
Python presentation
Python presentationPython presentation
Python presentationgaganapponix
 
03. language of computer & translators
03. language of computer & translators03. language of computer & translators
03. language of computer & translatorsTimesRide
 
Using Telegram to interact with an LED Matrix
Using Telegram to interact with an LED MatrixUsing Telegram to interact with an LED Matrix
Using Telegram to interact with an LED Matrixxorduna
 
Python games
Python gamesPython games
Python gamesdxbeeh
 
Python games
Python gamesPython games
Python gamesdxbeeh
 
Python Training in Pune
Python Training in PunePython Training in Pune
Python Training in PuneClassboat.com
 

La actualidad más candente (18)

PART 1 - Python Tutorial | Variables and Data Types in Python
PART 1 - Python Tutorial | Variables and Data Types in PythonPART 1 - Python Tutorial | Variables and Data Types in Python
PART 1 - Python Tutorial | Variables and Data Types in Python
 
Python
PythonPython
Python
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
rohit sharma - dev ops virtual assistant - automate devops stuffs using nlp a...
rohit sharma - dev ops virtual assistant - automate devops stuffs using nlp a...rohit sharma - dev ops virtual assistant - automate devops stuffs using nlp a...
rohit sharma - dev ops virtual assistant - automate devops stuffs using nlp a...
 
Python basic
Python basicPython basic
Python basic
 
20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshop
 
Python programming
Python programmingPython programming
Python programming
 
Python presentation
Python presentationPython presentation
Python presentation
 
Python Lecture 1
Python Lecture 1Python Lecture 1
Python Lecture 1
 
Code in python
Code in pythonCode in python
Code in python
 
Why learn python in 2017?
Why learn python in 2017?Why learn python in 2017?
Why learn python in 2017?
 
Learning Python
Learning PythonLearning Python
Learning Python
 
03. language of computer & translators
03. language of computer & translators03. language of computer & translators
03. language of computer & translators
 
Using Telegram to interact with an LED Matrix
Using Telegram to interact with an LED MatrixUsing Telegram to interact with an LED Matrix
Using Telegram to interact with an LED Matrix
 
Python games
Python gamesPython games
Python games
 
Python games
Python gamesPython games
Python games
 
Python Training in Pune
Python Training in PunePython Training in Pune
Python Training in Pune
 
Numpy tutorial
Numpy tutorialNumpy tutorial
Numpy tutorial
 

Similar a Lesson 304 05 jan14-1500-ay

W1-Intro to python.pptx
W1-Intro to python.pptxW1-Intro to python.pptx
W1-Intro to python.pptxNaziaPerwaiz2
 
Build a virtual pet with javascript (april 2017)
Build a virtual pet with javascript (april 2017)Build a virtual pet with javascript (april 2017)
Build a virtual pet with javascript (april 2017)Thinkful
 
Easy coding a multi device game with FireMonkey
Easy coding a multi device game with FireMonkeyEasy coding a multi device game with FireMonkey
Easy coding a multi device game with FireMonkeypprem
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsRuth Marvin
 
Application development with Python - Desktop application
Application development with Python - Desktop applicationApplication development with Python - Desktop application
Application development with Python - Desktop applicationBao Long Nguyen Dang
 
Ios training-cum-course-in-mumbai-
Ios training-cum-course-in-mumbai-Ios training-cum-course-in-mumbai-
Ios training-cum-course-in-mumbai-vibrantuser
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?Colin Riley
 
Blueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBlueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBhalaji Nagarajan
 
Ios-training-institute-in-mumbai
Ios-training-institute-in-mumbaiIos-training-institute-in-mumbai
Ios-training-institute-in-mumbaivibrantuser
 
Ios-training-institute-in-mumbai
Ios-training-institute-in-mumbaiIos-training-institute-in-mumbai
Ios-training-institute-in-mumbaivibrantuser
 
Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]Saajid Akram
 
Functional pogramming hl overview
Functional pogramming hl overviewFunctional pogramming hl overview
Functional pogramming hl overviewElad Avneri
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptxHaythamBarakeh1
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdfgmadhu8
 
Introduction to programming - class 11
Introduction to programming - class 11Introduction to programming - class 11
Introduction to programming - class 11Paul Brebner
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPTShivam Gupta
 

Similar a Lesson 304 05 jan14-1500-ay (20)

W1-Intro to python.pptx
W1-Intro to python.pptxW1-Intro to python.pptx
W1-Intro to python.pptx
 
Build a virtual pet with javascript (april 2017)
Build a virtual pet with javascript (april 2017)Build a virtual pet with javascript (april 2017)
Build a virtual pet with javascript (april 2017)
 
Easy coding a multi device game with FireMonkey
Easy coding a multi device game with FireMonkeyEasy coding a multi device game with FireMonkey
Easy coding a multi device game with FireMonkey
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
 
Application development with Python - Desktop application
Application development with Python - Desktop applicationApplication development with Python - Desktop application
Application development with Python - Desktop application
 
Ios training-cum-course-in-mumbai-
Ios training-cum-course-in-mumbai-Ios training-cum-course-in-mumbai-
Ios training-cum-course-in-mumbai-
 
ForLoops.pptx
ForLoops.pptxForLoops.pptx
ForLoops.pptx
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?
 
Blueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBlueprints: Introduction to Python programming
Blueprints: Introduction to Python programming
 
python.pdf
python.pdfpython.pdf
python.pdf
 
Ios-training-institute-in-mumbai
Ios-training-institute-in-mumbaiIos-training-institute-in-mumbai
Ios-training-institute-in-mumbai
 
Ios-training-institute-in-mumbai
Ios-training-institute-in-mumbaiIos-training-institute-in-mumbai
Ios-training-institute-in-mumbai
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]
 
Functional pogramming hl overview
Functional pogramming hl overviewFunctional pogramming hl overview
Functional pogramming hl overview
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptx
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdf
 
06b extra methods
06b extra methods06b extra methods
06b extra methods
 
Introduction to programming - class 11
Introduction to programming - class 11Introduction to programming - class 11
Introduction to programming - class 11
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 

Más de Codecademy Ren

Lesson 303 05 jan14-1500-ay
Lesson 303 05 jan14-1500-ayLesson 303 05 jan14-1500-ay
Lesson 303 05 jan14-1500-ayCodecademy Ren
 
Lesson 207 19 oct13-1500-ay
Lesson 207 19 oct13-1500-ayLesson 207 19 oct13-1500-ay
Lesson 207 19 oct13-1500-ayCodecademy Ren
 
Lesson 206 11 oct13-1500-ay
Lesson 206 11 oct13-1500-ayLesson 206 11 oct13-1500-ay
Lesson 206 11 oct13-1500-ayCodecademy Ren
 
Lesson 205 07 oct13-1500-ay
Lesson 205 07 oct13-1500-ayLesson 205 07 oct13-1500-ay
Lesson 205 07 oct13-1500-ayCodecademy Ren
 
Lesson 204 03 oct13-1500-ay
Lesson 204 03 oct13-1500-ayLesson 204 03 oct13-1500-ay
Lesson 204 03 oct13-1500-ayCodecademy Ren
 
Lesson 203 18 sep13-1500-ay
Lesson 203 18 sep13-1500-ayLesson 203 18 sep13-1500-ay
Lesson 203 18 sep13-1500-ayCodecademy Ren
 
Lesson 202 02 oct13-1800-ay
Lesson 202 02 oct13-1800-ayLesson 202 02 oct13-1800-ay
Lesson 202 02 oct13-1800-ayCodecademy Ren
 
Lesson 201 14 sep13-1500-ay
Lesson 201 14 sep13-1500-ayLesson 201 14 sep13-1500-ay
Lesson 201 14 sep13-1500-ayCodecademy Ren
 
Lesson 110 24 aug13-1400-ay
Lesson 110 24 aug13-1400-ayLesson 110 24 aug13-1400-ay
Lesson 110 24 aug13-1400-ayCodecademy Ren
 
Lesson 112 24 aug13-2300-ay
Lesson 112 24 aug13-2300-ayLesson 112 24 aug13-2300-ay
Lesson 112 24 aug13-2300-ayCodecademy Ren
 
Lesson 109 23 aug13-1430-ay
Lesson 109 23 aug13-1430-ayLesson 109 23 aug13-1430-ay
Lesson 109 23 aug13-1430-ayCodecademy Ren
 
Lesson 108 23 aug13-1430-ay
Lesson 108 23 aug13-1430-ayLesson 108 23 aug13-1430-ay
Lesson 108 23 aug13-1430-ayCodecademy Ren
 
Lesson 107 23 aug13-1430-ay
Lesson 107 23 aug13-1430-ayLesson 107 23 aug13-1430-ay
Lesson 107 23 aug13-1430-ayCodecademy Ren
 
Lesson 106 23 aug13-1430-ay
Lesson 106 23 aug13-1430-ayLesson 106 23 aug13-1430-ay
Lesson 106 23 aug13-1430-ayCodecademy Ren
 
Lesson 105 23 aug13-1430-ay
Lesson 105 23 aug13-1430-ayLesson 105 23 aug13-1430-ay
Lesson 105 23 aug13-1430-ayCodecademy Ren
 
Lesson 102 23 aug13-1430-ay
Lesson 102 23 aug13-1430-ayLesson 102 23 aug13-1430-ay
Lesson 102 23 aug13-1430-ayCodecademy Ren
 
Lesson 101 23 aug13-1430-ay
Lesson 101 23 aug13-1430-ayLesson 101 23 aug13-1430-ay
Lesson 101 23 aug13-1430-ayCodecademy Ren
 
Lesson 102 25 aug13-2200-ay
Lesson 102 25 aug13-2200-ayLesson 102 25 aug13-2200-ay
Lesson 102 25 aug13-2200-ayCodecademy Ren
 
Lesson 103 23 aug13-1430-ay
Lesson 103 23 aug13-1430-ayLesson 103 23 aug13-1430-ay
Lesson 103 23 aug13-1430-ayCodecademy Ren
 
Lesson 104 23 aug13-1430-ay
Lesson 104 23 aug13-1430-ayLesson 104 23 aug13-1430-ay
Lesson 104 23 aug13-1430-ayCodecademy Ren
 

Más de Codecademy Ren (20)

Lesson 303 05 jan14-1500-ay
Lesson 303 05 jan14-1500-ayLesson 303 05 jan14-1500-ay
Lesson 303 05 jan14-1500-ay
 
Lesson 207 19 oct13-1500-ay
Lesson 207 19 oct13-1500-ayLesson 207 19 oct13-1500-ay
Lesson 207 19 oct13-1500-ay
 
Lesson 206 11 oct13-1500-ay
Lesson 206 11 oct13-1500-ayLesson 206 11 oct13-1500-ay
Lesson 206 11 oct13-1500-ay
 
Lesson 205 07 oct13-1500-ay
Lesson 205 07 oct13-1500-ayLesson 205 07 oct13-1500-ay
Lesson 205 07 oct13-1500-ay
 
Lesson 204 03 oct13-1500-ay
Lesson 204 03 oct13-1500-ayLesson 204 03 oct13-1500-ay
Lesson 204 03 oct13-1500-ay
 
Lesson 203 18 sep13-1500-ay
Lesson 203 18 sep13-1500-ayLesson 203 18 sep13-1500-ay
Lesson 203 18 sep13-1500-ay
 
Lesson 202 02 oct13-1800-ay
Lesson 202 02 oct13-1800-ayLesson 202 02 oct13-1800-ay
Lesson 202 02 oct13-1800-ay
 
Lesson 201 14 sep13-1500-ay
Lesson 201 14 sep13-1500-ayLesson 201 14 sep13-1500-ay
Lesson 201 14 sep13-1500-ay
 
Lesson 110 24 aug13-1400-ay
Lesson 110 24 aug13-1400-ayLesson 110 24 aug13-1400-ay
Lesson 110 24 aug13-1400-ay
 
Lesson 112 24 aug13-2300-ay
Lesson 112 24 aug13-2300-ayLesson 112 24 aug13-2300-ay
Lesson 112 24 aug13-2300-ay
 
Lesson 109 23 aug13-1430-ay
Lesson 109 23 aug13-1430-ayLesson 109 23 aug13-1430-ay
Lesson 109 23 aug13-1430-ay
 
Lesson 108 23 aug13-1430-ay
Lesson 108 23 aug13-1430-ayLesson 108 23 aug13-1430-ay
Lesson 108 23 aug13-1430-ay
 
Lesson 107 23 aug13-1430-ay
Lesson 107 23 aug13-1430-ayLesson 107 23 aug13-1430-ay
Lesson 107 23 aug13-1430-ay
 
Lesson 106 23 aug13-1430-ay
Lesson 106 23 aug13-1430-ayLesson 106 23 aug13-1430-ay
Lesson 106 23 aug13-1430-ay
 
Lesson 105 23 aug13-1430-ay
Lesson 105 23 aug13-1430-ayLesson 105 23 aug13-1430-ay
Lesson 105 23 aug13-1430-ay
 
Lesson 102 23 aug13-1430-ay
Lesson 102 23 aug13-1430-ayLesson 102 23 aug13-1430-ay
Lesson 102 23 aug13-1430-ay
 
Lesson 101 23 aug13-1430-ay
Lesson 101 23 aug13-1430-ayLesson 101 23 aug13-1430-ay
Lesson 101 23 aug13-1430-ay
 
Lesson 102 25 aug13-2200-ay
Lesson 102 25 aug13-2200-ayLesson 102 25 aug13-2200-ay
Lesson 102 25 aug13-2200-ay
 
Lesson 103 23 aug13-1430-ay
Lesson 103 23 aug13-1430-ayLesson 103 23 aug13-1430-ay
Lesson 103 23 aug13-1430-ay
 
Lesson 104 23 aug13-1430-ay
Lesson 104 23 aug13-1430-ayLesson 104 23 aug13-1430-ay
Lesson 104 23 aug13-1430-ay
 

Último

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Último (20)

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

Lesson 304 05 jan14-1500-ay

  • 1. Unit 3: Python Lesson 4: Functions January 5, 2014
  • 2. Lesson 4: Functions Introduction to Programming Lesson 1 Designing a Game Lesson 8 Sorting and Searching Lesson 9 Hardware & Software Lesson 2 Working with Files Lesson 7 Advanced Algorithms Lesson 10 Boolean Logic Lesson 3 Loops Lesson 6 Navigating the Web (?) Lesson 11 Functions Lesson 4 Data Types Lesson 5 Putting It All Together Lesson 12 2
  • 3. Recap from last time (I) • Boolean Logic is a phrase used to describe the way computers think only in TRUE and FALSE • AND, NOT, and OR can be used to combine statements together, but their meanings are a little different from their English meanings • Remember that OR means one, or the other, or both! or Fish only or Chips only Fish and Chips 3
  • 4. Recap from last time (II) • IF statements allow a computer to perform differently when in different situations • Add ELSE to decide what will happen when the IF statement is FALSE • Add ELIF when you have more than two cases to choose from Manchester United wins! Liverpool wins! Penalty kicks 4
  • 5. Functions are important to understand • Computers often use functions to do all kinds of actions, whether it’s adding up numbers or even just showing us text on the screen • To understand how we can program computers to do things for us, we first need to understand more about functions 5
  • 6. A function is like a vending machine • A function is a reusable section of code that performs a specific task • Like a vending machine, a function is programmed to accept something from you (money) and give you something back in return (a fizzy drink) 6
  • 7. Any task can be made into a function (I) • When programming, you can write functions to do just about anything • For example, you could write a function that will accept two numbers and add them together for you Function 7
  • 8. Any task can be made into a function (II) • When programming, you can write functions to do just about anything • For example, you could write a function that will accept two numbers and add them together for you Function • Or you could write a function to accept a calendar date and figure out its day of the week Function 8
  • 9. Functions don’t need to accept anything (I) • Sometimes, functions will perform tasks that don’t require any input from you • For example, you could have a function that adds all the numbers from 1 to 100 and returns the result Function 5050 9
  • 10. Functions don’t need to accept anything (II) • Sometimes, functions will perform tasks that don’t require any input from you • For example, you could have a function that adds all the numbers from 1 to 100 and returns the result Function 5050 • You could even have a function that visits The Telegraph’s website and returns the top headline Function “Drivers face 60mph speed limit on motorways due to EU pollution rules” 10
  • 11. Writing functions in Python is easy! • For a function that will say “Happy Birthday!”, you just need to write: Don’t forget the colon! def birthday(): print “Happy Birthday!” 11
  • 12. Run this function by typing birthday() • For a function that will say “Happy Birthday!”, you just need to write: Don’t forget the colon! def birthday(): print “Happy Birthday!” birthday() • Now that the function is written, every time we run birthday(), , the computer will wish us Happy Birthday! Happy Function Happy Birthday! 12
  • 13. Use words inside the parentheses to give the function an input • We can rewrite our function to say any phrase by adding an input: def parrot(phrase): print phrase Any inputs go here 13
  • 14. Include the input when running the function • We can rewrite our function to say any phrase by adding an input: def parrot(phrase): print phrase Any inputs go here parrot(“Happy New Year!”) , the computer will wish us • Now if we run birthday(), Happy New Year! “Happy New Year!” Function Happy New Year! 14
  • 15. Exercise: Design a Dice Game (I) • For this exercise, we’ll write a function of our own to create a simple dice game in Python • First, open your internet browser, go to labs.codecademy.com, and click on the button for Python 15
  • 16. Exercise: Design a Dice Game (II) • Once the page finishes loading, you should see white space on the left. This is where we’ll be writing the code for our function • The black space on the left is where we’ll run our code to start a game of dice! We’ll start by writing code here We’ll go here later to play dice 16
  • 17. Exercise: Design a Dice Game (III) • On the left side, let’s write the code shown below Don’t forget the colon Remember to tab these lines over 17
  • 18. Exercise: Design a Dice Game (IV) • If you look closely, you’ll see some familiar print statements and an IF statement. Let’s go through this line-by-line 18
  • 19. Exercise: Design a Dice Game (V) Pick a random number from 1 to 6 19
  • 20. Exercise: Design a Dice Game (VI) Pick a random number from 1 to 6 Tell us what we rolled 20
  • 21. Exercise: Design a Dice Game (VII) Pick a random number from 1 to 6 Tell us what we rolled Pick another number from 1 to 6 and tell us what you rolled 21
  • 22. Exercise: Design a Dice Game (VIII) Pick a random number from 1 to 6 Tell us what we rolled Pick another number from 1 to 6 and tell us what you rolled If our roll was larger, tell us we won! 22
  • 23. Exercise: Design a Dice Game (IX) Pick a random number from 1 to 6 Tell us what we rolled Pick another number from 1 to 6 and tell us what you rolled If our roll was larger, tell us we won If your roll was larger, tell us we lost 23
  • 24. Exercise: Design a Dice Game (X) Pick a random number from 1 to 6 Tell us what we rolled Pick another number from 1 to 6 and tell us what you rolled If our roll was larger, tell us we won If your roll was larger, tell us we lost If the rolls were the same, tell us we tied 24
  • 25. Exercise: Design a Dice Game (V) • Now click and wait for a few seconds • Once a second yellow arrow appears on the right side, type to play the game! 25
  • 26. Summary (I) • A function is a reusable section of code that performs a specific task • Think of functions like vending machines – they can accept an input and return an output • Sometimes functions don’t even need an input to return an output Input money Output a fizzy drink 26
  • 27. Summary (II) • The syntax for writing a function in Python looks like this: def parrot(phrase): print phrase Any inputs go here • You run this function by typing its name: parrot(“Happy New Year!”) Happy New Year! 27
  • 28. What to do on your own 1. Go to URL to complete the Codecademy course online 2. Do the practice set on the material learned 1. Take the follow-up quiz to test your understanding 28