Just completed Homework 3, which really did require quite a bit of thinking, especially at the Sudoku problem! But the sense of achievement when you finally solve the problem feels great! I think the resources on the forums are great, lots of help from people who are very smart. But if you feel shy posting there, feel free to comment below. I’ll try my best to help!
We learned about the
while loops, which tests an expression and executes the code block below. Here we’ll meet a new kind of loop, called the
for loop. The for loop is especially useful for list:
oddNumbers = [1,3,5,[7,9]]
for num in oddNumbers:
# this will output:
There is a specific method called append() provided by lists. It is used to append an element to a list. This mutates the original list (it does not create a new list.:
listSay = ['How','Are']
print listSay # will give you ['How','Are','You']
This might remind you of the ‘+’ operator we use for joining strings, lists can use this operator too:
listGreet = ['You','Today']
hello = listSay + listGreet
print hello # will give you
# this however creates a new list and assigns it to the variable hello
We have learned about strings, and how strings are basically sequences of characters. In this new unit, Professor Evans introduces us to a new kind of data structure, called list. Lists are much more accommodating, they are sequences of anything. Here is an example of a list assignment:
stringSay = 'Hello!'
listSay = ['H','e','l','l','o','!']
Like how strings are recognized by the quotes, lists are identified by square brackets, and the elements are separated by commas. Remember how we could use square brackets to call out a particular character or a sequence of characters in the string, we can do that to lists too!
stringSay # refers to the character 'H'
listSay # refers to the character 'H'
stringSay[1:3] # refers to the string 'el'
listSay[1:3] # refers to a list ['e','l']
Now to the coolest part of lists, list can contain anything! Yes really anything: lists can contain numbers, strings, characters and even lists!
Loops go in loops:
Yea I probably don’t make much sense, but loops do exactly what they mean! Loops continue executing the block until a specific expression stops it. It is possible to write an infinite loop that keeps going on and evaluating the same block of code, but that will probably eat up your computer memory and crash. A simple kind of loop is called the
i = 0
while i < 10:
i = i + 1
A lot of times we face situations where we have to make decisions:
We may have to decide simple stuff like, “Should I bring and umbrella out?”, or more complex questions like “Should I, based on the historical pattern of rainfall, and weather forecasts from twenty different sources, go to the beach next Sunday?”. The complex question presents you with conditions, and you will base your decisions on those conditions. For example, if the weather forecast says that it will be 30 degrees Celsius, which means that it is likely to be sunny on Sunday, I will go to the beach. Written in Python, it may look like this:
if temp == 30:
Now that you know what are procedures and how to make them, we’ll go through how to use them
welcome = "hello"
The procedure described here basically takes a string named
s as an input and returns the last letter of the string as the output.