Python Unit 1
Python Unit 1
Unit-1
Python Basics
1. an identity,
2. a type, and
3. avalue.
1. IDENTITY
Unique identifier that differentiates an object from all others.Using the id()
built-in function (BIF) any object's identifier can be obtained.
2. TYPE
You canuse the type() BIF to reveal the type of a Python object.type() actually
returns an object.
3. VALUE
Data item that is represented by an object.
Object Attributes
Attributes are accessed in the dotted attribute notation. The most familiar
attributes arefunctions and methods.
Standard Types
1. Numbers
i) Integer
a) Boolean
b) Long integer
ii) Floating point real number
iii) Complex number
2. String
3. List
4. Tuple
5. Dictionary
1. i)Type Objects
You can findout the type of an object by calling type() with that object,
Example
>>>type(42)
<type 'int'>
ii)typeType Object
The type of all type objects is type.
Example
>>>type(type(42))
<type 'type'>
Python has a special type known as the Null object or NoneType. None value is
similar to the C value of NULL. None always evaluates to having a Boolean False
value.
Internal Types
The internal types are
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.
Attribute of code object
The actual byte-compiled code as a code object is one attribute belonging to a
function.
2. Frame Objects
These are objects representing execution stack frames in Python.
Attributes of frame objects
The attributes include a link to the previous stack frame, dictionaries for the
local and global namespaces, and the current instruction.
4. Slice Objects
Slice objects are created using the Python extended slice syntax.
This extended syntax allows for different types of indexing. Slice objects can
also be generated by the slice() BIF. These various types of indexing include
stride indexing, multi-dimensional indexing, and indexing using the Ellipsis type.
Example
i) Using stride index
>>> foostr = 'abcde'
>>> foostr[::-1]
'edcba'
>>> foostr[::-2]
'eca'
>>> foolist = [123, 'xba', 342.23, 'abc']
>>> foolist[::-1]
['abc', 342.23, 'xba', 123]
5. Ellipsis Objects
Ellipsis objects are used to represent the actual ellipses in the slice syntax
(...).
6. XRange Objects
XRange objects are created by the BIF xrange().It is used when memory is
Limited.
Standard Type Operators
Operator Function
expr1 < expr2 expr1 is less than expr2
expr1 > expr2 expr1 is greater than expr2
expr1 <= expr2 expr1 is less than or equal to expr2
expr1 >= expr2 expr1 is greater than or equal to expr2
expr1 == expr2 expr1 is equal to expr2
expr1 != expr2 expr1 is not equal to expr2 (C-style)
expr1 <> expr2 expr1 is not equal to expr2
Example
>>> 2 == 2
True
>>> 2.46 <= 8.33
True
>>> 'abc' == 'xyz'
False
Operator Function
obj1 is obj2 obj1 is the same object as obj2
obj1 is not obj2 obj1 is not the same object as obj2
Example
foo1 and foo2 reference the same object
foo1 = foo2 = 4.3
3. Boolean
Boolean logical operators are and, or, and not. All are python keywords. The not
operator has the highest precedence.
Operator Function
not expr Logical NOT of expr
expr1 and expr2 Logical AND of expr1 and expr2
expr1 or expr2 Logical OR of expr1 and expr2
Example
>>> x, y = 3.1415926536, -1024
>>> not (x is y)
True
>>> (x < 5.0) and (y > 2.718281828)
False
>>> (x < 5.0) or (y > 2.718281828)
True
Function Operation
1. cmp()
The cmp() BIF CoMPares two objects, say, obj1 and obj2. It 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.
Example
>>> a, b = -4, 12
>>> cmp(a,b)
-1
>>> cmp(b,a)
1
>>> b = -4
>>> cmp(a,b)
0
2. repr()
repr() REPResentation BIFs or the single back or reverse quote operator ( `` )
replicates elements of a list.
Example
3. str()
Example
>>> str(1)
'1'
>>> str(2e10)
'20000000000.0'
>>>
>>> str([0, 5, 9, 9])
'[0, 5, 9, 9]'
4. type()
type() takes an object and returns its type. The return value is a
type object.
Syntax
type(object)
Example
Operator/
Function Description Result
cmp(obj1, obj2) Compares two objects Int
repr(obj) String representation Str
str(obj) String representation Str
Determines object
type(obj) type Type
1. Storage Model
We can categorize the types is by how many objects can be stored in an object
of this type.
2. Update Model
Certain types allow their values to be updated and others do not.
i) Mutable objects
3. Access Model
We use the access model to access the values of our stored data. There are
three categories under the access model: direct, sequence, and mapping.
i) Direct types
Examples
All numeric types fit into this category.
Examples
Types that fall into this category include strings, lists, and tuples.
Mapping type elements (values) are unordered and accessed with a key, thus
making mapping types a set of hashed key-value pairs.
Examples
Dictionaries
Unsupported Types
A list of types that are not supported by Python.
1. char or byte
Python does not have a char or byte type to hold either single character or 8-
bit integers.
2. pointer
Since Python manages memory for you, there is no need to access pointer
addresses.
Python's plain integers are the universal "standard" integer type, obviating
the need for three different integer types, e.g., C's int, short, and long. You only
need to use a single type, the Python integer.
Numbers
Introduction to Numbers
Numbers provide literal or scalar storage and direct access. A number is also
an immutable type, meaning that changing or updating its value results in a
newly allocated object.
anInt = 1
aLong = -9999999999999999L
aFloat = 3.1415926535897932384626433832795
aComplex = 1.23+4.56J