Skip to content
Snippets Groups Projects
Commit 8db73bf9 authored by LOUIS TYRRELL OLIPHANT's avatar LOUIS TYRRELL OLIPHANT
Browse files

Lec 4 completed

parent 68bf92b8
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Warmup # Warmup
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 1 # Warmup 1
# Put the following in the correct order of operator precedence # Put the following in the correct order of operator precedence
# comparison: ==, !=, <, <=, >, >= # comparison: ==, !=, <, <=, >, >=
# signs: +x, -x # signs: +x, -x
# AND: and # AND: and
# add/subtract: +, - # add/subtract: +, -
# exponents: ** # exponents: **
# NOT: not # NOT: not
# OR: or # OR: or
# multiply/divide: *, /, //, % # multiply/divide: *, /, //, %
# 1. # 1.
# 2. # 2.
# 3. # 3.
# 4. # 4.
# 5. # 5.
# 6. # 6.
# 7. # 7.
# 8. # 8.
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 2 # Warmup 2
# What will be the output? Predict, then run the cell... # What will be the output? Predict, then run the cell...
print(5 + 2 * 3) # WRITE YOUR PREDICTION HERE print(5 + 2 * 3) # WRITE YOUR PREDICTION HERE
print(-10 ** 2) # WRITE YOUR PREDICTION HERE print(-10 ** 2) # WRITE YOUR PREDICTION HERE
print(5 // 3) # WRITE YOUR PREDICTION HERE print(5 // 3) # WRITE YOUR PREDICTION HERE
print(5 % 3) # WRITE YOUR PREDICTION HERE print(5 % 3) # WRITE YOUR PREDICTION HERE
print (-5 // 3) # WRITE YOUR PREDICTION HERE print (-5 // 3) # WRITE YOUR PREDICTION HERE
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 3 # Warmup 3
# What will be the output? Predict, then run the cell... # What will be the output? Predict, then run the cell...
print("CS220 is fun!" * 3) # WRITE YOUR PREDICTION HERE print("CS220 is fun!" * 3) # WRITE YOUR PREDICTION HERE
print("Hello\tWorld") # WRITE YOUR PREDICTION HERE print("Hello\tWorld") # WRITE YOUR PREDICTION HERE
print(3 + 4) # WRITE YOUR PREDICTION HERE print(3 + 4) # WRITE YOUR PREDICTION HERE
print("3" + '4') # WRITE YOUR PREDICTION HERE print("3" + '4') # WRITE YOUR PREDICTION HERE
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 4 # Warmup 4
# What will be the output? Predict, then run the cell... # What will be the output? Predict, then run the cell...
print(5 > 7) # WRITE YOUR PREDICTION HERE print(5 > 7) # WRITE YOUR PREDICTION HERE
print(not 12 <= 21) # WRITE YOUR PREDICTION HERE print(not 12 <= 21) # WRITE YOUR PREDICTION HERE
print(1 > 2 or 3 < 4) # WRITE YOUR PREDICTION HERE print(1 > 2 or 3 < 4) # WRITE YOUR PREDICTION HERE
print(0 == 0 and 7 != 7.1) # WRITE YOUR PREDICTION HERE print(0 == 0 and 7 != 7.1) # WRITE YOUR PREDICTION HERE
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 5 # Warmup 5
# What will be the output? Predict, then run the cell... # What will be the output? Predict, then run the cell...
print("Hello" == "World") # WRITE YOUR PREDICTION HERE print("Hello" == "World") # WRITE YOUR PREDICTION HERE
print("Hello" < "Hi") # WRITE YOUR PREDICTION HERE print("Hello" < "Hi") # WRITE YOUR PREDICTION HERE
print("H" < "W") # WRITE YOUR PREDICTION HERE print("H" < "W") # WRITE YOUR PREDICTION HERE
print("h" < "W") # WRITE YOUR PREDICTION HERE print("h" < "W") # WRITE YOUR PREDICTION HERE
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
![image.png](attachment:image.png) ![image.png](attachment:image.png)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Reminder... ## Reminder...
- An **operator** is either mathematical (\*, /, +, etc.,), string (+,\*), comparison (==, <, etc.,), or logical (`not`, `and`, `or`). - An **operator** is either mathematical (\*, /, +, etc.,), string (+,\*), comparison (==, <, etc.,), or logical (`not`, `and`, `or`).
- An **operand** is the data value that an operator operates on. - An **operand** is the data value that an operator operates on.
- **unary operators** of signs are applied on a single operand. - **unary operators** of signs are applied on a single operand.
- **binary operators** are applied on two operands. - **binary operators** are applied on two operands.
- An **expression** is the combination of **operator(s)** and **operand(s)**. - An **expression** is the combination of **operator(s)** and **operand(s)**.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Variables and Expressions # Variables and Expressions
## Readings: ## Readings:
- Chapter 2 of Think Python, - [Chapter 2 of Think Python](https://greenteapress.com/thinkpython2/html/thinkpython2003.html)
- [Chapter 3 of Python for Everybody](https://runestone.academy/ns/books/published//py4e-int/debugging/toctree.html) - [Chapter 3 of Python for Everybody](https://runestone.academy/ns/books/published//py4e-int/debugging/toctree.html)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Learning Objectives ## Learning Objectives
After this lecture you will be able to... After this lecture you will be able to...
- State and use variable naming rules - State and use variable naming rules
- Write assignment statements - Write assignment statements
- Identify in expressions: - Identify in expressions:
- operators and operands - operators and operands
- literal values and variables - literal values and variables
- order of operations - order of operations
- short circuiting in Boolean expressions - short circuiting in Boolean expressions
- Incorrect usage of Boolean operators - Correct and Incorrect usage of Boolean operators
- Define and give examples of 3 kinds of errors - Define and give examples of 3 kinds of errors
- Syntax error - Syntax error
- Runtime error - Runtime error
- Semantic - Semantic
- Use JupyterLab features: - Use JupyterLab features:
- shortcuts (SHIFT+ENTER, ESC+A, ESC+B, ESC+L, CTRL(CMD)+/) - shortcuts (SHIFT+ENTER, ESC+A, ESC+B, ESC+L, CTRL(CMD)+/)
- View Kernel state (variables) using debugger - View Kernel state (variables) using debugger
- 3 Ways of running Python code (Notebook, Python Console, Terminal)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# What will be the output? Predict, then run the cell... # What will be the output? Predict, then run the cell...
print(1 == 1 or (8/0) == -12) # WRITE YOUR PREDICTION HERE print(1 == 1 or (8/0) == -12) # WRITE YOUR PREDICTION HERE
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Short Circuiting in Boolean Expressions ## Short Circuiting in Boolean Expressions
- Logical operators are read left-to-right - Logical operators are read left-to-right
- If one of the operands in an OR statement is `True`, the entire expression is `True`. - If one of the operands in an OR statement is `True`, the entire expression is `True`.
- Likewise, if one of the oerands in an AND statement is `False`, the entire expression is `False`. - Likewise, if one of the oerands in an AND statement is `False`, the entire expression is `False`.
- Python doesn't waste time with Boolean expressions -- as soon as Python determines what the answer, the remainder of the expression will not be evaluated. - Python doesn't waste time with Boolean expressions -- as soon as Python determines what the answer, the remainder of the expression will not be evaluated.
Look closely at the following expressions and see if you know what it will evaluate to. Then enter each expression in the cell below and see if you are correct. Look closely at the following expressions and see if you know what it will evaluate to. Then enter each expression in the cell below and see if you are correct.
```python ```python
1 + 1 == 2 or 1 / 0 == -100 1 + 1 == 2 or 1 / 0 == -100
0 + 0 == 1 and 1 / 0 == -100 0 + 0 == 1 and 1 / 0 == -100
1 / 0 == -100 and 0 + 0 == 1 1 / 0 == -100 and 0 + 0 == 1
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# TODO -- Enter your code here # TODO -- Enter your code here
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## BE AWARE: Correct vs. Incorrect way of writing Boolean expressions ## BE AWARE: Correct vs. Incorrect way of writing Boolean expressions
You should only us Boolean values with logical operators (`and`, `or`, `not`)! You should only us Boolean values with logical operators (`and`, `or`, `not`)!
Take a look at the following code to see if 6 is greater than both 1 and 2. Take a look at the following code to see if 6 is greater than both 1 and 2.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# How to say is 6 greater than 1 and 2? # How to say is 6 greater than 1 and 2?
print(6 > 1 and 2) # What? print(6 > 1 and 2) # What?
print(6 > 1 and 6 > 2) # Correct print(6 > 1 and 6 > 2) # Correct
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Variables ## Variables
`10`, `22.0`, `"hello"`, `'goodbye'`, `True`, `False`... are known as **literal values**. `10`, `22.0`, `"hello"`, `'goodbye'`, `True`, `False`... are known as **literal values**.
However, variables *vary* over time. They are assigned using the `=` operator. The left hand side must be a variable, and the right hand side must be an expression! However, variables *vary* over time. They are assigned using the `=` operator. The left hand side must be a variable, and the right hand side must be an expression!
Variables look like... `x`, `player_score`, `my_cool_name`, `some_thing`... You can name them almost whatever you like. **You can use letters, numbers, and the underscore character when creating variable names, but the variable cannot start with a number. You also cannot use any Python keyword (e.g. False, and, etc.).** Variables look like... `x`, `player_score`, `my_cool_name`, `some_thing`... You can name them almost whatever you like. **You can use letters, numbers, and the underscore character when creating variable names, but the variable cannot start with a number. You also cannot use any Python keyword (e.g. False, and, etc.).**
Variable names should be meaningful. It is better to name a variable `name` than to name it `n` because a good variable name gives you information about the content of the variable. Variable names should be meaningful. It is better to name a variable `name` than to name it `n` because a good variable name gives you information about the content of the variable.
Here are a few good variable names: Here are a few good variable names:
* `cs220` * `cs220`
* `CS220` (Variables are CaSe SenSiTivE -- If they have different case letters then they are not the same variable) * `CS220` (Variables are CaSe SenSiTivE -- If they have different case letters then they are not the same variable)
* `cs_220` * `cs_220`
* `_cs220` * `_cs220`
Here are a few improper variable names: Here are a few improper variable names:
* `220class` -- Begins with a number so can't be a variable name * `220class` -- Begins with a number so can't be a variable name
* `and` -- A Python keyword so can't be a variable name * `and` -- A Python keyword so can't be a variable name
* `pi3.14` -- Contains a period so can't be a variable name * `pi3.14` -- Contains a period so can't be a variable name
* `x!` -- Contains an exclamation so can't be a variable name * `x!` -- Contains an exclamation so can't be a variable name
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# We create a variable called "x" and assign it the value 1.5. # We create a variable called "x" and assign it the value 1.5.
x = 1.5 x = 1.5
# We create a new variable called "y" and assign the value of x * 3 to it. # We create a new variable called "y" and assign the value of x * 3 to it.
y = x * 3 y = x * 3
# We print the values of x, y, and x + y. What do you think these will be? # We print the values of x, y, and x + y. What do you think these will be?
print(x) print(x)
print(y) print(y)
print(x + y) print(x + y)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Notice how we give meaningful variable names! # Notice how we give meaningful variable names!
# It would be confusing to have variable names such as v, w, x, y, and z. # It would be confusing to have variable names such as v, w, x, y, and z.
percent_burn = 0.25 percent_burn = 0.25
sheets_of_cookie_dough = 5 sheets_of_cookie_dough = 5
all_cookies = sheets_of_cookie_dough * 12 all_cookies = sheets_of_cookie_dough * 12
burned_cookies = all_cookies * percent_burn burned_cookies = all_cookies * percent_burn
good_cookies = all_cookies - burned_cookies good_cookies = all_cookies - burned_cookies
print('All Cookies') print('All Cookies')
print(all_cookies) print(all_cookies)
print() print()
print('Burned Cookies') print('Burned Cookies')
print(burned_cookies) print(burned_cookies)
print() print()
print('Good Cookies') print('Good Cookies')
print(good_cookies) print(good_cookies)
print() print()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## You try! ## You try!
Try to complete these problems on your own or with someone sitting next to you. Try to complete these problems on your own or with someone sitting next to you.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
hours = 40 hours = 40
pay_rate = 10.50 pay_rate = 10.50
# Uncomment the line below and complete the paycheck calculation! # Uncomment the line below and complete the paycheck calculation!
# paycheck = # paycheck =
print(paycheck) print(paycheck)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
student_age = 19 student_age = 19
# Create a variable called old_enough_to_vote. # Create a variable called old_enough_to_vote.
# If the student's age is >= 18, the value of this variable should be True. # If the student's age is >= 18, the value of this variable should be True.
# If the student's age is < 18, the value of this variable should be False. # If the student's age is < 18, the value of this variable should be False.
# Write your code here... # Write your code here...
print(old_enough_to_vote) print(old_enough_to_vote)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Calculate the compound growth of the principal using the given interest rate over the given time of years # Calculate the compound growth of the principal using the given interest rate over the given time of years
# and assign it to a variable called final_amount. # and assign it to a variable called final_amount.
# Recall the formula for compound interest is principal*(1+rate)^time # Recall the formula for compound interest is principal*(1+rate)^time
principal = 1000 principal = 1000
rate = 0.07 rate = 0.07
time = 30 time = 30
# Write your code here... # Write your code here...
print(final_amount) print(final_amount)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### "Syntactic Sugar" Shortcuts: ### "Syntactic Sugar" Shortcuts:
Quick ways to write code! `+=`, `-=`, `*=`, `/=`, etc. Quick ways to write code! `+=`, `-=`, `*=`, `/=`, etc.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
x = 2 x = 2
x += 3 # sames as x = x + 3 x += 3 # sames as x = x + 3
print(x) print(x)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
x = 8 x = 8
x -= 1 # sames as x = x - 1 x -= 1 # sames as x = x - 1
print(x) print(x)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Errors ## Errors
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
| Error Type | Definition | Example | | Error Type | Definition | Example |
| :- | :- | :- | | :- | :- | :- |
| Syntax Error | word soup, not grammatically sensible | `5 = x` | | Syntax Error | word soup, not grammatically sensible | `5 = x` |
| Runtime Error | need to run the program to encounter | `x = 5 / 0` | | Runtime Error | need to run the program to encounter | `x = 5 / 0` |
| Semantic Error | runs but produces the wrong answer | `square_area = square_side * 2` | | Semantic Error | runs but produces the wrong answer | `square_area = square_side * 2` |
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Bob runs a community bakery. Every day, he recieves a shipment of dough balls, each of which bakes 4 loaves of bread, and he evenly splits the bread among his customers. Some days he may recieve 10 customers, some days 20 customers, and some days none at all! Below is the code that tells Bob how much bread he gave to each customer. Bob runs a community bakery. Every day, he recieves a shipment of dough balls, each of which bakes 4 loaves of bread, and he evenly splits the bread among his customers. Some days he may recieve 10 customers, some days 20 customers, and some days none at all! Below is the code that tells Bob how much bread he gave to each customer.
Identify and correct a syntactic error, a runtime error, and a semantic error in his code. Identify and correct a syntactic error, a runtime error, and a semantic error in his code.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# These two variables can change based on the day! # These two variables can change based on the day!
balls_of_dough = 20 balls_of_dough = 20
num_customers = 15 num_customers = 15
balls_of_dough * 2 = bread_baked balls_of_dough * 2 = bread_baked
bread_per_customer = bread_baked / num_customers bread_per_customer = bread_baked / num_customers
print('Customer\'s loaves of bread:') print('Customer\'s loaves of bread:')
print(bread_per_customer) print(bread_per_customer)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Miscellaneous ## Miscellaneous
Some other things that are good to know! Some other things that are good to know!
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# The len function finds the length of some thing, if it has a length # The len function finds the length of some thing, if it has a length
name = 'Sam' name = 'Sam'
print(len("Stevie")) print(len("Stevie"))
print(len(name)) print(len(name))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Using this len function, we can align strings more easily! # Using this len function, we can align strings more easily!
player1_name = 'Alice' player1_name = 'Alice'
player2_name = 'bob' player2_name = 'bob'
player1_score = 10 player1_score = 10
player2_score = 8 player2_score = 8
#Scores are difficult to compare #Scores are difficult to compare
print(player1_name + ': ' + '|' * player1_score) print(player1_name + ': ' + '|' * player1_score)
print(player2_name + ': ' + '|' * player2_score) print(player2_name + ': ' + '|' * player2_score)
#Assuming name has a maximum of 9 characters #Assuming name has a maximum of 9 characters
#Now scores are easier to read and compare #Now scores are easier to read and compare
print(player1_name + ': ' + " " * (9 - len(player1_name)) + '|' * player1_score) print(player1_name + ': ' + " " * (9 - len(player1_name)) + '|' * player1_score)
print(player2_name + ': ' + " " * (9 - len(player2_name)) + '|' * player2_score) print(player2_name + ': ' + " " * (9 - len(player2_name)) + '|' * player2_score)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Jupyter Features ## JupyterLab Features
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
There are several shortcuts that will speed up your programming: There are several shortcuts that will speed up your programming:
### Nice Shortcuts ### Nice Shortcuts
* SHIFT + ENTER = cell execution (= Run) * SHIFT + ENTER = cell execution (= Run)
* ESC + A = add a cell above the current cell * ESC + A = add a cell above the current cell
* ESC + B = add a cell below the current cell * ESC + B = add a cell below the current cell
* ESC + L = toggle the line numbers * ESC + L = toggle the line numbers
* CTRL + / = toggle comment(s) (that is, adds/removes #) * CTRL + / = toggle comment(s) (that is, adds/removes #)
### Viewing Kernel State ### Viewing Kernel State
JupyterLab now has a debugger which can help you find and fix errors in your code. The **kernel** is the program that runs your Python code. It remembers the variables you have created. You can see and control how the kernel is doing by looking at the top bar in your Jupyter notebook: JupyterLab now has a debugger which can help you find and fix errors in your code. The **kernel** is the program that runs your Python code. It remembers the variables you have created. You can see and control how the kernel is doing by looking at the top bar in your Jupyter notebook:
![image.png](attachment:9b33c6ec-6cb7-465c-87fc-b161532c8153.png) ![image.png](attachment:9b33c6ec-6cb7-465c-87fc-b161532c8153.png)
In our notebook it is running the *Python 3 (ipykernel)*. The circle to the side will become filled in as it is executing your code: In our notebook it is running the *Python 3 (ipykernel)*. The circle to the side will become filled in as it is executing your code:
![image.png](attachment:5855ba66-52da-42fb-b147-19fe874fd36b.png) ![image.png](attachment:5855ba66-52da-42fb-b147-19fe874fd36b.png)
If the kernel ever gets stuck, you can click the stop button which will interrupt the kernel. Clicking the bug icon will turn on the debugger and open the debugger right sidepanel. If the kernel ever gets stuck, you can click the stop button which will interrupt the kernel. Clicking the bug icon will turn on the debugger and open the debugger right sidepanel.
![image.png](attachment:7d9946b0-4be0-4688-b8dd-3c603ca10d82.png) ![image.png](attachment:7d9946b0-4be0-4688-b8dd-3c603ca10d82.png)
The top portion within the sidepanel will show you the variables that the kernel currently has. Try creating variables in the cell below and see them appear in this variables section The top portion within the sidepanel will show you the variables that the kernel currently has. Try creating variables in the cell below and see them appear in this variables section
![image.png](attachment:6e6b904a-5ee6-4306-ba92-c067d5e3f26c.png) ![image.png](attachment:6e6b904a-5ee6-4306-ba92-c067d5e3f26c.png)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
## TODO -- enter your code here ## TODO -- enter your code here
my_pi = 3.14 my_pi = 3.14
my_name = "Louis" my_name = "Louis"
my_class = "CS220" my_class = "CS220"
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Ways of Running Python
Within JupyterLab you have three ways of running Python code -- Using a Python notebook, a Python console, or in a terminal. Let's practice each of these.
#### Python Notebooks
When you open a Python notebook, a Python kernel is started where all of the code in that notebook will run. You can see a list of running kernels in the left sidepanel under the second icon, kernels and terminals. You can use that list to shut down a kernel.
![image.png](attachment:90b3b3b8-2278-4769-a11c-6ca0ceb6a2bc.png)
![image.png](attachment:5bd66be3-0525-4974-8ea4-e11b4ec509ee.png)
If you shut down the kernel for the notebook by hovering your mouse pointer over the kernel and clicking its close X, the kernel icon for the notebook in the bar at the top of the notebook will turn to a lightning bolt and it will say "No Kernel" is running. You can restart the kernel by clicking on the "No Kernel" and selecting Python3 kernel.
![image.png](attachment:ae6dd724-3d9e-41cb-a6e9-600660a2d78a.png)
![image.png](attachment:b08f6050-b613-47c8-823f-b9836649beb1.png)
#### Python Console
Another way you can run Python is by starting up a Python console. You can either use the "Launcher" tab and click on the "console" option, or on the menu bar click File -> New -> Console. A console has a single cell at the bottom. You can type as much code as you like into the cell and execute it by typing `shift-enter`. The code will appear at the top of the console with the output from the code appearing directly underneath and the cell will be cleared for you to enter more code.
![image.png](attachment:e74cb408-b9ee-417c-aee2-1412ea06f3fb.png)
![image.png](attachment:bac9302a-76f4-48b8-a128-cd757967f1bb.png)
One nice thing about the console is it remembers what you typed. You can use the up and down arrows to cycle through the content you have typed. This is a nice place to practice writing code and working out what you code you want before you enter it into some other place like a notebook.
To shut down a console you need to not only close the tab but also click to open the kernel sidepanel and click to shutdown the kernel associated with the console.
#### Terminal
You can open a terminal by clicking in the launcher on "Terminal" or on the menu bar click File -> New -> Terminal. A terminal window is where you can enter commands that are carried out by your operating system and is different on different operating systems. The terminal is not running Python and does not accept Python code. However, recall that when you installed Anaconda one of the programs that was installed was Python. You can tell the OS to start up Python in this terminal and you can give the Python program a file that contains Python code, called a script. Python script files typically have a `.py` file extension. Python will execute the code in this script and then exit. If you downloaded the `hello.py` file and placed it in the same directory along with this notebook, then you should be able to type the following in a terminal and see the script run.
```
python hello.py
```
Here is what I see when I run the script and enter my name:
![image.png](attachment:7a473cf1-3ce0-49f5-bfdf-22b098bbd3dc.png)
We won't be writing scripts this semester, but it is a common way to write Python programs. You can close a terminal by closing its tab or typing the `exit` command.
%% Cell type:markdown id: tags:
## Summary ## Summary
In this lecture you practiced creating expressions, especially Boolean expressions with **short circuiting**, learned the rules for **variable names** and how to write an **assignment statement**. You should now be able to identify the three categories of programming **errors** (syntax, runtime, and semantic). In this lecture you practiced creating expressions, especially Boolean expressions with **short circuiting**, learned the rules for **variable names** and how to write an **assignment statement**. You should now be able to identify the three categories of programming **errors** (syntax, runtime, and semantic).
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Extra Practice ## Extra Practice
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Given the number of seconds, compute the number of hours, minutes, and seconds. # Given the number of seconds, compute the number of hours, minutes, and seconds.
# e.g. if seconds is 65, the correct output (with \n in-between) is 0 1 5 # e.g. if seconds is 65, the correct output (with \n in-between) is 0 1 5
# e.g. if seconds is 192, the correct output (with \n in-between) is 0 3 12 # e.g. if seconds is 192, the correct output (with \n in-between) is 0 3 12
# e.g. if seconds is 3739, the correct output (with \n in-between) is 1 2 19 # e.g. if seconds is 3739, the correct output (with \n in-between) is 1 2 19
seconds = 3739 seconds = 3739
# We know we'll have to create 2 more variables, hours and minutes. # We know we'll have to create 2 more variables, hours and minutes.
``` ```
......
name = input("What is your name? ")
print("Hello, "+name)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment