Python Symbol Review ex37

Card Set Information

Author:
Yongmin
ID:
171492
Filename:
Python Symbol Review ex37
Updated:
2012-09-27 15:54:49
Tags:
python
Folders:

Description:
Review of Python symbols and words ex37
Show Answers:

Home > Flashcards > Print Preview

The flashcards below were created by user Yongmin on FreezingBlue Flashcards. What would you like to do?


  1. Keywords: and
    called logical and operator. If both the operands are true then condition becomes true

    ex. (a and b) is true
  2. 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]
  3. 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
  4. Keywords: not
    negation for the defined operation

    ex. not (2 == 1)
  5. 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)
  6. 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
  7. 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
  8. 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()
  9. Keywords: or
    Boolean operator to accept if either condition is True, return True

    ex. if (1 == 1 or 1 == 2)
  10. 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
  11. 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)
  12. 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
  13. Keywords: if
    a conditional execution statement which execute some code if a statement is True

    ex. if you == x: print "whew!"
  14. Keywords: pass
    A null operation. When this is executed nothing happens.

    • ex. if x < 0: derp()
    • else pass
  15. 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)
  16. Keywords: break
    will exit out of the "nearest" loop

    • ex. while x < 10
    • if x < 0
    • break
    • print x
    • x = x + 1
  17. 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."
  18. 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:")
  19. Keywords: print
    the print statement will export the argument to STDOUT

    ex. print "sup"
  20. Keywords: class
    an object that contains functions and can be instantiated

    • ex. class Man:
    • def height(self):
    • print "TALL DUDE"
  21. Keywords: exec
    allows for the dynamic execution of Python code

    • ex. test =  print "Sup"
    • exec(test)
  22. 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"
  23. Keywords: raise
    used to call an exception that can be customized per situation

    ex. raise Exception, "PC LOAD LETTER"
  24. 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
  25. 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."
  26. 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 []
  27. 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.

    • ex. def x
    • return 2 * 2
  28. Keywords: def
    defines functions

    ex. def testfunc(x)
  29. 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
  30. 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)
  31. Keywords: try
  32. Data Types: True
    Boolean true

    • ex.
    • 1 == 1
    • 0 != 1
    • True or False
    • True and True
  33. Data Types: False
    Boolean false

    • 1 == 0
    • 1 != 1
    • False or False
    • True and False
  34. Data Types: None
    null, means non-existent, not known, or empty
  35. Data Types: strings
    a string is a sequence of characters which mathematical functions cannot be performed on

    represents textual data in computer programs
  36. Data Types: numbers
    numbers are a sequence of numbers that math can be performed on
  37. Data Types: floats
    a float is a string of numbers that is equal to 32-bit storage of integers and fractions representing decimal places

    • ex.
    • 1.35
    • 4.000
    • 0.25
  38. 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]
  39. String Escapes Sequences: \\
    • Name: Backslash()
    • What it does: An escape sequence that inserts a backslash in a string
  40. String Escapes Sequences: \'
    • Name: single quote (')
    • What it does: An escape sequence that inserts a single quote in a string
  41. String Escapes Sequences: \"
    • Name: Double quote (")
    • What it does: An escape sequence that inserts a double quote in a string
  42. String Escapes Sequences: \a
    • Name: ASCII Bell (BEL)
    • What it does: An escape sequence that inserts a beep noise in a string
  43. String Escapes Sequences: \b
    • Name: ASCII Backspace (BS)
    • What it does: An escape sequence that inserts a backspace in a string
  44. 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 ???
  45. String Escapes Sequences: \n
    • Name: Linefeed (LF)
    • What it does: An escape sequence that inserts a new line in a string
  46. 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
  47. String Escapes Sequences: \t
    • Name: Horizontal Tab (TAB)
    • What it does: An escape sequence that inserts a horizontal tab in the string
  48. String Escapes Sequences: \v
    • Name: ASCII Vertical Tab (VT)
    • What it does: An escape sequence that inserts a vertical tab in the string
  49. String Formats: %d
    • Name: signed decimal 
    • What it does: displays formatted variables in decimal numery system for users
  50. String Formats: %i
    • Name: signed decimal, same as %d 
    • What it does: displays formatted variables in decimal numery system for users
  51. String Formats: %o
    • Name: signed ocatal
    • What it does: displays formatted variables in ocatal numery system for users
  52. String Formats: %u
    • Name: signed decimal (obsolete)
    • What it does: displays formatted variables in decimal numery system for users
  53. String Formats: %x
    • Name: signed hex, lowercase
    • What it does: displays formatted variables in hex numery system for users
  54. String Formats: %X
    • Name: signed hex, uppercase
    • What it does: displays formatted variables in hex numery system for users
  55. String Formats: %e
    • Name: floating point in exponential format, lowercase
    • What it does: displays formatted variables in exponential format for users
  56. String Formats: %E
    • Name: floating point in exponential format, uppercase
    • What it does: displays formatted variables in exponential format for users
  57. String Formats: %f
    • Name: floating point decimal format, lowercase
    • What it does: displays formatted variables in floating point decimal format for users
  58. String Formats: %F
    • Name: floating point decimal format, uppercase
    • What it does: displays formatted variables in floating point decimal format for users
  59. 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
  60. 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
  61. String Formats: %c
    • Name: single character (accepts integer or single character string)
    • What it does: displays formatted variables in single character for users
  62. 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"
  63. String Formats: %s
    • Name: s string format (converts any Python object using str())
    • What it does: displays variables for users
  64. String Formats: %%
    • Name: sign
    • What it does: no argument is converted, results in a '%' character in the result
  65. Operators: +
    Addition
  66. Operators: -
    subtraction
  67. Operators: *
    multiplication
  68. Operators: **
    exponentiation, "to the power of"
  69. Operators: /
    division
  70. Operators: //
    floor division, quotient, division without any remainder
  71. Operators: %
    modulo, the remainder of division
  72. Operators: <
    less-than
  73. Operators: >
    greater-than
  74. Operators: <=
    less-than-or-equal
  75. Operators: >=
    greater-than-or-equal
  76. Operators: ==
    equal to
  77. Operators: !=
    not equal to
  78. Operators: <>
    not equal to 
  79. 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";
  80. 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"]
  81. 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'}
  82. Operators: @
  83. Operators: ,
  84. Operators: :
    create code
  85. Operators: .
    apply command to a file
  86. Operators: =
    simple assignment operator

    assigns value from right side operand to variable on left side operand
  87. Operators: ;
  88. 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
  89. 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
  90. 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
  91. 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
  92. 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
  93. 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
  94. 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

What would you like to do?

Home > Flashcards > Print Preview