Skip to content
Snippets Groups Projects
Commit 59ee0cfd authored by Andy Kuemmel's avatar Andy Kuemmel
Browse files

Update f22/andy_lec_notes/lec08_Sep23_Conditionals1/lec_08_conditionals1_template.ipynb

parent 7573f1c5
No related branches found
No related tags found
No related merge requests found
%% Cell type:raw id: tags:
# Lecture 8 Conditionals part 1
Learing Objectives:
Write Conditional Statements
- using Conditional execution ( if )
- using Alternate execution (if/else)
- using Chained conditionals (if/elif/elif/…/else)
Identify code blocks (indentation layers) in a program
- count the number of blocks in a segment of code
Determine the output of code with conditional statements
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Write Conditional Statements
%% Cell type:code id: tags:
``` python
# Example 1a: let's pick a number to classify English words
# as either short or long
# A word in English is "short" if it has _____ letters or less
# otherwise it is "long"
# talk to your neighbor and determine a number
word = "elephant"
word_length = len(word)
if word_length <=
# then...add an extra print statement inside each if block
```
%% Output
File "<ipython-input-2-fd17ff1eaf14>", line 10
if word_length <=
^
SyntaxError: invalid syntax
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Write Conditional Statements with Alternate Execution (else)
%% Cell type:code id: tags:
``` python
# Example 1b: Let's rewrite the code using an else statement
# this is called 'alterate execution'
word = 'elephant'
word_length = len(word)
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# Example 2: Let's classify young people by age
# what classifications would you use?
# a person who is _____ years old is a _________
# and so on.....
def categorize_age(age):
pass
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Write Conditional Statements with Chained Conditionals
%% Cell type:code id: tags:
``` python
# Redo Example 2 with chained chained conditions
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Identify nested code blocks (indentation layers) in a program
%% Cell type:markdown id: tags:
Check out slides 19-25
https://www.msyamkumar.com/cs220/s22/materials/lecture_ppts/lec_08_S22.pdf
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# Example 3: Date Printer
```
%% Cell type:markdown id: tags:
![date%20printer.png](attachment:date%20printer.png)
%% Cell type:code id: tags:
``` python
# first function: convert a month (int) into a 3 letter abbreviation or "N/A"
def month_to_str(month):
"""Convert a month (as an integer) into a string. 1 is Jan, 2 is Feb, etc."""
pass
```
%% Cell type:code id: tags:
``` python
# second function: convert a day (int) into a string writing '15th', '23rd' or "N/A"
def day_to_str(day):
"""
Covert a day into a date string with proper ending.
16 --> '16th' 23 --> '23rd'
"""
pass
print(day_to_str(4))
print(day_to_str(6))
print(day_to_str(11))
print(day_to_str(14))
print(day_to_str(-1))
```
%% Output
4th
6th
11th
14th
err
%% Cell type:code id: tags:
``` python
# third function: convert a year (int) into a string for year
# 2021 ---> '21 # return a string with ' and the last 2 digits
# 1996 ---> 1996 # if year before 2000 return all 4 digits, as a string
def year_to_str(year):
"""
Convert a year (as an integer) into a string.
If the year is before 2000, return the full 4 digits.
Otherwise, return the last 2 digits with a single quote in front.
"""
pass
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# now put it all together
def format_date(year=2021, month=1, day=1)
"""returns a string representing the date, such as Feb 10th of ‘22"""
pass
```
%% Cell type:code id: tags:
``` python
# Do this after lecture:
# Interactive Exercises
# Python for Everybody (helps you gain coding fluency...projects take less time)
```
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