IPND Notes

The purpose of this site is to show the notes that I, Felipe Fernandez, have taken during my study of the Intro to Programming Nanodegree course at Udacity.

Felipe's Projects Home
IPND Home
Section 1 - HTML/CSS
Section 2 - Programming
Section 3 - Object Oriented Programming
Section 4 - Allow Comments
Section 5 - Advanced Python Topics

Section 2 - Programming

Understanding of Programming

Programming is one of the core aspects of computer science. Programming means to write a precise sequence of steps, or instructions, for a computer to execute a program. These sequence of steps need to be precise for the computer to interpret them correctly and avoid ambiguity or misinterpretation. These precise sequence of steps are also known as instructions because they tell the computer what to do. These steps are written in a programming language which the computer can understand. Programming languages, like Python, Java, and C++, come with their own type of grammar, or syntax. Natural languages, such as English, Chinese, and Spanish, has too many words and can be easily misinterpreted which is why it is not used to program. When a computer executes a program, the computer is useful. It is able to execute instructions really, really fast. Without the program, the computer can't do anything isn't anything except an expensive paperweight. Examples of programs are web browsers, computer games, and compilers.

Understanding of Functions

If the same set of instructions need to be used more than once, functions need to be used to avoid repetition. Functions, or procedures, takes one or more inputs and returns one or more outputs. The "def" is used to make, or define, a function. This "def" is followed by the name of the function and the inputs it takes in the parentheses and ends in a colon. The "return" is used to return outputs. If no "return" is used, the function produces no output. Once a function returns the outputs, the function ends. When a function is used, it is said that the function is "called". When a function is called, it passes the value of the inputs. When the inputs being passed are variables, it is not the variable but the value of the variable that gets passed. Functions can also call other functions. The output of one function can also be the input of another function.
Defining a Function:

def add(a, b):
  return a + b
Call and Print a Function 1:

a = 1
b = 2
print add(a, b)
Call and Print a Function 2:

a = 1
b = 2
print add(a, add(a, b))

Basic Programming Knowledge

Commenting is a useful tool for programming. It helps other people who are reading the program to understand how it works. In Python, commented text begin with a pound sign (#), or hashtag as it is referred to in Twitter.

When programming, it is important to understand the order of operations. If not, you may get a result you weren't expecting. The answer for (2 * 2) + 1 is different from the answer of 2 * (2 + 1). A short summary of the order of operations is that first equations in parentheses are evaluated, then multiplication and division, and lastly addition and subtraction. When doing arithmetic in Python, a decimal value must be in the expression to receive a decimal expression. For example 1 / 2 will give you the answer 0. If we have instead 1.0 / 2, we will receive 0.5 as the answer.

Syntax for Python Arithmetic Expressions:
Expression -> Expression Operator Expression
Expression -> Number
Expression -> (Expression)
Operator -> +, -, *, /, ...
Number -> 0, 1, 2, 3, ...

It is cool that a program can use values to do calculations. But when someone else looks at the program, they won't know what the meaning of 1 + 2. What is it adding? Python allows use to use names for values to gives these value meaning. We can have the variable apples = 1 and the variable oranges = 2. We say we assign the value of 1 to apples and the value of 2 to oranges. We can have a variable fruits equal to the sum of apples and oranges as so: fruits = apples + oranges. The syntax for assigning a variable is Name = Expression, where name is the name of the variable. The reason these are called variables is because these value can vary. From the example before, you can reassign apples as apples = 2. So then the value of fruits will now be 4.

 
        apples = 1
        oranges = 2
        fruit = apples + oranges  # fruit will equal 3
        apples = 2
        fruit = apples + oranges  # fruit will equal 4
      

A string is a character sequence surrounded by quotes. This is useful for printing and manipulating characters. The quotes can be single quotes ' ', or double quotes " ". You can set a variable to a string as such: a = "Sup?!". You can use the + symbol to concatenate strings and the * symbol to repeat strings. It is important to add spacing in the string when you want a space.

        a = "Hey"
        b = " Bro"
        c = a + b + "!" * 3
        print c  # prints Hey Bro!!!
      

You can also get characters from string using the indexing method. For the a = "Hey", a[0] will be H, a[1] will be e, a[3] is y, and a[4] will be an error. You can use indexing to maninpulate characters within a string. Using negative numbers will index from the end of the string. The a[-1] will be y, a[-2] is e, and so on. To select a substring the syntax is string[start:stop]. Examples using the above code are c[1:1], which will be blank, c[2:3] will be y, c[2:5] will be y Br, c[5:-2] will be ro!. Leaving the start of the string[start:stop] syntax empty will start from the beginning of the string and leaving the stop empty will end and the end of the string. There is a function to find a string withing a string. The syntax for it is string.find('string') and it will return the index of where the string was found. Using c.find('Br') will return 4. If the string was not found, it will return -1. The second syntax variation of the find function is string.find('string', start), with start being the starting position of the string. The function c.find('Br', 5) will then result as -1 because Br does not exist after position 5 in string c.

If/else statements are used to make comparisons between the inputs and outputs. If the test expression is true, then the block, or sequence of instrunctions, is executed. If it is false, then the else is executed. Else can be blank if there is no block to run and you just want to jump out of the if statement. For while loops, if the test expression is true, then the block is executed until the test expression is false. The difference between if/else and while is that if/else is executed once for a true test expression and while loops are executed as long as the test expression is true, which can be forever. Break statements are used to get out the while loop. Below are the syntax of if/else statements, while loops, and a while loop with a break:

if Test_Expression:  
    Block
else:
    Block
while Test_Expression:
    Block
while Test_Expression: 
    Block
if Test_Expression:  
    break
Code After While Loop

Lists work much like strings, but instead of character sequences it has a sequence of anything. As with strings, you can use indexing to return items within a list. List can store different data types such as characters, strings, numbers, even other lists. Lists within lists are called nested lists. With lists, one can also change the value within an element of a list without creating a new list. This process is called mutation. Strings do not support mutation. Elements can be appended to a list. The syntax is list.append(element). List can also be concatenated, which creates a new list. The syntax is list + list. If you want to know the length of the number of elements in a list, you can use len. The syntax is len(list). Len can also be use on a string as such: len('string').

For loops are used to loop through elements in a list. The syntax is below. Name is given to the first element in the list which is followed by in, the list, and a colon. This line is followed by a block inside the for loop.

for Name in List:
    Block