Title | COMP10001 Mid Sem Summary |
---|---|
Course | Foundations Of Computing |
Institution | University of Melbourne |
Pages | 6 |
File Size | 129.5 KB |
File Type | |
Total Downloads | 773 |
Total Views | 986 |
[COMP10001] MID-SEM SUMMARYMid Sem Test Scope Lecture Content up to W Tutesheets up to W Grok Worksheets up to WS PRINTING AND INPUTTINGprint( object ) Displays object to user input( str ) Displays str to user and asks user for inputCOMMENTINGsingle line comment Single-line comment‘’’ multi- line co...
[COMP10001] 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
‘’’ multiline comment ‘’’
Multi-line comment
OPERATORS (Highest to Lowest Precedence) ()
brackets
**
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
!=
not equal to
<
less than
>
greater than
=
more than or equal to
Arithmetic Operators
Comparison Operators
in not
Boolean Operators
and or
CONDITIONAL if :
elif :
... elif :
else:
WHILE LOOP while :
break
Immediately exits while loop
continue
Jumps back to start of loop
FOR LOOP # with range() for i in range():
# iterate over a sequence (str/list/tuple) for i in : # i will become the char/ele
FUNCTIONS CONSTANT = 0 # declare constants before all functions def ():
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) str.rstrip(strip_str) str.lstrip(strip_str)
Removes all characters in strip_str from 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 {variable/operation/etc.}”
F-string declaration
f’{val :filling align window .precision format_code}’
Format specifier
Part
Explanation
:
Separates value from format specifier
filling
(char) fills extra space with filling (default=spaces)
align < left ^ middle > right , thousand-separating commas
Align formatted text to the left/middle/right (default=right)
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 f float
(default=auto)
s g d c
string ‘optimal’ float integer Unicode character
FLOAT float(value)
Convert value to float (real number) BOOLEAN
bool(value)
Convert value to boolean (truth value) LIST
list(value) l = []
Convert value to list (mutable sequence of values, where values can be of 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) d = {}
Convert to dictionary (collection of key and associated values; keys must be 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) set1 & set2
returns new set containing common elements between sets
set1.union(set2) set1 | set2
returns new set containing all unique elements from both sets
set.difference(set2) set1 - set2
returns new set containing difference 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 = True/False)
returns list of sorted characters/elements
chr(int)
convert integer to corresponding Unicode character
ord(char)
convert Unicode character to Unicode code...