Base Python
Basics
- help:
function_name?
print
- new line:
\n
- new line:
- Addition:
3+5
- Concatenate Strings:
"hello" + " " + "world"
- List:
x = [3, 4, 5]
- Can't add lists of umbers using
x + y
- Can't add lists of umbers using
For Loops
for x in []:
~stuff~
+=
increment
total = 0
for value, weight in zip([2,3,19],[0.2,0.3,0.5]):
total += weight * value
print('Weighted average is: {0}'.format(total))
zip()
loops over sequences of tuples- The reference docs.python.org/3/library/string.html includes many helpful and more complex string formatting examples
The function dir()
provides a list of objects in a namespace. When used with an object, it includes attributes and any methods associated with the object.
Defining Functions
there is already a function for plotting lines, this is just reference for defining functions
def abline(ax, b, m, *args, **kwargs):
"Add a line with slope m and intercept b to ax"
xlim = ax.get_xlim()
ylim = [m * xlim[0] + b, m * xlim[1] + b]
ax.plot(xlim, ylim, *args, **kwargs)
*args
allows any number of non-named arguments toabline
, while*kwargs
allows any number of named arguments (such aslinewidth=3
) toabline
- 4. More Control Flow Tools — Python 3.12.4 documentation