In Python, variables do not need to be declared before they are used. In other words, Python automatically decided on the correct variable type (integer, float, etc.) You can simply define a new variable using x = 5
. This is just like MATLAB.
We can see the value of the variable using the built-in print
function. For example, to print the value of x
, we would use print(x)
.
Note: When using Jupyter notebooks, it is very important to know that the cells can be executed out of order (intentionally or not). The state of the environment (e.g., values of variables, imports, etc.) is defined by the execution order.
You should see that the value 8
is now printed. This may seem problematic if you are used to programming in environments where the state is linked to the order of the commands as written, not as executed.
Again, notice that the state of the environment is determined by the execution order.
Note also that the square brackets to the left of the cell show the order that cells were executed. If you scroll to the top, you should see that the code cells show an execution order of [1]
, [2]
, [5]
, and [4]
, indicating the actual execution order.
There are some useful menu commands at the top of the Jupyter notebook to help with these problems and make sure you retain the execution order as expected.
Some important commands to remember:
Kernel | Restart & Clear Output
Cell | Run All Above
.Kernel | Restart & Run All
.We will use the autograder in Vocareum for homework assignments, which gives you instant feedback on your work. The autograder is confirmed to allow for unlimited submissions. You just need to wait 5 minutes between submissions.
It is very important carefully read the assignment instructions. The autograder is simply executing logical checks behind the scenes. For example, if the assignment instructions say to store your final answer in the variable final_ans
but you instead write my_ans
, the autograder will mark it as wrong.
We want to store the first seven elements of the Fibonaci sequence in the list fib
. Below is an answer with a few mistakes:
# YOUR SOLUTION HERE
fibs = [0, -1, 1, 2, 4, 5, 9, 13]
The autograde in Vocareum is nice for (near) instant feedback. But we'll also use Gradescope throughout the semester to collect assignments that require "pencil and paper" analysis or written discussion.
We'll occassionally ask you to submit handwritten work (e.g., mathematical models, pseudocode) via Gradescope. Here are the instructions: