Implementation
PCAP: Programming Fundamentals in Python (Part 1), pythoninstitute.org.
- CPython (interpreter written in C)
- Cython (compiles to C)
- Jython (compiles to Java bytecode)
- IDLE
- print("line\nline")
- print("line1", "line2", end="\n", sep="-")
- 123, 0o173, 0x7b
- 0.4, .4
- 30000000, 3E8, 6.62E-34
- "string\"string", 'string"string'
- 'string\'string', "string'string"
- 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
- 6//4=1
- -6//4=-2
- ! ~ + - (unary)
- ** (right-sided binding)
- */%
- +- (binary)
- << >>
- < <= > >=
- == !=
- &
- |
- &&
- ||
- = += -= *= /= %= &= ^= |= >>= <<=
- # comment to the end of the line
- a += 1 is same as a = a+1
- same for any two-argument operator
- input("message") returns a string
- int(string), float(string), str(number)
- string + string (concatenation)
- string * number, number * string (repetition)
- if true_or_not: do_this_if_true
- elif true_or_not: do_this_if_true
- else: else_do_this
- 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(first included, last excluded, increment)
- range(10) = range(0, 10) = range(0, 10, 1)
- 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[start included : end excluded]
- list[-1:1] is same as []
- list[:end]
- list[start:] is same as list[start:len(list)]
- 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:
- list1 + list2 (concatenation)
- list * number, number * list (repetition)
- 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]
- 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
- 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:
- tuple1 + tuple2 (concatenation)
- tuple * number, number * tuple (repetition)
- v1, v2, v3 = v2, v3, v1
- an associative array
- key is unique, integer, float or string
- dict = {'dog': 'chien', 'horse': 'cheval', 'cat': 'chat'}
- {} is empty
- 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
Enregistrer un commentaire