0% found this document useful (0 votes)
4 views3 pages

2 Python - Datatypes

The document provides an overview of Python data types, including their characteristics, built-in methods, and examples. It also introduces object-oriented programming concepts such as classes, objects, encapsulation, inheritance, and polymorphism. Additionally, it discusses the properties of Python objects, including identity, type, and value, as well as the distinction between mutable and immutable objects.

Uploaded by

Arun Sinha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

2 Python - Datatypes

The document provides an overview of Python data types, including their characteristics, built-in methods, and examples. It also introduces object-oriented programming concepts such as classes, objects, encapsulation, inheritance, and polymorphism. Additionally, it discusses the properties of Python objects, including identity, type, and value, as well as the distinction between mutable and immutable objects.

Uploaded by

Arun Sinha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Data Types 1

Datatypes - is an way to classify data items. Datatypes define two things –


1) What kind of data can be held
2) What all operations are allowed and what all operations are not
allowed on that data item

Data What it Stores / Range Built-in Methods


Type
int Whole numbers. Unlimited size (Python abs(), pow(), bit_length(),
automatically increases memory). Examples: to_bytes(), from_bytes()
-5, 0, 10, 999999999999
float Decimal numbers using double precision round(), as_integer_ratio(),
(~15–17 digits). Example: 3.14, -0.002 is_integer(), hex()
complex Complex numbers: a + bj. Example: 3+4j .real, .imag, conjugate()
bool Logical values: True or False bool(), logical operators (and,
or, not)
str Text strings (Unicode). Example: "hello" len(), upper(), lower(), strip(),
split(), replace(), find()
list Ordered collection of items. Can store mixed append(), extend(), insert(),
types. Example: [1, "a", 3.2] remove(), pop(), sort(),
reverse()
tuple Ordered immutable collection. Example: count(), index()
(1,2,3)
set Unordered collection of unique elements. add(), remove(), union(),
Example: {1,2,3} intersection(), difference()
range built-in sequence type in Python used to
generate a sequence of numbers, typically
used in loops and iteration.
dict Key–value pairs. Example: {"name":"Alex", keys(), values(), items(), get(),
"age":20} update(), pop()
NoneTy Represents absence of value (None) Mostly used for comparison
pe (is None)
Python Data Types 2

Refer to [Link] for all the INTEGER datatype operations


Refer to [Link] for all the SEQUENCE datatype operations

Everything in Python is an Object (of a


specific
Since class)
Python is a multi-paradigm language (Procedural, Object-oriented and Functional)
we need to know –
Object-oriented concepts – A Very short introduction

OOP Concept One-Line Definition


Class A template used to create objects
Object An instance of a class that contains actual data and can perform
actions
Property/ variables inside a class that store the characteristics of an object
Attribute
Behaviors/ functions defined inside a class that describe what an object can do
Methods
Encapsulation bundling data and the methods that operate on that data into a
single unit
Abstraction hiding complex implementation details and exposing only the
essential features
Inheritance one class (child) acquires the properties and behaviors of another
class (parent)
Polymorphism The ability of different objects to respond to the same operation in
their own way.

A car is a class. It’s Properties are - color, engine type, fuel-type. It’s Methods are –
start(), break(), accelerate().
A Blue-colored, 2000cc, Electric TATA Nano is an object. It’s properties are : color =
Blue, Engine Type = 2000 cc, Fuel-Type = Electric. It inherits all it’s methods from it’s
parent, additionally it has two methods called ac_on() and ac_off()
The engine system is enclosed inside the car, and you control it only through pedals and
steering instead of touching internal parts. This is Encapsulation.

Python Objects have three core properties


Proper Meaning Command What the output Important
ty means
Identit A unique identifier that id(object) memory location of Identity
y distinguishes the object from the object. never
all other objects in memory. changes
once the
object is
created.
Two
variables
can refer
to the
same
object.
Type  what kind of object it is type(objec Datatype of the object Once
Python Data Types 3

 what operations it supports t) created,


 what methods it contains the type of
an object
cannot
change.
Value The actual data or content Object Value of the object The value
stored inside the object. is the
actual data
stored
inside the
object.

Important Concept: Mutability

Objects are also classified based on whether their value can change.

Type Meaning Variable Example


Mutable the object value list, dictionary, set
changes
Immutab the object value Reference to object may int, float, string, tuple,
le does not change change range

Comparison of Mutable and Immutable Objects

1. Mutable and immutable objects are handled differently in Python. Immutable


objects are quicker to access and are expensive to change because it involves the
creation of a copy. Whereas mutable objects are easy to change.

2. The use of mutable objects is recommended when there is a need to change the size
or content of the object.

3. Exception: However, there is an exception in immutability as well. We know that a


tuple in Python is immutable. But the tuple consists of a sequence of names with
unchangeable bindings to objects.

Example - Consider a tuple tup = ([3, 4, 5], 'myname')

The tuple consists of a string and a list. Strings are immutable so we can't change their
value. But the contents of the list can change. The tuple itself isn’t mutable but contains
items that are mutable. As a rule of thumb, generally, Primitive-like types are probably
immutable, and Customized Container-like types are mostly mutable.

Refer to [Link] for Object Property Examples

You might also like