Variables, Data Types, Assignment Operators

  • Variables: Abstractions in programs that represent a value
  • Data Types: Numbers (integers, 123), Booleans (t/f), lists (items held in an order), strings (letters/words)
  • Assignment Operators:
Function Operator
greater than >
less than <
greater than or equal to >=
less than or equal to <=
equal to ==
not equal to !=
exponent **
addition +
subtraction -
multiplication *
division /
modulus: x & y = x - (x/y) * y %
Equal sign usage =, +=, -=, *=, /=, %= | EX: x+=10 is x = x + 10
number = 10  #defining the variable to number 10
x = number ** 2  #defining another variable by using that number variable
print(number*2)  # multiplying a variable by 2
print(x) # printing the second defined variable
20
100

Managing Complexity with Variables: Lists, 2D Lists, Dictionaries, Class

Lists: A collection of items in an order 2D Lists: A list containing other lists Dictionaries: Similar to a list but the items in the collection are assigned a value Class: Template for objects which defines behaviors of objects and organizes it

keypad =   [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
            [" ", 0, " "]]
print (keypad)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [' ', 0, ' ']]
// JAVASCRIPT example using variables and lists
const group = [
    "NAME 1", 
    "NAME 2", 
    "NAME 3", 
    "NAME 4"
]; 
console.log(group);
[ 'NAME 1', 'NAME 2', 'NAME 3', 'NAME 4' ]
// JAVASCRIPT example of a dictionary

const myDictionary = {
    key1: "value1",
    key2: "value2",
    key3: "value3"
  };
  console.log(myDictionary);
{ key1: 'value1', key2: 'value2', key3: 'value3' }

Algorithms, Sequence, Selection, Iteration

  • Algorithms: A series of operations that completes a task/solves a problem
  • Sequence: Sequence is the order in which commands are executed
  • Selection: Determines which "path" of operations a program will run
  • Iteration: Iteration is repetition - it is a way to repeat a function over and over until a condition is met rather than copy pasting the function
number = 2
def multiplyby8_4times(num):
    i = 0 
    while i < 4: 
        num = num * 8
        print(num)
        i += 1 
multiplyby8_4times(number)
16
128
1024
8192

Expressions, Comparison Operators, Booleans Expressions and Selection, Booleans

  • Expressions: A combination of operators (functions) and operands (numbers) that produces an output value
  • Booleans Expressions and Selection: expression is a logical statement that is either true or false / selection is the procedure to get the output
  • Booleans: functions that produce True or False outputs
  • Comparison Operators:
Function Operator
greater than >
less than <
greater than or equal to >=
less than or equal to <=
equal to ==
not equal to !=
// JAVASCRIPT example using variables, less than operators and boolean
const w = 4
const q = 1
Boolean(w < q)
false

Expressions and Iteration, Truth Tables

  • Expressions: Combination of operators and values to produce a new value
  • Iteration: Repetition, repeat a function over and over using a loop until a condition is met rather than copy pasting the function
  • Truth Tables: A mathematical table that is used to represent the truth values of a logical expression
numlist = [1, 2, 3, 4] 
for number in numlist:
    prod = number * 2
    print(str(number), "times 2 is equal to", str(prod) + ".")
1 times 2 is equal to 2.
2 times 2 is equal to 4.
3 times 2 is equal to 6.
4 times 2 is equal to 8.

Characters

Characters: Letters that make up words.

Letters = ["a, b, c, d, e, f"]

print(Letters)
['a, b, c, d, e, f']

Strings

Strings: Sequence of characters that represent text.

strings = ["apple, banana, carrot"]

print(strings)
['apple, banana, carrot']

Length

Length: The amount of characters in a string.

string = "hellobye"
x = string[0:8]
y = string[0:5]
z = string[5:8]
print(x)
print(len(string))
print(y)
print(z)
hellobye
8
hello
bye

Concatenation

Concatenation: combination of strings to make sentences.

string1 = ("Hello how are you?")
string2 = ("Im good!")

print(string1, string2)
Hello how are you? Im good!
string1 = "computer"
string2 = "science"
string3 = string1 + " " + string2
print(string3)
computer science
Traversing Strings: Iterate over the characters in teh string. 
greeting = "Hello, how are you?"
for o in greeting:
    print(o)
H
e
l
l
o
,
 
h
o
w
 
a
r
e
 
y
o
u
?

Python if

Python if: An if statement is used when you want something to happen IF another thing has occurred.

x = 1

if x == 1:
    print("x is 1")
x is 1

Elif

Elif: It stands for else if, this is used when the first statement is false but you want to check for another condition.

price = 100

if price > 100:
    print("price is greater than 100")
elif price == 100:
    print("price is 100")
elif price < 100:
    print("price is less than 100")
price is 100

Else Conditional

Else Conditional: This is used when the first statement is false so you want to run another statement.

hour = 19
if (hour < 18):
  greeting = ("Good day")
else:
  greeting = ("Good evening")

print (greeting)
Good evening

Nested Selection Statement

Nested selection statements: whenever selection statements are inside another selection statement.

hour = 19
if hour > 15:
    if hour == 15:
        print("it is 15 hours")
    elif hour == 16:
        print("it is 16 hours")
    elif hour == 17:
        print("it is 17 hours")
    elif hour == 18:
        print("it is 18 hours")
    elif hour == 19:
        print("it is 19 hours")
it is 19 hours

While loops with range

While loops with range: While loop is code that keeps running until a certain condition is met, the range would cause only the things in that range to print.

for num in range(1, 11):

    print(num)
1
2
3
4
5
6
7
8
9
10

With lists combining loops with conditionals to break:

With lists combining loops with conditionals to break: first create a list, then use a loop so it stops when it reaches a certain number in the list.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:
    if number == 7:
        break

print(number)
7

Continue procedural abstraction

Continue procedural abstraction: Variables are first defined, then later they are used again to solve the problem.

def multiply(x, y):
    return x * y

x = 3
y = 6

print(multiply(x, y))
18

Python def procedures

python def procedures: allows you to define a procedure so you can use it more efficiently later on.

def multiply(x, y):
    return x * y

Parameters

Parameters: Its a variable/placeholder for the values of a function.

def add(x, y): 
    return x + y # in this case the parameter is x and y

Return values

Return values: the value that function returns to when it wants to recall a variable.

def add(x, y):
    return x + y

x = 3
y = 6

print(add(x, y)) # recalls a variable previously defined
print(multiply(x, y)) # recalls a procedure previously defined
9
18