Lesson 1.8: Input/Output Operations and Unit Wrap-up

PCEP 1.5: Console I/O Operations

Opening Question

Think about your favorite app or website. How does it get information from you? How does it show you results?

Call on 2-3 students for examples

The print() Function Revisited

We've been using print() - but let's master it!

print("Hello, World!")
print(42)
print(3.14159)
print(True)

Try it: Run each line and observe the output

Multiple Arguments in print()

print() can handle multiple values at once

print("The answer is", 42)
print("Pi equals", 3.14159, "approximately")
print("Python", "is", "awesome!")

Your turn: Create a print statement with your name, age, and favorite color

The sep Parameter

Control what goes between your values

print("apple", "banana", "cherry")
print("apple", "banana", "cherry", sep=", ")
print("apple", "banana", "cherry", sep=" | ")
print("apple", "banana", "cherry", sep="")

Question: What's the default separator in print()?

The end Parameter

Control what happens at the end of print()

print("Hello", end=" ")
print("World!")
print("Python", end="!!!\n")
print("is fun")

Try it: Make "Hello World!" appear on the same line using two print() statements

Combining sep and end

Maximum control over your output

print("A", "B", "C", sep="-", end=" | ")
print("X", "Y", "Z", sep="+", end="\n")
print("Done!")

Challenge: Use sep and end to create this output:

1-2-3 >>> 4-5-6
Finished!

The input() Function

Getting information from users

name = input("What's your name? ")
print("Hello,", name)

Important: input() always returns a string!

Try it: Ask a user for their favorite number and print it back

input() Always Returns Strings

Even numbers come back as text

age = input("How old are you? ")
print("Next year you'll be", age + 1)  # Error!
print(type(age))  # What type is age?

Question: Why does this cause an error?

Type Conversion with input()

Converting strings to numbers

age_str = input("How old are you? ")
age = int(age_str)
print("Next year you'll be", age + 1)

# Or do it in one line:
height = float(input("How tall are you in meters? "))
print("That's", height * 100, "centimeters!")

Practice: Get two numbers from user, add them, and print the sum

Real-World Example

Let's build a simple calculator

print("Simple Calculator")
print("=" * 20)

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print("Results:")
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)

Your turn: Add division to this calculator

Common input() Mistakes

What's wrong with each of these?

# Example 1
age = input("Age: ")
if age > 18:
    print("Adult")

# Example 2  
number = int(input("Enter number: "))
print("You entered:", number, type(number))

Discuss: Identify the issues and how to fix them

Interactive Mini-Program Challenge

Create a program that:

  • Asks for user's first and last name
  • Asks for their birth year
  • Calculates and displays their age in 2025
  • Uses proper formatting with sep and end parameters

Time: 5 minutes to code this up!

Unit 1 Concept Review

Let's connect everything we've learned:

  • PCEP 1.1: Python as interpreted language, syntax/semantics
  • PCEP 1.2: Keywords, indentation, comments
  • PCEP 1.3: Literals, variables, numeral systems, PEP-8
  • PCEP 1.4: All operator types, precedence, type casting
  • PCEP 1.5: print() and input() functions

Question: Which concept was most challenging? Most useful?

PCEP Section 1 Readiness Check

Can you:

  • Explain compilation vs. interpretation?
  • Use proper Python indentation and comments?
  • Work with different numeral systems?
  • Apply operator precedence rules?
  • Create properly named variables following PEP-8?
  • Use print() with sep and end parameters?
  • Get user input and convert data types?

Self-assessment: Rate yourself 1-5 on each skill

Unit Assessment Preview

Your comprehensive project will demonstrate:

  • All PCEP Section 1 objectives
  • Interactive console application
  • Proper use of variables, operators, and I/O
  • PEP-8 compliance and documentation
  • Creative problem-solving

Think: What kind of interactive program would you like to build?

Exit Ticket

Quick check - complete this code:

name = ___("What's your name? ")
age = ___(input("What's your age? "))
___("Hello", ___, end="! ")
___("You are", age, "years old", ___=".\n")

Bonus: What will this output look like?

Next Up: Unit 2

Control Flow - Conditional Blocks and Loops

  • PCEP Section 2 (29% of exam)
  • Making decisions with if statements
  • Repeating actions with loops
  • Building more complex programs

Preview question: How might we make our programs make decisions based on user input?