Skip to main content

Recitation 10 -- Intro to Python

Skills: 8

Objectives

  • Translate function definitions from Pyret to Python syntax
  • Understand Python's type annotations and return statements
  • Convert conditional expressions between languages
  • Work with Python lists and built-in functions

I. Introduction (5 minutes)

Explain that we've learned the basics in Pyret—expressions, functions, and data. Today, we will transition to Python, which uses different notation but similar concepts.

II. Function Definitions and Types (15 minutes)

A. Basic Function Translation

Example

Pyret Version:

fun celsius-to-fahrenheit(temp :: Number) -> Number:
doc: "convert Celsius temperature to Fahrenheit"
(temp * 9/5) + 32
end

Python Version:

def celsius_to_fahrenheit(temp: float) -> float:
"""convert Celsius temperature to Fahrenheit"""
return (temp * 9/5) + 32

Key Differences:

  • def vs fun
  • Underscores vs hyphens in names
  • Colons for type annotations
  • Explicit return statement required
  • Triple quotes for documentation
  • Indentation instead of end

Check Understanding: Ask, "What happens if you forget the return statement in Python?"

B. Practice

Activity: Have students convert this Pyret function to Python:

fun calculate-tip(bill :: Number, percentage :: Number) -> Number:
doc: "calculate tip amount for a restaurant bill"
bill * (percentage / 100)
end

Solution:

def calculate_tip(bill: float, percentage: float) -> float:
"""calculate tip amount for a restaurant bill"""
return bill * (percentage / 100)

III. Conditionals in Python (15 minutes)

A. If-Else Translation

Example

Pyret Version:

fun letter-grade(score :: Number) -> String:
doc: "convert numeric score to letter grade"
if score >= 90:
"A"
else if score >= 80:
"B"
else if score >= 70:
"C"
else if score >= 60:
"D"
else:
"F"
end
end

Python Version:

def letter_grade(score: float) -> str:
"""convert numeric score to letter grade"""
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"

Key Differences:

  • elif instead of else if
  • Each branch needs its own return
  • Indentation determines code blocks

B. Practice

Interactive Exercise: Ask students to write a Python function for shipping cost:

  • Orders under $25: $5 shipping
  • Orders $25-$75: $3 shipping
  • Orders over $75: free shipping

Solution:

def shipping_cost(order_amount: float) -> float:
"""calculate shipping cost based on order amount"""
if order_amount < 25:
return 5.0
elif order_amount <= 75:
return 3.0
else:
return 0.0

IV. Python Lists vs Pyret Lists (15 minutes)

A. Creating and Basic Operations

Pyret vs Python Comparison:

Pyret:

my-list = [list: "red", "green", "blue", "yellow"]
first-color = my-list.first
rest-colors = my-list.rest
list-size = my-list.length()

Python:

my_list = ["red", "green", "blue", "yellow"]
first_color = my_list[0]
rest_colors = my_list[1:]
list_size = len(my_list)

Point out indexing differences (0-based vs first/rest) and built-in len() function.

B. Filter and Map Operations

Example Pyret Version:

import lists as L

numbers = [list: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = L.filter(lam(n): num-modulo(n, 2) == 0 end, numbers)
squares = L.map(lam(n): n * n end, numbers)

Python Version:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = list(filter(lambda n: n % 2 == 0, numbers))
squares = list(map(lambda n: n * n, numbers))

Key Differences:

  • Square brackets vs [list: ...]
  • lambda instead of lam
  • Must wrap filter and map with list()

V. Testing in Python (10 minutes)

A. Pyret where vs Python pytest

Pyret Testing:

fun double(n :: Number) -> Number:
doc: "double the input number"
n * 2
where:
double(5) is 10
double(0) is 0
double(-3) is -6
end

Python Testing:

import pytest

def double(n: float) -> float:
"""double the input number"""
return n * 2

def test_double():
assert double(5) == 10
assert double(0) == 0
assert double(-3) == -6

# For floating-point comparisons:
def test_double_approx():
assert double(3.3) == pytest.approx(6.6)

Key Differences:

  • Separate test functions instead of where blocks
  • assert statements for testing

B. Practice

Have students write tests for their shipping cost function:

Solution:

def test_shipping_cost():
assert shipping_cost(20) == 5.0
assert shipping_cost(50) == 3.0
assert shipping_cost(100) == 0.0

VI. For Loops and List Processing (10 minutes)

Example

Pyret (for each):

fun sum-numbers(numbers :: List<Number>) -> Number:
doc: "sum all numbers in a list"
var total = 0
for each(n from numbers):
total := total + n
end
total
end

Python:

def sum_numbers(numbers: list) -> float:
"""sum all numbers in a list"""
total = 0
for n in numbers:
total = total + n
return total

Key Differences:

  • No var keyword needed
  • Regular assignment = instead of :=
  • Simpler for loop syntax

VII. Recap and Wrap-Up (5 minutes)

Key Points Discussion:

  • Syntax Differences: def vs fun, underscores vs hyphens, explicit return
  • Conditionals: elif instead of else if, each branch needs return
  • Lists: Square brackets, 0-indexing, len() function
  • Testing: Separate test functions instead of where blocks

Reflection Questions:

  • idk lol i'll write these later