In Python, a function is a block of code that runs anytime it is invoked. We can also pass parameters to the functions. A function is a code block that only executes when it is invoked. You can send parameters into a function, which will return data as a result.
creating a function:
calling a function:
The parameters can be used to pass values to a function. In a function, we can also provide default values for parameters.
Lambda Function: A lambda function is a small unnamed function. A lambda function can have an unlimited number of arguments but can have only one expression which is shown below with the following example:
Classes and Object
Classes serve as a template for the creation of objects. Python is a language that is object oriented. In Python, almost everything is an object with properties and functions. A class can contain a variety of methods and functionalities as shown below the concept of class:
Object: We build objects to call methods in a class or to access a class's properties as show in an example below:
init function (): It is a built-in function that is invoked when a class is launched. Every class has a init function. The init function is used to assign values to objects or to do other
activities that are required when an object is formed as shown in the example below:
Python as an Object-Oriented Programming (Very important)
I am glad that if you came so far well at first Kudos! to you guys and you have almost absorbed the required but important ingredients. After swallowing these extra things as well I think u
would definitely be mastering the fundamental aspects cohesively in this invaluable language. So, let’s begin…Shall we?
Python is an object-oriented programming language. As a result, we can use the following Python notions:
✔ Data Abstraction: Data abstraction is the process of displaying only the details that are
required while masking the background operations. Python's abstraction is identical to that of any other scripting language.
✔ Data Encapsulation: Encapsulation is the process of enclosing data. Classes in Python
are an example of encapsulation since member functions, variables, and so on are wrapped into a class.
✔ Inheritance: Inheritance is an object-oriented approach in which a child class inherits all
of its parent class's properties. Python supports the following types of inheritance:
● The class being inherited from is known as the parent class, sometimes known as the base class.
● A child class is one that inherits from another class, often known as a derived class.
Now we have in order to create a parent class following example is shown below:
Furthermore, creating a child class that inherits the property of a base class:
✔ Polymorphism: Polymorphism refers to the method by which an object can be used in
multiple ways. The most common usage of a parent class reference is to link to a child class object. An example of calculation of an area and perimeter of a rectangle and a volume of a cylinder and its perimeter is shown below using the concept of Polymorphism:
Exceptional Handling in Python:
When an error occurs, the program does not stop and continues to execute the except block. If an error happens while we are writing a program, the program will terminate. However, we can manage these errors/exceptions in Python by using the try, except, and finally blocks as shown below:
When we include a finally block it then will be running regardless if there is an error or if the try except block does not raise an error.
File Handling in Python
The handling of files is an important concept in the Python programming language. Python offers a number of functions for creating, reading, writing, deleting, and updating files. The open() function is essential for working with files in Python.
The open() function accepts two arguments that are filename and there are four modes of opening a file which are shown below:
Figure: showing the modes of operations in file handling
Furthermore, you can select whether the file should be treated in binary or text form as shown below:
To create a file, we have:
To read a file, we have:
To append a file, we have:
To write a file, we have:
To delete a file, we have:
Additional Functions that are important in Python Programming
Super() Function: This function is one of the important features in Python that falls under inheritance which will make the child class inherit all of its parent's methods and the properties as shown in the example below:
Also, we can add an extra property to the class “CStudent” and access them as shown below:
Also, when creating objects, add a year argument and pass the proper year as shown below in an example as:
In a nutshell, these vital topics summon all those small foundations that you are going to
combine these things into larger scenario is the back bone for you to grow farther and further
which I think you would be mastering these things by heart if you made this so far with all those concepts when you instill them you would do great, I swear and because this is incredibly practical when you work with python, this is the basis of all programming languages which is in fact quite useful. You can start your journey to become a senior python developer once you've learned the fundamental ideas of the language.