CS101 6212140ab42e8

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Knowledge University Fundamentals of Programming

College of Science Languages


Computer Science Dept. First Semester, Level 1

BASICS OF FUNCTION AND CLASS

CHAPTER 4

Prepared by / Mr. Nashwan A. Othman


Email: nashwan.adnan@knu.edu.iq

2022 FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022

Everything you need to know about Python


Now that we are done with the different types of loops that we have in python, lets
understand the concept of functions in python.

Functions
A function in python is a block of code which will execute
whenever it is called. We can pass parameters in the functions as
well. To understand the concept of functions, lets take an example.

Suppose you want to calculate factorial of a number. You can do


this by simply executing the logic to calculate a factorial. But what
if you have to do it ten times in a day, writing the same logic again
and again is going to be a long task.

Instead, what you can do is, write the logic in a function. Call that
function every time you need to calculate the factorial. This will
reduce the complexity of your code and save your time as well.

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022


Everything you need to know about Python

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022

Everything you need to know about Python


Function Parameters
We can pass values in a function using the parameters. We can use also
give default values for a parameter in a function as well.

Run

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022


Everything you need to know about Python
Now that we have understood function calls, parameters and why we use them, lets
take a look at classes and objects in python.

Classes & Objects


What are Classes?
Classes are like a blueprint for creating objects. We can store
various methods/functions in a class.

What are Objects?


We create objects to call the methods in a class, or to access the
properties of a class.

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022

Everything you need to know about Python


Classes
A class is a collection of objects or you can say it is a blueprint of objects
defining the common attributes and behavior. Now the question arises,
how do you do that?
Well, it logically groups the data in such a way that code reusability
becomes easy. I can give you a real-life example- think of an office going
employee as a class and all the attributes related to it like emp_name ,
emp_age , emp_salary , emp_id as the objects in Python.

Let us see from the coding perspective that how do you instantiate a class
and an object.
Class is defined under a Class Keyword.

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022


Everything you need to know about Python
Objects
Objects are an instance of a class. It is an entity that has state and
behavior. In a nutshell, it is an instance of a class that can access the data.
Syntax: obj = class1()
Here obj is the object of class1.
Example: Creating an Object and Class in python:

Run
Explanation: emp1 and emp2 are the objects that are instantiated against the
class employee .Here, the word (__dict__) is a dictionary which prints all the values
of object emp1 against the given parameter (name, age, salary). (__init__) acts like
a constructor that is invoked whenever an object is created.
FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022

Everything you need to know about Python


Class and Object Example:

Run

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022


Everything you need to know about Python
Class and Object Example:

Run

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022

Everything you need to know about Python


__init__ function
It is an inbuilt function which is called when a class is being
initiated. All classes have __init__ function. We use the __init__
function to assign values to objects or other operations which are
required when an object is being created.

__init__ is a special method in Python classes, it is the constructor method for a class.
In the following example you can see how to use it. __init__ is called when ever an
object of the class is constructed. So, it allows the class to initialize the attributes of the
class.
init is short for initialization.

self
The self in keyword in Python is used to all the instances in a class. By using
the self keyword, one can easily access all the instances defined within a
class, including its methods and attributes.

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022


Everything you need to know about Python
Example

FUNDAMENTALS OF PROGRAMMING LANGUAGES, NASHWAN A. OTHMAN, 2022

You might also like