Lecture 4 - Formatted Output & Input

Page 1: Introduction

  • Course by Professor Omar D. Ellis, MISM
  • Welcoming message to students

Page 2: Hello World

  • Example of a basic program in Python

Page 3: Input Function Overview

  • The input function prompts user for data.
  • Displays message: "Enter the name of your city: "
  • User input is assigned to the variable town.

Page 4: Example of Input Function

  • Code:
fullName = input("Enter a full name:")
n = fullName.rfind(" ")
print("Last name:", fullName[n+1:])
print("First name(s):", fullName[:n])
  • Example Output:
    Last name: RooseveltFirst name(s): Franklin Delano

Page 5: Input Function Details

  • Always returns a string.
  • Can use with int, float, or eval functions for number input.
  • eval() executes expressions as Python statements; int and float are preferred for speed and safety.

Page 6: Input Conversion Example

  • Code:
letGrade = input("Enter your grade (A-F): ")
numGrade = int(input("Enter your grade (0-100): "))
average = float(input("Enter your average (0.0 - 100.0): "))
gpa = eval(input("Enter your GPA (0-4): "))
  • Example Output:
    Grade: 125Average: 42.25GPA: 3.5

Page 7: Terminal Output Examples

  • Running command line programs (python spinning_wheel.py, python progress.py).
  • Examples of command line output shown.

Page 8: Optional Print Argument - SEP

  • print(value0, value1, ..., valueN) defaults to space as separator.
  • Change separator with sep argument.

Page 9: Example of SEP Argument

  • Code:
print("Hello", "World!", sep="**")
  • Example Output:
    Hello**World!
    HelloWorld!
    1 two 3

Page 10: Optional Print Argument - END

  • Default behavior of print is to end with a newline.
  • Change ending with the end argument.

Page 11: Example of END Argument

  • Code:
print("Hello", end=" ")
print("World!")
  • Example Output:
    Hello World!

Page 12: expandtabs() Method

  • Controls spacing between horizontal tab stops (default is 8).

Page 13: Example of expandtabs()

  • Code using expandtabs(), demonstrating tab space customization.

Page 14: Justifying Output

  • Methods: ljust(), rjust(), and center() control data alignment in a specified width.

Page 15: Example of Justifying Output

  • Various applications of ljust, rjust, and center for formatting.

Page 16: Format Method Overview

  • format() method formats given arguments and inserts them where {} are placed.

Page 17: Example of format() Method

  • Code:
fruit1 = "apples"
fruit2 = "pears"
print("I want to buy {} and {}.".format(fruit1, fruit2))
  • Example Output:
    I want to buy apples and pears.

Page 18: Justifying Output with Format Method - Strings

  • Usage of {0:<ws}, {0:^ws}, {0:>ws} for string formatting.

Page 19: Justifying Output with Format Method - Numbers

  • Similar structure with number formatting using format.

Page 20: Example of Justifying with Formatters

  • Various uses of format specifiers for formatting output.

Page 21: Number Formatting

  • Different methods for formatting numbers, including commas and decimal precision.

Page 22: More on Number Formatting

  • Examples with outcomes comparing different formatting styles.

Page 23: F-Strings Overview

  • Create f-strings by prefixing with "f".

Page 24: Example of F-Strings

  • Code showing how to use f-strings for formatted output.

Page 25: F-Strings with Justification

  • Align and justify strings within f-strings.

Page 26: Example of F-Strings with Justification

  • Code demonstrating justification in f-strings.

Page 27: Comments in Python

  • Comments enhance code readability and are ignored by the interpreter.

Page 28: Benefits of Formatted Output and Input in Python

  • Benefits: Flexibility, Readability, User Interaction, and Precision Control.