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

FUNCTIONS&OOP

The document provides an overview of key programming concepts in Python, including functions, lists, tuples, dictionaries, sets, classes, and inheritance. It explains the structure and characteristics of these data types, as well as the principles of object-oriented programming such as encapsulation, inheritance, and method overriding. Additionally, it covers the use of parameters and arguments in functions, along with the concepts of recursion and arbitrary arguments.
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)
5 views3 pages

FUNCTIONS&OOP

The document provides an overview of key programming concepts in Python, including functions, lists, tuples, dictionaries, sets, classes, and inheritance. It explains the structure and characteristics of these data types, as well as the principles of object-oriented programming such as encapsulation, inheritance, and method overriding. Additionally, it covers the use of parameters and arguments in functions, along with the concepts of recursion and arbitrary arguments.
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
You are on page 1/ 3

 FUNCTIONS

- A function is a block of organized, reusable


code that is used to perform a single, related  WHAT IS A LIST?
action. Functions provide better modularity for - A list is a data structure that's built into Python
your application and a high degree of code and holds a collection of items. Lists have a
reusing. number of important characteristics:
- Function blocks begin with the keyword def - List items are enclosed in square brackets, like
followed by the function name and this [item1, item2, item3].
parentheses ( ( ) ). - Lists are ordered – i.e. the items in the list
- Any input parameters or arguments should be appear in a specific order. This enables us to
placed within these parentheses. You use an index to access to any item.
can also define parameters inside these - Lists are mutable, which means you can add or
parentheses. remove items after a list's creation.
- The code block within every function starts - List elements do not need to be unique. Item
with a colon (:) and is indented. duplication is possible, as each element has its
- The statement return [expression] exits a own distinct place and can be accessed
function, optionally passing back an separately through the index.
expression to the caller. A return statement - Elements can be of different data types: you
with no arguments is the same as return can combine strings, integers, and objects in
None. the same list.
- The list is a most versatile data type available
in Python which can be written as a list of
 PARAMETER & ARGUMENT comma-separated values (items) between
- A parameter is the variable listed inside the square brackets.
parentheses in the function definition. numbers= [1, 2, 3, 4, 5 ]
- An argument is the value that is sent to the food = ["cake", "burger", "fries"]
function when it is called. print(numbers[index number])
print(numbers[0]) #1
print(food[1]) #burger
 Arbitrary Arguments, *args
- If you do not know how many arguments that
will be passed into your function, add a *
before the parameter name in the function
definition.
- This way the function will receive a tuple of
arguments.

 Arbitrary Keyword Arguments, **kwargs


- If you do not know how many keyword  TUPLE
arguments that will be passed into your - A tuple is a collection of objects which
function, add two asterisk: ** before the ordered and immutable. Tuples are sequences,
parameter name in the function definition. just like lists. The differences between tuples
- This way the function will receive a dictionary and lists are, the tuples cannot be changed
of arguments unlike lists and tuples use parentheses,
whereas lists use square brackets.
 Recursion
- Python also accepts function recursion, which - Tuple items are indexed, the first item has
means a defined function can call itself. index [0], the second item has index [1] etc.
Recursion is a common mathematical and Allow duplicate value
programming concept. It means that a function
calls itself. This has the benefit of meaning tup1 = ('physics', 'chemistry', 1997, 2000);
that you can loop through data to reach a tup2 = (1, 2, 3, 4, 5 );
result. tup3 = "a", "b", "c", "d";
 WHAT IS A CLASS
- Objects with similar characteristics can fall
under one class.
- Example:
○ Dog represents all breeds of dogs: bulldog,
poodle, chihuahua,
 DICTIONARY etc.
- Each key is separated from its value by a ○ Each dog breed can be “derived” or “built
colon (:), the items are separated by commas, from” a general class
and the whole thing is enclosed in curly called Dog.
braces. An empty dictionary without any items
is written with just two curly braces, like this: ● In object-oriented terms, the Dog class is the
{}.Keys are unique within a dictionary while template or blueprint
values may not be. from which bulldog, poodle and chihuahua are
derived.
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} ● A class, therefore, is a template or a blueprint.
KEY = NAME An object is an
VALUE = Zara instance of a class.
● A class is:
○ Made up of attributes and behavior.
■ State or Attributes of the class are represented
by
variables called fields.
■ Class behavior is revealed through methods.

 CLASS MEMBERS
● Class members are declarations made inside
the body of the class.
The following are class members:
○ Fields
■ Also referred to as attributes.
 SET ■ These are data used by the class. They are
- A set is a collection which is unordered and variables
unindexed. In Python, sets are declared inside the class body.
written with curly brackets. ○ Methods
fruits= {"apple", "banana", "cherry"} ■ Also referred to as the behavior(s).
print(fruits) ■ These are program statements grouped
together to perform a specific function.
 LIST,TUPLE,SET AND DICTIONARY
1. List is a collection which is ordered and  CREATING AN OBJECT
changeable. Allows duplicate members. - Since classes are templates, they provide the
benefit of reusability.
2. Tuple is a collection which is ordered and - A class can be used over and over to create
unchangeable. Allows duplicate members. many objects.
- An object created out of a class will contain
the same variables that the class has. However,
3. Set is a collection which is unordered and the values of the variables are specific to the
unindexed. No duplicate members. object created.
- Creating an object out of a class is called class
4. Dictionary is a collection which is ordered* instantiation.
and changeable. No duplicate members.
to invoke the method, then the version in the
parent class will be executed, but if an object
of the subclass is used to invoke the method,
 The __init__ function then the version in the child class will be
- The __init__() function is called automatically executed. In other words, it is the type of the
every time the class is object being referred to (not the type of the
being used to create a new object. reference variable) that determines which
Constructor in other programming. version of an overridden method will be
- "__init__" is a reserved method in python executed.
classes. It is known as a constructor in object
oriented concepts. This method called when an
object is created from the class and it allow the
class to initialize the attributes of a class.

- The self parameter is a reference to the current


instance of the class, and is used to access
variables that belong to the class.

 INHERITANCE IN PYTHON
- In object-oriented programming (OOP),
inheritance is a mechanism that allows a class
to inherit properties and behaviours from
another class. It is a fundamental concept in
OOP that promotes code reuse and establishes
relationships between classes.

 INHERITANCE
- Inheritance in OOP’s is based on a hierarchical
relationship between classes, where a derived
class (also known as a subclass or child class)
inherits the characteristics of a base class (also
known as a superclass or parent class). The
derived class extends the functionality of the
base class by adding new features or
overriding existing ones.

- The key idea behind inheritance is that the


derived class inherits all the attributes (data
members) and behaviours (methods) of the
base class, and it can also introduce its own
specific attributes and behaviours. This allows
for creating a hierarchy of classes with
increasing specialization.

 METHOD OVERRIDING
- Method overriding, in object-oriented
programming, is a language feature that allows
a subclass or child class to provide a specific
implementation of a method that is already
provided by one of its superclasses or parent
classes.

- The version of a method that is executed will


be determined by the object that is used to
invoke it. If an object of a parent class is used

You might also like