Skip to content

Instantly share code, notes, and snippets.

@MichaelMartinez
Created November 7, 2012 04:21
Show Gist options
  • Save MichaelMartinez/4029547 to your computer and use it in GitHub Desktop.
Save MichaelMartinez/4029547 to your computer and use it in GitHub Desktop.
stuff1

This is the Python half-way review

Keywords and more

• and - use to denote && inclusive of a particular condition • del - delete a value • from - tells python what and where to look for functions • not - exclusive to a particular value, as in "luke, you are NOT my father" • while - run until the condition is no longer true • as - ??? • elif - else if, a serrogate of the if statement • global - global value, the entire app knows this member • or - use to denote || not inclusive as in "either OR" • with - ??? • assert - to assert a statement • else - also known as "otherwise" • if - asks about the status of a condition • pass - keep on truckin' • yield - stop • break - stop the switch right here, right now • except - everything but the value that comes after this statement • import - bring in some functions and modules • print - I want to see the results of this shit • class - a self contained mini python program • exec - execute this code • in - is it in a list or dict? We tell the program what list we want to iterate over • raise - Bring an exception to the front • continue - keep on truckin' • finally - if everything has run, I still want you to ru this • is - is tests for identity, not equality. That means Python simply compares the memory address a object resides in. is basically answers the question "Do I have two names for the same object?" - overloading that would make no sense. • return - Bring me back some shit to work with • def - this is a function intro • try - first try the clause • for - iterate over the items in any sequence... in the order they appear • lambda - lambda functions:

f = lambda x: x**2 + 2*x - 5

Those things are actually quite useful. Python supports a style of programming called functional programming where you can pass functions to other functions to do stuff. Example:

mult3 = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])

sets mult3 to [3, 6, 9], those elements of the original list that are multiples of 3. This is shorter (and, one could argue, clearer) than

def filterfunc(x):
    return x % 3 == 0
mult3 = filter(filterfunc, [1, 2, 3, 4, 5, 6, 7, 8, 9])

Of course, in this particular case, you could do the same thing as a list comprehension:

mult3 = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x % 3 == 0]

(or even as range(3,10,3)) but there are other cases, like constructing functions as return values from other functions, where you can't use a list comprehension and a lambda function may be the shortest way to write something out. Like

def transform(n):
    return lambda x: x + n
f = transform(3)
f(4) # is 7

For data types, write out what makes up each one. For example, with strings write out how you create a string. For numbers write out a few numbers.

• True - if x == True • False - if b == False • None - • strings - print "stuff" • numbers - 56 • floats - 45.758 • lists - my_list = [1, 2, 3, 5]

For string escape sequences, use them in strings to make sure they do what you think they do.

• \ - comment • \’ - single quote • " - double quote • \a - ASCII bell • \b - backspace • \f - Form feed • \n - new line • \r - carriage return • \t - tab • \v - vertical tab

Same thing for string formats: use them in some strings to know what they do.

• %d - decimal number • %i - integer • %o - octal • %u - unsigned decimal int • %x - hex integer • %X - Hex integer (upper case) • %e - exponential notation (lower case "e") • %E - exponential notation (upper case "E") • %f - floating point number • %F - same • %g - the shorter of %f and %e • %G - same • %c - character • %r - • %s - string • %% - literal %

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment