Week 1: Introduction to CS21 and Python Programming

Week 1 Goals

  • Understand course format and policies

  • Learn about the CS network and Linux

  • Learn to use the Visual Studio Code editor (code)

  • Learn how to write and run python programs

  • Learn basic python syntax and semantics

  • Learn the string, integer, and float data types

Course Overview

update21 and handin21

You will use two commands regularly through the course to get and submit files for the course: update21 and handin21. The instructions in Lab 0 show how to run these commands. You should run update21 before starting each lab assignment and handin21 to turn in lab assignments. You can run them as often as you like. update21 will not overwrite files you modify after running update21. handin21 can submit the same lab multiple times. Each handin submission is stored separately, but only most recent copy submitted prior to the deadline will be graded. You may continue to submit after the deadline, but these submissions will be ignored.

If update21 or handin21 does not work for you or it says you are not allowed to run these programs, email me. It’s usually my fault, not yours. We may need to add you to the class roster, change handin or change permissions on a file.

Writing our first full Python program

First, open a terminal to start typing linux commands.

Then run update21. If update21 worked for you, it should have created an welcome.py file in your ~/cs21/inclass/ directory. Let’s practice some basic Linux commands to:

  1. navigate to the correct folder using cd

  2. list the contents of a folder using ls

  3. open a file for editing using code

  4. editing, saving, and running our first Python program.

cheese[~]$ cd

cheese[~]$ ls
Desktop/  Documents/   cs21/

cheese[~]$ cd cs21

cheese[cs21]$ ls
inclass/  labs/

cheese[cs21]$ cd inclass/

cheese[inclass]$ ls
welcome.py

cheese[inclass]$ code welcome.py

Editing and Running Python programs

To open and edit a Python source code file, in a terminal, run the code editor with the name of the file you want to edit (e.g., prog.py):

$ code prog.py

To run a Python program, in a terminal, invoke the Python interpreter (python3) followed by the name of the file with the Python program you want to run (e.g. prog.py):

$ python3 prog.py

Week 1 Code

welcome.py

The welcome.py program shows an example Python program. Let’s open it in code and look at some of the main features of a Python program:

$ code welcome.py

Now, lets run the program using the Python interpreter. In a terminal window type:

$ python3 welcome.py

greeting.py

Open greeting.py with code and try running it with python. Let’s try to modify the program to prompt the user for a different greeting.

numbers.py

  • numbers.py introduces a second data type: integer. What was the first data type we saw?

  • Modify numbers.py to cast the user input to a float. Save, run, and test your program.

  • You can also use the python shell to try small examples. Type python3 in a terminal without a file name to enter the shell. Type Ctrl-D to exit the shell. Note the >>> prompt for the python shell.

  • Try a few operations in the shell

    >>> txt = "hello"
    >>> a = 2
    >>> x = 3.5
    >>> type(txt)
    >>> type(a)
    >>> type(x)
    >>> x + a
    >>> a + txt
    >>> txt + txt
    >>> ans = "2.7"
    >>> y = float(ans)
    >>> print(y)
    >>> type(y)
    >>> b = int(y)
    >>> type(b)
    >>> print(b)
    >>> c = int(ans)