Sequences of Data & Scope
CS 110
Reminders and Announcements
Next Week
Quiz 1 - Wednesday (2/5)
Academic Integrity
Sequences: Why are they useful?
5
New Data Type: Tuple
tuple_of_strings = ("first year", "sophomore", "junior", "senior")�
6
Tuples: The Rules
7
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
Lists: The Rules
my_list = [8, 3, 2, 5, 7]
# Access items by index
my_list[len(my_list) - 1]
my_list[4]
The Substitution Model
nums = [3.3, 4, 6, 3, 1.2, 1.4]
result = nums[0] + nums[2] + nums[4]
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
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)
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
Substitution Model
print(randint(0,10) + 6 * 3)
Substitution Model
print(randint(0,10) + 6 * 3)
print( 3 + 6 * 3)
Substitution Model
print(randint(0,10) + 6 * 3)
print(21)
Substitution Model
my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]
print(my_list[0])
Substitution Model
my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]
print(my_list[0])
print( (0, 1) )
Substitution Model
my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]
print(my_list[0])
print( (0, 1) )
(0, 1)
Substitution Model
my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]
print(my_list[2][1])
Substitution Model
my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]
print(my_list[2][1])
print( (3,9)[1])
Substitution Model
my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]
print(my_list[2][1])
print( 9)
Substitution Model
my_list = [(0, 1), (1, 8), (3, 9), (4, -5)]
print(my_list[2][1])
print( 9)
9
Variable Scope
25
Scope
26
Scope 1: Consider the following program...
def demo_1(name):
greeting = 'Welcome, ' + name
demo_1('Jimmy')
print(greeting)
27
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.
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
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
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.
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