Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs220-lecture-material
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CDIS
Computer Sciences
Courses
CS220
cs220-lecture-material
Commits
b933074c
Commit
b933074c
authored
2 years ago
by
Andy Kuemmel
Browse files
Options
Downloads
Patches
Plain Diff
Update f22/andy_lec_notes/lec08_Sep23_Conditionals1/lec_08_conditionals1_template.ipynb
parent
2fc533e2
No related branches found
Branches containing commit
No related tags found
1 merge request
!2
Kuemmel main patch 90133
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
f22/andy_lec_notes/lec08_Sep23_Conditionals1/lec_08_conditionals1_template.ipynb
+19
-48
19 additions, 48 deletions
...8_Sep23_Conditionals1/lec_08_conditionals1_template.ipynb
with
19 additions
and
48 deletions
f22/andy_lec_notes/lec08_Sep23_Conditionals1/lec_08_conditionals1_template.ipynb
+
19
−
48
View file @
b933074c
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Warmup: "
]
},
{
"cell_type": "markdown",
"cell_type": "raw",
"metadata": {},
"source": [
"#
February 11:
Conditionals part 1\n",
"#
Lecture 8
Conditionals part 1\n",
"Learing Objectives:\n",
"\n",
"Write Conditional Statements\n",
...
...
@@ -124,18 +115,9 @@
},
{
"cell_type": "code",
"execution_count":
3
,
"execution_count":
1
,
"metadata": {},
"outputs": [
{
"ename": "IndentationError",
"evalue": "expected an indented block (<ipython-input-3-f40f344477b1>, line 8)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-3-f40f344477b1>\"\u001b[0;36m, line \u001b[0;32m8\u001b[0m\n\u001b[0;31m def categorize_age(age):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n"
]
}
],
"outputs": [],
"source": [
"# Example 2: Let's classify young people by age\n",
"# what classifications would you use?\n",
...
...
@@ -189,28 +171,13 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# See the slides"
"Check out slides 19-25\n",
"https://www.msyamkumar.com/cs220/s22/materials/lecture_ppts/lec_08_S22.pdf"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
...
...
@@ -271,8 +238,10 @@
"source": [
"# second function: convert a day (int) into a string writing '15th', '23rd' or \"N/A\"\n",
"def day_to_str(day):\n",
" \"\"\"Covert a day into a date string with proper ending. \n",
" 16 --> '16th', 23 --> '23rd', \"\"\"\n",
" \"\"\"\n",
" Covert a day into a date string with proper ending. \n",
" 16 --> '16th' 23 --> '23rd'\n",
" \"\"\"\n",
" pass\n",
"\n",
"print(day_to_str(4))\n",
...
...
@@ -293,8 +262,11 @@
"# 1996 ---> 1996 # if year before 2000 return all 4 digits, as a string\n",
"\n",
"def year_to_str(year):\n",
" \"\"\"Convert a year (as an integer) into a string. If the year is < 2000, return the full 4 digits\n",
" Otherwise, return the last 2 digits with a single quote in front\"\"\"\n",
" \"\"\"\n",
" Convert a year (as an integer) into a string. \n",
" If the year is before 2000, return the full 4 digits.\n",
" Otherwise, return the last 2 digits with a single quote in front.\n",
" \"\"\"\n",
" pass\n"
]
},
...
...
@@ -325,15 +297,14 @@
"outputs": [],
"source": [
"# Do this after lecture:\n",
"# Worksheet (good exam practice)\n",
"# Interactive Exercises (has worksheet answers)\n",
"# Interactive Exercises \n",
"# Python for Everybody (helps you gain coding fluency...projects take less time)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3
(ipykernel)
",
"language": "python",
"name": "python3"
},
...
...
@@ -347,7 +318,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.9.1
2
"
}
},
"nbformat": 4,
...
...
%% Cell type:code id: tags:
```
python
# Warmup:
```
%% Cell type:markdown id: tags:
%% Cell type:raw id: tags:
#
February 11:
Conditionals part 1
#
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
```
%% Output
File "<ipython-input-3-f40f344477b1>", line 8
def categorize_age(age):
^
IndentationError: expected an indented block
%% 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:code id: tags:
```
python
# See the slides
```
%% Cell type:code id: tags:
```
python
```
%% Cell type:code id: tags:
%% Cell type:markdown id: tags:
```
python
```
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:

%% 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', """
"""
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 < 2000, return the full 4 digits
Otherwise, return the last 2 digits with a single quote in front"""
"""
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:
# Worksheet (good exam practice)
# Interactive Exercises (has worksheet answers)
# Interactive Exercises
# Python for Everybody (helps you gain coding fluency...projects take less time)
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment