Created
November 21, 2016 07:06
-
-
Save jjlumagbas/33ac5d223204dfe336a3dc1fd3b852d0 to your computer and use it in GitHub Desktop.
Local and global scope, mutable lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
greets = ["hi", "hello", "bye"] | |
print(greets[0].upper()) | |
def cap_list(lst): | |
pass # do this | |
cap_list(greets) | |
print(greets) # ["HI", "HELLO", "BYE"] | |
a = [1, 2, 3, 4, 5] | |
b = a[:] | |
def double_every_other(lst): | |
for i in range(1, len(lst), 2): | |
lst[i] = lst[i] * 2 | |
double_every_other(b) | |
a[0] = 5 | |
# given a list, produce a new list composed of | |
# times 2 of every element of the list | |
def times_2_pure(lst): | |
result = [] | |
for el in lst: | |
result = result + [el * 2] | |
return result | |
def times_2(lst): | |
for i in range(len(lst)): | |
lst[i] = lst[i] * 2 | |
times_2_pure(a) | |
print(a) | |
times_2(a) | |
print(a) | |
def double_money(start_bal, target_bal, rate): | |
start_bal = (start_bal * rate) + start_bal | |
start = 100 | |
double_money(start, 300, .10) | |
def print_reverse(lst): | |
for i in range(len(lst) - 1, -1, -1): | |
print(lst[i]) | |
print_reverse(b) | |
a = "Hello" | |
b = a | |
a = a + "!" | |
print(b) | |
a = [1, 2, 3, 4, 5] | |
b = a | |
a[0] = 5 | |
print(b) | |
a = 5 | |
b = a | |
a = 20 | |
print(b) | |
def modList(lst): | |
lst[0] = 5 | |
a = [1, 2, 3, 4, 5] | |
modList(a) | |
print(a) | |
# def f2(): | |
# print(5) | |
# | |
# def f3(): | |
# return print(5) | |
# | |
# x = f3() | |
# print(x) | |
# def first(a): | |
# a = 8 | |
# return a | |
# | |
# a = 20 | |
# first(a) | |
# print(a) | |
# | |
# def printNumber(a): | |
# a = 8 | |
# print(a) | |
# | |
# | |
# print("Hello") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment