lec-05
onlinequestions.org
202304170105I forgot to show you an example like this:
6 / 3 # predict this resultI also misspoke regarding print being a keyword in Python - it's not. It's a built-in function. But there are keywords and you can't play with them.
print "foo"print ("foo")print("\t\tfoo\nbar")stuff = 1.3
more_stuff = 2
stuff_stuff = stuff + more_stuff
Print(stuff_stuff)print("Sum is:", end="")
sum = 5 + 5.0
print(sum)
print(str(sum) * 2)⦾ keyboard input
⦾ strings
⦾ string operators
⦾ a wee bit of f-strings
read from an input device
write to an output device
read a value from storage
write a value to storage
"do math"
branch
loop (repeated branching)
read from an input device
write to an output device
read a value from storage
write a value to storage
"do math"
branch
loop (repeated branching)
Let's futz around with this input() thingie and see what we can find out.
(but there is some scariness here, I'll admit)
day_of_week = input("What day is it today?")
print(day_of_week)
print("Wow.")What will appear on the screen when we run this?
What value will be in day_of_week if we type Wed and press Enter?
What type does day_of_week have?
What will be printed?
Do you notice anything annoying about the prompt ending and the stuff entered by the user?
How can we fix that annoying thing?
What special name do we give the string "What day is it today?
Is the newline character included in day_of_week? How can you tell?
print("Welcome to the Day of the Week App")
print()
print("My only wish is to serve you in a servile manner.")
print()
print("What day is it today, oh munificent one? ", end="")
day_of_week = input()
print(day_of_week, "What a wonderful choice!", sep="...")What will appear on the screen?
What value will be in day_of_week if we type Wed and press Enter?
What does end="" do? Why is it used here?
What will be printed?
Is calling input() without a prompt a syntax error?
What's the equivalent way of writing line 5 & 6 on one line? Which way is "better"?
size_in_toasters = input(
"How big was that thing you saw, compared to a toaster? "
)
# assume a toaster has a size of 30.7 whatevers
size = 30.7 * size_in_toasters
print("Wow. That thing you saw was", size, "big?!?")What will appear on the screen? Will the weird formatting blow things up? Why/why not?
What value will be in size_in_toasters if we type 4 and press Enter?
What will be printed? Why?
What is that # blahblah thing called? What's it useful for?
What can we do to fix the problem?
size_in_toasters_as_text = input("How big ... ")
size_in_toasters = int(size_in_toasters_as_text)
size = 30.7 * size_in_toasterssize_in_toasters_as_text = input("How big ... ")
size = 30.7 * int(size_in_toasters_as_text)size_in_toasters = int(input("How big ... "))
size = 30.7 * size_in_toasterssize_in_toasters = input("How big ... ")
size = 30.7 * int(size_in_toasters)something is a bit off here...what is it?
input("Greetings, biped. How does it go?")| f | o | o |
|---|
flavour = "disturbingly bold cheddar"
mood = 'cautiously pessimistic'
you_can_do_that = "that's a bit tricksy"
and_this_too = '"Who thought this was a good idea?!?" she said.'
this_is_a_string = 3.13 # such a lieA symbol (sometimes a word) that means "do something - an operation - with the two thingies on either side of the operator".
= (the assignment operator)
* (the repetition operator)
+ (the concatenation operator)
prompt = "Gimme a number, or the string gets it."
forcibly_obtained_num = input(prompt)What kind of thing is going to be in forcibly_obtained_num?
assignment operators
heading = "Wokka Wokka"
separator = "-" * 11
print(heading)
print(separator)This can be a useful operator to have - in special circumstances - but you won't find it in all languages (hi, C++!).
It's also a bit of a gotcha when used with input():
days_left = input("How many days left? ")
hours_left = days_left * 24
print("There are", hours_left, "hours left.")word_to_excitify = input("Gimme a word to get excited about: ")
excitified_word = "!!!!" + word_to_excitify + "!!!!"
print(excitified_word)super super super common term
# assume we have these variables
colour = "brown"
animal = 'unicorn' # remember, single quotes ok!
height = 3
"Look!" + ' A' + " " + colour + " " + animal + "!"
"It must be at least " + height + " tall!"
What values will the expressions on lines 6 and 8 have?
Look. An orange word.
# assume we have these variables
width = 6
height = 5
area = width * height
# your expression hereWhat expression would you create to produce the string
'The area of a 6 x 5 rectangle is 30.0'?
Guess what the "f" stands for.
No, it's not that.
first_word = "fly"
second_word = "ointment"
formatted_string = f"{first_word} in the {second_word}."
print(formatted_string)
first_word = 'hole'
second_word = 'roof'
formatted_string = f'{first_word} in the {second_word}.'
print(formatted_string)room_width_ft = 10
room_length_ft = 20
print(f'Room area: {room_width_ft * room_length_ft}.')JP being a kind namer of things
not a big fan of this - how could we make this more readable?
p1_name = 'luX0R'
p1_score = 4
p2_name = 'MizKit'
p2_score = 12
print("12345678901234567890")
print(f"{'nickname':<10}{'score':>10}")
print(f"{p1_name:<10}{p1_score:>10}")
print(f"{p2_name:<10}{p2_score:>10}")12345678901234567890
nickname score
luX0R 4
MizKit 12
these numbers just here to help us see how wide things are
As per the last Weekend Suggestions (you're following those, right?), there are references available to you. Use them.
No, really.
What did we talk about?