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. add 1

Input/Output

parallelogram

It indicates that manual operation is needed. e.g. type in the number

Decision

rhombus

e.g. Is the number bigger than 10?

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 + /.

# This is a single line comment.
# This is a multi line 
# comment.

Another way to add multiline comments is to use triple-quote.

''' This is a multi line 
comment. '''

Print

print("hello world")

a = 5
print("a =", a)

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.

a = 3  # assign number 3 to variable a
a = a + 1

rhino = "Rhino"  # assign string "Rhino" to variable rhino

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).

print(type(3))  # int
print(type(3.14))  # float

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. removemethod removes a given item by value

The length of the list can be checked by len(list).

if Statement

if 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:

a = int(input('Enter a number "a" : '))
b = int(input('Enter a number "b" : '))
if a > b:
    print("a is bigger than b")
elif a < b:
    print("a is smaller than b")
else:
    print("a is equal to b")

Output:

Enter a number "a" : 10
Enter a number "b" : 5
a is bigger than b

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.

my_list = []
for i in range(11):
    my_list.append(i)

print(my_list)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The enumerate() method adds counter when we iterate an enumerate object.

cablenet = [1.6, 3.6, 2.4, 3.4, 2.7, 2.8, 3.3, 3.1, 3.7, 1.8, 1.8, 1.8, 2.6]
for i, cable in enumerate(cablenet):
    print(i, cable)

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.

i = 0
my_list = []
while i <= 10:
    my_list.append(i)
    i += 1

print(my_list)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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