Skip to content

Instantly share code, notes, and snippets.

@raacampbell
Created June 20, 2019 07:40
Show Gist options
  • Save raacampbell/8bc1db268f8074cd56e22a1ee2dcbd29 to your computer and use it in GitHub Desktop.
Save raacampbell/8bc1db268f8074cd56e22a1ee2dcbd29 to your computer and use it in GitHub Desktop.
Stuff in Python that's notable to people coming from a MATLAB background
## Multiple assignment
a=b=c=0 # all have the same value
a,b,c = 1,2,3 #a=1, b=2, c=3
a,*b,c = 1,11,22,33,44,2 #b contains all numbers >10
# Chaining comparison operators
a < b <= c < d # is like saying a<b and b<=c and c<d
# List comprehensions can include logical statements or be nested
result3 = [x+1 for x in some_sequence if x>23]
result5 = [x for sublist in listoflists for x in sublist] # flattens a list
# Else in a loop
for x in some_container:
if is_ok(x): break # item x is satisfactory, terminate loop
else:
# This runs at the end of the for loop if it terminated naturally. Same for while loop.
print('Beware: no satisfactory item was found in container') x=None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment