Why do some of the chr() functions return weird outputs? Like chr(0) is '00'. What is this.
This is a special character. Remember that including "" will make a newline in a string, so:
>>>print("Hello,\neverybody!")
Hello,
everybody!
>>>
And, notably, chr(10)
returns '\n'
. The numerical ASCII values are in a particular order (chr(97)
is a
, and chr(98)
is b
, and so on), the low-numbered ASCII values correspond to unusual special characters like newline ('\n'
) and '\x00'
). You can see a table of ASCII values and their corresponding characters here.
How do computers actually work though? Like on a physical level.
This is a question with a very long answer, and it's the focus of CS 31. If you're interested, I suggest you take another class! In CS21, we will be focussing on how to write programs and give instructions to the computer, and not what is physically happening inside as the result of our instructions.
For the purpose of CS21, the computer could have a lot of very clever mice running around, following your instructions and speedily writing the output to the screen. (This isn't what is happening, but I like to imagine it.)
Why did you forget your Nerf gun :(
I, too, am saddened.
I'm still a bit confused about why "!awesome" < "awesome" is true
When Python compares two strings, it goes letter-by-letter until it finds a place where the strings are different. Then, whichever letter has the smaller ASCII value, that word is earlier in the lexicographic ordering of words.
So comparing "!awesome" to "awesome", we first compare "!" to "a". ord('!')
is 33, and ord('a')
is 97, so exclamation points go before 'a' in lexicographic order. This means that
>>>"!awesome" < "awesome"
True
if you want to write if statements with multiple conditions, can you just add an and between conditions?
Definitely, but you should be careful to make sure that it means what you intend.
if (x < 5):
if (x > 0):
print("That value is between 5 and 0.")
...does the same thing as:
if (x < 5) and (x > 0):
print("That value is between 5 and 0.")
elif and nested if statements seem useful, but also seem likely to become very long very fast. Is there a way to communicate the same commands to the computer but shorter?
There will be multiple ways to write programs, and how elegant and simple your solution looks will depend on how you decide to set up your variables and conditionals. As always, we'll keep adding to our toolbox for writing interesting Python programs as we go through the semester!
Are there any instances in which we would actually use the ord() and chr() functions?
Definitely. For example, you might want to use them to write a substitution cipher, which switches letters around according to a pattern that is much easier to write using the numerical values than using the individual letters.
How do you tell when to use elif and when to use else or multiple if statements?
It will depend on the plan you make for how you design your program. (When in doubt, you can always try out one way, then go back and change it. Incremental development is a useful way to help decide how you want to use multiple if statements.)
A student wrote:
Lila = "cool cs21 professor"
if Lila == "left the class to perish":
print("NOOOOOO BETRAYAL")
elif Lila == "turned to the ninja side":
print("welcome, we have cookies (but you still left the class to
perish)")
elif Lila == "nerf gun master":
print("you're awesome!")
elif Lila == "badass sword wielder":
print("you're still awesome!")
elif Lila == "cool cs21 professor":
print("thank you for being a good sport! you rock!")
Awww, thanks.