# COMP1010 EXAM NOTES, Maddy (z5583311)

Basics:

- Indentation:
    - Refers to the spaces at the beginning of a line of code. Indicates a block of code
    - Must be 4 spaces!!!! Have to use the same number of spaces in the same block of code otherwise an error will occur
- Variables:
    - Created when a variable is applied to it
    - Example:
        - y = 3
- Casting:
    - Performed manually by using built-in functions to ensure a variable matches the required data type for an operation
    - Example:
        - y = int(3)   (y will be 3), z = float(3)  (z will be 3.0)
- Global variables:
    - Variables that exist outside of a function
    - If you create a variable with the same name inside a function, this variable will be local and can only be used inside the function while the global variable will remain unchanged
- F strings
    - Formats a string, able to combine strings and numbers
    - Example:
        - age = 20
        - x = (f"My name is Maddy, I am {age}")
        - print(x)
- Boolean values:
    - bool() function allows evaluation of a value, returning either True or False
    - Most values will return True if it has some sort of content, except empty strings and the number 0
- Loops
    - Used to repeat code multiple times
    - Two different types: **for** and **while**
    - For loop:
        - Repeats code over a sequence (such as a string, range or list)
            - for x in “meow”:
            - print(x)
    - While loop:
        - Repeats code while a condition is True
            - x = 1
            - while x < 5:
            - print(x)
            - x += 1
        - Condition is checked before each loop
        - Must eventually become false (or it loops forever!!!!!)
- Lists
    - Used to store multiple items in a single variable, created using square brackets: [   ]
    - Ordered: if new items are added to a list, they will be placed at the end of the list
    - Allows duplicate values

functions/keywords/statements

| Thing | What it does | Example |
| --- | --- | --- |
| print(…) | prints | **print**(”Hello World!”) |
| len(…) | finds the length of a string | x = "meow"
print(**len**(x)) |
| .upper + .lower | returns the string in upper/lower case | a = "Meow"
print(a.upper()) |
| if | evaluates a condition and executes the indented block below if the condition is True | x = 10
if x > 5:
    print(f"{x} is greater than 5") |
| elif | “else if”, checks multiple conditions in order | x = 10
if x > 10: 
    print("Greater than 10")
elif x == 10: 
    print("Exactly 10") |
| else | only runs if all previous conditions are False | x = 3
if x > 5: 
    print("Big")
else: 
    print("Small") |

data types

| Thing | What it does | Example |
| --- | --- | --- |
| str | text: stores text | x = “Hello World” |
| int | numeric: whole numbers | x = 20 |
| float | numeric: decimal numbers | x = 2.0 |
| complex | numeric: real + imaginary numbers | x = 2j |
| list | sequence: ordered, changable | x = [”cat”, “dog”, “hamster”] |
| tuple | sequence: ordered, unchangable | x = (”cat”, “dog”, “hamster) |
| range | sequence: sequence of numbers | x = range(6) |
| dict | mapping: key-value pairs (matches word/key to definition/value) | x = {"name" : "Maddy, "age" : 20} |
| set | set: unordered, unique items | **x = {"cat", "dog", "hamster"}** |
| frozenset | set: unchangable set | x = frozenset({"cat", "dog", "hamster"}) |
| bool | boolean: true or false values | x = True |
| nonetype | none: represents no value | None |

string methods (don’t know if i need these but theyll probs be good to have)

| Thing | What it does  | Example |
| --- | --- | --- |
| capitalize() | converts the first character of a string to upper case | text = "mEOW"
print(text.capitalize()) |
| casefold() | converts string into lower case (stronger than .lower()) | text = “MEOW”
print(text.casefold()) |
| center() | centers a string | text = “meow”
print(text.center(10)) |
| count() | returns the number of times a specified value occurs | text = "meow"
print(text.count("m")) |
| index() | searches the string for the specified value and returns its position | text = "meow"
print(text.index("e"))
(output would be “1”) |
| .isalpha() | returns True if all characters in the string are in the alphabet | text = "Meow"
print(text.isalpha()) |
| .isdigit() | returns True if all characters in the string are digits | text = "12345"
print(text.isdigit()) |
| .isalnum() | returns True if all characters are alphanumeric | text = "Meow123"
print(text.isalnum()) |

List methods:

| Thing | What it does | Example |
| --- | --- | --- |
| .append(…) | adds to the end of a list | list = [1, 2]
list.append(3)
print(list) |
| .insert(…) | adds to a position in a list | list = [1, 2, 4]
list.insert(2, 3)
print(list) |
| .remove(…) | removes a value from a list (by value)  | list = [1, 2, 4]
list.remove(2)
print(list) |
| .pop(…) | removes a value from a list (by index position) | list = [10, 20, 30]
list.pop(1)
print(list) |

operators: used to perform operations on variables and values

| Thing | What it does | Example |
| --- | --- | --- |
| + | adds 2 values, can also add a variable and a value, or 2 variables | total = 10 + 10
total2 = total + 10 |
| - | subtraction | x - y |
| * | multiplication | x * y |
| / | division | x / y |
| % | modulus operator, gives the remainder after division | x % y |
| =  | assignment operator, stores a value in a variable | x = 5 |
| += | compound assignment operator, adds to a variable, then stores the result in the same variable | x = 5
x += 3 |
| -= | compound assignment operator, subtracts a value from a variable, then stores the result in the same variable | x = 5
x -= 3 |
| *= | compound assignment operator, multiplies a variable by a value, then stores the result in the same variable | x = 5
x *= 3 |
| /= | compound assignment operator, divides a variable by a value, then stores the result in the same variable | x = 10
x /= 2 |
| %= | compound assignment operator, takes the remainder of a variable after division and stores it back in the same variable | x = 10
x %= 3 |
| == | comparison operator, checks if two values are equal | x = 5
print(x == 5)
(output would be “True”) |
| != | comparison operator, checks if two values are not equal | x = 5
print(x != 3)
(output would be “True”) |
| > | comparison operator, greater than | x = 10
x > 9 |
| < | comparison operater, less than  | x = 10
x < 15 |
| >= | greater than or equal to | x >= y |
| <= | less than or equal to | x <= y |
| and | logical operator, combines two conditions and returns True, only if both conditions are true | x = 10
print(x > 5 and x < 20) |
| or | logical operator, returns True if at least one condition is true | x = 10
print(x > 5 or x < 3) |
| not | logical operator, reverses the boolean value | x = 10
print(not x > 5) |
| in (opposite is “not in”) | membership operator, used to check if a value is a part of a sequence (such as a list, dict or string) | print("m" in "meow") |

example code that i still dont really understand:

> for x in “meow”:
    print (x)
#prints each character in the string, one at a time with each letter on a new line
> 

Web apps!!!!!

- 3 main parts:
    - front end → what the user sees (HTML)
    - back end → Python logic
    - data → lists, JSON files

user inputs data (forms/buttons) → Python processes info → output (web page result)

HTML elements:

| Thing | What it does  | Example |
| --- | --- | --- |
| <!DOCTYPE html> | declares that its HTML 5 | <!DOCTYPE html> |
| <html> | root of the page | <html>…</html> |
| <head> | info about the page, not directly displayed | <head>…</head> |
| <title> | the title thats viewed in the browser tab | <title>Title</title> |
| <body> | visible content on the web page | <body>meow</body> |
| <h1> | main heading | <h1>Meow</h1> |
| <h2> | sub heading | <h2>Meow?</h2> |
| <p> | paragraph | <p>meow meow meow</p> |
| <br> | line break | meow<br>meow |
| <hr> | horizontal rule, renders as a horizontal line | <hr> |
| <strong> | makes text bold | <strong>Hi</strong> |
| <em> | makes text italic (emphasis) | <em>meow</em> |
| <a> | link | <a href=”[https://ar.pinterest.com/pin/1149403136149798999/](https://ar.pinterest.com/pin/1149403136149798999/)”>meow</a> |
| <img> | image insert | <img src=”images/cat.jpg” alt=”funny cat”> |
| <video> | video file insert | <video controls> 
    <source src="video.mp4"                            type="video/mp4">
</video> |
| <audio> | audio file insert | <audio src="audio-file.mp3" controls>
    <source src="music.mp3"                            type="audio/mpeg">
</audio> |
| <div> | a block container that defines a division or a segment  | <div>…</div> |
| <span> | similar to <div>, but is an inline container | <p>meow <span style="color:pink">meow</span> meow</p> |
| <header> | represets a container for introductory content | <header>Hello</header> |
| <footer> | defines a footer for a document | <footer>funnyemailthatdefinitelyexists@gmail.com</footer> |
| <section> | defines a section in a document | <section>
<h1>Hi</h1>
<p>meow meow meow</p>
</section> |
| <article> | specifies an independent, self-contained content, i.e. must make sense on its own!!! | <article>
<h2>MEOWWWW</h2>
<p>im getting real tired of typing meow for all these examples</p>
</article> |
| <ul> | unordered list (bullet points) | <ul> 
    <li>meow</li> 
    <li>meow?</li> 
    <li>meow!</li>
</ul> |
| <ol> | ordered list (numbered) | <ol>
    <li>meow…</li>
    <li>MEOW….</li>
</ol> |
| <form> | used to create a HTML form that accepts user input | <form action="/submit" method="post">…</form> |
| <input> | input box!!! | <input type=”text”> |
| <label> | defines a label for an element | <label for=”meow”>Meow:</label> |
| <button> | defines a clickable button | <button type="button">Meow!</button> |
| <select>, <option> | an element used to create a drop-down list (include label in the front!)
options are options… | <select name="cats" id="cats">
<option value="small cat">Small cat</option>
</select> |

other important html stuff:

| href | defines link destination | <a href="..."> |
| --- | --- | --- |
| src | defines file source | <img src="..."> |
| alt | image description (text) | <img alt="cat"> |
| id | unique identifier | <div id="main"> |
| class | group styling | <div class="box"> |
| type | input type | <input type=”password”> |

basic html for signup page:

> 
> 
> 
> <form action="/action_page.php" method="get">
> 
> <label for="fname">First name:</label>
> 
> <input type="text" id="fname" name="fname"><br><br>
> 
> <label for="lname">Last name:</label>
> 
> <input type="text" id="lname" name="lname"><br><br>
> 
> <input type="submit" value="Submit">
> 
> </form>
> 

REMEMBER: pyhtml is basically just html things but the <> are brackets instead 

Basic CSS stuff:

syntax: selector → property → value

IMPORTANT! everything must be inside one style, i.e.: <h1 style="color:pink; font-family: Arial;">Meow</h1>

Text styling:

| Thing | What it does | Example |
| --- | --- | --- |
| color | changes text colour (must be spelt the american way.. learnt this the hard way) | <h1 style="color:pink;">Meow</h1> |
| font-family | changes text font | <h1 style=”font-family: Arial;”>Hi</h1> |
| font-size | changes text size | <h1 style=”font-size: 80px;”>MEOWWW</h1> |
| text-align | aligns text to a specific position | <h1 style=”text-align: center;”>Hi</h1> |

box model:

| Thing | What it does | Example |
| --- | --- | --- |
| margin | space outside of the element |  <style>
    .box {
        background-color: pink;
        margin: 50px;
    }
</style> |
| padding | space inside an element | padding: 10px;  |
| border | the border around an element | border: 1px solid black; |
| width | the width of an element | width: 200px; |
| height | the height of an element | height: 100px; |

other:

| Thing | What it does | Example |
| --- | --- | --- |
| background-color | changes the background colour of an element | background-color: pink; |
| text-align | aligns text  | text-align: center; |

JSONS!!!!!!!!!!!!

JSONS (Javascript Object Notation): a method of storing structured data 

JSON:

> {
”name”: “Maddy”,
”age”: 20,
”student”: true
}
> 

Python (dictionary):

> {”name”: “Maddy”, “age”: 20, “student”: True}
> 

NESTED JSON!!!!!! kinda similar to SQL????

basically a thing inside a thing, example:

- A class → students → student details

better example of a nested JSON:

> {
    ”class”: {
        “name”: “COMP1010”,
        “students”: [
            {”name”: “Maddy”, “dob”: “2005-10-10”}
        ]
    }
}
> 

reading JSON files in Python:

1. import json file

> import json
> 
1. open and load the file

> with open(”data.json”) as file:
    data = json.load(file)
> 
1. get thing

> data[”class’][”name”]
> 
1. output

> COMP1010
> 

loop for extracting multiple things?:

> for student in data[”class”][”students”]:
    print(student[”name”])
>