# Basic Types
and Operations --- CS 65 // 2021-02-09 ## Extra Credit Activities - Songs for A New World - Basketball game tomorrow at 6 ## Assignment 2 - Due Thursday before class - Any questions? # Questions ## ...about anything? # TYPES ## Types in Programming Languages - Basic values in algorithms usually have some **type** + e.g. bread, PB, preserves, in our PB&J algorithm - A type can help us know **how to use** the values ## Five Questions ...to ask when encountering a new type --- 1. What is its **name**? 2. What is its **purpose**? 3. How do I **express** values of it to the computer? 4. How does the computer **display** values of it to us? 5. What **operations** can be performed on it? ## Types in Python `34`, `34.0`, and `"34"` have different *types* in Python --- + What is the type of `34`? + What is the type of `34.0`? + What is the type of `"34"`? # INTEGER (int) - **Purpose**: + Manipulating whole valued numbers + **Exact** number and can be arbitrary large or small - **Expressed as**: + `34` + `-123` - **Operations**: + `+`, `/`, `-`, `*`, ... # FLOAT - **Purpose**: + Manipulating fractional numbers + **Inexact** and accumulate errors over time - **Expressed as**: + `34.0` + `-3.1415` - **Operations**: + `+`, `/`, `-`, `*`, ... # STRING (str) - **Purpose**: + Manipulating textual information - **Expressed as**: + `"abc"` + `'abc'` - **Operations**: + `print`, `input`, `+` (concatenate) # EXPRESSIONS ## Expressions - Expressions are any valid combination of basic values, operations, and variables that evaluate to a single value + `(5 + x) * 3` - Expressions are evaluated according to **order of operations** or **precedence** + Multiplication happens before addition, etc. ## Creating Variables - To create (or modify) a variable, you can use an **assignment statement** which has the form: ```py # name = expression val = (3 + 4) / 2 ``` - First `(3 + 4) / 2` is evaluated to produce `3.5` - Then the variable `val` is created and points to `3.5` ## `input` and `print` - When writing Python in **script mode**, it is useful to ask the user running the script a question or print a message to them - `input` is a function that requests keyboard input - `print` is a function that prints textual output ```py print("Hello, there!") name = input("What is your name? ") print("The name you entered is", name) ``` # Self Checks ## A Complete Program ```py [|5|12|8|] # A simple program illustrating chaotic behavior # # Author: # John Zelle, from Python Programming, Third Edition def main(): print("This program illustrates a chaotic function") x = float(input("Enter a number between 0 and 1: ")) for i in range(10): x = 3.9 * x * (1 - x) print(x) main() ``` # Lab Time