CS 114

Introduction to computer algorithms, problem specification, correctness, computer structure, sets, truth tables, functions, and iteration. Presentation of basic principles of a current programming language.

Objective 1:

Which of the following are not used in expressions

  • variables
  • statements
  • operators
  • literals

For loop scenario question

Counted loops

 

#A simple program illustrating chaotic behavior where there are two numbers input and results displayed #in a column format
# Dr. Tonya Pierce
#chaos2.py

def main(): #This is the function definition. Every program you write will have a main function definition. #The purpose of this function is to start the program and provide the flow of the overall function. We will #discuss this further at a later date. For now, make sure you include a main function definition in every #program.
    print("This program illustrates chaotic behavior")# This is an output statement. The items in quotes #are literal values. This means the program will print literally what is in the quotes.
    x = eval(input("Enter a number between 0 and 1: "))#These are input statements. Again, what is in #quotes will be literally displayed to the user. Here you are providing instructions to the user to tell them #what they are to input. The input outside the parentheses tells you that you are asking for input. The #eval is used to tell Python that you are expecting a numeric value. All input is treated as alphanumeric. #The eval tells it to convert it to numeric. The eval is only used if you are expecting numeric values that #you expect to perform calculations on.
    y = eval(input("Enter a number between 0 and 1: "))
    for i in range(10): #This is your for loop. Notice that everything that is part of the for loop is indented #under the for loop.
             x = 3.9 * x * (1-x)#These are your calculations
             y = 3.9 * y * (1-y)
    print(x,"                      ",y)#This is your output. Notice the spacing between x and y. Simply adding #spaces will not work, as Python will ignore the spaces, unless you put those spaces in quotation  #marks, and then it will become a literal. Also notice that this is not indented under the for loop but is #indented under the def main() function. This is because the output is part of the main definition but #not part of the loop.

main()#This is the call to function statement. This is saying to now implement the main definition that #was previously defined. Notice that it is left-aligned under the main definition. This is because it is not #part of the main definition. Notice that everything that is part of the main definition is indented under #the main definition.

 

  • A counted loop is designed to iterate a specific number of times.

Objective 2:

Numeric operations explained

Operations on floats produce floats, and operations on ints produce ints. However, in the case of division, things get a bit more interesting. As the table shows, Python provides two different operators for division. The usual symbol / is used for "regular" division and a double slash // is used to indicate integer or floor division.

operator operation
+ addition
- subtraction
* multiplication
/ float division
** exponentiation
abs() absolute value
// integer division
% remain

The / operator always returns a float. Regular division often produces a fractional result, even thought the operands may be ints. Python accomodates this by always returning a floating point number.

To get a division that returns an integer result, you can use the integer division (floor division) operation //. Integer division always produces an integer. Think of integer division as "gozinta". The expression 10/3, 3 goes into 10 three times, with a remainder of one.

Numeric operations

  • 10/3
    3.3333335
  • 10*3
    30
  • 10//3
    3
  • 10**3
    1000
  • abs(3-10)
    7
  • 10%3
    1