Week 2

Variables

A variable is a descriptive name to label and store data in memory. Overall, variables can be thought of as the “data” that we use in our program

Variables can be assigned to be either values or expressions using the = operator

Variables help programmers more clearly understand and read the meaning of code. They also allow for the reuse of common values and in turn, the reusability of code

Practice:


Functions

A function is a self contained module of code that accomplishes a specific task by transforming a particular input into a corresponding output. We write functions to generalize a given task so the function can be “called” over and over again in the future

The general form of a function is as follows:

def function_name(parameters):
    body

Practice:


Modules

Most programming languages (Python included) contain many libraries of pre-built functions for your own convenience called modules

To get a list of which functions a module contains, in the shell, you can use the command dir(module) where module is the name of the module you want to check

To find out more about a particular function, in the shell, you can use the command help(module.function) where module is the name of the module the function resides in and function is the name of the function you want to check

To use a module’s functions, you must first import the module by writing import module at the top of your program replacing module with the module you wish to import

Example:

>>> python
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', ...
>>> help(math.floor)
...

Practice (using the module math):


Strings

Python uses a built-in type called str which stands for “string”. We can iterate and index the characters of a string but the string itself is immutable (we cannot modify the values of a string once created, but we can construct new strings)

For the following print statements, recall that the + operator concatenates (combines) strings and the , operator can be used to separate items in a print statement

String Iteration Practice:

A method is a special type of function that belongs to a specific data type and is designed to make use of a specific value it is “called on”. These functions cannot be called on their own and must be attached to a type in order to be called

For example, the str method upper() uses the value of the string it is attached to and returns its uppercase equivalent

>>> 'keviniscool'.upper()
'KEVINISCOOL'

String Methods Practice:

For the following practice, you are NOT allowed to use any of Python’s str methods. However, you may use functions from __builtins__

String Functions Practice:


Lists

Python uses a built-in type called list which represents a list. A list is similar to the concept of “arrays” seen in other programming languages with the differences being that a Python list can contain any collection of types (not just a single type) and that when instantiating (creating) a list, you do not need to initially specify the length of the list

We process lists in a similar manner to strings. Suppose we have the list:

fruits = ['apple' ,'banana', 'orange', 'cherry', 'grape', 'onion']

Then fruits[1:4] would return a new list equal to ['banana', 'orange', 'cherry']

Practice:


While Loops

A while loop loops through an iterable object as long as a given condition is true. Use while loops to complete the following functions. Do not use for loops. The type of the parameter is given in parentheses. Do not actually use list as the name of a parameter or variable

Practice:


Nested Lists

Because a list can contain any type; it is possible for a list to contain an element that is a list in itself. When this happens, it is called a nested list or a list of lists

Example:

avengers = [["peter parker", "spiderman", True, 22], ["tony stark", "ironman", False, 53], ["stephen strange", "doctor strange", True, 42], ["steve rogers", "captain america", False, 37], ["bruce banner", "the hulk", False, 51], ["peter quill", "starlord", True, 39]]

We can access each element of the list avengers using its index:

>>> avengers[0]
["peter parker", "spiderman", False]

We can element each item of the inner lists using the approriate inner index:

>>> avengers[0][1]
"spiderman"

In the above example, we access the 0th element of the outer list and then the 1st element of the inner list to obtain the final value of "spiderman"

Practice:


Text Files

In this section, you will practice opening, reading, and writing text files. Use dir and help to get information on file methods provided by Python

Before you begin, download the text file data.txt and the starter code file file_basics.py. Implement the three function stubs according to their docstring descriptions


Resources