Functions

Introduction to Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Functions

  • Nothing new!

  • type()

  • Piece of reusable code

  • Solves particular task

  • Call function instead of writing code yourself

Introduction to Python

Example

fam = [1.73, 1.68, 1.71, 1.89]
fam
[1.73, 1.68, 1.71, 1.89]
max(fam)
1.89

ch_3_1_slides.012.png

Introduction to Python

Example

fam = [1.73, 1.68, 1.71, 1.89]
fam
[1.73, 1.68, 1.71, 1.89]
max(fam)
1.89

ch_3_1_slides.013.png

Introduction to Python

Example

fam = [1.73, 1.68, 1.71, 1.89]
fam
[1.73, 1.68, 1.71, 1.89]
max(fam)
1.89

ch_3_1_slides.014.png

Introduction to Python

Example

fam = [1.73, 1.68, 1.71, 1.89]
fam
[1.73, 1.68, 1.71, 1.89]
max(fam)
1.89
tallest = max(fam)
tallest
1.89
Introduction to Python

round()

round(1.68, 1)
1.7
round(1.68)
2
help(round) # Open up documentation
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative.
Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.026.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.027.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.028.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.029.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.030.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.031.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.032.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.033.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.034.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.035.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.036.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 

ch_3_1_slides.037.png

Introduction to Python

round()

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None. 
    Otherwise the return value has the same type as the number. ndigits may be negative. 
  • round(number)
  • round(number, ndigits)
Introduction to Python

Find functions

  • How to know?

  • Standard task -> probably function exists!

  • The internet is your friend

Introduction to Python

Let's practice!

Introduction to Python

Preparing Video For Download...