-
Keywords: and
called logical and operator. If both the operands are true then condition becomes true
ex. (a and b) is true
-
Keywords: del
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]
-
Keywords: from
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
-
Keywords: not
negation for the defined operation
ex. not (2 == 1)
-
Keywords: while
The while loop continues until the expression becomes false. The expression has to be a logical expression and must return either a true or false value.
ex. while (x < 10)
-
Keywords: as
used in a "with" statement to define the alias to assign each result of with x to
ex. with open("x.log") as x
-
Keywords: elif
A conditional execution statement following an "if". Shorthand for an else-if.
- ex. if x == 0: do stuff
- elif x == 1: do more stuff
-
Keywords: global
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()
-
Keywords: or
Boolean operator to accept if either condition is True, return True
ex. if (1 == 1 or 1 == 2)
-
Keywords: with
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
-
Keywords: assert
The assert statement verifies that the expression defined resolves to true. If it does not it will raise an Assertion Error with an optional expression2
ex. assert (x == 1 or x == 2)
-
Keywords: else
When all conditional execution statements in an If, Elif block return false, the else will catch and execute
- ex. if x == 1: do 1
- elif x == 2: do 2
- else: do 3
-
Keywords: if
a conditional execution statement which execute some code if a statement is True
ex. if you == x: print "whew!"
-
Keywords: pass
A null operation. When this is executed nothing happens.
- ex. if x < 0: derp()
- else pass
-
Keywords: yield
Return a generator object (like a list of tuple) instead of a static object
- ex. def direct(path):
- yield os.path.walk(path)
-
Keywords: break
will exit out of the "nearest" loop
- ex. while x < 10
- if x < 0
- break
- print x
- x = x + 1
-
Keywords: except
when a try statement fails the except catches the failure and returns
- ex. while True:
- try:
- num = raw_input(int())
- break
- except:
- print "That's not an int."
-
Keywords: import
the import statement initializes an external module so its functions can be used in the current environment
- ex. import os
- print os.path("c:")
-
Keywords: print
the print statement will export the argument to STDOUT
ex. print "sup"
-
Keywords: class
an object that contains functions and can be instantiated
- ex. class Man:
- def height(self):
- print "TALL DUDE"
-
Keywords: exec
allows for the dynamic execution of Python code
- ex. test = print "Sup"
- exec(test)
-
Keywords: in
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"
-
Keywords: raise
used to call an exception that can be customized per situation
ex. raise Exception, "PC LOAD LETTER"
-
Keywords: continue
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 big"
- continue
- print x
-
Keywords: finally
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."
-
Keywords: is
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 []
-
Keywords: return
Outputs the value returned by a function. This cannot be a generator object as return will only return that it is a generator function.
-
Keywords: def
defines functions
ex. def testfunc(x)
-
Keywords: for
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
-
Keywords: lambda
creates an anonymous function that is not bound to a specific name. Is is also called an inline function
- ex. for i in (1, 2, 3, 4, 5):
- a = lambda x: x * x
- print a(i)
-
-
Data Types: True
Boolean true
- ex.
- 1 == 1
- 0 != 1
- True or False
- True and True
-
Data Types: False
Boolean false
- 1 == 0
- 1 != 1
- False or False
- True and False
-
Data Types: None
null, means non-existent, not known, or empty
-
Data Types: strings
a string is a sequence of characters which mathematical functions cannot be performed on
represents textual data in computer programs
-
Data Types: numbers
numbers are a sequence of numbers that math can be performed on
-
Data Types: floats
a float is a string of numbers that is equal to 32-bit storage of integers and fractions representing decimal places
-
Data Types: lists
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 (mutable, and can containe mixed data types)
ex. animals = [bears, koalas, squirrels]
-
String Escapes Sequences: \\
- Name: Backslash()
- What it does: An escape sequence that inserts a backslash in a string
-
String Escapes Sequences: \'
- Name: single quote (')
- What it does: An escape sequence that inserts a single quote in a string
-
String Escapes Sequences: \"
- Name: Double quote (")
- What it does: An escape sequence that inserts a double quote in a string
-
String Escapes Sequences: \a
- Name: ASCII Bell (BEL)
- What it does: An escape sequence that inserts a beep noise in a string
-
String Escapes Sequences: \b
- Name: ASCII Backspace (BS)
- What it does: An escape sequence that inserts a backspace in a string
-
String Escapes Sequences: \f
- Name: ASCII Formfeed (FF)
- What it does: An escape sequence that inserts a new page for a printer (and ejects current page) in a string ???
-
String Escapes Sequences: \n
- Name: Linefeed (LF)
- What it does: An escape sequence that inserts a new line in a string
-
String Escapes Sequences: \r
- Name: Carriage Return (CR)
- What it does: An escape sequence that returns the position at end of line to the beginning of line
-
String Escapes Sequences: \t
- Name: Horizontal Tab (TAB)
- What it does: An escape sequence that inserts a horizontal tab in the string
-
String Escapes Sequences: \v
- Name: ASCII Vertical Tab (VT)
- What it does: An escape sequence that inserts a vertical tab in the string
-
String Formats: %d
- Name: signed decimal
- What it does: displays formatted variables in decimal numery system for users
-
String Formats: %i
- Name: signed decimal, same as %d
- What it does: displays formatted variables in decimal numery system for users
-
String Formats: %o
- Name: signed ocatal
- What it does: displays formatted variables in ocatal numery system for users
-
String Formats: %u
- Name: signed decimal (obsolete)
- What it does: displays formatted variables in decimal numery system for users
-
String Formats: %x
- Name: signed hex, lowercase
- What it does: displays formatted variables in hex numery system for users
-
String Formats: %X
- Name: signed hex, uppercase
- What it does: displays formatted variables in hex numery system for users
-
String Formats: %e
- Name: floating point in exponential format, lowercase
- What it does: displays formatted variables in exponential format for users
-
String Formats: %E
- Name: floating point in exponential format, uppercase
- What it does: displays formatted variables in exponential format for users
-
String Formats: %f
- Name: floating point decimal format, lowercase
- What it does: displays formatted variables in floating point decimal format for users
-
String Formats: %F
- Name: floating point decimal format, uppercase
- What it does: displays formatted variables in floating point decimal format for users
-
String Formats: %g
- Name: floating point format, lowercase exponential format if less than -4, decimal format otherwise
- What it does: displays formatted variables in floating point format for users
-
String Formats: %G
- Name: floating point format, uppercase exponential format if less than -4, decimal format otherwise
- What it does: displays formatted variables in floating point format for users
-
String Formats: %c
- Name: single character (accepts integer or single character string)
- What it does: displays formatted variables in single character for users
-
String Formats: %r
- Name: r string format (converts any Python object using repr())
- What it does: for debugging, displays the "raw" data of the variable. Also known as the "representation"
-
String Formats: %s
- Name: s string format (converts any Python object using str())
- What it does: displays variables for users
-
String Formats: %%
- Name: sign
- What it does: no argument is converted, results in a '%' character in the result
-
-
-
Operators: *
multiplication
-
Operators: **
exponentiation, "to the power of"
-
-
Operators: //
floor division, quotient, division without any remainder
-
Operators: %
modulo, the remainder of division
-
-
Operators: >
greater-than
-
Operators: <=
less-than-or-equal
-
Operators: >=
greater-than-or-equal
-
-
Operators: !=
not equal to
-
Operators: <>
not equal to
-
Operators: ( )
tuple
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be changed, i.e. tuples are immutable and tuples use parenthesis and lists use square brackets
- ex.
- tup1 = (1, 2, 3, 4, 5,);
- tup3 = "a", "b", "c", "d";
-
Operators: [ ]
list
The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets
- ex.
- list1 = ['physics', 'chemistry', 1997, 2000];
- list2 = [1, 2, 3, 4, 5]
- list3 = ["a", "b", "c", "d"]
-
Operators: { }
set, dictionary
A dictionary is mutable and is another container type that can store any number of Python objects, including other container types.
- ex.
- dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
-
-
-
-
Operators: .
apply command to a file
-
Operators: =
simple assignment operator
assigns value from right side operand to variable on left side operand
-
-
Operators: +=
Addition AND assignment operator
add to the variable by argument and re-declare as the new value
contraction of +=, x = x + y is the same as x += y
-
Operators: -=
subtract AND assignment operator
subtract from the variable by the argument and re-declare as the new variable
contraction of -=, x = x - y is the same as x -= y
-
Operators: *=
multiply AND assignment operator
multiply the variable by the argument and re-declare as the new value
contraction of *=, x = x * y is the same as x *= y
-
Operators: /=
divide AND assignment operator
divide the variable by the argument and re-declare as the new value
contraction of / and =, x = x / y is the same as x /= y
-
Operators: //=
floor division and assigns a value
get the quotient of the variable by the argument and re-declare as the new value
c //= a is equivalent to c = c // a
-
Operators: %=
modulo AND assignment operator,
Get the remainder by dividing the variable by the argument and re-declaring it as the new value
contraction of % and =, x = x % y is the same as x %= y
-
Operators: **=
exponentiate AND assignment operator
exponentiate the variable by the argument and re-declare as the new value
x **= y is equivalent to x = x ** y
|
|