You are on page 1of 6

MID-SEM SUMMARY

Mid Sem Test Scope


- Lecture Content up to W5
- Tutesheets up to W6
- Grok Worksheets up to WS10

PRINTING AND INPUTTING


print(object) Displays object to user

input(str) Displays str to user and asks user for


input

COMMENTING
# single line comment Single-line comment

‘’’ Multi-line comment


multi-
line
comment
‘’’

OPERATORS (Highest to Lowest Precedence)


() brackets Arithmetic Operators

** exponent

% modulus/remainder

// floor/integer division

/ division

* multiplication

- subtraction (also a unary operator;


make number negative)

+ addition (also a unary operator;


make number positive)

== equal to Comparison Operators

!= not equal to

< less than

> greater than

<= less than or equal to

>= more than or equal to


in

not Boolean Operators

and

or

CONDITIONAL
if <condition1>:
<statements/operations to be executed if condition1 is True>
elif <condition2>:
<statements/operations to be executed if condition2 is True>
...
elif <conditionx>:
<statements/operations to be executed if conditionx is True>
else:
<statements/operations to be executed if all conditions are False>

WHILE LOOP
while <condition>:
<statements/operations to be executed while condition is True>

break Immediately exits while loop

continue Jumps back to start of loop

FOR LOOP
# with range()
for i in range(<int>):
<statements/operations to be executed if i is less than <int>>

# iterate over a sequence (str/list/tuple)


for i in <sequence>:
# i will become the char/ele

FUNCTIONS
CONSTANT = 0 # declare constants before all functions
def <function-name>(<parameters>):
<function-body>

DATA TYPES
INTEGERS

int(value) Convert value to integer (whole number)


STRINGS

str(value) Convert value to string (a chunk of


text)

str + str String concatenation

str * int String replication

str[index] String indexing

str[start_index:end_index:step_size] String slicing (does not include


end_index btw)

str.upper() Make all letters uppercase

str.lower() Make all letters lowercase

str.strip(strip_str) Removes all characters in strip_str from


str.rstrip(strip_str) str
str.lstrip(strip_str) rstrip: strip from right
lstrip: strip from left

str.split(delimiter) Return a list of string-delimited


substrings in string

f-strings

f’string {variable/operation/etc.}’ F-string declaration


f”string {variable/operation/etc.}”

f’{val : filling align window .precision Format specifier


format_code}’

Part Explanation

: Separates value from format specifier

filling (char) fills extra space with filling


(default=spaces)

align Align formatted text to the


< left left/middle/right (default=right)
^ middle
> right
, thousand-separating commas

window (int) sets formatted text window to be


`window` characters long (default=length
of formatted value)

.precision (int) formats the number to `precision`


decimal places // indicates number of
characters in string

Format_code (default=auto)
f float
s string
g ‘optimal’ float
d integer
c Unicode character

FLOAT

float(value) Convert value to float (real number)

BOOLEAN

bool(value) Convert value to boolean (truth value)

LIST

list(value) Convert value to list (mutable sequence


of values, where values can be of
l = [] different types) // initialize a list

list(range(start, end, step)) returns a list from start to end (end


not included)

list[index] List indexing; index can be positive


(start from 0) or negative (start from
-1)

list[start_index:end_index:step_size] returns elements from start-index to


end-index (end-index not included)

list.append(item) append/add item to a list

list.remove(item) remove first instance of item from list

list.pop(index) remove item of index

list.sort(list) sorts (mutates) the list

TUPLE

tuple(value) convert iterable value (list, tuple,


string) to tuple // initialize a tuple
tup = (val1,)

DICTIONARY

dict(value) Convert to dictionary (collection of key


and associated values; keys must be
d = {} unique; values can be of different
types) // initialize a dictionary

d[key] Access value associated to key - returns


`KeyError` if key doesn’t exist

d.get(key) Returns value associated to key -


returns `None` if key doesn’t exist

key in d Test for presence of key


d.pop(key) Deleted the key-value pair and returns
the value of returns `None` if key
doesn’t exist

del d[key] Deletes key-value pair

d.copy() Makes a shallow copy of dictionary

d.clear() Deletes all key-value pairs from the


dictionary

d.keys() Returns iterable collection of keys

d.values() Returns iterable collection of values

d.items() Returns iterable collection of 2-tupe:


(key, value)

my_dict = {'first': 1, 'second': 2, Unpacking Dictionaries


'third': 3}
for key, value in my_dict.items():
print(key, value)
# first 1
# second 2
# third 3

SETS

set(value) Convert value to set // initialize a set


(immutable, unordered collection of
unique data(no duplicates))

set.pop() Remove and retrieve a random element

set.remove(ele) Removes specific element

set1.intersect(set2) returns new set containing common


set1 & set2 elements between sets

set1.union(set2) returns new set containing all unique


set1 | set2 elements from both sets

set.difference(set2) returns new set containing difference


set1 - set2 between sets

set.copy() Creates a copy of the set

set.clear() Deletes all elements from the set

set1.issubset(set2) Returns True if all items in set2 exist


in set1, and False otherwise

GENERAL METHODS/FUNCTIONS
type(object) find out type of object
len(str/list/tuple/set/dict) used to find number of characters in a
string or elements in a list/tuple/set
or number of key-value pairs in a dict

sorted(str/list/tuple/set, reverse = returns list of sorted


True/False) characters/elements

chr(int) convert integer to corresponding Unicode


character

ord(char) convert Unicode character to Unicode


code

You might also like