Statements and syntax

Statements and syntax

  • Statements: Python hierarchy; Statements (one statement per line and indent all in a nested block); Interactive loops.

Single statement span multiple lines: enclosing in parentheses, square brackets or curly braces. Code a compound statement on a single line: only if the body consist of only non-compound statement (eg. print(a)). Type semicolon at the end of a statement: for squeeze onto a single line of code, it can lead to code that is difficult to read. What is a try statement for?: catch and recover from exceptions.

  • Assignments, expressions and prints: Assignment statements; Expression statements; Print operations.

Name ways assign 3 variable to the same value: (A=B=C=D), (A,B,C=0,0,0) or (A=0;B=0;C=0). Why might you need to care when assigning 3 variables to a mutable object?: because propagate the result if is a list or a dictionary, with number or string is irrelevant. Use print to send text to an external file: print(X, file=F).

  • If test and syntax rules: If statements; Python syntax revisited; Truth values and boolean tests; The if/else ternary expression.

Code a multi-way branch: if statement with multiple elif clauses; Dictionaries indexing if the dict contains callable functions coded with def statements or lambda expressions. Code and if/else statement: if/else, and/or. Make s single statement span many times: brackets.

  • While and for loops: While loops; Break, continue, pass and the loop else.

Differences between while and for: for iterate across items in a sequence. Difference between break and continue: break exits the loop immediately. How can you counter-based loop?: using a range function in for loops.

  • Iterations and comprehensions: List comprehension; New iteration; Other iteration.

How are for loops and iterable objects related?: next built-in function. Name 4 iteration contexts: for, map, in, sorted, sum, any, all; list, tuple, join. Best way to read line by line from a text file: next method.

  • Documentation interlude: Common coding gotchas. Docstrings, online and manual resources, PyDoc's help function.

3 Ways you can view documentation strings: help. Obtain a list of the available attributes in an object: dir(x); [a for a in dir(X) if not a.starts with('__')]. Get a list of all available modules on your computer.

Última actualización

¿Te fue útil?