Lecture 2 Slides - Programming!

1 of 16

Writing our First Programs

CS 110

1

2 of 16

On your phone / tablet, either scan the QR code here (or up on the screen) or go to pollev.com/baincs110

Step 1. CLICK LOGIN

Step 2. USE @U.NORTHWESTERN EMAIL ADDRESS

Step 3. CLICK LOGIN WITH NORTHWESTERN

Step 4. Follow sign-in steps with MFA

Step 5. You may need to go back to pollev.com/baincs110 after you've setup your account. If it asks you for a Screen Name use your NetID (“abc1234”)!

How to login to PollEverywhere!

3 of 16

Announcements

  1. Sign into PollEverywhere to complete MQ0
    1. If you’re watching the recording, you’ll need to complete the Canvas version of MQ 0
    2. If you’re in class you’ll see your grade updated on Canvas by the end of the day.
  2. Exercise 0 is due this evening! We'll complete activities 2 - 4 in-class but don't forget to fill out the Google Form in Activity 0.
  3. Prof. Bain will have Office Hours today in TCH L370 until about 12pm in case you need help installing Python (part of Activity 1).

Next Week

  • Monday - Operators and Variables in Python
  • Wednesday - Functions in Python (Pre-Recorded - Out-of-Class MQ 1) + Tutorial 1
  • Friday - Writing our Own Functions + MQ 2
    • Exercise 1 - Due at 11:59pm on Canvas

4 of 16

A typical week…

Monday - Attend lecture, take a look at tutorial, and homework

Tuesday - Watch the pre-recorded lecture for Wednesday; peruse the tutorial

Wednesday - Come to the tutorial session, try your best to iron out all of your questions; see if you can do any of the HW

Thursday - by now, you should have a strong sense of how long it will take you to complete HW Exercise; if you need more time, apply for late penalty waiver

Friday - come to class, participate in activities; finish up HW

Sat/Sun - take the weekend off

Resources available throughout the week: Canvas, Class, Office Hours, and edSTEM

5 of 16

Turtle activities!

On our course web page

5

6 of 16

Understanding the Words / Atoms of Programming

6

Python is made up of different kinds of “words”, which can be used to construct sentences and paragraphs (statements and code blocks). Some types of ‘words’ in Python include:

  • pieces of data like numbers, words, images, etc.
  • operators are symbols that perform actions
  • variables are containers that can hold data
  • functions are blocks of code encased inside a function name
  • reserved words (special words in python)
  • modules

7 of 16

The first rule of programming!

Python will only do something you ask it to.

8 of 16

Arithmetic Operators

8

+

Addition

Adds values on either side of the operator

-

Subtraction

Subtracts right hand operand from left hand operand

*

Multiplication

Multiplies values on either side of the operator

/

Division

Divides left hand operand by right hand operand

**

Exponent

Performs exponential (power) calculation on operators

%

Modulus

Divides left hand operand by right hand operand; returns remainder

//

Quotient

Divides left hand operand by right hand operand; returns quotient

9 of 16

Variables

Named containers that allow us to store and access data in Python.

10 of 16

Variables

  • Variables are named containers for storing and/or referencing data
  • You assign values to variables using the assignment operator (equal sign)

variable_name = piece_of_data

10

11 of 16

Variables

Turn the following statement into a sentence:

favorite_cookie = "oreo"

11

12 of 16

Variables

Turn the following statement into a sentence:

favorite_cookie = "oreo"

12

13 of 16

Variables

Turn the following statement into a sentence:

favorite_cookie = "oreo"

Assign the text "oreo" to a container called favorite_cookie.

13

14 of 16

Variables

Turn the following statement into a sentence:

favorite_cookie = "oreo"

Assign the text "oreo" to a container called favorite_cookie.

Big Idea: Our goal for any line of Python we write is to be able to explain what it does in plain english.

14

15 of 16

Naming Variables

  • Variable names in Python can be any length and can consist of:
    • uppercase and lowercase letters (A-Z, a-z)
    • digits (0-9)
    • and the underscore character (_).
  • The first character of a variable name cannot be a digit.

Example 1:

favorite_cookie = "oreo"

Example 2:

1st_cookie = "oreo"

15

16 of 16

Reminders

  • For MQ0
    • If you’re watching the recording, you’ll need to complete the Canvas version of MQ 0
    • If you’re in class you’ll see your grade updated on Canvas by the end of the day (as long as you used your NetID as your screen name) - you don’t need to do the Canvas version
  • Exercise 0 is due this evening! We'll complete activities 2 - 4 in-class but don't forget to fill out the Google Form in Activity 0.
  • Prof. Bain will have Office Hours today in TCH L370 until about 12pm in case you need help installing Python (part of Activity 1).

Next Week

  • Monday - Operators and Variables in Python
  • Wednesday - Functions in Python (Pre-Recorded - Out-of-Class MQ 1) + Tutorial 1
  • Friday - Writing our Own Functions + MQ 2
    • Exercise 1 - Due at 11:59pm on Canvas