Unit 3 Vocab
- Variables, Data Types, Assignment Operators
- Managing Complexity with Variables: Lists, 2D Lists, Dictionaries, Class
- Algorithms, Sequence, Selection, Iteration
- Expressions, Comparison Operators, Booleans Expressions and Selection, Booleans
- Expressions and Iteration, Truth Tables
- Characters
- Strings
- Length
- Concatenation
- Python if
- Elif
- Else Conditional
- Nested Selection Statement
- While loops with range
- With lists combining loops with conditionals to break:
- Continue procedural abstraction
- Python def procedures
- Parameters
- Return values
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
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)
// JAVASCRIPT example using variables and lists
const group = [
"NAME 1",
"NAME 2",
"NAME 3",
"NAME 4"
];
console.log(group);
// JAVASCRIPT example of a dictionary
const myDictionary = {
key1: "value1",
key2: "value2",
key3: "value3"
};
console.log(myDictionary);
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)
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)
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) + ".")
Letters = ["a, b, c, d, e, f"]
print(Letters)
strings = ["apple, banana, carrot"]
print(strings)
string = "hellobye"
x = string[0:8]
y = string[0:5]
z = string[5:8]
print(x)
print(len(string))
print(y)
print(z)
string1 = ("Hello how are you?")
string2 = ("Im good!")
print(string1, string2)
string1 = "computer"
string2 = "science"
string3 = string1 + " " + string2
print(string3)
Traversing Strings: Iterate over the characters in teh string.
greeting = "Hello, how are you?"
for o in greeting:
print(o)
x = 1
if x == 1:
print("x is 1")
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")
hour = 19
if (hour < 18):
greeting = ("Good day")
else:
greeting = ("Good evening")
print (greeting)
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")
for num in range(1, 11):
print(num)
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers:
if number == 7:
break
print(number)
def multiply(x, y):
return x * y
x = 3
y = 6
print(multiply(x, y))
def multiply(x, y):
return x * y
def add(x, y):
return x + y # in this case the parameter is x and y
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