SlideShare a Scribd company logo
1 of 30
Download to read offline
ACM init()
Day 4: May 12, 2015
From last time
• Loops!
• While loop- don't make them infinite!
• While-else
• For loops
• For-else loops exist as well
Functions
• A function is a reusable section of code written to
perform a specific task in a program.
• Why use functions?
• If there is a mistake in your program it is much easier
to debug if everything is separated out
• If you need to do the same thing over and over again
a function can reduce repetition
Example
First challenge
Modify the function to find the factorial of any number the user
inputs.
Answer
This is review.
How do we write a function?
A function has two parts: a name and a body.
def function_name(optional args):
code you want to run
Don't forget the ':'!
Function examples
How do we use a function?
Calling a function is easy! All you have to do is type the
function's name. For example, if we wanted a function that tells
us "Hi," we would do this:
The () are where you can put optional arguments.
Function with arguments
This function computes the area of a rectangle. It takes two
arguments: the lengths of the sides.
Challenge
Write a function that multiplies any two numbers the user types
in. Print out the result.
Answer
New challenge
Using the same multiply function, modify the program so it also
squares the product of the two numbers.
!
For example, if the input is (2,3) the result should be 36.
!
You cannot modify the multiply function!
Answer
We can call a function from another function!
Try this!
Write a by_three function that takes a single integer parameter,
num, and returns True if that number is evenly divisible by
three and False if not. Print out a statement with your results.
Answer
Importing
I'm lazy. I don't like constantly writing my own functions. Python
has some built in for us! We have to import them though.
Try this!
What if we want to take the square root of a number.
Fix it by importing a math
library
First tell Python we want to use the math library. Then
whenever you use a function from that library put 'math' in front
of it to access it!
This works.
The math library
math has a lot of functions you can use. For example:
exponent
log
sin
pi
factorial
...
https://docs.python.org/2/library/math.html
Let's be even lazier!
Typing math.function() constantly is annoying. If we only want
to use one function from a module, there is a shortcut!
from module import function
Import part 2
Typing 'from math import sqrt' allows us to use the sqrt
function without constantly typing math. We can only use the
sqrt function though.
Super lazy!
Is there a way to import every function from a module? Yep!
from module import *
Now we can use everything without having to type the module
name in front.
Warning
Be careful about universal imports. Some modules have a lot
of functions in them, so there is a risk you will try to define your
own function of the same name. This will cause issues.
!
Stick with importing individual functions or using
module.function()
Built-in functions
Python has some built in functions that we can use without
importing anything. We've seen a few already with strings: len,
lower, and upper.
max()
If you give max a list of numbers it will return the maximum
value.
min()
If you give min a list of numbers it will return the minimum
value.
abs()
abs() returns the absolute value of a function.
type()
Not quite as simple- type() returns the type of the argument
you gave it.
What we did today
• Functions!
• Defining
• Arguments
• Imports
• Built-in

More Related Content

What's hot

Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 
Is Python turning into Java? PyData 2017 Berlin Lightning Talk
Is Python turning into Java? PyData 2017 Berlin Lightning TalkIs Python turning into Java? PyData 2017 Berlin Lightning Talk
Is Python turning into Java? PyData 2017 Berlin Lightning TalkSteffen Wenz
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
Python Introduction | JNTUA | R19 | UNIT 1 | Functions
Python Introduction | JNTUA | R19 | UNIT 1 | FunctionsPython Introduction | JNTUA | R19 | UNIT 1 | Functions
Python Introduction | JNTUA | R19 | UNIT 1 | FunctionsFabMinds
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniquesPrabhjit Singh
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in PythonPranavSB
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: FunctionsVivek Bhargav
 
PVS-Studio team is about to produce a technical breakthrough, but for now let...
PVS-Studio team is about to produce a technical breakthrough, but for now let...PVS-Studio team is about to produce a technical breakthrough, but for now let...
PVS-Studio team is about to produce a technical breakthrough, but for now let...PVS-Studio
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003Prashant odhavani
 
Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Rafael Camacho Dejay
 
Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1Syed Farjad Zia Zaidi
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialKuntal Bhowmick
 
Conditional Rendering and rendering list in LWC
Conditional Rendering and rendering list in LWCConditional Rendering and rendering list in LWC
Conditional Rendering and rendering list in LWCSmritiSharan1
 

What's hot (20)

Inline functions
Inline functionsInline functions
Inline functions
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Thinking in Functions
Thinking in FunctionsThinking in Functions
Thinking in Functions
 
Is Python turning into Java? PyData 2017 Berlin Lightning Talk
Is Python turning into Java? PyData 2017 Berlin Lightning TalkIs Python turning into Java? PyData 2017 Berlin Lightning Talk
Is Python turning into Java? PyData 2017 Berlin Lightning Talk
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Python Introduction | JNTUA | R19 | UNIT 1 | Functions
Python Introduction | JNTUA | R19 | UNIT 1 | FunctionsPython Introduction | JNTUA | R19 | UNIT 1 | Functions
Python Introduction | JNTUA | R19 | UNIT 1 | Functions
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniques
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in Python
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: Functions
 
PVS-Studio team is about to produce a technical breakthrough, but for now let...
PVS-Studio team is about to produce a technical breakthrough, but for now let...PVS-Studio team is about to produce a technical breakthrough, but for now let...
PVS-Studio team is about to produce a technical breakthrough, but for now let...
 
Robustifying concurrent.futures, Thomas Moreau
Robustifying concurrent.futures, Thomas MoreauRobustifying concurrent.futures, Thomas Moreau
Robustifying concurrent.futures, Thomas Moreau
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 
Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02
 
Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
 
Templates1
Templates1Templates1
Templates1
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
Conditional Rendering and rendering list in LWC
Conditional Rendering and rendering list in LWCConditional Rendering and rendering list in LWC
Conditional Rendering and rendering list in LWC
 
Start with swift
Start with swiftStart with swift
Start with swift
 

Viewers also liked

The Library in Your Pocket - NJLibraryLink
The Library in Your Pocket - NJLibraryLinkThe Library in Your Pocket - NJLibraryLink
The Library in Your Pocket - NJLibraryLinkMeredith Farkas
 
Libraries to Go: Mobile Tech in Libraries
Libraries to Go: Mobile Tech in LibrariesLibraries to Go: Mobile Tech in Libraries
Libraries to Go: Mobile Tech in LibrariesEllyssa Kroski
 
14180203 an-introduction-to-android
14180203 an-introduction-to-android14180203 an-introduction-to-android
14180203 an-introduction-to-androidNeha Jain
 

Viewers also liked (6)

Introduction to Talis
Introduction to TalisIntroduction to Talis
Introduction to Talis
 
My Digital Library: Leveraging Today’s Mobile and Participatory Information E...
My Digital Library: Leveraging Today’s Mobile and Participatory Information E...My Digital Library: Leveraging Today’s Mobile and Participatory Information E...
My Digital Library: Leveraging Today’s Mobile and Participatory Information E...
 
The Library in Your Pocket - NJLibraryLink
The Library in Your Pocket - NJLibraryLinkThe Library in Your Pocket - NJLibraryLink
The Library in Your Pocket - NJLibraryLink
 
Libraries to Go: Mobile Tech in Libraries
Libraries to Go: Mobile Tech in LibrariesLibraries to Go: Mobile Tech in Libraries
Libraries to Go: Mobile Tech in Libraries
 
14180203 an-introduction-to-android
14180203 an-introduction-to-android14180203 an-introduction-to-android
14180203 an-introduction-to-android
 
Intro to HBase
Intro to HBaseIntro to HBase
Intro to HBase
 

Similar to ACM init()- Day 4

Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdfziyadaslanbey
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingestorebackupr
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfSudhanshiBakre1
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxdunhamadell
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdflailoesakhan
 
Updated Week 06 and 07 Functions In Python.pptx
Updated Week 06 and 07 Functions In Python.pptxUpdated Week 06 and 07 Functions In Python.pptx
Updated Week 06 and 07 Functions In Python.pptxCruiseCH
 
Tagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsTagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsPhilip Schwarz
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAMAN ANAND
 

Similar to ACM init()- Day 4 (20)

Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Function
FunctionFunction
Function
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
 
Functions
FunctionsFunctions
Functions
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
Updated Week 06 and 07 Functions In Python.pptx
Updated Week 06 and 07 Functions In Python.pptxUpdated Week 06 and 07 Functions In Python.pptx
Updated Week 06 and 07 Functions In Python.pptx
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Tagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsTagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also Programs
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 

More from UCLA Association of Computing Machinery

More from UCLA Association of Computing Machinery (17)

ACM init() Day 3
ACM init() Day 3ACM init() Day 3
ACM init() Day 3
 
ACM init() Day 2
ACM init() Day 2ACM init() Day 2
ACM init() Day 2
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
UCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meetingUCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meeting
 
UCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your DomainUCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your Domain
 
Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)
 
An Introduction to Sensible Typography
An Introduction to Sensible TypographyAn Introduction to Sensible Typography
An Introduction to Sensible Typography
 
Building a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground UpBuilding a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground Up
 
ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
 
ACM init() Day 5
ACM init() Day 5ACM init() Day 5
ACM init() Day 5
 
Init() Day 4
Init() Day 4Init() Day 4
Init() Day 4
 
Init() Lesson 3
Init() Lesson 3Init() Lesson 3
Init() Lesson 3
 
Init() Lesson 2
Init() Lesson 2Init() Lesson 2
Init() Lesson 2
 
ACM Init() lesson 1
ACM Init() lesson 1ACM Init() lesson 1
ACM Init() lesson 1
 
ACM Fall General Meeting
ACM Fall General Meeting ACM Fall General Meeting
ACM Fall General Meeting
 
ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM Teach - Hackathon Tips and Tricks - Spring 2014ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM Teach - Hackathon Tips and Tricks - Spring 2014
 
ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014
 

Recently uploaded

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 

Recently uploaded (20)

Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 

ACM init()- Day 4

  • 1. ACM init() Day 4: May 12, 2015
  • 2. From last time • Loops! • While loop- don't make them infinite! • While-else • For loops • For-else loops exist as well
  • 3. Functions • A function is a reusable section of code written to perform a specific task in a program. • Why use functions? • If there is a mistake in your program it is much easier to debug if everything is separated out • If you need to do the same thing over and over again a function can reduce repetition
  • 5. First challenge Modify the function to find the factorial of any number the user inputs.
  • 7. How do we write a function? A function has two parts: a name and a body. def function_name(optional args): code you want to run Don't forget the ':'!
  • 9. How do we use a function? Calling a function is easy! All you have to do is type the function's name. For example, if we wanted a function that tells us "Hi," we would do this: The () are where you can put optional arguments.
  • 10. Function with arguments This function computes the area of a rectangle. It takes two arguments: the lengths of the sides.
  • 11. Challenge Write a function that multiplies any two numbers the user types in. Print out the result.
  • 13. New challenge Using the same multiply function, modify the program so it also squares the product of the two numbers. ! For example, if the input is (2,3) the result should be 36. ! You cannot modify the multiply function!
  • 14. Answer We can call a function from another function!
  • 15. Try this! Write a by_three function that takes a single integer parameter, num, and returns True if that number is evenly divisible by three and False if not. Print out a statement with your results.
  • 17. Importing I'm lazy. I don't like constantly writing my own functions. Python has some built in for us! We have to import them though.
  • 18. Try this! What if we want to take the square root of a number.
  • 19. Fix it by importing a math library First tell Python we want to use the math library. Then whenever you use a function from that library put 'math' in front of it to access it! This works.
  • 20. The math library math has a lot of functions you can use. For example: exponent log sin pi factorial ... https://docs.python.org/2/library/math.html
  • 21. Let's be even lazier! Typing math.function() constantly is annoying. If we only want to use one function from a module, there is a shortcut! from module import function
  • 22. Import part 2 Typing 'from math import sqrt' allows us to use the sqrt function without constantly typing math. We can only use the sqrt function though.
  • 23. Super lazy! Is there a way to import every function from a module? Yep! from module import * Now we can use everything without having to type the module name in front.
  • 24. Warning Be careful about universal imports. Some modules have a lot of functions in them, so there is a risk you will try to define your own function of the same name. This will cause issues. ! Stick with importing individual functions or using module.function()
  • 25. Built-in functions Python has some built in functions that we can use without importing anything. We've seen a few already with strings: len, lower, and upper.
  • 26. max() If you give max a list of numbers it will return the maximum value.
  • 27. min() If you give min a list of numbers it will return the minimum value.
  • 28. abs() abs() returns the absolute value of a function.
  • 29. type() Not quite as simple- type() returns the type of the argument you gave it.
  • 30. What we did today • Functions! • Defining • Arguments • Imports • Built-in