1-2

Variables

  • Variables are containers that hold a certain value/ data
  • there are different datatypes that a variable can store
    • string (text/ letters)
    • int (number)
    • boolean (true false)
  • to assign a value to a variable, you use an assignment operator which is (=)
    • -> in pseudocode
  • variables should align with the data that it stores
name = "claire"
# can print just the variable instead of the whole name
print(c)
# this datatype is string so you need ""
claire
a = 1
# ints don't need ""
print(a)
1
Answer = True
print(a)
True

Strings and Lists

  • strings can be a series of words or numbers (including phone numbers)
  • lists store a series of variables, words, or numbers
  • syntax: square brakets
  • easier than assigning variable to each value
name1 = "claire"
name2 = "claire"
name3 = "grace"
name4 = "annika"
print(name1, name2, name3, name4)

# yes
names = ["claire", "claire", "grace", "annika"]
print(names)
claire claire grace annika
['claire', 'claire', 'grace', 'annika']

3-4

Algorithms

  • set of instructions to do certain tasks
  • parts of an algorithm
    • sequence (order)
    • selection (decision dependant on condition)
    • iteration (loop until condition is met)
Sequence
  • step 1
  • step 2
  • step 3 ##### Selection
  • if this is right, then do this
  • otherwise, do that ##### Iteration
  • repeat this step until you get this

Arithmetic Operators

  • add
  • subtract
  • multiply
  • divide
  • mod (remainder)
Answer = 1 + 1
print(Answer)
2
Answer = 1 - 1
print(Answer)
0
Answer = 1 * 1
print(Answer)
1
Answer = 1 / 1
print(Answer)
1.0
Answer = 1 % 1
print(Answer)
# 1/1 has no remainder
0

Strings

  • len (string)
  • concat (str1, str2)
  • substring (str1, str2, length)
len("claire")
# print the number of letters
6
string = "ClaireChen"
print(string[2:6])
# prints 2nd to 6th letter
aire

5-7

  • Boolean (true false)
  • rational operators
    • equal to
    • greater than
    • less than
  • logical operators
    • not (opposite)
    • and (if both apply)
    • or (if either apply)
isRaining = False
# if it is not raining
result = not(isRaining)
# the result is the opposite of raining (is not raining)
print(result)
True
grade = 95
if grade > 70 and grade <= 100:
    print("passed")
passed
flower = "red"
if flower == "red" or "green":
    print("red or green flower")
# either red or green
red or green flower

Conditionals

  • if
  • then
  • else if/ elif
  • this is selection
flower = 3
if flower == 1:
    print("flower")
elif flower >= 1:
    print("bouquet")
bouquet

Nested Conditionals

  • conditionals within conditionals
print("Have you watched criminal minds?")
reply1 = input("yes or no?")
# first conditional
if reply1 == "yes":
# within the condition yes, you can enter your fav character
    print("Who's your favorite character")
    reply2 = input("favorite character")
# if you enter Emily, it prints yay, othewise it prints oh
    if reply2 == "Emily":
        print("yay")
    else:
        print("oh")
# from the first yes or no conditional, if you dont say yes, it prints you should
else:
    print("you should")
Have you watched criminal minds?
Who's your favorite character
yay

8-10

  • Iteration: repeats a specified number of times or until a given condition is met

  • Iteration Statements: repeats zero or more times, until a stopping condition is met

  • Traversing Lists: all elements in the list are accessed (can be partial or complete)
i = 0
# starting from 0, if from one it would print 4 times
while (i < 5):
# print repeat 5 times
    print("repeat")
    i = i + 1
repeat
repeat
repeat
repeat
repeat
for i in range(1,11):
# start from 1 up until 11, kind of like intervals in math
    print(i)
1
2
3
4
5
6
7
8
9
10

difference between for and while loops

for loop is used when the number of iterations is known while loop is repeated until the statement in the program is proved wrong

list recap

  • Append adds something to the list (at the end of the list)
  • Insert adds something to the list (anywhere in the list)
  • Removes whatever is at the index you put
  • you can assign the values of a list to another list

12-13

  • Procedures: basically same as function
  • Parameters are independent variables used in the procedure to produce a result. It allows a procedure to execute without initially knowing specific input values.
x = 5
y = 3

# kind of states the variables being used in the function
def multiply(x, y):
    product = x * y
    return product

answer = multiply(x, y)
print("The product of", x, "times", y, "is", answer)

how to call a function

  • nameFunction(parameters)
num = 5
# the function math is being called
# the parameter is x
def math(x):
    op1 = x * 2
    op2 = op1 - 9
    return op2

the return statemnt

  • A return statement ends the execution of a function, and returns control to the calling function
# called function named divide
# parameters are num1 and num2
def divide(num1,num2):
      x = num1/num2
      return x

Vocab

  • Modularity - breaking a complex program into smaller, independent parts (modules) that can be used and reused in different parts of the program
  • Abstraction - hiding the details of how a particular code or system works and exposing only the essential features or functions (make it look clean)
  • Duplication - having multiple duplicate code blocks, often decreasing readability and efficiency (bad)
  • Logic - the sequence of steps and operations that a computer follows to execute a program, including the specific instructions and decision-making processes built into the code
# this function squares numbers to avoid duplicates
def square(a):
    return a * a
# this function returns the square root of the sum of the two squared legs to find the hypotenuse
def hypotenuse_abstracted(leg1, leg2):
    leg1_squared = square(leg1)
    leg2_squared = square(leg2)
    return math.sqrt(leg1_squared + leg2_squared)

14-15

Libraries

  • A library has functions that can later be accessed in new programs
  • A library is a collection of code from another source that can be used to add functionality to a program
    • save time and effort in the development process
    • " ." tells the program to look for the library and use its code
import math
x = int(input())
math.sqrt(x)
3.0

Random

  • randomly generates a number between a set of two given numbers
  • have to import the library random
import random

answer1 = random.randint(0,3)
print(answer1)
1

random has a lot of different methods like

- seed() | Initialize the random number generator

- getstate() | Returns the current internal state of the random number generator

- setstate() | Restores the internal state of the random number generator

- getrandbits() | Returns a number representing the random bits

- randrange() | Returns a random number between the given range

- randint() | Returns a random number between the given range

16

Simmulation

  • def: a virtual experiment or an immitation of a process
  • ex. testing the safety of a car or a game

Experiment

  • a process done to make a discovery, test a hypothesis, or demonstrate a known fact
  • simulations can be safer, cheaper, and more efficient, but also less accurate
  • simulations aren't always the same

17-18

Collatz

The Collatz conjecture is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1.

Hailstone numbers

The sequence of integers generated by Collatz conjecture are called Hailstone Numbers. Examples: Input : N = 7 Output : Hailstone Numbers: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 No.

Iteration

The action or a process of iterating or repeating: such as. : a procedure in which repetition of a sequence of operations yields results successively closer to a desired result.

Undecidable problems

An undecidable problem is one that should give a "yes" or "no" answer, but yet no algorithm exists that can answer correctly on all inputs.

Unsolvable problems

An unsolvable problem is one for which no algorithm can ever be written to find the solution.