Coding with Quantifiers

Basic Concepts of Coding

Functions

What is a function?

Something that recieves and input, does something to it, and gives an output.

Example

\(f(x) = x + 2\) is a function

In Python it looks like:

def f(x):
    return x + 2

“return” exits the program and outputs \(x+2\)

Common Objects in Python

  • Integer: A Integer (\(\mathbb{Z}\))
  • List: An ordered set of objects
    • \([1,2,3]\), \([\textrm{``apples''}, 1, \texttt{True}]\)
  • Boolean: Something that is either \(\texttt{True}\) or \(\texttt{False}\)
    • A proposition is a boolean.
    • A predicate given an input is a boolean.

Conditionals

A conditional checks some condition (expressed as a boolean) and uses it to decide what the program should do.

Example

example:
    input: boolean x
    
    if x: 
        y = x + 1
    else:
        y = x + 2
    return y

Conditionals

Example 2

example:
    input: boolean x

    if not x: 
        y = x + 2
    else:
        y = x + 1
    return y

Loops

A loop lets you look at every element in a list and do something each time.

Example - For Loop

example:
    for x in [1,2,3,4]:
        print x + 5

Prints out \(6,7,8,9\)

Loops

Example - While Loop

example:
    x = 1
    while x < 5:
        print x + 5
        x = x + 1

Also prints out \(6,7,8,9\)

Coding Existentials

We are going to practice writing pseudocode for our existentials