Scientific Programming Midterm

30 flashcardsby Eucalyptus
Study these flashcardsFree · spaced repetition · iPhone, Android, Mac & web

All cards

1
What is a major difference between Python 2 and 3?
In python 2, print is not a function and is written as,
print "text"
2
Tuple
Created with (), immutable, ordered list
3
List
Created with [], mutable
4
What is the syntax for slicing?
list[begin: end: skip] = list[1: 5: 2]
5
How does list slicing work?
Begin: Index started at
End: Index ended at, this index is not included in the slice
Skip: Index to skip up to, using 2 would skip the next one and include the one after
6
Strings are immutable, what does this mean?
They cannot be changed, modifications to string create entirely new strings.
The old one is automatically destroyed by python.
7
Which objects are mutable?
Dictionaries, lists
8
Which objects are immutable?
ints, floats, tuples, strings
9
Special methods include
__getitem__()
__len__()
10
For loop syntax
for i in list:
print(i)
11
flush()
Write to the file immediately, do not buffer
12
close()
Close the file
13
The with statement,
Opens a file without needing to close it
14
With statement syntax
with open("output.dat", "w") as f:
for x in dictsList:
print("${:<10.2f} ${:<10.2f} ${:<10.2f} ${:<10.2f}".format(x["SubTotal"], x["Tax"], x["Tip"], x["Total"]), file = f)
15
Function syntax
def multiplyByTwo(num):
'''
Documentation goes here!
'''
return num * 2
16
Lambda or anonymous function syntax
is_2020 = lambda x: x["Year"] == "2020"
17
What is try / except
Code will attempt to run, and if an error is ran into, the error is handeled in the except block
18
Raise syntax
if x < 0
raise Exception("No numbers less than 0")
19
What will i and entry do here?
for i, entry in enumerate(list):
i will act as a counter for how many iterations of the loop have been ran.
entry is the data for that specific index (i)
20
What will i do here?
I = iter([1,2,3,4])
Create an iterator that allows you to iterate through the list.
You can use next to make it go from 1 to 2.
21
What does this do?
L= [1, 2, 3]
R= ['a', 'b', 'c']
for lval, rval in zip(L,R):
print(lval, rval)
Iterates over multiple iterables in parallel.
22
What does map do here?
square = lambda x: x ** 2
for val in map(square, range(10)):
print(val, end=' ')
Applys a function to every element in the iterable.
23
What does filter do here?
is_even = lambda x: x % 2 == 0
for val in filter(is_even, range(10)):
print(val, end=' ')
Applys a function to every element in an iterable, but returns true or false.
24
Is the range function an iterable? How about an iterator?
Yes.
No.
25
What list will
L = [n ** 2 for n in range(6)]
create?
[0, 1, 4, 9, 16, 25]
26
Syntax for a conditional list comphrehension:
[val for val in range(20) if val % 3 > 0]
27
What is a generator?
An iterable object that produces values in sequence.
The entire object is not stored.
28
Generator syntax:
G = (n**2 for n in range(10))
29
What does yield do?
Allows you to continue where you left off in a function, replaces return.
30
What is the difference between the split and strip functions?
Strip removes a substring or specified characters from the beginning or end of an input string.
Split breaks the string based on a specified delimiter.
Study all 30 cards for free