Week I Cheat Sheet
Flowchart
A flowchart is a step-by-step approach until you find the answer. Flowcharts help you to visualize the processes in small steps and they are very similar to how the computer executes your instructions.
Part | Shape | Explanation |
Start/End | rounded rectangles | Start is required of all flowcharts, while some flowcharts may not have an end. |
Process | rectangle | It involves the action, to do something. e.g. |
Input/Output | parallelogram | It indicates that manual operation is needed. e.g. |
Decision | rhombus | e.g. |
Arrow | arrow | It indicates the flow of the chart. |
Comment
Comments in Python begin with a hash mark #
and whitespace character. Comments are useful as notes for readers of the program to understand what the program is doing. You can use #
in Python to comment a single line or consecutively. The keyboard shortcut is ctrl + /
.
Another way to add multiline comments is to use triple-quote.
Print
Variables & Literal Constants
Constants, or literal constants are numbers like 2
, 3.14
, or strings like "rhino"
, "cat"
. They are constant because their value cannot be changed. For example, 2
would only represent the number 2 and nothing else.
A variable is created when you assign it to a value or an object. You could imagine variables as names or sticky notes of the value you want to store. The value can vary, but the name won't change. When you want to access the information you have stored on your computer, you call them by their name.
Numbers
There are mainly two types of numbers in Python - intergers and floats. Integers are numbers without a frational part, e.g., 3
, 100
. Floating-point numbers are numbers with a decimal point in them, e.g., 3.14
, 5.3E-4
(E
= powers of 10).
List
In a list, items are ordered, changeable, and allow duplicate values. List is mutable, which means the items inside the list can be modified.
A list could grow and schrink on demand.
append
method expands the list’s size and inserts an item at the end.
insert
method insert an item at a certain / arbirtrary position.
pop
method (or an equivalent del
statement) removes an item at a given offset.
remove
method removes a given item by value
The length of the list can be checked by len(list)
.
if
Statement
if
Statementif
statement is used to check a condition and decide whether or not to execute a code block. if
the condition is True
, we run a block of statements (if-block
), else
we process another block of statements (else-block
). The else
clause is optional. If we need to check multiple conditions, we could add multipleelif-block
between if-block
and else-block
.
Eample:
Output:
For Loop & While Loop
For loop and while loop are both loop statements.
A for loop is frequently used for iterating over a sequence of objects (list, tuple, dictionary, set, string). For example, using for loop and range()
can give us a sequence of numbers from 0 to 10.
The enumerate()
method adds counter when we iterate an enumerate object.
The while
statement allows you to repeatedly execute a block of statements as long as a condition is True
. In this example, we use while
loop to generate a list containing integer numbers from 0 to 10.
The break
statement is used to break out of a loop statement
The continue
statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.
Pythonic styles
https://www.python.org/dev/peps/pep-0008/#naming-conventions
Last updated