Aside

Udacity CS 101: Homework 3

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!

Advertisements

Udacity CS 101: Lists Part III (Loops and Pop)

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:

print num

# this will output:

# 1

# 3

# 5

# [7,9]

Continue reading

Udacity CS 101: Lists Part II (append, +, len)

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']listSay.append('You')

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:

listSay = ['How','Are']

listGreet = ['You','Today']

hello = listSay + listGreet

print hello # will give you ['How','Are','You','Today']

# this however creates a new list and assigns it to the variable hello

Continue reading

Udacity CS 101: Lists Part I ([], Mutation, Alias)

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[0] # refers to the character 'H'

listSay[0] # 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!

Continue reading

Udacity CS 101: Loops

Loops go in loops:

Decision

Loops, they loop.
Image courtesy of http://www.thelineofbestfit.com

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 while loop:

i = 0
while i < 10:

print i
i = i + 1

Continue reading

Udacity CS 101: Conditional Statements

A lot of times we face situations where we have to make decisions:

Decision

To be or not to be, that is the question.
Image courtesy of http://vipdictionary.com

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:

def to_the_beach(temp):

if temp == 30:

return go

Continue reading

Udacity CS 101: Procedures Part II

Now that you know what are procedures and how to make them, we’ll go through how to use them

def the_last_letter(s):

return s[-1]

welcome = "hello"

print the_last_letter(welcome)

The procedure described here basically takes a string named s as an input and returns the last letter of the string as the output.

Continue reading