Accéder au contenu principal

Python cheatsheet

Implementation
  • CPython (interpreter written in C)
  • Cython (compiles to C)
  • Jython (compiles to Java bytecode)
  • IDLE
Print
  • print("line\nline")
  • print("line1", "line2", end="\n", sep="-")
Number
  • 123, 0o173, 0x7b
  • 0.4, .4
  • 30000000, 3E8, 6.62E-34
String quote escaping
  • "string\"string", 'string"string'
  • 'string\'string', "string'string"
Type
  • 2*3 is int, 2*3.0 is float, 2.0*3 is float, same for: ** / // % + -
  • 6/2 is float
  • 4 is int, 4.0 is float
  • 2==2.0 is True
Integer division
  • 6//4=1
  • -6//4=-2
Operator priority
  1. ! ~ + - (unary)
  2. ** (right-sided binding)
  3. */%
  4. +- (binary)
  5. << >>
  6. < <= > >=
  7. == !=
  8. &
  9. |
  10. &&
  11. ||
  12. = += -= *= /= %= &= ^= |= >>= <<=
Comment
  • # comment to the end of the line
Shortcut operator
  • a += 1 is same as a = a+1
  • same for any two-argument operator
Type conversion
  • input("message") returns a string
  • int(string), float(string), str(number)
String operator
  • string + string (concatenation)
  • string * number, number * string (repetition)
Condition
  • if true_or_not: do_this_if_true
  • elif true_or_not: do_this_if_true
  • else: else_do_this
Loop
  • while true_or_not: do_this_while_true
  • else: always_executed
  • for i in range(10): do_this
  • else: always_executed
  • for i in list:
  • pass (empty instruction), break, continue
Range
  • range(first included, last excluded, increment)
  • range(10) = range(0, 10) = range(0, 10, 1)
List
  • a sequence of data which can be scanned by the for loop
  • list = name of a memory location where the list is stored
  • list2 = list1 point to the same list, list2 = list1[:] is a copy
  • list = [10, 5, 7], list[0] = 20, list[-1] = 3
List slicing
  • list[start included : end excluded]
  • list[-1:1] is same as []
  • list[:end]
  • list[start:] is same as list[start:len(list)]
List management
  • len(list)
  • list.append(4), list.insert(where, what), list.sort()
  • del list[1], del list[1:3], del list[:] deletes all values, del list deletes the list
  • elem in list, elem not in list
  • for i in list:
List operator
  • list1 + list2 (concatenation)
  • list * number, number * list (repetition)
Multidimensional list
  • row = ["a" for i in range(8)] is one dimension
  • board = [["a" for i in range(8)] for j in range (8)] is two dimension, board[0][0]
Function
  • def function():
  • function and variable with same name forbidden
  • def function(a, b, c, d=4), function(1, 2, 3, 4), function(c=3, a=1, b=2), function(1, c=3, b=2)
  • return, return None, return 13, return list
  • global var
Tuple
  • a tuple is an immutable sequence type
  • tuple = (1, 2, 3), tuple = 1, 2, 3
  • tuple = (1,), tuple = 1, whereas var = (1) is not a tuple
  • tuple = () is empty
  • tuple[0], tuple[-1], tuple[1:], tuple[:-2]
  • for i in tuple:
Tuple operator
  • tuple1 + tuple2 (concatenation)
  • tuple * number, number * tuple (repetition)
  • v1, v2, v3 = v2, v3, v1
Dictionary
  • an associative array
  • key is unique, integer, float or string
  • dict = {'dog': 'chien', 'horse': 'cheval', 'cat': 'chat'}
  • {} is empty
Dictionary management
  • len(dict)
  • del dict['dog']
  • for key in sorted(dict.keys()):
  • for en,fr in dict.items():
  • for fr in dict.values():

PCAP: Programming Fundamentals in Python (Part 1), pythoninstitute.org.

Commentaires