Chapter 2



#The process of turning an expression into an underlying data type is called evaluation.
-------------
>>> 32
32


#Notice that when the shell shows the value of a string, it puts the sequence of characters in single quotes.
--------------------
>>> "Hello"
'Hello'


#Python is actually storing the characters "3" and "2," not a representation of the number 32.
------------------
>>> "32"
'32'


#Here is an interaction with the Python interpreter that illustrates the use of variables as expressions.
------------------
>>> x = 5
>>> x
5

>>> print(x)
5


#More complex and interesting expressions can be constructed by combinĀ­ing simpler expressions with operators.
#The corresponding Python operators are +, -, *, /, and **.
#Here are some examples of complex expressions from chaos.py and convert. py
-----------------------
3.9 * x * (1 - x)
9/5 * celsius + 32


#Python also provides a way to add strings. This is called concatenation.
------------------------------
>>> "Bat" + "man"
'Batman'


#Executing the input statement caused Python to print out the prompt "Enter your name:" and then the interpreter paused waiting for user input.
--------------------------------------------------------------
>>> name = input("Enter your name: ")
Enter your name: John
>>> name
'John'


#Remember to eval the input when you want a number instead of some raw text (a string).
#Program Examples:
--------------------------------
x = eval(input("Please enter a number between 0 and 1: "))
celsius = eval(input("What is the Celsius temperature? "))


#Another Example:
-------------------------------
>>> ans = eval(input("Enter an expression: "))
Enter an expression: 3 + 4 * 5
>>> print(ans)
23

#Python evaluated this expression (via eval) and assigned the value to the variable ans.
#When printed, we see that ans got the value 23 as expected.

Beware
#The eval function is very powerful and also potentially dangerous -- code injection attack


#This is called simultaneous assignment - SEE avg2.py
---------------------------------------------------------------
# avg2.py
# A simple program to average two exam scores
# Illustrates use of multiple input
>>> def main():
               print("This program computes the average of two exam scores.")
               score1, score2 = eval(input("Enter two scores separated by a comma: "))
               average = (score1 + score2) / 2
               print("The average of the scores is:", average)

main()


''' #The simplest kind of loop is called a definite loop.
#This is a loop that will execute a definite number of times or iterate.
#A for loop is used to execute this.




#The length of the list determines the number of times the loop executes.
#Statements like the for loop are called control structures
   because they control the execution of other parts of the program.

--------------------------------------
>>> for i in [0, 1, 2, 3]:
                print(i)

0
1
2
3


>>> for odd in [1, 3, 5, 7, 9]:
              print(odd * odd)


1
9
25
49
81


#range is a built-in Python function for generating a sequence of numbers "on the fly."
-------------------------------
>>> list(range (10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


#Let's close the chapter with one more example of the programming process in action.
#SEE futval.py