Augmented assignment/ Assignment operators
Augmented assignment: Python supports augmented assignment for common arithmetic and logical operators. Note: This is not an exhaustive list. Input and output
General variable declaration and assignment
Variables: The basic mechanism by which data is organised and stored (long-term, short-term, and communication etc.). Variables must be declared before referred to in other statements. Note: Variables can be reassigned as many times as needed. General for loops
For loops: A type of definite iterations. Also reference to as control strctures. Loop index: The variable after the for is called the loop index. It takes on each successive value in sequence. Dealing with Strings
Strings: Strings are used to represent a sequence of characters, such as: names, addresses, general text etc. They are written in double quotes. Slicing: The the substring starts at index1 but the last character is at index2-1. The indexes given must both be ints. Slicing: If either start or end expression is missing, then the start or the end of the string is used. Other: Not an exhausted list of functions, other useful ones include strip(), count(), find() and split() etc. Searching
Simple searching: The problem with this is that the index method raises an exception if the sought item is not present. Linear search: As soon as a value is encountered that is greater than the target value, the linear search can be stopped without looking at the rest of the data. Binary search: If the data is already sorted, at each step divide the remaining group of numbers into two parts and ignore the irrelevant one Dealing with tuples
Tuple: A sequence which looks like a list but uses () rather than []. They are immutable, so are used to represent sequences that are not supposed to change. Lambda function: A small anonymous function which can take any number of arguments, but can only have one expression. |
Arithmetic operators
Arithmetic operators: Used to perform common mathematical operations. Precedence: Precedence and associativity are as normal as in maths. Comments
Comments: Comments are ignored by the computer, they exist simply to make the code easier for people to understand. Dealing with lists
Lists: Lists are sequences of arbitrary values enclosed in square brackets. They can hold any datatype. Mutable: Lists are mutable, meaning they can be changed. Strings can not be changed. Note: Not an exhaustive list TBC check Numeric data types
Note: The float type stores only an approximation to the real number being represented. Note: Operations on ints produce ints (excluding /), operations on floats produce floats. Type conversion: Combining an int with a float in an expression will return a float. And we can use the int and round functions to explicitly convert between different types. Converting a float to an int will truncate. Type: We can use the type function to find the data type. General if -elif-else-statements
if statements: The condition statement is evaluated and if it evaluates to True, the indented statements in the body are executed; otherwise, execution proceeds to next statement. Note: Don't forget the colon! General while loops
While loop: A form of indefinite/conditional interation loop. It keeps iterating until the boolean condition is no longer true. Break statement
Break statement: Executing break cases Python to immediately exit the enclosing loop. Note: It is sometimes used to exit what looks like an infinite loop. Loop and a half: The loop exit is in the middle of the loop body. It is an elegant way to avoid the priming read in a sentinel loop. Note: Avoid using break often within loops, because the logic of a loop is hard to follow when there are multiple exits. Continue statement
Continue statement: Returns the control to the beginning of the loop escaping the rest of the code body. Recursion
Recursion: A description of something that refers to itself is called a recursive definition. Base case: Recursion is not circular because we eventually get to the base case that has a closed expression that can be directly computed. Dealing with dictionaries
Dictionary: Widely used collection/compound data type. Allows us to look up information associated with arbitrary keys (mapping) Note: The order of the keys won't matter. |
String formatting
Meanings: index - which parameter to insert into the slot; width tells us how many spaces to use to display the value; 0 means to use as much space as necessary; precision is the number of decimal places Fixed point numbers: Denoted using f in the example. Logical/Boolean operators
Precedence: The interpretation of the expressions relies on the precedence rules for the operators. Range function
Importing modules
Module: A file consisting of Python code which can define functions, classes, variables and may also include runnable code. Note: When Python imports a module, it executes each line. Modules need to be imported in a session only once. Library: A library is a module with some useful definitions/functions. Defining a function
Function: A function is a block of organised, reusable code that is used to perform a single, related action. It is invoked or executed by typing its name. Parameters: Parameters can be used to customise the output of a function. A function that has parameters requires arguments. If that parameter is not specified an error is returned. Relational/Comparison operators
Relational operators: Operators used to compare two values - usually numbers, but also sometimes other types. Precedence: All have lower precedence than all arithmetic operators, and higher than all logical operators. File processing
File: A sequence of data that is stored in secondary memory (disk drive). They can contain any data type, and usually contains more than one line of text. Note: When you've finished working with a file, it needs to be closed. In some cases, not properly closing a file could result in data loss. Note: Multiple calls to readline() is inefficient. Note: May use writelines() for writing sequence(list) of strings. Exception handling
Try-except: When python encounters a try statement, it attempts to execute the try body. If an exception is raised, the handler is executed. If not, control passes to the statement after. Note: There can be multiple except blocks. This acts like 'elif'. Except: A bare except acts like an 'else' and catches any errors without a specific exception type. Note: Exceptions are intended for exceptional circumstances and should not be used as a substitute for if statements. PseudoRandom numbers
Pseudorandom number generator: Starts with a seed value to produce a "random" output. The next time a random number is required, the current value is fed back into the function to produce a new number. Note: This sequence of numbers appears to be random, but if you start the process over again with the same seed number, you'll get the same sequence of "random" numbers. |
Cheatography
https://cheatography.com
Python - Beginner Syntax Cheat Sheet (DRAFT) by soleille01
A summary of all the Python programming language syntax I have learned so far. Still a noob so will have to be updated now and then.
This is a draft cheat sheet. It is a work in progress and is not finished yet.