Sitemap

Python Learning (Part 7)

3 min readOct 26, 2019

--

In this article we will be covering the topics like

1. Classes and Objects
2. Type Casting and Loss of data

Classes and Objects in Python

Python is an Object Oriented Programming language.
A class is a user defined blueprint from which the objects are created.
e.g. let’s say we have class which is Employee.
The Employee contains all the details of each and every employee working in a project or company.

Syntax for class:
class ClassName:
Statemets

Syntax for object:
object-name = class-name (arguments)

Example:

The __init__() function is used to assign values to object properties (in our case p1) or other operations that are necessary to do when the object is being created.

The ‘self’ parameter is a reference to the current instance of the class and is used to access variables that belongs to the class (in our case EmployeeName and EmployeeAge).

Output:

Press enter or click to view image in full size

Object Methods:
Objects can also contain methods.
Methods in objects are functions that belong to the object.

Output:

Press enter or click to view image in full size

Type Casting and Loss of data:

What is Casting? :
Casting is a process of converting from one data type into another data type.

Get Arjun Sharma’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s understand Type Casting in Python with following example:

In above example, “i” has integer value as 100 and we are going to convert it into float in “f”
Let’s check in output whether the data type has changed it into float or not?

Output:

Press enter or click to view image in full size

Now we would like to see how type casting can be responsible for loss of data

Let’s take the same example but now this time we would reverse the condition

Here we have “f” value as 100.45 and we are trying to convert it into an integer type in “i”.
In result we we would get “i” value as 100 only and other data which is present in “f” would get disappear as shown below

Output:

Press enter or click to view image in full size

Get to see below and learn with project oriented video on Python topic: -

--

--