29
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it Docstrings Use to explain the code Must be a string and the first line in a function or module • Include parameter names, mention their types and describe the return value and type

Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Embed Size (px)

Citation preview

Page 1: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Last WeekModules• Save functions to a file, e.g., filename.py• The file filename.py is a module• We can use the functions in filename.py

by importing it

Docstrings• Use to explain the code• Must be a string and the first line in a

function or module• Include parameter names, mention their

types and describe the return value and type

Page 2: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

This Week

More on modules

New Python statementsif statementprint statement details

input builtin functionfor … in …: statement

The boolean type

Page 3: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Modules

• Sometimes we want to use some functions frequently

• Save them to a file, e.g., filename.py

• Include them with the Python command import filename

• We call this file a module.

Page 4: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Modules

• Python has builtin functions, e.g., – pow(x,y) returns xy

– sqrt(x) returns √x

• Organized into different modules (stored in different files)

• pow(x,y) and sqrt(x) belong to the math module

Page 5: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

More on importimport math

– Need to say math.sqrt(2,3) to use sqrt()

– Prevents ambiguity

– But inconvenient to type math.sqrt

Solution– Only import specific functions:

>>> from math import pow, sqrt – Import all functions when you know there

are no conflicts>>> from math import *

– Now we can say sqrt(5)

Page 6: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

The __builtins__ Module

•Ways to get help with functions:

– dir(module): list the functions in a module

– help(function): show the docstrings for a function or module

– import module: include the functions defined in a module

Page 7: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

The “if” StatementEnglish example:

Check The Temperature:

If the temperature > 0 then it is “above the freezing point”

Otherwise, if the temperature = 0 thenit is “at the freezing point”

Otherwise it is “below the freezing point”

Page 8: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

The “if” StatementPython example:

def check_temp(temperature):

If the temperature > 0 then it is “above the freezing point”

Otherwise, if the temperature = 0 thenit is “at the freezing point”

Otherwise it is “below the freezing point”

Page 9: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

The “if” StatementPython example:

def check_temp(temperature):if temperature > 0:

return “above the freezing point”

Otherwise, if the temperature = 0 thenit is “at the freezing point”

Otherwise it is “below the freezing point”

Page 10: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

The “if” StatementPython example:

def check_temp(temperature):

if temperature > 0:

return “above the freezing point”

elif temperature == 0:

return “at the freezing point”

Otherwise it is “below the freezing point”

Page 11: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

The “if” StatementPython example:

def check_temp(temperature):

if temperature > 0:

return “above the freezing point”

elif temperature == 0:

return “at the freezing point”else:

return “below the freezing point”

Page 12: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

The “if” Statement

English Python

If condition if condition:

Otherwise, if condition elif condition:

Otherwise else:

Page 13: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Nested “if” StatementsEnglish example:

Check The Temperature:

If the temperature > 0 then if the temperature >100 then

it is “above the boiling point”Otherwise, if the temperature> 37 then

it is “above body temperature”Otherwise,

it is “above the freezing point”Otherwise, if the temperature = 0 then

it is “at the freezing point”Otherwise

it is “below the freezing point”

Page 14: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Nested “if” StatementsEnglish example:

def check_temp(temperature):

if temperature > 0:

if the temperature >100 thenit is “above the boiling point”

Otherwise, if the temperature> 37 thenit is “above body temperature”

Otherwise, it is “above the freezing point”

elif temperature == 0:

return “at the freezing point”else:

return “below the freezing point”

Page 15: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Nested “if” StatementsEnglish example:

def check_temp(temperature):

if temperature > 0:

if temperature > 100:

return “above the boiling point”Otherwise, if the temperature > 37 then

it is “above body temperature”Otherwise,

it is “above the freezing point”elif temperature == 0:

return “at the freezing point”else:

return “below the freezing point”

Page 16: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Nested “if” StatementsEnglish example:

def check_temp(temperature):

if temperature > 0:

if temperature > 100:

return “above the boiling point”elif temperature > 37:

return “above body temperature”Otherwise,

it is “above the freezing point”elif temperature == 0:

return “at the freezing point”else:

return “below the freezing point”

Page 17: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

English example:

def check_temp(temperature):

if temperature > 0:

if temperature > 100:

return “above the boiling point”elif temperature > 37:

return “above body temperature”else:

return “above the freezing point”elif temperature == 0:

return “at the freezing point”else:

return “below the freezing point”

Nested “if” Statements

>100

>37 and <=100

>0 and <=37

Page 18: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Getting User Input

When we want to display information to the screen we use print.

>>> print(“hello”)>>> print(5+6)

When we want get input from the user we use the builtin function input.

>>> input()>>> name = input(“Enter name:”)

Page 19: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Getting User Input

Q. What does input return?

A. input() always returns a string.

Suppose we need an integer value from the user.

>>> age = input(“Enter your age:”)

Q. How can we convert input’s return value to be an integer?A.

>>> age = int(input(“Enter your age:”))

Page 20: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

More on StringsQ. What is a string exactly?

A. A sequence of characters with position numbers called indices.

“Hello World”: “H” is at index 0.

Q. How can we select parts of a string?

A. We can select a particular character by specifying its index:

>>>”Hello World”[2]“l”

Page 21: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

More on Strings We can select part of a string by specifying a range.

>>>”Hello Class”[1:6]“ello ”

Q. What string would “Hello Class”[3:] return?A. >>>”Hello Class”[3:]

“lo Class”

Q. What string would “Hello Class”[:2] return?A. >>>”Hello Class”[:2]

“He”

Page 22: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

More on Strings

Q. What else can we do to strings?

A. There are many methods that apply to strings:• “Hello World”.replace(“World”,“Class”)• “Hello Class”.count(“l”)• “Hello Class”.find(“as”)

The functions replace, count and find are called methods because they behave like operators.

Q. How would you find all the methods for strings?

A. dir(str)

Page 23: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

String Methods

s.isupper():

returns True if s is all upper case, False otherwise.

s.islower():

returns True if s is all lower case, False otherwise.

s.isdigit():

returns True if s is a number, False otherwise.

s.upper():

returns a copy of s in all upper case.

s.lower():

returns a copy of s in all lower case.

Page 24: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

String Methods

len(s): returns the length of s.

sub in s: returns true if sub is a substring of s.

We know what “anna” + “anna” returns.

Q. What about “anna”*4 ?

“annaannaannaanna”

Page 25: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Visiting the Items in a String S

Printing out the characters of the string

English:for each char in S

print the charPython:

for char in S:

print(char)

Notes:char is a variable namefor and in are Python key words

Page 26: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

for loopsFormat:

for variable in string:

statements

Example with strings:

name = ”Edward”

new = “”

for letter in name:

new = letter + new

print(new)

Page 27: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Strings Using Conversion SpecifiersWe sometimes would like to insert values of variables into strings:A1 = 60A2 = 75A3 = 88

We would like: ‘The average of 60, 75 and 88 is 74.33.’

How do we print this with our variables?

>>>print(‘The average of’, A1, ‘,’, A2, ‘ and ’, A3, ‘ is ’, (A1+A2+A3)/3)

Does this work?

Page 28: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Strings Using Conversion SpecifiersWe displayed:‘The average of 60 , 75 and 88 is 74.33333333333333 .’

Q. What’s wrong?A. Spacing is wrong around commas and periods.

We have many more decimal places than wanted.

Q. How can we fix it?A. Use conversion specifiers.

>>>print(‘The average of %d, %d and %d is %.2f’ %(A1, A2, A3, (A1+A2+A3)/3.0))

The average of 60, 75 and 88 is 74.33.

Page 29: Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it

Common Conversion Specifiers

%d display the object as a decimal integer

%f display the object as a floating point with 6 decimal places%.2f display the object as a floating point with 2 decimal places%s display the object as a string

Q. What else do we use % for?

A. Modulus. We say that % is overloaded.