Intro to Programming
- "#" is sued to specify a comment
- Does calculations via PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction
- docstring: provide a description for a given function that I created
- triple-quoted string (which may span multiple lines) that comes immediately after the header of a function
- None: (This is similar to the concept of "null" in other languages.)
- Variables: created to save calculations; can't have spaces; can't start with a number (must be a letter or underscore); use = sign to assign a value
- can be multiplied against each other, etc
- Methods (vs Functions)
- Functions are standalone and can be used independently.
- Methods are associated with objects or classes and operate on their specific instances (e.g. like
- Methods have access to the data (attributes) of the object they belong to, while functions do not have such inherent access (see examples in Other Tings)
- Functions:

- A block of code designed to perform a specific task; I build the function, telling it what to do and spit out
- Every function header begins with
def, which tells Python that we are about to define a function.
- For every function, the parentheses enclosing the function argument(s) must be followed by a colon
:
- colon (
:) at the end of the if line indicates that a new code block is starting. Subsequent lines which are indented are part of that code block.
- input_var is the "argument" - the argument is the name of the variable that will be used as input to the function
- Every line of code in the function body must be indented exactly four spaces. You can do this by pushing the space bar four times, or by hitting the "Tab" button once on your keyboard
- Variables defined inside the function body cannot be accessed outside of the function
- if you need any information from a function, you need to make sure that appears in the return statement at the end of the function
- Variables defined inside a function (like
pay_aftertax) have a local scope of that function only. However, as you've seen, variables defined outside all functions (like pay_parttime) have a global scope and can be accessed anywhere
- Returns
- Without a
return statement, least_difference is completely pointless, but a function with side effects may do something useful without returning anything.
- Two examples of this:
print() and help() don't return anything. We only call them for their side effects (putting some text on the screen). Other examples of useful side effects include writing to a file, or modifying an input.
- Functions that operate on other functions are called "higher-order functions.
- Functions Library
math.ceil() It takes as a number as input and rounds the number up to the nearest integer.
round() function. It lets you round a number to a specified number of decimal places, e.g. round(almost_pi, 5)
ndigits=-1 rounds to the nearest 10, ndigits=-2 rounds to the nearest 100 and so on. Useful when dealing with large numbers and want to approximate
abs returns the absolute value of an argument; e.g. print(abs(-32)) is equal to 32
len() - gives you the length of a string; does not include include quotation marks
- Print function used for both text (with quotes) and arithmetic (without quotes)
- To print multiple things in Python with a single command, we need only separate them with a comma.
- we can specify a value for
sep to put some s; e.g. special string in between our printed arguments; print(1, 2, 3, sep=' < ') returns 1 < 2 < 3
- Return() statement - ends the function call and returns the result to the caller
- help(round) - gives you in e.g. on the function "round"
- In the same way that we can pass functions to the
help function (e.g. help(max)), we can also pass in methods: e.g. help(x.bit_length)
- print(type(x)) - returns what kind of type variable is
- Errors
NameError: it's an indication that you should check how you have spelled the variable (have a typo)
- Response Library
- <class 'int'> = Integer data type