c10552: intro to computation - mit esp › download › 38d76032-c73a-431c-be8a...c10552: intro to...
TRANSCRIPT
![Page 1: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/1.jpg)
C10552: Intro to Computation
Lecture 2July 17, 2016
![Page 2: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/2.jpg)
Lecture 1 Recap
- Loops are useful for repeating codefor i in range(10): print i
- If statements are useful for conditional codeif current_temp > 80: print “It’s really hot out there!”
![Page 3: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/3.jpg)
Lecture 2 Overview
- HW solutions- One more type of loop: while
- “Run while condition is true”
- Lists- Functions- Live coding
![Page 4: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/4.jpg)
Homework: finding prime divisors
The following paragraph is an excerpt from a book published in 1874:
Can the reader say what two numbers multiplied together will produce the number 8,616,460,799? I think it unlikely that anyone but myself will ever know; for they are two large prime numbers, and can only be rediscovered by trying in succession a long series of prime divisors until the right one be fallen upon.
Find these two numbers.
![Page 5: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/5.jpg)
Homework: finding prime divisors
Plan for the code:
- Use the writer’s suggestion: try every possible number!
![Page 6: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/6.jpg)
Homework: finding prime divisors
Plan for the code:
- Use the writer’s suggestion: try every possible number!- If any number less than 8,616,460,799 divides it evenly,
print it.
![Page 7: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/7.jpg)
Homework: finding prime divisors
Plan for the code:
- Use the writer’s suggestion: try every possible number!- If any number less than 8,616,460,799 divides it evenly,
print it.
- We’ll need…
![Page 8: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/8.jpg)
Homework: finding prime divisors
Plan for the code:
- Use the writer’s suggestion: try every possible number!- If any number less than 8,616,460,799 divides it evenly,
print it.
- We’ll need… a for loop, an if statement, and the modulo operator
![Page 9: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/9.jpg)
Live coding!
Plan for the code:
- Use the writer’s suggestion: try every possible number!- If any number less than 8,616,460,799 divides it evenly,
print it.
- We’ll need… a for loop, an if statement, and the modulo operator
![Page 10: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/10.jpg)
Live coding!
>>> for i in xrange(1, 8616460799):... if 8616460799 % i is 0:... print i... 18968196079
![Page 11: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/11.jpg)
The while loop
![Page 12: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/12.jpg)
The while loop
- “Run while condition is true”
red_sox = 2; yankees = 1while red_sox > yankees: print “go sox” # runs forever!
Condition is checked before each iteration.
![Page 13: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/13.jpg)
The while loop
- “Run while condition is true”
red_sox = 2; yankees = 1while red_sox > yankees: print “go sox” # runs forever!
Anything after # is a comment.It is for your own eyes.
The computer will ignore it.
![Page 14: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/14.jpg)
The while loop
- “Run while condition is true”
red_sox = 2; yankees = 1while red_sox > yankees: print “go sox” # runs forever!
- While loops do not have a predetermined number of iterations. (unlike for loops)
Anything after # is a comment.It is for your own eyes.
The computer will ignore it.
![Page 15: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/15.jpg)
The while loop: example
- What is the output of the following code?n = 10while n < 100: n = n * 2 print n
![Page 16: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/16.jpg)
The while loop: example
- What is the output of the following code?n = 10while n < 100: n = n * 2 print n204080160
![Page 17: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/17.jpg)
The while loop: example
- What is the output of the following code?n = 10while n < 100: n = n * 2 print n204080160 what?
![Page 18: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/18.jpg)
The while loop: example
- What is the output of the following code?n = 10while n < 100: n = n * 2 print n204080160 what?
Remember that the condition is
checked before each iteration!
![Page 19: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/19.jpg)
Lists
- One of the most important tools in programming- Simple syntax in Python:
my_list = [1,2,3,4,5]- range(...) is actually a list!
>>> print range(10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- You can iterate over lists
![Page 20: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/20.jpg)
Lists
- You can iterate over lists>>> my_friends = ["jim", "al", "mary"]>>> for friend in my_friends:... print "hello", friend... hello jimhello alhello mary
![Page 21: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/21.jpg)
Lists
- You can iterate over lists>>> my_friends = ["jim", "al", "mary"]>>> for friend in my_friends:... print "hello", friend... hello jimhello alhello mary
If you are not iterating over range(...), use a descriptive name instead of i
![Page 22: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/22.jpg)
Lists
- You can access list elements>>> my_friends = ["jim", "al", "mary"]>>> print my_friends[0]jim>>> print my_friends[1]al >>> print my_friends[2]mary
Note that numbering starts from 0
![Page 23: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/23.jpg)
Modifying lists- You can modify elements in-place
>>> my_friends = ["jim", "al", "mary"]>>> my_friends[0] = “joe”>>> my_friends['joe', 'al', 'mary']
![Page 24: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/24.jpg)
Modifying lists- You can append to lists
>>> my_friends = ["jim", "al", "mary"]>>> my_friends.append("alice")>>> my_friends['jim', 'al', 'mary', 'alice']
- You can add two lists to each other>>> your_friends = ["jack", "mary", "jessica"]>>> my_friends + your_friends['jim', 'al', 'mary', 'alice', 'jack', 'mary', 'jessica']
![Page 25: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/25.jpg)
Modifying lists- You can append to lists
>>> my_friends = ["jim", "al", "mary"]>>> my_friends.append("alice")>>> my_friends['jim', 'al', 'mary', 'alice']
- You can add two lists to each other>>> your_friends = ["jack", "mary", "jessica"]>>> my_friends + your_friends['jim', 'al', 'mary', 'alice', 'jack', 'mary', 'jessica'] Repetitions are allowed!
![Page 26: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/26.jpg)
Lists are flexible!- They can include any type of data…
>>> my_favorite_numbers = ["one", 2, 42.0]- … even other lists…
>>> your_fav_numbers = [5, 100, 65536]>>> our_fav_numbers = [my_fav_numbers, your_fav_numbers]>>> our_fav_numbers[['one', 2, 42.0], [5, 100, 65536]]
- … even themselves!
>>> my_favorite_numbers.append(my_favorite_numbers)>>> my_favorite_numbers['one', 2, 42.0, [...]]>>> my_favorite_numbers[3] is my_favorite_numbersTrue
![Page 27: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/27.jpg)
Lists are flexible!- They can include any type of data…
>>> my_favorite_numbers = ["one", 2, 42.0]- … even other lists…
>>> your_fav_numbers = [5, 100, 65536]>>> our_fav_numbers = [my_fav_numbers, your_fav_numbers]>>> our_fav_numbers[['one', 2, 42.0], [5, 100, 65536]]
- … even themselves!
>>> my_favorite_numbers.append(my_favorite_numbers)>>> my_favorite_numbers['one', 2, 42.0, [...]]>>> my_favorite_numbers[3] is my_favorite_numbersTrue
![Page 28: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/28.jpg)
Lists are flexible!- They can include any type of data…
>>> my_favorite_numbers = ["one", 2, 42.0]- … even other lists…
>>> your_fav_numbers = [5, 100, 65536]>>> our_fav_numbers = [my_fav_numbers, your_fav_numbers]>>> our_fav_numbers[['one', 2, 42.0], [5, 100, 65536]]
- … even themselves!
>>> my_favorite_numbers.append(my_favorite_numbers)>>> my_favorite_numbers['one', 2, 42.0, [...]]>>> my_favorite_numbers[3] is my_favorite_numbersTrue
![Page 29: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/29.jpg)
Strings: pretty much like lists- Time to formally define strings!
- You have seen a string before: print “hello world”- Most of the operations are the same as lists…
>>> my_string = "hello world">>> my_string[0]'h'>>> your_string = “123”>>> my_string + your_string'hello world123'
- … but a string only contains characters, and it cannot be modified in-place
>>> my_string[0] = 'a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment
![Page 30: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/30.jpg)
Strings: pretty much like lists- Time to formally define strings!
- You have seen a string before: print “hello world”- Most of the operations are the same as lists…
>>> my_string = "hello world">>> my_string[0]'h'>>> your_string = “123”>>> my_string + your_string'hello world123'
- … but a string only contains characters, and it cannot be modified in-place
>>> my_string[0] = 'a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment
i.e. string variable
![Page 31: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/31.jpg)
Strings: pretty much like lists- A string only contains characters, and it cannot be modified in-place
>>> my_string[0] = 'a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment
- You need to build a new string if you want to modify it>>> english_word = "hello">>> german_word = "">>> for letter in english_word:... if letter is 'e':... german_word = german_word + "a"... else:... german_word = german_word + letter... >>> german_word'hallo'
![Page 32: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/32.jpg)
Functions- We sometimes have to do the same set of things over, and
over, and over again…- Functions are a great way to “pack” your code
def my_function(input_value):# Multiplies input_value by 2.
return input_value * 2
![Page 33: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/33.jpg)
Functions- We sometimes have to do the same set of things over, and
over, and over again…- Functions are a great way to “pack” your code
def my_function(input_value):# Multiplies input_value by 2.
return input_value * 2
Treat this as a black box.
![Page 34: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/34.jpg)
Functions- Abstraction: very important concept in coding- don’t reinvent the wheel!
def shift_letter(letter):# Given a letter, returns the next# letter in the alphabet.
return chr(ord(letter)+1)
![Page 35: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/35.jpg)
Functions- Abstraction: very important concept in coding- don’t reinvent the wheel!
def shift_letter(letter):# Given a letter, returns the next# letter in the alphabet.
return chr(ord(letter)+1)
You may have no idea what the red outlined text means. That is fine. You only need to know what this function does.
![Page 36: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/36.jpg)
Functions- Abstraction: very important concept in coding- don’t reinvent the wheel!
def shift_letter(letter):# Given a letter, returns the next# letter in the alphabet.
return chr(ord(letter)+1)
You may have no idea what the red outlined text means. That is fine. You only need to know what this function does.
![Page 37: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/37.jpg)
Functions- Let’s test our black box…
>>> shift_letter('a')'b'>>> shift_letter('v')'w'>>> shift_letter('z')'{'>>> shift_letter('d')'e'>>> shift_letter('m')'n'
![Page 38: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/38.jpg)
Functions- Let’s test our black box…
>>> shift_letter('a')'b'>>> shift_letter('v')'w'>>> shift_letter('z')'{'>>> shift_letter('d')'e'>>> shift_letter('m')'n'
![Page 39: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/39.jpg)
Functions- Let’s test our black box…
>>> shift_letter('a')'b'>>> shift_letter('v')'w'>>> shift_letter('z')'{'>>> shift_letter('d')'e'>>> shift_letter('m')'n'
Issue: Function does not return a letter for ‘z’
![Page 40: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/40.jpg)
- Let’s test our black box…>>> shift_letter('a')'b'>>> shift_letter('v')'w'>>> shift_letter('z')'{'>>> shift_letter('d')'e'>>> shift_letter('m')'n'
Functions
Issue: Function does not return a letter for ‘z’
Solution: Modify function so that ‘z’ maps to ‘a’
![Page 41: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/41.jpg)
Functionsdef shift_letter(letter):# Given a letter, returns the next# letter in the alphabet.
return chr(ord(letter)+1)
![Page 42: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/42.jpg)
Functionsdef shift_letter(letter):# Given a letter, returns the next# letter in the alphabet.
return chr(ord(letter)+1)
def shift_letter(letter):# Given a letter, returns the next# letter in the alphabet.
return chr((ord(letter)-97+1)%26+97)Exercise: understand how this function works.Hint: ord(‘a’) is equal to 97. Solution at the end.
![Page 43: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/43.jpg)
Functions>>> shift_letter('a')'b'>>> shift_letter('b')'c'>>> shift_letter('z')'a'>>> shift_letter('w')'x'>>> shift_letter('x')'y'
![Page 44: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/44.jpg)
Functions>>> shift_letter('a')'b'>>> shift_letter('b')'c'>>> shift_letter('z')'a'>>> shift_letter('w')'x'>>> shift_letter('x')'y'
All set!
![Page 45: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/45.jpg)
Live coding!
Using shift_letter, write a simple encryption algorithm that shifts each letter by a given number (not necessarily 1). Assume the input text is lowercase.
def encrypt(input_text, shift):# Shifts each letter in input_text# by the given shift value.# Returns the new string.
![Page 46: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/46.jpg)
We’re done!See you next week!
![Page 47: C10552: Intro to Computation - MIT ESP › download › 38d76032-c73a-431c-be8a...C10552: Intro to Computation Lecture 2 July 17, 2016 Lecture 1 Recap - Loops are useful for repeating](https://reader033.vdocuments.us/reader033/viewer/2022060412/5f10e09b7e708231d44b3ebf/html5/thumbnails/47.jpg)
Solution to Exercisedef shift_letter(letter):
# ord(...) returns 97 for ‘a’.# Subtract that from letter# to get the alphabetical rank of letter.# Note that this will be using zero-indexing# (i.e. ‘a’ will be 0)alpha_rank = ord(letter) - 97# shift by 1new_alpha_rank = alpha_rank + 1# use the modulo operator so 26 maps to 0new_alpha_rank = new_alpha_rank % 26# ord() and chr() are inverses of each other.# Add the subtracted 97 back and return using chr.return chr(new_alpha_rank + 97)