Chapter 3



#Python provides a special function called type that tells us the data type (or "class") of any value.
------------------------------------
>>> type(3)
class 'int'
>>> type(3.14)
class 'float'
>>> type(3.0)
class 'float'

>>> myInt = -32
>>> type(myInt)
class 'int'

>>> myFloat= 32. 0
>>> type(myFloat)
class 'float'


#Consider the following interaction with Python:
------------------------------------
>>> 3 + 4
7
>>> 3.0 + 4.0
7.0
>>> 3 * 4
12
>>> 3.0 * 4.0
12. 0
>>> 4 ** 3
64
>>> 4.0 ** 3
64. 0
>>> 4.0 ** 3.0
64. 0
>>> abs(5)
5
>>> abs(-3.5)
3.5


#For the most part, operations on floats produce floats, and operations on ints produce ints.
#Python (as of version 3.0) provides two different operators for division.
#The usual symbol (/) is used for regular division and a double slash (//)
#is used to indicate integer division.

-----------------------------------
>>> 10 / 3
3.3333333333333335
>>> 10.0 / 3.0
3.3333333333333335
>>> 10 / 5
2.0
>>> 10 // 3
3
>>> 10.0 // 3.0
3.0
>>> 10 % 3
1
>>> 10.0 % 3.0
1.0


#Notice that the / operator always returns a float. This is called an explicit type conversion.
------------------------------------
>>> int(4.5)
4
>>> int(3.9)
3
>>> float(4)
4.0
>>> float(4.5)
4.5
>>> float(int(3.3))
3.0
>>> int(float(3.3))
3
>>> int(float(3)) 3


#The built-in round function, which rounds a number to the nearest whole value.
---------------------------------------
>>> round(3.14)
3
>>> round(3.5)
4


#Round off a float to a specific number of digits:
-------------------------------
>>> pi = 3.141592653589793
>>> round(pi, 2)
3.14
>>> round(pi,3)
3.142


#The type conversion functions int and float can also be used to convert strings of digits into numbers.
--------------------------------
>>> int("32")
32
>>> float("32")
32.0
>>> float("9.8")
9.8


#Program to compute the factorial of a number.
#Illustrates for loop with an accumulator.
#SEE factorial.py

------------------------------------
def main():
      n = int(input("Please enter a whole number: "))
      fact =1
      for factor in range(n,1,-1):
           fact = fact * factor
      print("The factorial of", n, "is", fact)


main()