Lessons so far

  • 1: Sections 3.1 and 3.2: Variables, Assignments, and Data Abstractions
  • 2: Sections 3.3 and 3.4: Mathematical Expressions and Strings
  • 3: Sections 3.5, 3.6, and 3.7: Boolean Expression, Conditionals, and Nested Conditionals
  • 4: Sections 3.8 and 3.10: Boolean Iteration and Lists
  • 5: Sections 3.9 and 3.11: Developing Algorithms and Binary Search
  • 6: Sections 3.12 and 3.13: Calling and Developing Procedures
  • 7: Sections 3.14 and 3.15: Libraries and Random Values
  • 8: Section 3.16: Simulations
  • 9: Sections 3.17 and 3.18: Algorithm Efficiency and Undecidable Problems

Vocab:

Variables

Used to store a value, and organize data with a label. A variable consists of a name, a value, and the type of that value.

fruit = "apple" #fruit is the name, 'apple' is the value, and string is the type
score = 0 #score is the name, 0 is the value, integer is the type

Lessons that discuss this:

  • Lesson 1
  • Lesson 2


Data Types

There are 3 data types: integer (a number), string (text), and boolean (states whether something is true or false). All three can be stored in variables

integer = 9

string = "words" #surrounded by quotations

boolean = False # True or false

Lessons that discuss this:

  • Lesson 1
  • Lesson 2


Assignment Operators

Allows a program to change the value or variables

Operator Description Syntax Outcome when print
= Assign value of right to left a = b b
+= Adds right and left together and assigns that value to right a += b a + b
-= Subtracts left from right and assigns that value to right a -= b a - b
*= Multiplies right with left and assigns that value to left a *= b a * b
/= Divides left with right and assigns that value to left a /= b a / b
**= Calculates exponent(raise power) value using and assigns that value to left a **= b a ^ b


Lessons that discuss this:

  • Lesson 1
  • Lesson 2


Lists

A sequence of values stored under one variable. Used to store multiple related values under one label and help organize code better

fruits = ["apple", "pear", "banana"]
scores = [90, 87, 45]

Lessons that discuss this:

  • Lesson 3


2D Lists/ Arrays (Matrix)

A list made up of lists. Allows lists to have 2 dimensions, divided up in rows and columns

tic_tac_toe =[["0", "X", "0"],
              ["0", "0", "X"],
              ["X", "X", "0"]]

# one way to print w/ dimensionality: 

m = 0
while m < len(tic_tac_toe):
    print(*tic_tac_toe[m]) # the * tells the program to print the values as strings
    m = m + 1

Lessons that discuss this:

  • Lesson 4:


Dictionaries

Used to store values in pairs. The value on the left is the key while the value on the right is the value. A dictionary can't have duplicate keys with different values.

info = {"name": "Ekam",
        "grade": 11,
        "YOB": 2006}

print(info["name"]) #putting a key in square brackets causes the value to be printed

Lessons that discuss this: ??


Algorithms

A set of instructions that can can achieve a task

Lessons that discuss this:

  • Lesson 2


Sequencing, Selection, and Iteration

Parts of an algorithm:

  • Sequencing: The specific order of the steps of an algorithm. Changing the sequencing can change the outcome
  • Selection: Making a decision (if this is true, do these steps.. if not, do these steps)
  • Iteration: Repeating a part of code for a defined number of times, or even forever (while loops)

Lessons that discuss this:

  • Lesson 2


Expressions:

Mathematical expressions that can be put into code

Operation Syntax
x + y Addition
x - y Subtraction
x * y Multiplication
x / y Division
x // y Quotient
x % y Remainder
x ** y Exponentiation

Lessons that discuss this:

  • Lesson 2


Comparison Operators:

Used to compare two values with each other.. useful in Boolean expressions

Operator Description
> Greater than
< Less than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to

Lessons that discuss this:

  • Lesson 3


Boolean Expressions and Selection/Iteration:

Boolean is whether something is true or false. Selections can be made based on boolean expressions, where different outcomes are given when something is true or false. One type of selection could be whether to repeat a certain loop or not, and the decision is made based on a Boolean expression. This could be achieved with while loops

num = 5

# selection:
if num < 100 : 
    print("The number is small")
else: 
    print ("The number is large")
    
# iteration: 

while num < 10:
    num = num + 1
    print(num)
The number is small
6
7
8
9
10

Lessons that discuss this:

  • Lesson 3


Truth Tables:

Tables used to organize different possible combinations and whether they would be true or not based on the given parameters:

*Note: Binary can be used to represent T of F as well -- 0 is F and 1 is T

  • AND truth tables: Both A AND B must be true for the overall operation to be true


A B Outcome
F F F
F T F
T F F
T T T
  • OR truth tables: Either A OR B must be true for the overall operation to be true


A B Outcome
F F F
F T T
T F T
T T T
  • XOR truth tables: Either A OR B must be true for the overall operation to be true, except if both are true then the overall outcome is F again


A B Outcome
F F F
F T T
T F T
T T F


Lessons that discuss this:

  • Mr. Mort's lesson.. Week 2


Characters

Not talked about yet

A letter.. Python does not have a separate data type for characters, so single letters will still be considered a string (or a variable if its used for that)


Strings:

A string is a type of value, that includes text. Strings are usually denoted by surrounding the text in quotation marks. Even numbers can be classified as strings if they're put in quotation marks

string = "hello"

Lessons that discuss this:

  • Lesson 1


Length:

The function len() returns the amount of characters in a string. This can also be used for variables that have a string value.

len("supercalifragilisticexpialidocious") #this word has 34 characters, so the function should print 34
34

Lessons that talk about this:

  • none


Concatenation:

string1 = "book"
string2 = "case"