Course 4.1Lesson 3 of 5
Inside the computer every letter is a number. ord gives the number of a letter and chr gives the letter back, and the letters of the alphabet follow each other, so ord of a letter minus ord of a is its place counting from 0. A Caesar cipher moves every letter a few places along the alphabet, and the percent 26 operation makes the letters at the end wrap round to the start. Decoding moves them back by the same amount. A palindrome reads the same backwards and two anagrams use the same letters, and both are tested after cleaning the text. An f-string can format numbers and align columns.
Try the code and the simulations beside the lesson.
After this lesson you can
ord and chr,Inside the computer every letter is stored as a number. Two functions switch between the two:
ord("a") gives the number of a letter. It is 97 for a, 98 for b, and 122 for z.chr(97) gives the letter that has the number 97.Because the letters of the alphabet follow each other, ord("c") - ord("a") is 2: the place of c in the alphabet, counting from 0. This is the key to the codes.
Julius Caesar is said to have hidden his messages by moving every letter a few places along the alphabet. This is a Caesar cipher. With a shift of 3, a becomes d, and hello becomes khoor.
Predict first. What does the shift of 3 turn hello, world into? What will decoding it give? Then run it.
Decoding needs no new function. Moving by -3 goes back. And the % of Python never gives a negative answer for a positive divisor, so -3 works correctly.
The cipher is easy to break, as you will see in the project of the next course. But it shows the idea of a key (the shift) and a code (the rule).
A shift of 26 brings every letter back home, so shifting by 26 − s undoes a shift of s.
level, noon, and even the sentence "A man, a plan, a canal: Panama" when we ignore the spaces, the commas and the capital letters.listen and silent.To test them, clean the text first, and then compare: clean == clean[::-1] for a palindrome, and sorted(a) == sorted(b) for anagrams. The slice [::-1] reads the text backwards.
An f-string can format a number or align a column:
{price:.2f} shows two digits after the point.{name:<8} puts the text at the left in a space of 8 characters.{marks:>6} puts it at the right in a space of 6.{n:,} puts a comma between the thousands.{x:.1%} shows a fraction as a percentage.% 26. z shifted by 3 becomes a symbol and not c.A to Z separately."Level" is not equal to its reverse because of the capital L.sorted(a) == sorted(b) for texts with spaces. Clean first.: in the format or the wrong side for < and >.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
ord() gives the number of a letter, and chr() gives the letter back.(place + shift) % 26. The % 26 wraps the alphabet round.[::-1], anagrams with sorted..2f, <8, >6, , and %.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
hello into?% 26 needed?{x:>6} do?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.