Week3 Programming Fundamentals Lecture 8
Week3 Programming Fundamentals Lecture 8
PROGRAMMING
LECTURE 8
10/16/2018 1
CREATING OBJECTS
To create an integer object with value 3 (and assign it to variable x), we can use this
statement:
x=3
Note that the type of the integer object that is created is not explicitly specified. Python also
supports a way to create objects that makes the object type explicit:
x = int(3)
print(x)
3
The function int() is called a constructor; it is used to explicitly instantiate an integer object.
The value of the object is determined by the function argument: The object created with
int(3) has value 3.
COPY RIGHT - ASST. PROF. SYED FAISAL ALI 10/22/2018 2
CREATING OBJECTS (INT, FLOAT)
x = int()
x
0
So the default value for integers is 0.
The constructor functions for the floating point, list, and string types are float(), list(), and str(), respectively.
We illustrate their usage with no argument to determine the default values for those types. For float objects,
the default value is 0.0:
y = float()
y
0.0
10/22/2018 3
CREATING OBJECTS (STR, LST)
The default values for strings and lists are, respectively, '' (the empty string) and [] (the
empty list):
>>> s = str()
>>> s
''
>>> lst = list()
To clear the screen in Python
>>> lst console
[]
>>>import os
>>>clear = lambda:
os.system('cls')
>>>clear() 10/22/2018 4
IMPLICIT TYPE CONVERSIONS
10/22/2018 5
CONVERSION FROM INT TO FLOAT !
Recall that the range of values that int objects can have is much larger than the range of
float objects. While the int type is contained in the float type, this doesn’t imply that int
values can always be converted to a float value. For example, the expression 2**10000+3
evaluates without difficulties to an int value, but its conversion to float results in an
overflow:
>>> 2**10000+3.0
Traceback (most recent call last):
File "<pyshell#139>", line 1, in <module>
2**10000+3.0
OverflowError: Python int too large to convert to C double
10/22/2018 6
EXPLICIT TYPE CONVERSIONS (INT TO FLOAT AND FLOAT TO
IN)
Type conversions can also be done explicitly using the constructor functions we just
introduced. For example, the int() constructor creates an integer from a float input argument; it
does so by removing the fractional part of the argument:
>>> int(3.4)
3
>>> int(-3.6)
-3
The float() constructor applied to an integer will change the representation to a floating point
one, unless an overflow occurs.
>>> float(3)
3.0
10/22/2018 7
EXPLICIT TYPE CONVERSIONS (STR TO INT AND INT TO STR)
The conversion from string to a number type will work only if it makes sense (i.e., the string is a
valid representation of a value of the type); otherwise it results in an error:
>>> int('3.4')
Traceback (most recent call last):
File "<pyshell#123>", line 1, in <module>
int('3.4')
ValueError: invalid literal for int() with base 10: '3.4'
>>> float('3.4')
3.4
The string constructor str(), when applied to a number, returns the string representation of the
number:
>>> str(2.72)
'2.72'
10/22/2018 8
PYTHON STANDARD LIBRARY
The core Python programming language comes with functions such as max() and sum()and
classes such as int, str, and list. While those are by no means all the built-in Python functions and
classes, the core Python language is deliberately small for efficiency and ease of-use purposes.
In addition to the core functions and classes, Python has many, many more functions and classes
defined in the Python Standard Library.
The Python Standard Library consists of thousands of functions and classes organized into
components called modules.
Each module contains a set of functions and/or classes related to a particular application domain.
More than 200 built-in modules together form the Python Standard Library.
Each module in the Standard Library contains functions and classes to support application
programming in a certain domain.
10/22/2018 9
PYTHON STANDARD LIBRARY
• Network programming
• Web application programming
• Graphical user interface (GUI) development
• Database programming
• Mathematical functions
• Pseudorandom number generators
We will eventually use all of these modules. Right now we will see how to use the math and
fraction modules.
10/22/2018 10
MODULE MATH
The core Python language supports only basic mathematical operators; we have learned
about them earlier in this chapter.
For other mathematical functions such as the square root function or the trigonometric
functions, the math module is required.
The math module is a library of mathematical constants and functions.
To use a math module function, the module must first be explicitly imported:
10/22/2018 11
MODULE MATH
The square root function sqrt() is defined in module math, but we cannot use it like this:
>>> sqrt(3)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
sqrt(3)
NameError: name 'sqrt' is not defined
Clearly, the Python interpreter doesn’t know about sqrt, the name of the square root function. We
must tell the interpreter explicitly where (i.e., which module) to look for it:
10/22/2018 12
PRACTICE PROBLEM 2.9
What is the type of the object that these expressions evaluate to?
10/22/2018 13