Session 1:
Computer languages are specific ways for instructions to be given to the computer and make it do what you want it to
Python is one such language
“Syntax“ of a programming language can be thought of as the grammar/rule for the code
print(“~HeLL@ wOrl$d\“)
Print function will return whatever is put inside of it to the console
Identify legal/illegal variable names
Def. Variables
Variables are a named location used for the memory/storage of data of a specified type
For good form, variables should be declared at the beginning of each program and not ‘on the fly’
Rules for variables:
Variable can not contain any spaces
The first character of the name must be a lower case letter(not digit, uppercase letter or special character), all characters succeeding the first character can be a digit, upper case letter or a special character(only _)
Can not use any special characters except ‘_’
Uppercase and lowercase letters should be distinct
Can not use keywords(words that are reserved for python, it will confuse the compiler and result in error)
Primary data types in Python
String
A collection of characters within quotation marks(“0.5 7”, “-9.92”, “hafk widd bei hd 6 y8Y*&”, ‘bo O’)
String Manipulation:
Concatenation: str3 = “str1” + “str2”
Python can only concatenate strings
print(“hi”*3) → hihihi
print(“hi”, 5) → hi 5
Integer
Any number below, above or inclusive of zero that is not having decimal properties(0, -5, 6, 7, 8, -28)
Float
Any number, positive or negative, with a decimal point(4.0, 5.03, 1.57, -8.56)
Boolean
Used to represent the truth value of an expression(True or False)
Type Casting(note: python will cast with only 1 step, if more than 1 step needed, it’ll give error)
print(type(15)) → <class ‘int’>
print(type(1.5)) → <class ‘float’>
print(type(“15”)) → <class ‘str’>
print(type(True)) → <class ‘bool’>
print(type((int)”5.1”)) →Error(missing bracket around 5.1)
print(type((float)(5))) → <class 'float'>
print(type((int)(5.1))) → <class 'int'>
print(type((str)(5))) → <class 'str'>
print(type((str)(5.1))) → <class 'str'>
print(type((float)(”5.1”))) → <class 'float'>
print((int)(“5.1”)) → error
print((int)(“5”)) → 5
print((float)(“5.1”)) → 5.1
print((int)(5.7)) → 5(directly cuts off the decimals)
Make sure to check the # of brackets, there shouldn't be brackets around “type”
Mathematical operators
See Also3. An Informal Introduction to PythonTop 100 Model-Based Development Using MATLAB Simulink Interview Questions - CS Electrical & ElectronicsNumPy 1.12.0 Release Notes — NumPy v2.0 ManualNumPy 1.24 Release Notes — NumPy v2.0 ManualImport math
In-built: +(add), -(subtract), (multiply), /(divide), //(int division), %(modulus), *(exponent)
Python follows BEDMAS
3 Types of division:
48/5 → 9.6
48//5.0 → 9.0 (*if 1st # is < then 2nd, it’ll return 0*)
48%5.0 → 3.0 (*if 1st # is < then 2nd, it’ll return the 1st # *)
Session 2:
Boolean Conditions
Def. A python data type with two values(True or False)
Boolean operators: ==, !=, >, <, >=, <=
Compound Boolean operators: not(toggles boolean value), and(true if both conditions are true), or(true if either condition is true)
Selection If Statements
Conditionals(selection constructs), are used in programs that require different execution paths which are dependant on the evaluation of certain values
1 path if statement: condition must be a statement that evaluates to boolean value and indentation is needed to be inside the if statement
The else statement does not have any given condition, if the first condition is false, the compiler executes the else statement
There can be infinite elif statements, conditionals can be nested, elif statements need to have conditions
Multi-path conditions:
If condition:
Do_something
else:
Do_alternative
If condition:
do_something
elif:
Do_alternative1
Else:
Do_alternative2
Character and ASCII conversions
ASCII: American Standard Code for Informational Interchange
It’s a numeric value given for characters for computers to store and manipulate
ord() converts a character into an integer and chr() does the reverse
This function actually returns the Unicode code point of that character, Unicode provides a number to a character
ASCII only has 128 characters, current Unicode has 100,000+ characters
A = 65, ASCII value = 65x + 1 - 1 where x is the order of the letter in the alphabet
a = 97, ASCII value = 97x + 1 - 1 where x is the order of the letter in the alphabet
Generally:
“0” < “1” < … < “9”
“A” < “B” < …”Z”
“a” < “b” < … “z”
A blank precedes any digit or letter
All digits precede all letters
Upper case letters precede lower case letters
String Conditionals
You can compare strings b/c the compiler will convert it into a numerical value
“hello”<”hi”
Random library
The following functions will generate pseudo(random) numbers
random.randint(a, b) inclusive returns a random integer from a to b
random.random() returns a random float from zero(inclusive) to one(not inclusive)
random.uniform(a, b) inclusive returns a random float between a and b
While Loops
A.k.a. Conditional loop, only follows though when the given condition is true and keeps checking the condition until the condition becomes false
If the condition is false, it will skip over the loop and execute whatever is after it
Sentinel value
Used to indicate that the wanted set of values has been reached
Formatting input and output
Rounding numerical values
Ex. print(round(math.sqrt(88), 2))
+ vs , for concatenation
“+” only concatenates two strings and does not automatically add a space in between
“,” can concatenate any floats, integers or strings and adds a space in between
Printing on one line
print("Line 1: ", end ="")
print("continuing on line 1...")
For Loops
A.k.a counted loop, repeats whatever is inside it a specified number of times
for i(can be any variable) in range(starting_num, ending_num(exclusive), interval_num): (tab)Do_something
In this case, “i” is the special counter called the index which is only usable within the loop
use while loop when the number of repetitions is unknown, and vice versa
Four Types of Errors
Syntax or compilation error is caused by error in the syntax of the program(occurs when you try to run it, so it will not run at all)
Runtime error occurs when the program is running but crashes
Logical error: is not an error given by the compiler, the program will still execute but it does not do what is intended
Style error: is not an error given by the compiler but it is when the format of the program does not conform to the regular practice
Programs should be made robust so that they can they can check the user’s incorrect input and make sure it doesn’t cause the program to crash
Session 3:
String Manipulation
Length Function
Returns length of the function you specify
Ex. len(“Hello World”) = 12
Accessing a specific character
To access the first character in the string variable ‘name’ with name = “Williamson” use the following syntax: name[0] = ‘W’
Accessing a substring
A substring is a portion of a string
Access from the 3rd to the 5th characters as follows: name[2:5] = ‘lli’
First number is inclusive, second number is exclusive
name[-1:2:-2] = “nsal” # start from the last letter and print every other letters until the third letter backward
find()
The method find() looks for a particular substring within a string
It will return the index of the location of the first occurrence of that substring or -1 if the substring does not occur
string.find(value, start, end)
index() vs find()
If the value is not found, the find() method returns -1, but the index() method will raise error
replace()
The replace() method replaces a specified phrase with another specified phrase
string.replace(oldvalue, newvalue, count)
count()
The count() method returns the number of times a specified value appears in the string
string.count(value, start, end)
Robust Input
Make sure user is entering the value that the program is expecting so that it doesn't crash
Use: isalpha(), isdigit(), isupper(), islower() functions to verify
Mutability of strings
Strings are immutable(you can’t change an existing string)
Ex. Using [] operator on the left side of an assignment
Ex. greeting = 'Hello, world!' greeting[0] = 'J'
= TypeError: object does not support item assignment
Lists
An array assigns one name to multiple locations in memory and uses an index to distinguish between the different locations.
Python does not have support for arrays but has lists which work similarly
Ex. info = [“kate”, 4, True]
Uses an index system similar to strings, ex. [0] is = “kate”
List functions:
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Session 4:
Def. Function:
A block of code that will only run and/or impact the program. It will return a processed result based on a given input
Def. Procedure
A block of code that will only run when called upon. It will process a result based on a given input but won’t return anything.
parameters vs. arguments
Both are information passed into the function
Parameters are given in the brackets where the function is defined
Arguments are passed through the brackets where the function is called
localvar. vs. global var.
Local variables are declared within the scope of a function or procedure
Global variables are declared outside of a function and are accessible throughout the program(excluding the function)
The keyword ‘def’ is the signature of a function as it indicates the declaration of a function
Session 7:
The skeleton
the general template for a html program uses the below tags:
<!DOCTYPE html> <!--Define the document as html-->
<html lang="en"> <!--says that whatever is between these tags is html english--><head> <!--header tag has general information about the document-->
<meta charset="utf-8"> <!--defines the charset-->
<title>replit</title> <!--title of the page-->
<link href="style.css" rel="stylesheet" type="text/css"/> <!--connects another css to this html page-->
</head><body>
Hello world <!--whatever is between the body tags are what displays on the page-->
</body></html>
HTML vs. CSS
HTML stands for Hyper Text Markup Language add the functionalities
CSS stands for Cascading Style sheets and is used to style and design the page
Relative vs absolute hyperlinks
absolute links go directly to the page needed, it has a specified path
relative links will only have the domain name in the URL
<!--Creating Hyperlink-->
<a href="google.com/monkeys">click me<a><!--Comments work like this in HTML!-->