lab 1 - getting started with python (jan 17)

in this course, we will be using the programming language python 3. the current version as of early jan 2020 is 3.8.1. earlier versions of python 3 are acceptable to use, though you may not have the exact same functionality as everyone else. however, we will not be doing very advance things in python, so it should not be a real issue. (if you are curious, you can read about the main differences between different versions of python 3.)

note that python 3 is not entirely compatible with python 2 due to some different syntax. for the labs and lab homeworks, and any programming questions on exams, you must use python 3, not python 2 (and consequently not sage) or other languages.

warning: i use python 2 all the time, so inevitably i will accidentally use python 2 syntax from time to time. please be understanding.

the first lab session is to get you up and running with python and started on some very basic programming.

run, python, run!

there are many ways you can run python. python is an interpreted language, and when you run python in interactive mode it executes commands as you type, like a calculator. this is sufficient for today's lab, but soon you will want to write programs in an editor and save them as files, both so you can edit/debug/fix your programs and so that you can use functions you've written in previous sessions. here are some directions for how to run python both just in interactive mode and also with an editor. if you are already familiar with python, just get started. otherwise, pick one of the following:

option 1: run python online.

this is easy in terms of initial set up, but might be less convenient than option 2 for serious use, depending on what you have and how you like to work. there are various sites you can use to write and run python programs. here are 3 possibilities, each with different features:

option 2: run python on your computer.

python basics

  1. go through this python tutorial part 1 to learn some basics of python and do the exercises. while you can copy-paste some things to save some time, if you are new to python you should type in most of the example code by hand yourself to help you learn it better.
  2. if you finish that, start going through the first half of this python tutorial part 2 (the sections on python functions and control statements) and go through the exercises.

you may not finish this all during the lab period, but you are expected to finish this on your own before the first homework.

lab homework 1 (due fri jan 24)

here are some simple exercises with lists, loops and functions.
  1. write a function called "count_vec(n)" that uses a while loop to return (not print) a vector (list of numbers) [1, 2, 3, ..., n]. (do not use the range function) test this for n=1, 2, 10, 0, -3, 2.5. (it's okay if your function results in an error for "unexpected" inputs like 0, -3, 2.5. but you should understand how it behaves and why for unexpected inputs. if you like, you may use control statements to return an error message if the input is invalid, but this is not necessary.)
  2. write a function called "squares(n)" that uses a for loop and the range function to return the vector [1, 4, 9, ..., n^2]. test this for n=1, 2, 10, 0, -3, 2.5.
  3. write a function called "scale_vec(v,c)" that takes in a vector (list of numbers) v and number c returns the vector c times v (i.e., each element of the list should be multiplied by c). test this for v=squares(5) and the values c=-1, 0, 0.5, 1, 2, 10.
  4. write a function called "shift_vec(v,c)" that takes in a vector (list of numbers) v and number c returns the vector v+c (i.e., each element of the list should be added to by c). test this for v=squares(5) and the values c=-1, 0, 0.5, 1, 2, 10.
  5. write a function called "count_mat(n)" that returns an n by n matrix whose coefficients are the integers from 1 to n^2, in order. Here we represent a matrix as a list of n row vectors. for example count_mat(2) should return the list [[1, 2], [3, 4]], which we view as the 2x2 matrix with top row [1 2] and bottom row [3 4]. test this for n=0, 1, 2, 5.

further references: for various assignments, including possibly the above one, you may need to learn a little more python on your own. check out the official python beginner's guide for more resources. in particular, this wikibooks tutorial for non-programmers looks good. i also like the official python tutorial, though it assumes some familiarity with programming. also, feel free to ask others (possibly me) for help. and i hear the internet knows everything.



labs page
course home