Your first Python statement

  • print() function

Documentation:

Examples:

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


Your second Python statement

  • input() function

Documentation:

Examples:

>>> input()
123
'123'

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

Beginning facts

  • Python interpreter: executes code written in Python

  • Interactive interpreter: allows users to execute on line at a time

  • prompt >>> is the interactive interpreter

  • Code, statement, program

  • comments # or """

  • output
    print("Hello, World!")

    • inside the quotes is a string literal

Comments

"""  All of this is a comment and is not considered code. 
You can have multiple lines like this """ 
print("this code is executed")

# this prints my name
print("my name is Dr. Simmons")
  • notice the newline when you execute these lines of code

Variables (Identifiers)

  • start with a lowercase letter
  • _ to separate words
  • avoid keywords/reserved words
  • use meaningful identifiers
  • variable is a name associate with a place to hold data
  • e.g., number_cats, cat_name, total
 number_cats = 2
 univ_name = "JMU" #string literal

Assignment

  • assignment operator
  • creates a variable or changes the value of an existing variable
  • lhs and rhs
  • which variable's value was changed?
  • output the value of a variable

Example:

value = 0.0
NICKEL = 0.05  # constant
number_nickels = 15
value = number_nickels * NICKEL  #rhs is an expression
print("the value is", value)

Expressions

weekly_rate = 0
payrate = 25
hours_per_week = 20
weekly_rate = payrate * hours_per_week

Input

  • input() reads text entered by the user
  • excecution is suspended until the user enters data and presses return
  • the input obtained from input() is a string (series of characters)
    lastname = input("what is your first name? ")
    print(lastname)
  • what is inside the quotes?
  • what is put into the lhs of the assignment?

More about output ...

  • Strings may use single quotes (') or double quotes (")
    • Single quotes are the default, but I tend to use double
  • Escape sequences: \n newline, \t tab, \\ backslash
  • print text on the same row
    print("hello",  end= " ")
    print("Sharon")
  • multiple items in a print statement
    year = 2023
    month = "August"
    print("the year is", year, "and the month is", month)
  • calculations in a print statement
    print("next year will be", year+1)
  • another example
print('She said, "Hello!"\nI was shocked!')

Output continued

-newline

    print('hello')
    print('world')
  • what happens?
  • how can you get the equivalnet output with one statement?

Types and operators: getting started

  • Types of data: more to learn
    • string
      • "Sharon"
    • integer
      • 10
    • float
      • 15.356
  • Operators for data: more to learn
    • add +
    • substract -
    • multiply *
    • divison /

Input, strings, int() and math operators

  • try this:
hours = int(input("How many hours did you work this week? "))
days = int(input("How many days did work this week? "))
print("You worked an average per day of ", hours/days, "hours")
  • Try the following, then fix the errors.
temperature = input("enter the current tempature: ")
night_temp = temperature - 20
  • Try the following:
cats = input("How many cats do have? ")
dogs = input("How many dogs do have? ")
total = cats + dogs
print(total)
  • what happened?

Errors

  • syntax error
  • runtime error
  • logic error
    Examples:
print('She said, "Hello!"\nI was shocked!)
total = 10
print(total+1)
name = "Sharon"
age = int(name)
height = 25
width = 16
area = width + height

Character and Escape Sequences

  • Unicode
    • example D is 48 (see chart)
    • D is character
    • string is a series of characters
print(ord('D'))
print(chr(100))
  • Escape sequence
    • backslash then a character
    • special meaning
    print('Dr. Simmons\' cats are named Ricky and Lucy')

Example Python Program

””” 
Application that converts inches to centimeters.
”””
inch = 0
cent = 0.0
CENT_PER_INCH = 2.54
# Prompt the user and get the value.
inch = int(input("How many inches?"))
# Convert and output the result.
cent = inch * CENT_PER_INCH
print(inch, "in =", cent, "cm")


A few terms learned so far ...

  • Integrated Development Environment IDE: e.g., Thonny
  • identifier
  • statement
  • expression
  • assignment
  • input
  • output
  • processing
  • comments
  • anything else?
  • f-strings f"{...}" (f = formatted string literal)