117
CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 11 24 April 2018 1 / 51

CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

CSci 127: Introduction to Computer Science

hunter.cuny.edu/csci

CSci 127 (Hunter) Lecture 11 24 April 2018 1 / 51

Page 2: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Announcements

Summer Prep Course for DiscreteMathematics:

I Math10N03: “Pre-Discrete MathematicsWorkshop”

I Focuses on increasing algebraic mastery,especially with manipulating expressionsinvolving multiple variables, indices, aswell as summation notation.

I The focus is on the mastery of skillscritical to success in CSci 150.

I The course is 1 hour, 1 credit,Wednesdays, 9:50-11:50am.

I See CUNYFirst for registration.

CSci 127 (Hunter) Lecture 11 24 April 2018 2 / 51

Page 3: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Announcements

Summer Prep Course for DiscreteMathematics:

I Math10N03: “Pre-Discrete MathematicsWorkshop”

I Focuses on increasing algebraic mastery,especially with manipulating expressionsinvolving multiple variables, indices, aswell as summation notation.

I The focus is on the mastery of skillscritical to success in CSci 150.

I The course is 1 hour, 1 credit,Wednesdays, 9:50-11:50am.

I See CUNYFirst for registration.

CSci 127 (Hunter) Lecture 11 24 April 2018 2 / 51

Page 4: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Announcements

Summer Prep Course for DiscreteMathematics:

I Math10N03: “Pre-Discrete MathematicsWorkshop”

I Focuses on increasing algebraic mastery,especially with manipulating expressionsinvolving multiple variables, indices, aswell as summation notation.

I The focus is on the mastery of skillscritical to success in CSci 150.

I The course is 1 hour, 1 credit,Wednesdays, 9:50-11:50am.

I See CUNYFirst for registration.

CSci 127 (Hunter) Lecture 11 24 April 2018 2 / 51

Page 5: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Announcements

Summer Prep Course for DiscreteMathematics:

I Math10N03: “Pre-Discrete MathematicsWorkshop”

I Focuses on increasing algebraic mastery,especially with manipulating expressionsinvolving multiple variables, indices, aswell as summation notation.

I The focus is on the mastery of skillscritical to success in CSci 150.

I The course is 1 hour, 1 credit,Wednesdays, 9:50-11:50am.

I See CUNYFirst for registration.

CSci 127 (Hunter) Lecture 11 24 April 2018 2 / 51

Page 6: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Announcements

Summer Prep Course for DiscreteMathematics:

I Math10N03: “Pre-Discrete MathematicsWorkshop”

I Focuses on increasing algebraic mastery,especially with manipulating expressionsinvolving multiple variables, indices, aswell as summation notation.

I The focus is on the mastery of skillscritical to success in CSci 150.

I The course is 1 hour, 1 credit,Wednesdays, 9:50-11:50am.

I See CUNYFirst for registration.

CSci 127 (Hunter) Lecture 11 24 April 2018 2 / 51

Page 7: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Announcements

Summer Prep Course for DiscreteMathematics:

I Math10N03: “Pre-Discrete MathematicsWorkshop”

I Focuses on increasing algebraic mastery,especially with manipulating expressionsinvolving multiple variables, indices, aswell as summation notation.

I The focus is on the mastery of skillscritical to success in CSci 150.

I The course is 1 hour, 1 credit,Wednesdays, 9:50-11:50am.

I See CUNYFirst for registration.

CSci 127 (Hunter) Lecture 11 24 April 2018 2 / 51

Page 8: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Today’s Topics

Recap of Python & Circuits

High vs. Low-Level Programming

A Simplified Machine Language

Final Exam Overview

CSci 127 (Hunter) Lecture 11 24 April 2018 3 / 51

Page 9: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Python & Circuits Review: 10 Weeks in 10 Minutes

A whirlwind tour of the semester, so far...

CSci 127 (Hunter) Lecture 11 24 April 2018 4 / 51

Page 10: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 1: print(), loops, comments, & turtles

Introduced comments & print():

#Name: Thomas Hunter ← These lines are comments

#Date: September 1, 2017 ← (for us, not computer to read)

#This program prints: Hello, World! ← (this one also)

print("Hello, World!") ← Prints the string ”Hello, World!” to the screen

As well as definite loops & the turtle package:

CSci 127 (Hunter) Lecture 11 24 April 2018 5 / 51

Page 11: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 1: print(), loops, comments, & turtles

Introduced comments & print():

#Name: Thomas Hunter ← These lines are comments

#Date: September 1, 2017 ← (for us, not computer to read)

#This program prints: Hello, World! ← (this one also)

print("Hello, World!") ← Prints the string ”Hello, World!” to the screen

As well as definite loops & the turtle package:

CSci 127 (Hunter) Lecture 11 24 April 2018 5 / 51

Page 12: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 1: print(), loops, comments, & turtles

Introduced comments & print():

#Name: Thomas Hunter ← These lines are comments

#Date: September 1, 2017 ← (for us, not computer to read)

#This program prints: Hello, World! ← (this one also)

print("Hello, World!") ← Prints the string ”Hello, World!” to the screen

As well as definite loops & the turtle package:

CSci 127 (Hunter) Lecture 11 24 April 2018 5 / 51

Page 13: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()

A variable is a reserved memory location for storing a value.Different kinds, or types, of values need different amounts of space:

I int: integer or whole numbersI float: floating point or real numbersI string: sequence of charactersI list: a sequence of items

e.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 14: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()A variable is a reserved memory location for storing a value.

Different kinds, or types, of values need different amounts of space:I int: integer or whole numbersI float: floating point or real numbersI string: sequence of charactersI list: a sequence of items

e.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 15: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()A variable is a reserved memory location for storing a value.Different kinds, or types, of values need different amounts of space:

I int: integer or whole numbers

I float: floating point or real numbersI string: sequence of charactersI list: a sequence of items

e.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 16: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()A variable is a reserved memory location for storing a value.Different kinds, or types, of values need different amounts of space:

I int: integer or whole numbersI float: floating point or real numbers

I string: sequence of charactersI list: a sequence of items

e.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 17: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()A variable is a reserved memory location for storing a value.Different kinds, or types, of values need different amounts of space:

I int: integer or whole numbersI float: floating point or real numbersI string: sequence of characters

I list: a sequence of itemse.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]

I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 18: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()A variable is a reserved memory location for storing a value.Different kinds, or types, of values need different amounts of space:

I int: integer or whole numbersI float: floating point or real numbersI string: sequence of charactersI list: a sequence of items

e.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 19: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()A variable is a reserved memory location for storing a value.Different kinds, or types, of values need different amounts of space:

I int: integer or whole numbersI float: floating point or real numbersI string: sequence of charactersI list: a sequence of items

e.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]

I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 20: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()A variable is a reserved memory location for storing a value.Different kinds, or types, of values need different amounts of space:

I int: integer or whole numbersI float: floating point or real numbersI string: sequence of charactersI list: a sequence of items

e.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 21: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 2: variables, data types, more on loops & range()A variable is a reserved memory location for storing a value.Different kinds, or types, of values need different amounts of space:

I int: integer or whole numbersI float: floating point or real numbersI string: sequence of charactersI list: a sequence of items

e.g. [3, 1, 4, 5, 9] or [’violet’,’purple’,’indigo’]I class variables: for complex objects, like turtles.

More on loops & ranges:

CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51

Page 22: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 3: colors, hex, slices, numpy & images

CSci 127 (Hunter) Lecture 11 24 April 2018 7 / 51

Page 23: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 3: colors, hex, slices, numpy & images

CSci 127 (Hunter) Lecture 11 24 April 2018 7 / 51

Page 24: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 3: colors, hex, slices, numpy & images

CSci 127 (Hunter) Lecture 11 24 April 2018 7 / 51

Page 25: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 4: design problem (cropping images) & decisions

First: specify inputs/outputs. Input file name, output file name,upper, lower, left, right (“bounding box”)

Next: write pseudocode.1 Import numpy and pyplot.2 Ask user for file names and dimensions for cropping.3 Save input file to an array.4 Copy the cropped portion to a new array.5 Save the new array to the output file.

Next: translate to Python.

CSci 127 (Hunter) Lecture 11 24 April 2018 8 / 51

Page 26: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 4: design problem (cropping images) & decisions

First: specify inputs/outputs. Input file name, output file name,upper, lower, left, right (“bounding box”)

Next: write pseudocode.1 Import numpy and pyplot.2 Ask user for file names and dimensions for cropping.3 Save input file to an array.4 Copy the cropped portion to a new array.5 Save the new array to the output file.

Next: translate to Python.

CSci 127 (Hunter) Lecture 11 24 April 2018 8 / 51

Page 27: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 4: design problem (cropping images) & decisions

First: specify inputs/outputs. Input file name, output file name,upper, lower, left, right (“bounding box”)

Next: write pseudocode.1 Import numpy and pyplot.2 Ask user for file names and dimensions for cropping.3 Save input file to an array.4 Copy the cropped portion to a new array.5 Save the new array to the output file.

Next: translate to Python.

CSci 127 (Hunter) Lecture 11 24 April 2018 8 / 51

Page 28: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 4: design problem (cropping images) & decisions

First: specify inputs/outputs. Input file name, output file name,upper, lower, left, right (“bounding box”)

Next: write pseudocode.1 Import numpy and pyplot.2 Ask user for file names and dimensions for cropping.3 Save input file to an array.4 Copy the cropped portion to a new array.5 Save the new array to the output file.

Next: translate to Python.

CSci 127 (Hunter) Lecture 11 24 April 2018 8 / 51

Page 29: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 4: design problem (cropping images) & decisions

CSci 127 (Hunter) Lecture 11 24 April 2018 9 / 51

Page 30: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 5: logical operators, truth tables & logical circuits

in1 in2 returns:

False and False FalseFalse and True FalseTrue and False FalseTrue and True True

CSci 127 (Hunter) Lecture 11 24 April 2018 10 / 51

Page 31: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 5: logical operators, truth tables & logical circuits

in1 in2 returns:

False and False FalseFalse and True FalseTrue and False FalseTrue and True True

CSci 127 (Hunter) Lecture 11 24 April 2018 10 / 51

Page 32: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 6: structured data, pandas, & more design

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 11 24 April 2018 11 / 51

Page 33: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 6: structured data, pandas, & more design

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 11 24 April 2018 11 / 51

Page 34: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 6: structured data, pandas, & more design

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 11 24 April 2018 11 / 51

Page 35: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 6: structured data, pandas, & more design

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 11 24 April 2018 11 / 51

Page 36: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 6: structured data, pandas, & more design

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 11 24 April 2018 11 / 51

Page 37: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 7: functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 11 24 April 2018 12 / 51

Page 38: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 7: functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 11 24 April 2018 12 / 51

Page 39: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 7: functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 11 24 April 2018 12 / 51

Page 40: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 7: functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:

Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 11 24 April 2018 12 / 51

Page 41: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 7: functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 11 24 April 2018 12 / 51

Page 42: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 7: functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,

which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 11 24 April 2018 12 / 51

Page 43: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 7: functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 11 24 April 2018 12 / 51

Page 44: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 8: function parameters, github

Functions can have inputparameters.

Surrounded by parenthesis, bothin the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 11 24 April 2018 13 / 51

Page 45: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 8: function parameters, github

Functions can have inputparameters.

Surrounded by parenthesis, bothin the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 11 24 April 2018 13 / 51

Page 46: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 8: function parameters, github

Functions can have inputparameters.

Surrounded by parenthesis, bothin the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 11 24 April 2018 13 / 51

Page 47: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 8: function parameters, github

Functions can have inputparameters.

Surrounded by parenthesis, bothin the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 11 24 April 2018 13 / 51

Page 48: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 8: function parameters, github

Functions can have inputparameters.

Surrounded by parenthesis, bothin the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 11 24 April 2018 13 / 51

Page 49: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 8: function parameters, github

Functions can have inputparameters.

Surrounded by parenthesis, bothin the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters.

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 11 24 April 2018 14 / 51

Page 50: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 9: top-down design, folium

CSci 127 (Hunter) Lecture 11 24 April 2018 15 / 51

Page 51: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 10: indefinite loops, searching data, random()

Indefinite (while) loops allow you torepeat a block of code as long as acondition holds.

Very useful for checking user inputfor correctness.

Python’s built-in random packagehas useful methods for generatingrandom whole numbers and realnumbers.

To use, must include:import random.

CSci 127 (Hunter) Lecture 11 24 April 2018 16 / 51

Page 52: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 10: indefinite loops, searching data, random()

Indefinite (while) loops allow you torepeat a block of code as long as acondition holds.

Very useful for checking user inputfor correctness.

Python’s built-in random packagehas useful methods for generatingrandom whole numbers and realnumbers.

To use, must include:import random.

CSci 127 (Hunter) Lecture 11 24 April 2018 16 / 51

Page 53: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 10: indefinite loops, searching data, random()

Indefinite (while) loops allow you torepeat a block of code as long as acondition holds.

Very useful for checking user inputfor correctness.

Python’s built-in random packagehas useful methods for generatingrandom whole numbers and realnumbers.

To use, must include:import random.

CSci 127 (Hunter) Lecture 11 24 April 2018 16 / 51

Page 54: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Week 10: indefinite loops, searching data, random()

Indefinite (while) loops allow you torepeat a block of code as long as acondition holds.

Very useful for checking user inputfor correctness.

Python’s built-in random packagehas useful methods for generatingrandom whole numbers and realnumbers.

To use, must include:import random.

CSci 127 (Hunter) Lecture 11 24 April 2018 16 / 51

Page 55: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Python & Circuits Review: 10 Weeks in 10 Minutes

Input/Output (I/O): input() and print();pandas for CSV files

Types:

I Primitive: int, float, bool, string;I Container: lists (but not dictionaries/hashes

or tuples)

Objects: turtles (used but did not design our own)

Loops: definite & indefinite

Conditionals: if-elif-else

Logical Expressions & Circuits

Functions: parameters & returns

Packages:

I Built-in: turtle, math, randomI Popular: numpy, matplotlib, pandas, folium

CSci 127 (Hunter) Lecture 11 24 April 2018 17 / 51

Page 56: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Python & Circuits Review: 10 Weeks in 10 Minutes

A whirlwind tour with10 (or so) challenges...

CSci 127 (Hunter) Lecture 11 24 April 2018 18 / 51

Page 57: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 1

Predict what the code will do:

CSci 127 (Hunter) Lecture 11 24 April 2018 19 / 51

Page 58: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 2

Predict what the code will do:

CSci 127 (Hunter) Lecture 11 24 April 2018 20 / 51

Page 59: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 3

Predict what the code will do:

CSci 127 (Hunter) Lecture 11 24 April 2018 21 / 51

Page 60: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 4

Extend this program to also allow drawing in purple & stamping:

CSci 127 (Hunter) Lecture 11 24 April 2018 22 / 51

Page 61: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 5

When does this circuit yield true?

That is, what values for the inputs give an output value of true?

CSci 127 (Hunter) Lecture 11 24 April 2018 23 / 51

Page 62: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 6

Predict what the following will do:

print("Queens:", pop["Queens"].min())

print("S I:", pop["Staten Island"].mean())

print("S I:", pop["Staten Island"].std())

pop.plot.bar(x="Year")

pop.plot.scatter(x="Brooklyn", y= "Total")

pop["Fraction"] = pop["Bronx"]/pop["Total"]

CSci 127 (Hunter) Lecture 11 24 April 2018 24 / 51

Page 63: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 7

Fill in the function body:

CSci 127 (Hunter) Lecture 11 24 April 2018 25 / 51

Page 64: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 8

What are the formal parameters for the functions?

What is the output of:

r = foo([1,2,3,4])

print("Return: ", r)

What is the output of:

r = foo([1024,512,256,128])

print("Return: ", r)

CSci 127 (Hunter) Lecture 11 24 April 2018 26 / 51

Page 65: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 9

What does this code do?

CSci 127 (Hunter) Lecture 11 24 April 2018 27 / 51

Page 66: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples: Week 10

Predict what the code will do:

Write a function that asks a user for number after 2000 but before2018. The function should repeatedly ask the user for a number untilthey enter one within the range and return the number.

CSci 127 (Hunter) Lecture 11 24 April 2018 28 / 51

Page 67: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Python & Circuits Review: 10 Weeks in 10 Minutes

Input/Output (I/O): input() and print();pandas for CSV files

Types:

I Primitive: int, float, bool, string;I Container: lists (but not dictionaries/hashes

or tuples)

Objects: turtles (used but did not design our own)

Loops: definite & indefinite

Conditionals: if-elif-else

Logical Expressions & Circuits

Functions: parameters & returns

Packages:

I Built-in: turtle, math, randomI Popular: numpy, matplotlib, pandas, folium

CSci 127 (Hunter) Lecture 11 24 April 2018 29 / 51

Page 68: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Lecture Slip: Commenting Code

In pairs or triples:

What does the code do?

Add comments to explaineach line.

CSci 127 (Hunter) Lecture 11 24 April 2018 30 / 51

Page 69: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Low-Level vs. High-Level Languages

(codeCommit)

Can view programming languages on a continuum.

Those that directly access machine instructions & memory and havelittle abstraction are low-level languages(e.g. machine language, assembly language).Those that have strong abstraction (allow programming paradigmsindependent of the machine details, such as complex variables,functions and looping that do not translate directly into machinecode) are called high-level languages.Some languages, like C, are in between– allowing both low levelaccess and high level data structures.

CSci 127 (Hunter) Lecture 11 24 April 2018 31 / 51

Page 70: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Low-Level vs. High-Level Languages

(codeCommit)

Can view programming languages on a continuum.Those that directly access machine instructions & memory and havelittle abstraction are low-level languages

(e.g. machine language, assembly language).Those that have strong abstraction (allow programming paradigmsindependent of the machine details, such as complex variables,functions and looping that do not translate directly into machinecode) are called high-level languages.Some languages, like C, are in between– allowing both low levelaccess and high level data structures.

CSci 127 (Hunter) Lecture 11 24 April 2018 31 / 51

Page 71: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Low-Level vs. High-Level Languages

(codeCommit)

Can view programming languages on a continuum.Those that directly access machine instructions & memory and havelittle abstraction are low-level languages(e.g. machine language, assembly language).

Those that have strong abstraction (allow programming paradigmsindependent of the machine details, such as complex variables,functions and looping that do not translate directly into machinecode) are called high-level languages.Some languages, like C, are in between– allowing both low levelaccess and high level data structures.

CSci 127 (Hunter) Lecture 11 24 April 2018 31 / 51

Page 72: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Low-Level vs. High-Level Languages

(codeCommit)

Can view programming languages on a continuum.Those that directly access machine instructions & memory and havelittle abstraction are low-level languages(e.g. machine language, assembly language).Those that have strong abstraction (allow programming paradigmsindependent of the machine details, such as complex variables,functions and looping that do not translate directly into machinecode) are called high-level languages.

Some languages, like C, are in between– allowing both low levelaccess and high level data structures.

CSci 127 (Hunter) Lecture 11 24 April 2018 31 / 51

Page 73: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Low-Level vs. High-Level Languages

(codeCommit)

Can view programming languages on a continuum.Those that directly access machine instructions & memory and havelittle abstraction are low-level languages(e.g. machine language, assembly language).Those that have strong abstraction (allow programming paradigmsindependent of the machine details, such as complex variables,functions and looping that do not translate directly into machinecode) are called high-level languages.Some languages, like C, are in between– allowing both low levelaccess and high level data structures.

CSci 127 (Hunter) Lecture 11 24 April 2018 31 / 51

Page 74: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Machine Language

(Ruth Gordon & Ester Gerston programming the ENIAC, UPenn)

CSci 127 (Hunter) Lecture 11 24 April 2018 32 / 51

Page 75: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Machine Language

(wiki)

CSci 127 (Hunter) Lecture 11 24 April 2018 33 / 51

Page 76: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Machine Language

(wiki)

We will be writing programs in asimplified machine language, WeMIPS.

It is based on a reduced instruction setcomputer (RISC) design, originallydeveloped by the MIPS ComputerSystems.

Due to its small set of commands,processors can be designed to run thosecommands very efficiently.

More in future architecture classes....

CSci 127 (Hunter) Lecture 11 24 April 2018 34 / 51

Page 77: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Machine Language

(wiki)

We will be writing programs in asimplified machine language, WeMIPS.

It is based on a reduced instruction setcomputer (RISC) design, originallydeveloped by the MIPS ComputerSystems.

Due to its small set of commands,processors can be designed to run thosecommands very efficiently.

More in future architecture classes....

CSci 127 (Hunter) Lecture 11 24 April 2018 34 / 51

Page 78: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Machine Language

(wiki)

We will be writing programs in asimplified machine language, WeMIPS.

It is based on a reduced instruction setcomputer (RISC) design, originallydeveloped by the MIPS ComputerSystems.

Due to its small set of commands,processors can be designed to run thosecommands very efficiently.

More in future architecture classes....

CSci 127 (Hunter) Lecture 11 24 April 2018 34 / 51

Page 79: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Machine Language

(wiki)

We will be writing programs in asimplified machine language, WeMIPS.

It is based on a reduced instruction setcomputer (RISC) design, originallydeveloped by the MIPS ComputerSystems.

Due to its small set of commands,processors can be designed to run thosecommands very efficiently.

More in future architecture classes....

CSci 127 (Hunter) Lecture 11 24 April 2018 34 / 51

Page 80: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

“Hello World!” in Simplified Machine Language

(WeMIPS)

CSci 127 (Hunter) Lecture 11 24 April 2018 35 / 51

Page 81: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

WeMIPS

(Demo with WeMIPS)

CSci 127 (Hunter) Lecture 11 24 April 2018 36 / 51

Page 82: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

In Pairs or Triples:

Write a program that prints out the alphabet: a b c d ... x y z

CSci 127 (Hunter) Lecture 11 24 April 2018 37 / 51

Page 83: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

WeMIPS

(Demo with WeMIPS)

CSci 127 (Hunter) Lecture 11 24 April 2018 38 / 51

Page 84: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 85: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 86: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 87: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.

I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 88: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.

I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 89: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.

I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 90: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 91: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:

I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 92: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.

I Style of questions: what does the code do? short answer, writefunctions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 93: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 94: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Format

The exam is 2 hours long.

There are 4 different versions to discourage copying.

It is on paper. No use of computers, phones, etc. allowed.

You may have 1 piece of 8.5” x 11” piece of paper.I With notes, examples, programs: what will help you on the exam.I No origami– it’s distracting to others taking the exam.I Best if you design/write yours since excellent way to study.

The exam format:I 10 questions, each worth 10 points.I Style of questions: what does the code do? short answer, write

functions, top down design, & write complete programs.

Past exams available on webpage (includes answer keys).

CSci 127 (Hunter) Lecture 11 24 April 2018 39 / 51

Page 95: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Recap: Python, Languages, & Design

On lecture slip, write down a topic you wish wehad spent more time (and why).

Python language

Logical Circuits

Simplified Machine Language

Design: from written description (‘specs’) tofunction inputs & outputs (‘APIs’)

CSci 127 (Hunter) Lecture 11 24 April 2018 40 / 51

Page 96: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Recap: Python, Languages, & Design

On lecture slip, write down a topic you wish wehad spent more time (and why).

Python language

Logical Circuits

Simplified Machine Language

Design: from written description (‘specs’) tofunction inputs & outputs (‘APIs’)

CSci 127 (Hunter) Lecture 11 24 April 2018 40 / 51

Page 97: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Recap: Python, Languages, & Design

On lecture slip, write down a topic you wish wehad spent more time (and why).

Python language

Logical Circuits

Simplified Machine Language

Design: from written description (‘specs’) tofunction inputs & outputs (‘APIs’)

CSci 127 (Hunter) Lecture 11 24 April 2018 40 / 51

Page 98: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Recap: Python, Languages, & Design

On lecture slip, write down a topic you wish wehad spent more time (and why).

Python language

Logical Circuits

Simplified Machine Language

Design: from written description (‘specs’) tofunction inputs & outputs (‘APIs’)

CSci 127 (Hunter) Lecture 11 24 April 2018 40 / 51

Page 99: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Recap: Python, Languages, & Design

On lecture slip, write down a topic you wish wehad spent more time (and why).

Python language

Logical Circuits

Simplified Machine Language

Design: from written description (‘specs’) tofunction inputs & outputs (‘APIs’)

CSci 127 (Hunter) Lecture 11 24 April 2018 40 / 51

Page 100: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Top-Down Design & APIsFor each question, write only the function header (name & inputs) andreturn values (often called the Application Programming Interface (API)):

Write a function that takes a weight in kilograms and returns theweight in pounds.

Write a function that takes a string and returns its length.

Write a function that, given a DataFrame, returns the minimal valuein the first column.

Write a function that takes a whole number and returns thecorresponding binary number as a string.

Write a function that computes the total monthly payment whengiven the initial loan amount, annual interest rate, number of years ofthe loan.

(Hint: highlight key words, make list of inputs, list of outputs, then puttogether.)

CSci 127 (Hunter) Lecture 11 24 April 2018 41 / 51

Page 101: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Top-Down Design & APIsFor each question, write only the function header (name & inputs) andreturn values (often called the Application Programming Interface (API)):

Write a function that takes a weight in kilograms and returns theweight in pounds.

Write a function that takes a string and returns its length.

Write a function that, given a DataFrame, returns the minimal valuein the first column.

Write a function that takes a whole number and returns thecorresponding binary number as a string.

Write a function that computes the total monthly payment whengiven the initial loan amount, annual interest rate, number of years ofthe loan.

(Hint: highlight key words, make list of inputs, list of outputs, then puttogether.)

CSci 127 (Hunter) Lecture 11 24 April 2018 41 / 51

Page 102: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final Overview: Top-Down Design & APIsFor each question, write only the function header (name & inputs) andreturn values (often called the Application Programming Interface (API)):

Write a function that takes a weight in kilograms and returns theweight in pounds.

Write a function that takes a string and returns its length.

Write a function that, given a DataFrame, returns the minimal valuein the first column.

Write a function that takes a whole number and returns thecorresponding binary number as a string.

Write a function that computes the total monthly payment whengiven the initial loan amount, annual interest rate, number of years ofthe loan.

(Hint: highlight key words, make list of inputs, list of outputs, then puttogether.)

CSci 127 (Hunter) Lecture 11 24 April 2018 41 / 51

Page 103: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a weight in kilograms and returns theweight in pounds.

def kg2lbs(kg):

...

return(lbs)

CSci 127 (Hunter) Lecture 11 24 April 2018 42 / 51

Page 104: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a weight in kilograms and returns theweight in pounds.

def kg2lbs(kg):

...

return(lbs)

CSci 127 (Hunter) Lecture 11 24 April 2018 42 / 51

Page 105: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a weight in kilograms and returns theweight in pounds.

def kg2lbs(kg)

lbs = kg * 2.2

return(lbs)

CSci 127 (Hunter) Lecture 11 24 April 2018 43 / 51

Page 106: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a string and returns its length.

def sLength(str):

...

return(length)

CSci 127 (Hunter) Lecture 11 24 April 2018 44 / 51

Page 107: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a string and returns its length.

def sLength(str):

...

return(length)

CSci 127 (Hunter) Lecture 11 24 April 2018 44 / 51

Page 108: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a string and returns its length.

def sLength(str):

length = len(str)

return(length)

CSci 127 (Hunter) Lecture 11 24 April 2018 45 / 51

Page 109: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that, given a DataFrame, returns the minimal valuein the “Manhattan” column.

def getMin(df):

...

return(min)

CSci 127 (Hunter) Lecture 11 24 April 2018 46 / 51

Page 110: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that, given a DataFrame, returns the minimal valuein the “Manhattan” column.

def getMin(df):

...

return(min)

CSci 127 (Hunter) Lecture 11 24 April 2018 46 / 51

Page 111: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question below, write the function header (name & inputs) andreturn values (often called the Application Programming Interface (API)):

Write a function that, given a DataFrame, returns the minimal valuein the “Manhattan” column.

def getMin(df):

mM = df[’Manhattan’].min()

return(mM)

CSci 127 (Hunter) Lecture 11 24 April 2018 47 / 51

Page 112: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a whole number and returns thecorresponding binary number as a string.

def num2bin(num):

...

return(bin)

CSci 127 (Hunter) Lecture 11 24 April 2018 48 / 51

Page 113: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a whole number and returns thecorresponding binary number as a string.

def num2bin(num):

...

return(bin)

CSci 127 (Hunter) Lecture 11 24 April 2018 48 / 51

Page 114: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that takes a whole number and returns thecorresponding binary number as a string.

def num2bin(num):

binStr = ""

while (num > 0):

#Divide by 2, and add the remainder to the string

r = num %2

binString = str(r) + binStr

num = num / 2

return(binStr)

CSci 127 (Hunter) Lecture 11 24 April 2018 49 / 51

Page 115: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that computes the total monthly payment whengiven the initial loan amount, annual interest rate, number of years ofthe loan.

def computePayment(loan,rate,year):

....

return(payment)

CSci 127 (Hunter) Lecture 11 24 April 2018 50 / 51

Page 116: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question, write the function header (name & inputs) and returnvalues (often called the Application Programming Interface (API)):

Write a function that computes the total monthly payment whengiven the initial loan amount, annual interest rate, number of years ofthe loan.

def computePayment(loan,rate,year):

....

return(payment)

CSci 127 (Hunter) Lecture 11 24 April 2018 50 / 51

Page 117: CSci 127: Introduction to Computer Science · CSci 127 (Hunter) Lecture 11 24 April 2018 6 / 51 Week 2: variables, data types, more on loops & range() A variable is a reserved memory

Final OverviewFor each question below, write the function header (name & inputs) andreturn values (often called the Application Programming Interface (API)):

Write a function that computes the total monthly payment whengiven the initial loan amount, annual interest rate, number of years ofthe loan.

def computePayment(loan,rate,year):

(Some formula for payment)

return(payment)

CSci 127 (Hunter) Lecture 11 24 April 2018 51 / 51