Python Installation and Setup
- Install Python
We will use Python 3.13 (3.12 should work too) for the programming assignments. You may already have Python installed on your personal machines. We suggest, though, to use Conda, as it provides an easy way to manage different Python environments. Conda can be installed through Anaconda.
- Create the Environment
Once Conda is installed, create a Python 3.13 environment named 'cs170', as follows:
conda create -n cs170 python=3.13 numpy matplotlib
The above command will also install the 'numpy' and 'matplotlib' packages. numpy is the most commonly used numeric computation package in Python. The matplotlib package is used for data visualization, which our code exploits for displaying useful plots in a number of assignments.
- Accessing the Environment
At any time, you can enter the Python environment you just created by calling:
activate cs170
You can leave the environment by simply typing:
deactivate
More details regarding the environment management can be found here.
- Testing your installation
To test your installation, please download the warmup.py file from here. In your Conda environment run:
python warmup.py
If everything has been set up successfully, you should be able to see a confirmation message that also prompts you to modify the code.
Python References
In this course, you are expected to have some experience in Python programming. Some useful resources are:
Warm-Up Project
To play a bit with Python, we ask you to modify three functions in the provided code. Note that you don't have to submit your solution. This assignment will not count towards your final grade.
Question 1 - Max Index
Modify the max_index function in warmup.py to return the index of the maximum element in an unsorted list.
Question 2 - Two Sum
Modify the two_sum function in warmup.py to return two values from a given list of integers such that they add up to a specific target value.
Question 3 - Factorial
Modify the factorial function in warmup.py to compute the factorial of a given number, n, using recursion. The factorial of n is defined as:
n! = 1, if n = 0 or n = 1,
n! = 1 * 2 * ... * (n-1) * n = n * (n-1)!, if n > 1