The Big Picture

Review of the first two weeks

CS 149, 04-Sep-2023

Some vocabulary

print() function

Documentation:

Examples:

>>> print("Hello!")
Hello!

>>> print(1, 2, 3, sep=" + ")
1 + 2 + 3

>>> print("Same ", end="")
    print("line!")
Same line!

input() function

Documentation:

Examples:

>>> input()
123
'123'

>>> input("Your name? ")
Your name? Dr. Mayfield
'Dr. Mayfield'

Type conversion

Documentation:

Examples:

>>> int("149")
149

>>> float("1.49")
1.49

>>> str(149)
'149'

Variables (Identifiers)

Documentation:

Examples:

prefix = "CS"
number = 149
univ_name = "JMU"

Operators

  • Arithmetic: +, -, *, **, /, //, %
  • Strings: + (concatenation), * (repetition)

Examples:

>>> 1 - 2**3
-7

>>> 21 // 5
4

>>> 21 % 5
1

>>> "Ho"*3 + "!"
'HoHoHo!'

Formatting

  • Strings may use single quotes (') or double quotes (")
    • Single quotes are the default, but I use double quotes
  • Escape sequences: \n newline, \t tab, \\ backslash
  • f-strings f"{...}" (f = formatted string literal)

Examples:

>>> print('She said, "Hello!"\nI was shocked!')
She said, "Hello!"
I was shocked!

>>> f"{3.14159:.3f}"
'3.142'

Website example

See https://w3.cs.jmu.edu/mayfiecs/cs149/

course = input("Enter the course: ")
semester = input("Enter the semester: ")

print(f"Welcome to {course}, {semester}!")
  • Notice the entire output is one f-string
  • Multiple variables are substituted with {}
  • Formatting codes like :.3f are optional

The math module

Documentation:

Examples:

>>> math.pi
3.141592653589793

>>> math.floor(math.pi)
3

>>> math.ceil(math.pi)
4