EGR 103/Spring 2019/Skills Quiz
Jump to navigation
Jump to search
Contents
ints and floats
- Operators (** * / % // + -) -- work as expected as long as you expect x**y to be x to the yth power!
- += and -= to add or subtract a value from a variable
- Relational operators (< <= == >= > !=)
- round(blah, [ndigits=0]) -- rounds to the 10**(-ndigits) place; round(123.456, -1) is 120.0 for instance.
- int(blah) returns the integer value of blah if blah is a string with a valid integer in it
- float(blah) returns the float value of blah if blah is a string with a valid float in it
lists
- Sequence made up of any types of items, including other lists
- + will concatenate lists ([1, 2] + [3] yields [1, 2, 3])
- Have to be lists -- [1, 2] + 3 yields an error!
- += will append an item to a list - that item must be a list!
- * will repeat lists -- [1, 2] * 3 yields [1, 2, 1, 2, 1, 2]
- Useful commands (assume the variable collection is a list):
- max(collection), min(collection) -- returns the appropriate value if all items are the same type (i.e. all numbers or all strings or all lists)
- slicing
- collection.copy() to create duplicate list somewhere else in memory
tuples
- Immutable - cannot change values
- Sequence made up of any types of items, including other tuples
- Single-element tuples created by putting , after item: x = (3,)
- + will concatenate tuples ((1, 2) + (3,) yields (1, 2, 3))
- Have to be tuples -- (1, 2) + 3 or (1, 2) + (3) both yield an errors!
- += will append an item to a tuple and replace the old tuple with the new one - that item must be a tuple!
- * will repeat tuples -- (1, 2) * 3 yields (1, 2, 1, 2, 1, 2)
- Useful commands (assume the variable collection is a tuple):
- max(collection), min(collection) -- returns the appropriate value if all items are the same type (i.e. all numbers or all strings or all lists)
- slicing
- collection.copy() to create duplicate list somewhere else in memory
strings
- Immutable - cannot change characters
- + will concatenate strings ('Go' + ' ' + 'Duke' yields 'Go Duke')
- * will repeat strings ('Go' * 4 yields 'GoGoGoGo')
- ord(letter) can work on a single-character to return the ASCII value of that single letter; chr(number) will give the string related to the ASCII value of number. Since this only works on one character, need to map it to get all of them.
- Useful commands (assume the variable phrase is some string):
- phrase = input("prompt: ") -- input always gets a string, use int() or float() to convert to something else if needed
- phrase.lower(), phrase.upper(), phrase.capitalize() -- returns a string with words having appropriate cases
slices
- work on strings, lists, and tuples
- thing[a:b:c] will slice the elements in thing starting with the ath item and going to just before the bth item, skipping c items at a time.
- For a thing with N elements,
- Default for a is 0 if c is positive or N if c is negative
- Default for b is N if c is positive or "up to and including the 0th entry" if c is negative
- If going backwards, you must include a negative c; thing[4:2] will return an empty version the same type as thing
logic
- Logical operators (not, and, or)
- "in" operator
- check if integer is in a list or tuple
- check if a letter or string is in a list of strings or in a string -- note different between
"Ha" in "Happy"
(which is True) and"Ha" in ["Happy"]
, which is false - Note: if you are checking if a list is in another list, the whole list has to be there! So,
1 in [1, 2, 3]
is true,[1] in [1, 2, 3]
is false, and[1] in [[1], 2, 3]
is true.
math
- requires import math or import math as m
- Constants: m.pi and m.e
- Trig -- only works on a single value (m.sin(), m.cos(), m.asin(), etc); degrees are in radians
- Rounding -- m.ceil(), m.floor()
- Others:
- m.log(x, [b==m.e]) returns base b logarithm of x
- m.exp(x) returns m.e**x
numpy
- requires import numpy or import numpy as np
- np.linspace()
- np.random.randint(), np.random.uniform(), np.random.normal(), np.random.seed()
- np.polyfit(x, y, ord) and np.polyval(coefs, new_x)
- np.loadtxt('filename')
printing
- Be able to use print using replacement fields and the format command
- Given a replacement field of the form "{" [field_name] ["!" conversion] [":" format_spec] "}", be able to use:
- field name: if blank, take next field on stack; if a number, take that entry in the stack
- conversion - N/A
- format spec - given the general format spec of [[fill]align][sign][#][0][width][,][.precision][type], be able to use:
- sign: if this is +, put - and + in front of numbers; otherwise, ignore +
- width: reserve at least this much space to print the number; if the number needs more space, it will take it. If it needs left, default alignment is to the right for a number and to the left for a string
- precision: number of digits after the decimal point
- type: s for strings, f for regular floating point, e for scientific notation
- default is for print to append '\n' to go to next line; use print(thing, end="") to stop that from happening
plotting
- requires import matplotlib.pyplot as plt
- Start with
- fig,ax = plt.subplots(num=1, clear=True)
- fig.clf()
- ax.plot(y)
- ax.plot(x, y)
- ax.plot(x, y, format)
- ax.set_xlabel(), ax.set_ylabel(), ax.set_title(), ax.grid()
- fig.savefig(NAME)
- Need to know for the format:
- colors: b g r c m y k w
- line styles: - -- -. :
- marker styles: . o s + D
- Can only put one color in format
functions
- definition and default cases
- required inputs,
- returns (returns a tuple - can either grab all or each individually)
program flow
- if trees
- May include multiple elif branches
- May include else branch at the end
- for loops
- for k in LIST: iterates over each item in the list
- for k in TUPLE: iterates over each item in the tuple
- for k in STRING: iterates over each character in the string
- for k in range(stop): iterates over integers from 0 to stop-1
- for k in range(start, stop): iterates over integers from start to stop-1
- for k in range(start, stop, step): iterates over integers from start by step to the value before hitting or going over stop
- nested versions of all of the above
unix
Be able to define the following commands, show common use cases
- pwd
- ls
- also ls -a
- cd
- mkdir
- wget
- Given a web address for a file, know how to get the file
- tar
- specifically tar -kxvf FILE.tar
- cp
- also cp -ir
- emacs
- latex
- dvipdf
- evince
- & after a command
Not on test
- LaTeX
- binary