Contents

  1. demo_str.py
  2. escape_sequence.py
  3. format_examples.py
  4. sales_tax2.py
  5. sales_tax.py
  6. temp_table.py
  7. temp_table_starter.py

demo_str.py 1/7

[
top][prev][next]
# Demonstrate long strings and escape sequences
# by Sara Sprenkle

string = """This is a long string.
Like, really long.
Sooooo loooooong"""

print(string)

print("To print a \\, you must use \"\\\\\"")


escape_sequence.py 2/7

[
top][prev][next]
# Practice with escape sequences
# CS111

# Display To print a tab, you must use '\t'.
# If you use double quotes, you don't _need_ to escape the single quote
print("To print a tab, you must use a '\\t'.")
print('To print a tab, you must use a \'\\t\'.')

# Display I said, "How are you?"
print("I said, \"How are you?\"")
print('I said, "How are you?"')


format_examples.py 3/7

[
top][prev][next]
# Formatting examples, from handout
# CSCI111

x = 10
y = 3.5
z = "apple"
print("{:6i}".format(x))
print("{:6.2f}".format(x))
print("{:6.2f}".format(y))
print("{:06.2f}".format(y))
print("{:+6.2f}".format(y))
print("{:^10s}".format(z))
print("*{:^10s}*".format(z))
print("{:5d} {:<7.3f}".format(x,y))

sales_tax2.py 4/7

[
top][prev][next]
# Compute the cost of an item, plus sales tax.
# The displayed cost uses a format specifier.
# by Sara Sprenkle

SALES_TAX=.053  # the sales tax in VA

value = eval(input("How much does your item cost? "))

with_tax = value * (1+SALES_TAX)

print("Your item that cost ${:.2f}".format(value), end=' ')
print("costs ${:.2f} with tax".format(with_tax))

sales_tax.py 5/7

[
top][prev][next]
# Compute the cost of an item, plus sales tax
# Demonstrate need for/use of format specifiers
# by Sara Sprenkle

SALES_TAX=.053  # the sales tax in VA

# Test with a variety of values
value = eval(input("How much does your item cost? "))

with_tax = value * (1+SALES_TAX)

print("Your item that cost $", value, end=' ')
print("costs $", with_tax, "with tax.")

temp_table.py 6/7

[
top][prev][next]
# Print out the table of temperatures
# By CS111

# Better to calculate these conversions but that's not the 
# focus today.

# First, figure out the format specifier for each column.
# - determine the type
# - determine the width
# - determine the flags and/or precision

# Second, fill in the values into each of those columns.

print("{:6s} {:>10s} {:>10s}".format("Temp F", "Temp C", "Temp K"))
print("{:6s} {:>10s} {:>10s}".format("-"*6, "-"*6, "-"*6))

ftemp = -459.67
ctemp = -273.15
ktemp=0

print("{:6.1f} {:10.1f} {:10.1f}".format(ftemp, ctemp, ktemp))


ftemp = 0
ctemp = -17.77778
ktemp= 255.222
print("{:6.1f} {:10.1f} {:10.1f}".format(ftemp, ctemp, ktemp))


ftemp = 32
ctemp = 0
ktemp= 273.15
print("{:6.1f} {:10.1f} {:10.1f}".format(ftemp, ctemp, ktemp))



temp_table_starter.py 7/7

[
top][prev][next]
# Print out the table of temperatures
# By CS111

# Better to calculate these conversions but that's not the focus today

# Some starter code; not filled in with printing the table.
# See temp_table_final.py

ftemp = -459.67
ctemp = -273.15
ktemp=0


ftemp = 0
ctemp = -17.77778
ktemp= 255.222



ftemp = 32
ctemp = 0
ktemp= 273.15



Generated by GNU Enscript 1.6.6.