Unit - 1 - III Cs - Python
Unit - 1 - III Cs - Python
UNIT - I
Python – origins – features – variable and assignment - Python basics – statement and syntax –
Identifiers – Basic style guidelines – Python objects – Standard types and other built-in types – Internal
types – Standard type operators – Standard type built-in functions.
PYTHON
Python is an elegant and robust programming language that delivers both the power and general
applicability of traditional compiled languages with the ease of use of simpler scripting and
interpreted languages.
ORIGINS
Work on Python began in late 1989 by Guido van Rossum, then at CWI (Centrum Wiskunde
Informatics, the National Research Institute for Mathematics and Computer Science) in the
Netherlands. It was eventually released for public distribution in early 1991.
At the time, van Rossum was a researcher with considerable language design experience with the
interpreted language ABC.
ABC programming language is said to be the predecessor of Python language, which was capable of
Exception Handling and interfacing with the Amoeba Operating System.
Python has come a long way to become the most popular coding language in the world. Python has
just turned 30 and just recently at pycon22(python conference) a new feature was released by
Anaconda foundation it’s known as pyscript with this now python can be written and run in the
browser like JavaScript.
1
FEATURES
There are many features of Python language, but those that make it so popular and application-
oriented in many domains are listed below.
1. High Level
The languages are higher-level data structures that reduce the "framework" development time that
was once required.
Useful types like Python's lists (resizable arrays) and dictionaries (hash tables) are built into the
language. Providing these crucial building blocks in the core language encourages their use and
minimizes development time as well as code size, resulting in more readable code.
Because there is no one standard library for heterogeneous arrays (lists in Python) and hash tables
(Python dictionaries or "dicts" for short) in C, they are often implemented and copied to each new
project. This process is messy and error prone.
2. Object Oriented
Object-oriented programming (OOP) adds another dimension to structured and procedural
languages where data and logic are discrete elements of programming.
Python is an object-oriented (OO) language, all the way down to its core. However, Python is not
just an OO language like Java or Ruby. It is actually a pleasant mix of multiple programming
paradigms.
3. Scalable
In Python, where we can grow our code from project to project, add other new or existing Python
elements, and reuse code at your whim.
Python encourages clean code design, high-level structure, and "packaging" of multiple
components, all of which deliver the flexibility, consistency, and faster development time required
as projects expand in breadth and scope.
The term "scalable" is most often applied to measuring hardware throughput and usually refers to
additional performance when new hardware is added to a system.
4. Extensible
As the amount of Python code increases in our project, we will still be able to organize it logically
by separating our code into multiple files, or modules, and be able to access code from one
module and attributes from another.
2
This type of extensibility in a language provides engineers with the flexibility to add-on or
customizes their tools to be more productive, and to develop in a shorter period of time.
Although this feature is self-evident in mainstream third-generation languages (3GLs) such as C,
C++, and even Java, the ease of writing extensions to Python in C is a real strength of Python.
Furthermore, tools like PyRex, which understands a mix of C and Python, make writing
extensions even easier as they compile everything to C for you.
Python extensions can be written in C and C++ for the standard implementation of Python in C
(also known as CPython). The Java language implementation of Python is called Jython, so
extensions would be written using Java.
Finally, there is IronPython, the C# implementation for the .NET or Mono platforms. We can
extend IronPython in C# or Visual Basic.NET.
5. Portable
Python is written in C, and because of C's portability, Python is available on practically every type
of platform that has an ANSI C compiler.
Although there are some platform-specific modules, any general Python application written on
one system will run with little or no modification on another.
Portability applies across multiple architectures as well as operating systems.
6. Easy to Learn
Python has relatively few keywords, simple structure, and a clearly defined syntax. This allows
the user to pick up the language in a relatively short period of time.
7. Easy to Read
Python code is much more clearly defined and visible to the eye. Python does not give as much
flexibility to write obfuscated code compared to other languages, making it easier for others to
understand our code faster and vice versa.
8. Easy to Maintain
Maintaining source code is part of the software development lifecycle. Our software usually
continues to evolve until it is replaced or obsoleted. Quite often it lasts longer than a
programmer's stay at a company. Much of Python's success is that source code is fairly easy to
maintain, dependent, of course, on size and complexity.
3
9. Robust
Python provides "safe and sane" exits on errors, allowing the programmer to be in the driver's
seat.
When our Python crashes due to errors, the interpreter dumps out a "stack trace" full of useful
information such as why our program crashed and where in the code (file name, line number,
function call, etc.) the error took place. These errors are known as exceptions.
These exception handlers can take steps such as defusing the problem, redirecting program flow,
perform cleanup or maintenance measures, shutting down the application gracefully, or just
ignoring it.
Python's robustness is beneficial for both the software designer and the user. There is also some
accountability when certain errors occur that are not handled properly.
5
6. Statements (code blocks) grouped as suites.
7. Suites delimited via indentation.
8. Python files organized as modules.
1. Comments (#)
Python comment statements begin with the pound sign or hash symbol (#). A comment can begin
anywhere on a line. All characters following the # to the end of the line are ignored by the
interpreter.
2. Continuation (\)
Python statements are, in general, delimited by NEWLINEs, meaning one statement per line.
Single statements can be broken up into multiple lines by use of the backslash.
The backslash symbol (\) can be placed before a NEWLINE to continue the current statement onto
the next line.
# check conditions
if (weather_is_hot == 1) and \
(shark_warnings == 0):
send_goto_beach_mesg_to_pager()
There are two exceptions where lines can be continued without backslashes.
1. A single statement can take up more than one line when enclosing operators are used, i.e.,
parentheses, square brackets, or braces.
2. When NEWLINEs are contained in strings enclosed in triple quotes.
# display a string with triple quotes
print '''hi there, this is a long message for you
that goes over multiple lines... you will find
out soon that triple quotes in Python allows
this kind of fun! it is like a day on the beach!'''
3. Multiple Statement Groups as Suites (:)
Groups of individual statements making up a single code block are called "suites" in Python.
Compound or complex statements, such as if, while, def, and class, are those that require a
header line and a suite.
Header lines begin the statement (with the keyword) and terminate with a colon and are followed
by one or more lines that make up the suite. The combination of a header line and a suite as a
clause.
6
4. Suites Delimited via Indentation
Python employs indentation as a means of delimiting blocks of code. Code at inner levels is
indented via spaces or tabs. All the lines of code in a suite must be indented at the exact same
level (e.g., same number of spaces).
Indented lines starting at different positions or column numbers are not allowed; each line would
be considered part of another suite and would more than likely result in syntax errors.
A new code block is recognized when the amount of indentation has increased, and its termination
is signaled by a "dedentation" or a reduction of indentation matching a previous level's.
The decision to create code blocks in Python to avoid "dangling-else"-type problems,
including ungrouped single statement clauses.
Finally, no "holy brace wars" can occur when using indentation.
5. Multiple Statements on a Single Line (;)
The semicolon (;) allows multiple statements on a single line given that neither statement starts a
new code block. Here is a sample snip using the semicolon:
import sys; x = 'foo'; sys.stdout.write(x + '\n')
6. Modules
Each Python script is considered a module. Modules have a physical presence as disk files.
When a module gets large enough or has diverse enough functionality, it may make sense to move
some of the code out to another module.
Code that resides in modules may belong to an application (i.e., a script that is directly executed),
or may be executable code in a library-type module that may be "imported" from another module
for invocation.
Modules can contain blocks of code to run, class declarations, function declarations, or any
combination of all of those.
IDENTIFIERS
Identifiers are the set of valid strings that are allowed as names in a computer language. The
identifiers are reserved words that may not be used for any other purpose, or else a syntax error
will occur.
Python also has an additional set of identifiers known as built-ins, and although they are not
reserved words, use of these special names is not recommended.
7
Valid Python Identifiers
The rules for Python identifier strings are like most other high-level programming languages that
come from the C world:
1. First character must be a letter or underscore ( _ ).
2. Any additional characters can be alphanumeric or underscore.
3. Case-sensitive.
No identifiers can begin with a number, and no symbols other than the underscore are ever
allowed.
Case-sensitivity means that identifier foo is different from Foo, and both of those are different
from FOO.
Keywords
Python is a growing and evolving language, a list of keywords as well as an iskeyword() function
are available in the keyword module.
Python Keywords
and as assert break class Continue Def del elif else except
exec finally for from global if import in is lambda not
or pass print raise return try while with yield none
Built-ins
Python has a set of "built-in" names available at any level of Python code that are either set and/or
used by the interpreter.
Built-ins should be treated as "reserved for the system" and not used for any other purpose.
Python does not support overloading of identifiers, so only one name "binding" may exist at any
given time.
Treat them like global variables that are available at any level of Python code.
Special Underscore Identifiers
Python designates special variables with underscores both prefixed and suffixed. Here is a
summary of the special underscore usage in Python:
1. _xxx Do not import with 'from module import *'
2. __xxx__ System-defined name
3. __xxx Request private name mangling in classes
8
BASIC STYLE GUIDELINES
Comments
Comments should not be absent, nor should there be novellas. Keep the comments explanatory,
clear, short, and concise, but get them in there. In the end, it saves time and energy for everyone.
Documentation
Python also provides a mechanism whereby documentation strings can be retrieved dynamically
through the __doc__ special variable.
The first unassigned string in a module, class declaration, or function declaration can be accessed
using the attribute obj.__doc__ where obj is the module, class, or function name.
Indentation
Indentation plays a major role, to decide on a spacing style that is easy to read as well as the least
confusing.
Common sense also plays a role in choosing how many spaces or columns to indent.
1 or 2 Probably not enough; difficult to determine which block of code statements
belong to.
8 to 10 May be too many; code that has many embedded levels will wrap around,
causing the source to be difficult to read.
Four spaces is very popular, not to mention being the preferred choice of Python's creator. Five
and six are not bad, but text editors usually do not use these settings, so they are not as commonly
used. Three and seven are borderline cases.
Choosing Identifier Names
Decide on short yet meaningful identifiers for variables. Although variable length is no longer an
issue with programming languages of today, it is still a good idea to keep name sizes reasonable
length.
Python Style Guide(s)
Guido van Rossum wrote up a Python Style Guide ages ago. It has since been replaced by no
fewer than three PEPs: 7 (Style Guide for C Code), 8 (Style Guide for Python Code), and 257
(DocString Conventions). These PEPs are archived, maintained, and updated regularly.
There is also another PEP, PEP 20, which lists the Zen of Python, starting the journey to discover
what Pythonic really means.
9
MODULE STRUCTURE AND LAYOUT
Modules are simply physical ways of logically organizing all your Python code. Within each file,
to set up a consistent and easy-to-read structure. One such layout is the following:
(1) startup line (Unix)
(2) module documentation
(3) module imports
(4) variable declarations
(5) class declarations
(6) function declarations
(7) "main" body
10
Typical Python file structure
1. Startup line
The startup line allows for script execution by name only (invoking the interpreter is not required).
2. Module documentation
Summary of a module's functionality and significant global variables; accessible externally as
module.__doc__.
3. Module imports
Import all the modules necessary for all the code in current module; modules are imported once
(when this module is loaded); imports within functions are not invoked until those functions are
called.
4. Variable declarations
Declare global variables that are used by multiple functions in this module. The use of local
variables over globals, for good programming style mostly, and to a lesser extent, for improved
performance and less memory usage.
5. Class declarations
A class is defined when this module is imported and the class statement executed. Documentation
variable is class.__doc__.
6. Function declarations
Functions that are declared here are accessible externally as module.function(); function is defined
when this module is imported and the def statement executed. Documentation variable is
function.__doc__.
7. "main" body
All code at this level is executed, whether this module is imported or started as a script; generally
does not include much functional code, but rather gives direction depending on mode of
execution.
Most projects tend to consist of a single application and import any required modules. Thus it is
important to bear in mind that most modules are created solely to be imported rather than to
execute as scripts.
All Python statements in the highest level of code that is, the lines that are not indented will be
executed on import, whether desired or not. Because of this "feature," safer code is written such
that everything is in a function except for the code that should be executed on an import of a
module.
11
Create Tests in the Main Body
For good programmers and engineers, providing a test suite or harness for our entire application is
the goal. Python simplifies this task particularly well for modules created solely for import.
The test software should run only when this file is executed directly, i.e., not when it is imported
from another module, which is the usual case.
The advantage of this mechanism by using the __name__ variable. If this module was called as a
script, plug the test code right in there, perhaps as part of main() or test() (or whatever you decide
to call your "second-level" piece of code) function, which is called only if this module is executed
directly.
The Python standard library also provides the unittest module, sometimes referred to as PyUnit, as
a testing framework.
PYTHON OBJECTS
Python uses the object model abstraction for data storage. Although Python is classified as an
"object-oriented programming (OOP) language," OOP is not required to create perfectly working
Python applications.
All Python objects have the following three characteristics: an identity, a type, and a
value.
IDENTITY Unique identifier that differentiates an object from all others. TYPE An object's type
indicates what kind of values an object can hold, what operations can be applied to such objects,
and what behavioral rules these objects are subject to.
VALUE Data item that is represented by an object.
All three are assigned on object creation and are read-only with one exception, the value. If an
object supports updates, its value can be changed; otherwise, it is also read-only.
An object's value can be changed is known as an object's mutability.
Object Attributes
Python objects have attributes, data values or executable code such as methods, associated with
them. Attributes are accessed in the dotted attribute notation, which includes the name of the
associated object.
The most familiar attributes are functions and methods, but some Python types have data attributes
associated with them. Objects with data attributes include (but are not limited to): classes, class
instances, modules, complex numbers, and files.
12
STANDARD TYPES
1. Numbers (separate subtypes; three are integer types)
1.1 Integer
1.1.1 Boolean
1.1.2 Long integer
1.2 Floating point real number
1.3 Complex number
2. String
3. List
4. Tuple
5. Dictionary
OTHER BUILT-IN TYPES
1. Type
2. Null object (None)
3. File
4. Set/Frozenset
5. Function/Method
6. Module
7. Class
Type Objects and the type Type Object:
The amount of information necessary to describe a type cannot fit into a single string; therefore
types cannot simply be strings, nor should this information be stored with the data.
Example 1: The type of an object by calling type() with that object:
>>> type(42)
<type 'int'>
The output of <type 'int'>, this is not just a simple string; it is tell that 42 is an integer.
Example 2: Now the type of any type object:
>>> type(type(42))
<type 'type'>
Yes, the type of all type objects is type. The type type object is also the mother of all types and is
the default metaclass for all standard Python classes.
Classes are now types, and instances are now objects of their respective types.
13
None, Python's Null Object:
Python has a special type known as the Null object or NoneType. It has only one value, None. The
type of None is NoneType.
It does not have any operators or BIFs. None has no (useful) attributes and always evaluates to
having a Boolean False value.
INTERNAL TYPES
1. Code
2. Frame
3. Traceback
4. Slice
5. Ellipsis
6. Xrange
1. Code Objects
Code objects are executable pieces of Python source that are byte-compiled, usually as return
values from calling the compile( ) BIF. Such objects are appropriate for execution by either exec
or by the eval () BIF.
Code objects themselves do not contain any information regarding their execution environment,
but they are at the heart of every user-defined function, all of which do contain some execution
context.
Along with the code object, a function's attributes also consist of the administrative support that a
function requires, including its name, documentation string, default arguments, and global
namespace.
2. Frame Objects
These are objects representing execution stack frames in Python. Frame objects contain all the
information the Python interpreter needs to know during a runtime execution environment.
3. Traceback Objects
If exceptions are not caught or "handled," the interpreter exits with some diagnostic information
similar to the output shown below:
Traceback (innermost last): File "<stdin>", line N?, in ??? ErrorName: error reason
The traceback object is just a data item that holds the stack trace information for an exception and
is created when an exception occurs. If a handler is provided for an exception, this handler is
given access to the traceback object.
14
4. Slice Objects
Slice objects are created using the Python extended slice syntax. This extended syntax allows for
different types of indexing. These various types of indexing include stride indexing, multi-
dimensional indexing, and indexing using the Ellipsis type.
The syntax for multi-dimensional indexing:
Sequence [start1 : end1, start2 : end2], or using the ellipsis, sequence [..., start1 : end1].
Slice objects can also be generated by the slice() BIF.
Stride indexing for sequence types allows for a third slice element that allows for "step"-like
access with a syntax of sequence[starting_index : ending_index ;: stride]
5. Ellipsis Objects
Ellipsis objects are used to represent the actual ellipses in the slice syntax (...). Like the Null
object None, ellipsis objects also have a single name, Ellipsis, and have a Boolean TRue value at
all times.
6. XRange Objects
XRange objects are created by the BIF xrange( ), a sibling of the range() BIF, and used when
memory is limited and when range( ) generates an unusually large data set.
For an interesting side adventure into Python types, the reader to take a look at the types module
in the standard Python library.
15
expr1 != expr2 expr1 is not equal to expr2
expr1 <> expr2 expr1 is not equal to expr2
Numeric types will be compared according to numeric value in sign and magnitude, strings will
compare lexicographically, etc.
>>> 2 == 2 True
>>> 2.46 <= 8.33 True
>>> 5+4j >= 2-3j True
>>> 'abc' == 'xyz' False
>>> 'abc' < 'xyz' True
>>> [3, 'abc'] == ['abc', 3] False
>>> [3, 'abc'] == [3, 'abc'] True
Multiple comparisons can be made on the same line, evaluated in left-to-right order:
>>> 3 < 4 < 7 # same as ( 3 < 4 ) and ( 4 < 7 ) True
>>> 4 > 3 == 3 # same as ( 4 > 3 ) and ( 3 == 3 ) True
>>> 4 < 3 < 5 != 2 < 7 False
Object Identity Comparison
Python also supports the notion of directly comparing objects themselves. Objects can be assigned
to other variables (by reference).
Because each variable points to the same (shared) data object, any change effected through one
variable will change the object and hence be reflected through all references to the same object.
Example 1: foo1 and foo2 reference the same object
foo1 = foo2 = 4.3
The object's reference is assigned to both foo1 and foo2, resulting in both foo1 and foo2 aliased to
the same object.
16
references. foo2 then becomes a new and additional reference for the original value. So both foo1
and foo2 now point to the same object.
Example 3: foo1 and foo2 reference different objects
foo1 = 4.3
foo2 = 1.3 + 3.0
A numeric object is created, then assigned to foo1. Then a second numeric object is created, and
this time assigned to foo2.
Although both objects are storing the exact same value, there are indeed two distinct objects in the
system, with foo1 pointing to the first, and foo2 being a reference to the second.
Each object has associated with it a counter that tracks the total number of references that exist to
that object. This number simply indicates how many variables are "pointing to" any particular
object. This is called the reference count.
Python provides the is and is not operators to test if a pair of variables do indeed refer to the same
object. For example:
a is b is an equivalent expression to id(a) == id(b)
Standard Type Object Identity Comparison Operators
Operator Function
obj1 is obj2 obj1 is the same object as obj2
obj1 is not obj2 obj1 is not the same object as obj2
For example:
>>> a = [ 5, 'hat', -9.3]
>>> b = a
>>> a is b
True
>>> a is not b
False
>>>
>>> b = 2.5e-5
>>> a is b
17
False
>>> a is not b
True
Boolean
Expressions may be linked together or negated using the Boolean logical operators and, or, and
not, all of which are Python keywords.
The not operator has the highest precedence and is immediately one level below all the
comparison operators.
Standard Type Boolean Operators
Operator Function
not expr Logical NOT of expr (negation)
expr1 and expr2 Logical AND of expr1 and expr2 (conjunction)
expr1 orexpr2 Logical OR of expr1 and expr2 (disjunction)
For example:
>>> x, y = 3.1415926536, -1024
>>> x < 5.0
True
>>> not (x < 5.0)
False
>>> (x < 5.0) or (y > 2.718281828)
True
>>> (x < 5.0) and (y > 2.718281828)
False
>>> not (x is y)
True
These expressions have an implicit and operator joining them together.
>>> 3 < 4 < 7 # same as "( 3 < 4 ) and ( 4 < 7 )"
True
STANDARD TYPE BUILT-IN FUNCTIONS
Python also provides some BIFs that can be applied to all the basic object types: cmp(), repr(),
str(), type(), and the single reverse or back quotes (``) operator, which is functionally equivalent to
repr().
18
Standard Type Built-in Functions
Operator Function
cmp(obj1, obj2) Compares obj1 and obj2, returns integer i where:
i < 0 if obj1 < obj2
i > 0 if obj1 > obj2
i == 0 if obj1 == obj2
repr(obj) or `obj` Returns evaluatable string representation of obj
str(obj) Returns printable string representation of obj
type(obj) Determines type of obj and return type object
type( ):
The syntax for type() is:
type(object)
The type( ) takes an object and returns its type. The return value is a type object.
>>> type(4) # int type
<type 'int'>
>>>
>>> type('Hello World!') # string type
<type 'string'>
>>>
>>> type(type(4)) # type type
<type 'type'>
The format is usually of the form: <object_something_or_another>. Any object displayed in this
manner generally gives the object type, an object ID or location, or other pertinent information.
cmp( ):
The cmp() BIF Compares two objects, say, obj1 and obj2, and returns a negative number (integer)
if obj1 is less than obj2, a positive number if obj1 is greater than obj2, and zero if obj1 is equal to
obj2.
Here are some samples of using the cmp() BIF with numbers and strings.
>>> a, b = -4, 12
>>> cmp(a,b)
-1
>>> cmp(b,a)
19
1
>>> b = -4
>>> cmp(a,b)
0
>>>
>>> a, b = 'abc', 'xyz'
>>> cmp(a,b)
-23
>>> cmp(b,a)
23
>>> b = 'abc'
>>> cmp(a,b)
0
str( ) and repr( ) (and `` Operator):
The str( ) String and repr( ) Representation BIFs or the single back or reverse quote operator ( `` )
to either re-create an object through evaluation or obtain a human-readable view of the contents of
objects, data values, object types, etc.
To use these operations, a Python object is provided as an argument and some type of string
representation of that object is returned.
In the examples that follow, we take some random Python types and convert them to their string
representations.
>>> str(4.53-2j)
'(4.53-2j)'
>>>
>>> str(1)
'1'
>>>
>>> str(2e10)
'20000000000.0'
>>>
>>> str([0, 5, 9, 9])
'[0, 5, 9, 9]'
20
>>>
>>> repr([0, 5, 9, 9])
'[0, 5, 9, 9]'
>>>
>>> `[0, 5, 9, 9]`
'[0, 5, 9, 9]'
The str() has the job of delivering a "printable" string representation of an object, which may not
necessarily be acceptable by eval(). There is a caveat that while most return values from repr() can
be evaluated, not all can:
>>> eval(`type(type))`)
File "<stdin>", line 1
eval(`type(type))`)
^ Syntax Error: invalid syntax
type( ) and isinstance( ):
Python does not support method or function overloading, so we are responsible for any
"introspection" of the objects that our functions are called with.
Fortunately, Python provides a BIF just for that very purpose. type() returns the type for any
Python object, not just the standard types.
Example: A script that shows how we can use isinstance() and type() in a runtime environment.
Checking the Type (typechk.py)
The function displayNumType() takes a numeric argument and uses the type() built-in to indicate
its type.
1 #!/usr/bin/env python
2
3 def displayNumType(num):
4 print num, 'is',
5 if isinstance(num, (int, long, float, complex)):
6 print 'a number of type:', type(num).__name__
7 else:
8 print 'not a number at all!!'
9
10 displayNumType(-69)
21
11 displayNumType(9999999999999999999999L)
12 displayNumType(98.6)
13 displayNumType(-5.2+1.9j)
14 displayNumType('xxx')
Running typechk.py, we get the following output:
-69 is a number of type: int
9999999999999999999999 is a number of type: long
98.6 is a number of type: float
(-5.2+1.9j) is a number of type: complex
xxx is not a number at all!!
Python Type Operator and BIF Summary
The progressing shaded groups indicate hierarchical precedence from highest-to-lowest order.
Elements grouped with similar shading all have equal priority.
Standard Type Operators and Built-in Functions
Operator/Function Description Result
String representation
String representation str
Built-in functions
cmp(obj1, obj2) Compares two objects int
repr(obj) String representation str
str(obj) String representation str
type(obj) Determines object type type
Value comparisons
< Less than bool
> Greater than bool
<= Less than or equal to bool
>= Greater than or equal to bool
== Equal to bool
!= Not equal to bool
<> Not equal to bool
Object comparisons
22
is The same as bool
is not Not the same as bool
Boolean operators
not Logical negation bool
and Logical conjunction bool
or Logical disjunction bool
UNIT – I – COMPLETED
23