1.
8 Categorizing the standard types
There are three different models we have come up with to help categorize the
standard types. It shows the inter-relationships between the types
1. Storage Model
2. Update Model
3. Access Model
1. Storage Model
How many objects can be stored in an object of this type. It can hold either single or
multiple values.
It contains 2 types of storage
1. Atomic or scalar storage -> single object
It can store number or string
2. Container Storage -> Multiple objects
It can store lists, tuples, dictionaries
2. Update Model
It checks when the object is created. Can objects can be changed or can their
values be updated.
It contains 2 types
1. Mutable objects -> those values can be changed
It can lists, dictionaries
2. Immutable Objects -> those values cannot be changed
It can store numbers, strings, tuples
Eg:-
>>>x=’python numbers’ >>>i=0
>>>print id(x) print id(i)
16191392 77498552
>>>x=’are immutable’
>>>print id(x)
16191392
3. Access Model
In this we deal with how do we access the values of our stored data
3 types
1. Direct -> Numbers -> It indicate single element
2. Sequence -> strings, lists, tuples
3. Mapping -> Dictionaries
Categorizing the standard types
Data type Storage Model Update Model Access Model
Numbers Scalar Immutable Direct
Strings Scalar Immutable Sequence
Lists Container Immutable Sequence
Tuples Container Immutable Sequence
Dictionaries Container Mutable Mapping
1.9 Unsupported Types
List of types that are not supported in python
1. Char or byte
2. Pointer
3. Int versus short versus long
Python only uses Integer instead of long int and short int
4. Float versus double
In python float type is actually a double
1.10 Built-in Functions
1. Standard type functions
Standard built-in functions are cmp(), str(), and type(). These functions will
compare two numbers, convert numbers into strings and tell you a numbers type,
respectively.
Eg:-
Cmp(-6,2) str(0xff) type(0xff)
-1 ‘255’ <type’int’>
2. Numeric Type Functions
Bool(obj) -> Returns the boolean value of obj
Eg:- The value of executing object non-zero
Int(obj, base=10) -> Returns integer representation
Long(obj, base10) -> Returns long representation
Float(obj) -> Returns floating point representation
Complex(str) or complex(real, imag=0.0) -> Returns complex number
representation of str
3. Operational
5 operational built-in functions
1. Abs()
2. Coerce()
3. Divmod()
4. Pow()
5. Round()
1. Abs()
It returns the absolute value of the given argument
Eg:-
Abs(-1) abs(10.)
1 10.0
2. coerce()
It is a way for the programmer to explicitly coerce a pair of numbers
rather than letting the interpreter do it. It returns a tuple containing the
converted pair of numbers
Eg:-
>>>coerce(1,2)
(1,2)
>>>coerce(1.3,134L)
(1.3, 134.0)
3. Divmod():-
It contains division and modulus operations into a single function call
that returns the pair(quotient, reminder) as a tuple.
Eg:-
>>>divmod(10, 3)
(3, 1)
>>>divmod(10,2.5)
(4.0, 0.0)
4. Pow()
Both pow() and the double star(**) operator perform exponentiation
Eg:-
>>>pow(2.5) >>>pow(5,2)
32 25
5. Round()
Syntax:
Round(flt,ndig=0)
It normally rounds a floating point number to the nearest integral
number and returns that result still as a float. ndig -> returns a specific number
of decimal places.
Eg:-
>>>round(3) >>>round(3.45)
3.0 3.0
>>>round(-3.5) >>> x=round(5.76543,2)
-4.0 print(x) o/p: 5.17
6. Inter-only Functions
2 categories
1. Base representation -> hex(), oct()
2. ASCII conversion featuring -> chr(), ord()
Base Representation
Oct(), hex()
Eg:- >>> hex(255) >>> oct(255)
‘0xff’ ‘0377’
Hex(230948231) >>> oct(230948231)
’0x1606627’
ASCII conversion:-
chr(), ord()
eg:- >>>ord(‘a’) >>>chr(91)
97 ‘a’
>>>ord(‘A’) >>>chr(65L)
65 ‘A’
1.11 Related Modules
There are number of modules in the python standard library that add on to the
functionality of the operators and built-in functions for numeric types.
Module Contents
Decimal Decimal floating point class decimal
Arrray Efficient arrays of numeric values(characters, ints, floats etc)
Math/cmath Standard c library mathematical functions, most functions
available in math are implemented for complex numbers in the
cmath module
Operator Numeric operators available as function calls i.e,
[Link](m,n) is equivalent to the difference(m-n) for
number m and n
Random Various pseudo-random number generator(obsoletes rand and
WHRandom)
Random
Randint() -> takes two integer values and returns a random integer
Randrange() -> takes the same input as range() and returns a random integer that
falls with in that range
Uniform -> It returns a float
Random -> smaller number is fixed at 0.0 and the larger number is fixed at 1.0
Choice -> Given a sequence and selects and returns a sequence item
Strings
String literals in python are surrounded by either single quotation marks or double
quotation marks.
‘hello’ is the same as “hello”
Strings in python are arrays of bytes representing Unicode characters
Python does not have a character datatype, a single character is simply a string with a
length of 1.
Square brackets are used to access elements of the string.
Accessing characters and substrings in a string
Python strings are “immutable” which they cannot be changed after they are created.
String data can be accessed directly using a numeric index or key value. This process is
referred to as indexing.
In python, strings are ordered sequences of character data and thus can be indexed in
this way
Individual characters in a string can be accessed by specifying the string name followed
by a number in square brackets([]).
String Indexing
String indexing in python is zero based
The first character in the string has index 0, the next has index 1, and so on.
The index of the last character will be the length of the string minus one.
f O o b a r
0 1 2 3 4 5
>>s=’foobar’
S[0]=’f’ s[3]=’b’ len(s)=6
S[1]=’o’ s[4]=’a’ s[len(s)-1]=’r’
S[2]=’o’ s[5]=’r’
Attempting to index beyond the end of the string result in an error
Substring in a string
Python has a feature called “string” that can be used for getting sub-string from strings
Syntax:
Stringname[starting index : ending index]
S=’hello, everybody’ s[:5]=’Hello’
s[0]=’H’ s[5:]=’everybody’
s[:3]=’hel’ s[:]=’Hello,everybody!’
s[2:5]=’llo’
String and Number system
Print function is used to print the string.
Print(s) s string name
There are 3 numeric types in python
Int
Float
Complex
Variables of numeric types are created when you assign a value to them
Example
X=1 # int
Y=2.8 # float
Z=ij # complex
To verify the type of any object in python, use the type() function
print(type(x)) <class ‘int’>
print(type(y)) <class ‘float’>
print(type(z)) <class ‘complex’>
int:-
int or integer is a whole number, positive or negative with out decimals of unlimited
length.
Ex: x=1
Y=356789101112
Z=-324567
Float:-
Float or floating point number is a number, positive or negative containing one or more
decimals
Ex:-
X=1.10
Y=1.0
Z=-35.59
Complex:-
Complex numbers are written with a j as the imaginary part
Ex:- x=3+5j
Y=5j
Z=-5j