Lecture 10 Slides - Sequences and Scope

1 of 32

Sequences of Data & Scope

CS 110

2 of 32

Reminders and Announcements

  • Ex 3 due Tonight!
    • Couple of new functions (wedge and portion) if you’re interested - see edSTEM!
  • Quiz 1 coming up next week (no Tutorial next week; Ex 4 will be focused on reading programs rather than writing them)
    • Students with testing accommodations have already received an email from me
    • Deadline to let me know of any grading issues (tutorials, MQs, etc. is the start of Q1). After that point, all grades are locked in.

Next Week

  • Monday - Q1 Review (In-Person MQ 7)
  • Wednesday - Quiz 1 in-class
  • Friday - Intro to Control Flow
  • Ex 4 Due

3 of 32

Quiz 1 - Wednesday (2/5)

  • In-person here in our normal classroom during our normal time.
  • Taken on the Lockdown Browser on your personal computer; details on how to set it up are on our Canvas page as well as on the Quiz 1 page.
  • Make sure to try the Lockdown Browser version of the Practice Quiz before Monday.
  • You must be able to connect to eduroam
  • You must bring your Wildcard
  • YOU WILL NOT RECEIVE EXTRA TIME IF YOU COME TO THE QUIZ WITHOUT IT SETUP.
  • Problems are closer to our mini-quiz and the canvas practice quizzes
  • Covers everything up to and including Monday
  • Specifically won't emphasize memorization though the functions highlighted on the study guide you should know how to call without any other help
  • CHARGE YOUR COMPUTER THE NIGHT BEFORE.

4 of 32

Academic Integrity

  • You are encouraged to ask one another questions about assignments, but you must do your own work.
  • Easy way to avoid: do not look at anyone's code other than mine or your own. This includes people who are not in our class.
  • We will consider the following to be academic violations:
    • Turning in someone else’s file or asking someone else to write it for you.
    • Reading and reproducing someone else’s code for a problem
    • Cutting, pasting, transcribing, etc. someone else’s code, in whole or in part
    • Allowing anyone else to type or edit your code other than someone on the course staff.
    • Giving your code to someone else or posting your code in any public location
    • Using any tools to generate code for ANY assignment in this course.
  • Everything you submit in this class is subject to an in-person audit by course staff.
  • If at any point you write a program you can’t explain, you should stop.

5 of 32

Sequences: Why are they useful?

  • Allows us to store collections of data and allow us to easily read and process data via loops

  • Examples:
    • A sequence of notes for creating songs
    • A sequence of colors to make a color palette
    • A pair of coordinates (x,y)
    • A sequence of coordinates to draw shapes (points, lines, polygons)
    • A sequence of canvas objects that you can move and interact with
    • ...

5

6 of 32

New Data Type: Tuple

  • A tuple is an immutable sequence of values.
  • Useful for organizing and conveniently accessing related data.
  • In Python we define them like this:

tuple_of_strings = ("first year", "sophomore", "junior", "senior")

6

7 of 32

Tuples: The Rules

  • The items in a tuple can be of any type
  • A tuple can be of any length
  • You can access any tuple element through 0-based indexing
  • You can slice tuples
  • You cannot edit tuples (immutability)
  • You can concatenate tuples to create new, longer tuples

7

8 of 32

New Data Types: Lists and Tuples

A list is a mutable sequence of values:�

empty_list = []

list_of_strings = ["first year", "sophomore", "junior", "senior"]

A tuple is an immutable sequence of values:�

empty_list = ()

tuple_of_strings = ("first year", "sophomore", "junior", "senior")

8

9 of 32

Lists: The Rules

  • The items in a list can be of any type
  • A list can be of any length
  • You can access any list element through 0-based indexing
  • You can slice a list
  • You can edit a list (mutable)
  • You can concatenate lists using the + operator
  • You can sort lists (as long as Python knows how to sort your data)
  • You can add elements to a list using the append function
  • You can remove elements in a list using the pop function
  • You can update specific elements in a list using the offset notation from #3.

10 of 32

my_list = [8, 3, 2, 5, 7]

# Access items by index

my_list[len(my_list) - 1]

my_list[4]

11 of 32

The Substitution Model

nums = [3.3, 4, 6, 3, 1.2, 1.4]

result = nums[0] + nums[2] + nums[4]

12 of 32

The Substitution Model

nums = [3.3, 4, 6, 3, 1.2, 1.4]

result = nums[0] + nums[2] + nums[4]

3.3 + 6 + 1.2

13 of 32

The Substitution Model

nums = [3.3, 4, 6, 3, 1.2, 1.4]

result = nums[0] + nums[2] + nums[4]

3.3 + 6 + 1.2

print(result)

14 of 32

The Substitution Model

nums = [3.3, 4, 6, 3, 1.2, 1.4]

result = nums[0] + nums[2] + nums[4]

3.3 + 6 + 1.2

print(result)

10.5

15 of 32

Substitution Model

print(randint(0,10) + 6 * 3)

16 of 32

Substitution Model

print(randint(0,10) + 6 * 3)

print( 3 + 6 * 3)

17 of 32

Substitution Model

print(randint(0,10) + 6 * 3)

print(21)

18 of 32

Substitution Model

my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]

print(my_list[0])

19 of 32

Substitution Model

my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]

print(my_list[0])

print( (0, 1) )

20 of 32

Substitution Model

my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]

print(my_list[0])

print( (0, 1) )

(0, 1)

21 of 32

Substitution Model

my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]

print(my_list[2][1])

22 of 32

Substitution Model

my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]

print(my_list[2][1])

print( (3,9)[1])

23 of 32

Substitution Model

my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]

print(my_list[2][1])

print( 9)

24 of 32

Substitution Model

my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]

print(my_list[2][1])

print( 9)

9

25 of 32

Variable Scope

25

26 of 32

Scope

  • Scope — refers to the part of the program where a variable is accessible
  • Global variable — a variable which is defined in the main body of a file (best avoided)
  • Local variable — a variable which is defined inside a function. Not accessible from outside the function
    • The parameter names in the function definition behave like local variables, but they contain the values that we pass into the function when we call it.

26

27 of 32

Scope 1: Consider the following program...

def demo_1(name):

greeting = 'Welcome, ' + name

demo_1('Jimmy')

print(greeting)

27

28 of 32

Scope 1: Local variables cannot be accessed outside function

def demo_1(name):

greeting = 'Welcome, ' + name

demo_1('Jimmy')

print(greeting)

TAKEAWAY: If you want access to a value after the function ends, you have to return it. WE CAN TURN THIS INTO A GOOD PROGRAMMING PRACTICE.

28

local variable

greeting variable was local to the demo_1 function. Throws error.

29 of 32

name = 'Lindsay'

def demo_2(name):

print(name)

demo_2('Walter')

TAKEAWAY: Local variables take precedence over global variables. THIS IS A BAD PROGRAMMING PRACTICE

Scope 2: Which name will print to the screen?

29

Global variable

Parameters are local variables. �They take precedence over global variables

30 of 32

Scope 3: Global variables can be accessed inside a function

name1 = 'Lindsay'

def demo_3(name):

print(name)

print(name1)

demo_3('Walter')

THIS IS AN OKAY (BUT NOT GREAT) PROGRAMMING PRACTICE

30

local variable

global variable

31 of 32

Scope 4: Python assumes you’re creating a new local variable

�name = 'Lindsay'

def modify_name(new_name):

name = new_name

print(name)�modify_name('Walter')�print(name)

31

WOOF. Don't do this.

32 of 32

Note: We won’t do this until after Q1. I’m just putting this here for those who want to know if you CAN do this.

Scope 5: Global variables can updated with global keyword

name = 'Lindsay'

def modify_name(new_name):

global name

name = new_name

print(name)�modify_name('Walter')�print(name)

32

Tells python to use the GLOBAL version of the variable