day two: the internet
this is block 1
this is block 1
this is block 2
this is block 2
this is block 2
this is block 3
this is block 3
this is block 2
this is block 2
this is block 1
this is block 1
>>> age = 18
>>> if age == 12:
. . . print('Hello')
. . . else:
. . . print('Goodbye')
>>> pets = 2
>>> if pets == 1:
. . . print("You have 1 pet")
. . . elif pets == 2:
. . . print("You have 2 pets")
. . . elif pets < 3:
. . . print("You have less than 3 pets")
. . . else:
. . . print("You have more than 2 pets")
What will be printed?
>>> if age ==10 or age == 11 or age ==12:
. . . print("You are %s" % age)
>>> if age >= 10 and age <= 12:
. . . print("You are %s" % age)
for variable in list:
The variable iterates through the list and is assigned the value of the current iteration
for x in range(5):
y = x + 1
print(x+y)
for s in myList:
print(s)
>>> myList = ['a', 'b', 'c']
>>> for u in myList:
... print(u)
... for v in myList:
... print(v)
If Dean has 100 memes in his meme folder, the size of his meme folder grows by 3% every year, how many memes will he have for each year, up to 10 years?
while condition:
# block of code
Answer the previous question using a while loop
If you don't remember the task:
If Dean has 100 memes in his meme folder, the size of his meme folder grows by 3% every year, how many memes will he have for each year, up to 10 years?
def function(parameter):
# block of code
You define (make) the function like this:
def printName(name):
print("Hello, %s!" % name)
And call (use) it like this:
printName("John")
Another example, except with return instead of print():
def savings(chores, job, spend):
return chores + job - spend
# or return(chores+job-spend)
And call it like this:
print(savings(10, 10, 5))
# OR
money = savings(10, 10, 5)
# This is a global variable
a = 0
if a == 0:
# This is still a global variable
b = 1
def my_function(c):
# this is a local variable
d = 3
print(c)
print(d)
# Now we call the function, passing the value 7
# as the first and only parameter
my_function(7)
# a and b still exist
print(a)
print(b)
# c and d don't exist anymore -- these statements
# will give us name errors!
print(c)
print(d)