Shared Flashcard Set

Details

Learn Python the Hard Way - Exercise 37
Learn Python the Hard Way - Exercise 37
89
Computer Science
Professional
11/23/2010

Additional Computer Science Flashcards

 


 

Cards

Term
and
Definition
Called Logical AND operator. If both the operands are true then then condition becomes true.

ex. (a and b) is true.
Term
del
Definition
The del statement is a way to remove an item from a list given its index instead of its value: the del statement.

ex. a = [-1, 1, 66.25, 333, 333, 1234.5]
del a[0]
a
[1, 66.25, 333, 333, 1234.5]
Term
from
Definition
The root of a from-import statement. The from-import statement is used to import specific objects from a module.

ex. from x import y
Term
not
Definition
A negation for the defined operation.

ex. not(2 == 1)
Term
while
Definition
The while loop continues until the expression becomes false. The expression has to be a logical expression and must return either a true or a false value.

ex. while (x < 10):
Term
as
Definition
Used in a 'with' statement to define the alias to assign each result of with x to.

ex. with open("x.log") as x
Term
elif
Definition
A conditional execution statement following an 'if'. Shorthand for an else-if.

ex. if x==0: do stuff
elif x==1: do more stuff
Term
global
Definition
The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals.

ex. globes = 0
def glob_1():
global globes
globes = 1
def glob_2():
print globes
glob_1()
glob_2()
Term
or
Definition
Boolean operator to accept if either condition is True, return True.

ex. if (1==1 or 1==2):
Term
with
Definition
The with statement is used to wrap the execution of a block with functionality provided by a separate guard object (see context-managers). This allows common try-except-finally usage patterns to be encapsulated for convenient reuse.

ex. with open("bzr.log") as x:
data = x.read()
print data
Term
assert
Definition
The assert statement verifies that the expression defined resolves to true. If it does not it will raise an AssertionError with an optional expression2.

ex. assert (x==1 or x==2)
Term
else
Definition
When all conditional execution statements in an If, Elif block return false, the else will catch and execute.

ex. if x==1: do1
elif x==2: do2
else: do3
Term
if
Definition
A conditional execution statement which executes some code if a statement is True.

ex. if you == stinkypant: print "whew!"
Term
pass
Definition
A null operation. When this is executed nothing happens.

ex. if x<0: derp()
else pass
Term
yield
Definition
Return a generator object(like a list or tuple) instead of a static object.

ex. def direct(path):
yield os.path.walk(path)
Term
break
Definition
Will exit out of the 'nearest' loop.

ex. while x<10:
if x<0:
break
print x
x = x+1
Term
except
Definition
When a try statement fails the except catches the failure and returns.

ex. while True:
try:
num = raw_input(int())
break
except:
print "Thats not an int."
Term
import
Definition
The import statement initializes an external module so its functions can be used in the current environment.

ex. import os
print os.path("c:")
Term
print
Definition
The print statement will export the argument to STDOUT.

ex. print "sup"
Term
class
Definition
A object that contains functions and can be instantiated.

ex. class Man:
def height(self):
print "TALL DUDE"
Term
exec
Definition
Allows for the dynamic execution of Python code.

ex. test = print "Sup"
exec(test)
Term
in
Definition
When used with an if, allows the code to loop through all members of a generator object.

ex. x = 1
if x in [1,2,3,4,5]: print "Sup"
Term
raise
Definition
Used to call an exception that can be customized per situation.

ex. raise Exception, "PC LOAD LETTER"
Term
continue
Definition
When encountered, causes the application to skip the rest of the current loop.

ex. k = [1,2,3,4,5]
for x in k:
if x>3:
print "TOO BEEG"
continue
print x
Term
finally
Definition
The finally statement is the cleanup function in a 'try' statement. While all of the try...except...else are executed, if an exception is raised it is parsed and printed in the finally statement. This also causes any application that is preemptively interrupted to still execute the finally statement before closing out of the loop.

ex. try:
d = file("herp.txt")
while True:
line = d.readline()
if len(line) == 0:
break
time.sleep(2)
print line,
finally:
d.close()
print "Cleaning up your mess."
Term
is
Definition
Compares the object identity of two objects. Two objects referencing the same memory location will return True, but objects that are 'equal' will not.

ex. 1 == 1
1 is 1
[] == []
[] is []
Term
return
Definition
Outputs the value returned by a function. This cannot be a generator object as return will only return that it is a generator function.

ex. def poop
return 2 * 2
Term
def
Definition
Define a function.

ex. def testfunc(poops)
Term
for
Definition
Iterate through a generator object. The for specifically defines a variable to store each individual iteration as the generator counts through.

ex. i = ["Hello","world!"]
for x in i:
print x
Term
lambda
Definition
Creates an anonymous function that is not bound to a specific namespace.

ex. for i in (1,2,3,4,5):
a = lambda x: x * x
print a(i)
Term
True
Definition
Boolean true.
Term
False
Definition
Boolean false.
Term
None
Definition
Null
Term
strings
Definition
A string is a sequence characters which mathematical functions cannot be performed on.
Term
numbers
Definition
Numbers are a sequence of numbers that math can be performed on.
Term
floats
Definition
A float is a string of numbers that is equal to 32-bit storage of integers and fraction representing decimal places.
Term
lists
Definition
A list is a dynamic storage of multiple variables in a single object that can be iterated. Unlike a tuple, lists can be changed after creation.
Term
ES: \\
Definition
\
Term
ES: \'
Definition
'
Term
ES: \"
Definition
"
Term
ES: \a
Definition
ASCII bell
Term
ES: \b
Definition
ASCII backspace
Term
ES: \f
Definition
ASCII formfeed
Term
ES: \n
Definition
newline
Term
ES: \r
Definition
carriage return
Term
ES: \t
Definition
tab
Term
ES: \v
Definition
vertical tab :-/
Term
%d
Definition
signed decimal
Term
%i
Definition
signed decimal (same as %d)
Term
%u
Definition
Signed decimal (obsolete)
Term
%o
Definition
octal
Term
%x
Definition
signed hex, lowercase
Term
%X
Definition
signed hex, uppercase
Term
%e
Definition
floating point in exponential format, lowercase
Term
%E
Definition
floating point in exponential format, uppercase
Term
%f
Definition
floating point decimal format
Term
%F
Definition
floating point decimal format
Term
%g
Definition
floating point format. Lowercase exponential format if less than -4, decimal format otherwise
Term
%G
Definition
floating point format. Uppercase exponential format if less than -4, decimal format otherwise
Term
%c
Definition
Single character
Term
%r
Definition
String, converts using repr(), "x"
Term
%s
Definition
string, converts using str(), x
Term
%%
Definition
No argument converted. Results in '%' in the result
Term
+
Definition
add
Term
-
Definition
subtract
Term
*
Definition
multiply
Term
**
Definition
Exponentiation, 'to the power of'
Term
/
Definition
divide
Term
//
Definition
quotient, division without any remainder
Term
%
Definition
remainder
Term
<
Definition
less than
Term
>
Definition
greater than
Term
<=
Definition
less than or equal to
Term
>=
Definition
greater than or equal to
Term
==
Definition
comparison
Term
!=
Definition
not equal to comparison
Term
<>
Definition
not equal to comparison (obsolete)
Term
()
Definition
tuple
Term
[]
Definition
list
Term
{}
Definition
set
Term
@
Definition
no idea!
Term
=
Definition
declaration of a variable
Term
+=
Definition
add to the variable by argument and re-declare as the new value
Term
-=
Definition
subtract from the variable by the argument and re-declare as the new value
Term
/=
Definition
divide by the variable by the argument and re-declare as the new value
Term
*=
Definition
multiply the variable by the argument and re-declare as the new value
Term
//=
Definition
get the quotient of the variable by the argument and re-declare as the new value
Term
%=
Definition
Get the remainder by dividing the variable by the argument and re-declaring it as the new value
Term
**=
Definition
exponentiate the variable by the argument and re-declare as the new value
Supporting users have an ad free experience!