Midterm Review

Download Report

Transcript Midterm Review

Review
CMSC 201
Overview
What we’ve covered so far:
•
•
•
•
•
•
Computer Basics
Python syntax
Variables and expressions
Decision logic
Lists and Loops
Python Tricks
Overview
What we’ve covered so far:
•
•
•
•
•
•
•
Computing Basics
Python syntax
Variables and expressions
Decision logic
Lists and Loops
Functions
Python Tricks
Computing Basics
What you should know:
• Basic linux commands (ls, cd, mkdir, rmdir, mv, cp)
• Binary numbers
Exercise
Convert the following binary numbers to decimal:
• 1011
• 11101
Convert the following decimal numbers to binary:
• 81
• 36
Exercise
Convert the following binary numbers to decimal:
• 1011 => 11
• 11101 => 29
Convert the following decimal numbers to binary:
• 81 => 1010001
• 36 => 100100
Variables and Expressions
You need to know:
• Order of operations
• Python operators (**, %)
• How to assign and use variables
Exercise
What does the following code snippet print?
a = 2 ** 4 + 8 % 3
print(a)
Decision Logic
What you need to know:
• Boolean logic (how and, or, and not work)
• How if/elif/else statements work.
Exercise
What does the following code snippet print?
a = True
b = 24
c = 12
if(a or b == c) and (b % c == 0 and (not b < c and b != c)):
print(“Here we are!”)
elif a:
print(“There we were”)
else:
print(“We are here”)
Lists and Loops
You should know:
• How to write a while loop
• How to write a for loop
• How to create, access, and mutate a list
• How to iterate over a list with a loop
• How to iterate over a list using the range() function
• How to access information in nested lists
• List slicing
Exercise
Given the list:
myList = [ [1, 2, “hello”], [4, 5], 6]
Change the “hello” to the number 7.
Exercise
Given the list:
myList = [ [1, 2, “hello”], [4, 5], 6]
Change the “hello” to the number 7.
myList[0][2] = 7
Exercise
Write a program that uses a while loop to find the sum of the
numbers between 1 and 10.
Rewrite the same program using a for loop and the range()
function.
Exercise
Write a program that uses a while loop to find the sum of the
numbers between 1 and 10.
Rewrite the same program using a for loop and the range()
function.
count = 0
listCounter = 0
while listCounter < 11:
count = count + listCounter
listCounter = listCounter + 1
print(count)
Exercise
Write a program that uses a while loop to find the sum of the
numbers between 1 and 10.
Rewrite the same program using a for loop and the range()
function.
count = 0
for i in range(1, 11):
count = count + i
print(count)
Exercise
Use nested loops to create the following list:
[ [0, 0, 0], [0, 0, 0], [0, 0, 0]]
Exercise
Use nested loops to create the following list:
[ [0, 0, 0], [0, 0, 0], [0, 0, 0]]
result = []
for i in range(0, 2):
temp = []
for j in range(0, 2):
temp.append(0)
result.append(temp)
Exercise
Use list slicing to print only the second half of myList.
Use list slicing to print every third element of myList.
Exercise
Use list slicing to print only the second half of myList.
print(myList[len(myList // 2):])
Use list slicing to print every third element of myList.
print(myList[::3])
Exercise
What does the following print?
for i in range(0, 10, 2):
for j in range(0, i):
print(j)
Exercise
What does the following print?
for i in range(0, 10, 2):
for j in range(0, i):
print(j)
01
0123
012345
0123456
012345678
Functions
You need to know:
• How to write a function
• What arguments and return values are
• Scope
• Mutability
• How references work
• Global variables
Exercise
What does this print?
def main():
myList = [3, 4, 5]
otherList = myList
otherList[0] = 0
someFunc(otherList)
print(myList)
def someFunc(tempList):
tempList = [0] * 10
main()
Exercise
def main():
myList = [3, 4, 5]
otherList = myList
otherList[0] = 0
someFunc(otherList)
print(myList)
def someFunc(tempList):
tempList = [0] * 10
main()
[0, 4, 5]
Exercise
What does this print?
def main():
myList = [3, 4, 5]
otherList = myList
otherList[0] = 0
otherList = someFunc(otherList)
print(myList)
def someFunc(tempList):
tempList = [0] * 10
return tempList
main()
def main():
myList = [3, 4, 5]
Exercise
otherList = myList
otherList[0] = 0
otherList = someFunc(otherList)
print(myList)
def someFunc(tempList):
tempList = [0] * 10
return tempList
main()
[0, 4, 5]
Exercise
What will this code print?
NUM_QUESTIONS = 20
def main():
NUM_QUESTIONS = 100
someFunc()
def someFunc():
print(NUM_QUESTIONS)
Exercise
What will this code print?
NUM_QUESTIONS = 20
def main():
NUM_QUESTIONS = 100
someFunc()
def someFunc():
print(NUM_QUESTIONS)
20