Top 5 things about built-in functions and libraries.
By the end of this notebook you will know:
These learning goals will be very important to successfully complete assignment 1.
len
takes exactly one.int
, str
, and float
take one argument and return a new value.print
takes zero or more arguments.print
with no arguments prints a blank line.max
, min
, and round
:¶max
to find the higher value of one or more values.min
to find the lower valueREMEMBER: A library is a collection of modules, but these terms are often used interchangeably, especially since many libraries only consist of a single module, so don’t worry if you mix them up.
import
to load a library module into a program's memory.module_name.thing_name
..
to mean 'part of'.import numpy
print('pi is', numpy.pi) # pi is a variable in the math module
print('cos(pi) is', numpy.cos(numpy.pi)) # cos is a function in the math module
pi is 3.141592653589793 cos(pi) is -1.0
REMEMBER:
help
to learn about the content of a library module:help(numpy)
You can also view the library module's documentation online. For numpy there is a reference: https://numpy.org/doc/stable/reference/index.html
import numpy as np
print('cos(pi) is', np.cos(np.pi))
cos(pi) is -1.0
author = 'Garcia Lorca
File "<ipython-input-4-1e8d00075c36>", line 1 author = 'Garcia Lorca ^ SyntaxError: EOL while scanning string literal
print("poet"
File "<ipython-input-5-21df1632eb5c>", line 1 print("poet" ^ SyntaxError: unexpected EOF while parsing
age = 53
remaining = 100 - aege
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-6-e6aede4d6492> in <module> 1 age = 53 ----> 2 remaining = 100 - aege NameError: name 'aege' is not defined
EXERCISES - 5 min
easy_string = "abc"
print(max(easy_string))
rich = "gold"
poor = "tin"
print(max(rich, poor))
print(max(len(rich), len(poor)))
max(len(rich), poor)
run or produce an error message? If it runs, does its result make any sense?numpy
module can you use to calculate a square root without using sqrt
?longest_word = 'pneumonoultramicroscopicsilicovolcanoconiosis'
Suppose you want to select a random character from longest_word
:
Which standard library module could help you?
import numpy as np
angle = ____.degrees(____.pi / 2)
print(____)
as
. Which form do you find easier to read?