The goal is to learn to write some basic python programs.

Basics

In this section we introduce loops and function definitions.

(a)
The Python syntax for a for loop is as follows:
==============================
 
 
for variable in iterable:  
        code  
 
==============================

In the above code, for indicates the start of a for loop, variable is just a variable name of your choice, in is a keyword, and iterable is a Python object that can be iterated over, like a list. This Python code almost reads as an English statement. It is saying, for each item in iterable, whose value we assign to variable each time, perform the instructions given by code.

The example below prints the values 0 to 9.

(b)
Thy Python syntax for a while loop is as follows:
                                                                  

                                                                  
==============================
 
 
while condition:  
        code  
 
==============================

In the above code, while indicates the start of the while loop and condition is the statement that is checked to see if the code in the while loop should be exectued (the while loop will end whenever condition is false). Note that for a while loop, if variables related to condition are not updated within the loop, it is possible to create an infinite loop.

In many cases for and while loops can be used to execute the same computation. Typically a for loop is used if it is known in advance how many iterations are going to be needed to complete some computation. A while loop, on the other hand, is often used when the number of iterations needed is not obvious, but a stopping condition is clear.

The example below prints the values of 0 to 9.

(c)
If/Else statements help direct your program if there are multiple cases and use the following syntax:
     ==============================
      
if condition:  
        code1  
elif:  
        code2  
else:  
        code3  
==============================

The example below computes the absolute value of x.

(d)
A Python function will have the following syntax:
==============================
 
def functionName(inputs):  
        code  
        return variable  
==============================

The key word def denotes the beginning of a function definition. Using the same guidelines for variable names, functionName is your choice for the name of the function and will be used to ‘call’ it as needed. A collection of comma separated values in inputs will act as the inputs to the function. The portion labeled code is simply the set of instructions to be completed whenever the function is called. Finally, we have the line with the keyword return. So far, we have been using print to see the result of a calculation on the screen. In the case of a function, we will likely want the resulting output to be used in another calculation. The keyword return provides us with this ability. Any variable after return represents the output value of the function and can be assigned to another variable or passed into another function without displaying the value on the screen. The keyword print, on the other hand, prints the value to the screen but does not provide us with any data that can be assigned elsewhere. When using print, the computer essentially forgets what it just computed.

Below is an example of the function that computes the square of any number n.

Problems

(a)
Write a function that computes .
(b)
Write a function that computes the derivative of .
(c)
Write an if/else statement that returns a 1 if a number is positive, 0 if is zero, and -1 if is negative.
(d)
Write a for loop that computes “”.
(e)
Write a function that takes in a positive integer as an input and returns the sum “”.
(f)
Write a for loop that counts the number of e’s in the string “new york city college of technology”.