Some core concept of Python

Python is a popular, versatile and easy-to-learn language. It’s the go-to language for AI, Machine Learning and Data Science. Some say it’s also the easiest programming language to get started with.If this sounds like a programming language you want to learn, keep reading!

We’ll cover some of the important concept of Python in this blogs and the topics covered are as follows :

  1. Function
  2. Modules
  3. List Comprehensions
  4. Iterators
  5. Generators
  6. Classes and objects
  7. Descriptors

Now we’ll look into each and every topics in detail with examples. So, lets begin…

Function

A function is a block of code which only runs when it is called.You can pass data, known as parameters, into a function.A function can return data as a result.Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.

Syntax of Function

Types of Function

  1. Buil-in function: Functions that are built into Python
  2. User-defined functions : Functions defined by the users themselves.

Examples of a built-in function

Source : data365

Here’s the list of available built-in functions which are available in Python.

Examples of User-defined function

Modules

Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application. A file containing Python code, for example: example.py, is called a module, and its module name would be example.

To understand the concept of Module, let us take one example. We’ll create a module. Type the following and save it as example.py.

Here, we have defined a function add() inside a module named example. The function takes in two numbers and returns their sum.

How to import modules in Python?

We use the import keyword to do this. To import our previously defined module example, we type the following in the Python prompt.Using the module name we can access the function using the dot . operator. For example:

List Comprehensions

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

Syntax

newlist = [expression for item in iterable if condition == True]

Look at the both examples to understand the difference:

Iterators

An iterator is an object that contains a countable number of values.An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().

Example

mytuple = (“apple”, “banana”, “cherry”)
myit = iter(mytuple)

print(next(myit))
print(next(myit))
print(next(myit))

Syntax

Note: Even strings are iterable objects, and can return an iterator

Generators

Generators are very easy to implement, but a bit difficult to understand.

Generators are used to create iterators, but with a different approach. Generators are simple functions which return an iterable set of items, one at a time, in a special way.

When an iteration over a set of item starts using the for statement, the generator is run. Once the generator’s function code reaches a “yield” statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn.

Here is a simple example of a generator function which returns 7 random integers:

Classes and Objects

Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a “blueprint” for creating objects.

Now we’ll see how we can create a class and object..

The examples above are classes and objects in their simplest form, and are not really useful in real life applications.

To understand the meaning of classes we have to understand the built-in __init__() function.

The __init__() Function

All classes have a function called __init__(), which is always executed when the class is being initiated.

Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created.

Example

Create a class named Person, use the __init__() function to assign values for name and age:

Note: The __init__() function is called automatically every time the class is being used to create a new object.

Descriptors

Python descriptors are created to manage the attributes of different classes which use the object as reference. In descriptors we used three different methods that are __getters__(), __setters__(), and __delete__(). If any of those methods are defined for an object, it can be termed as a descriptor. Normally, Python uses methods like getters and setters to adjust the values on attributes without any special processing. It’s just a basic storage system. Sometimes, You might need to validate the values that are being assigned to a value. A descriptor is a mechanism behind properties, methods, static methods, class methods, and super().

Example

Creating descriptors using class methods

This example demonstrates the simplicity of controlling attribute assignment in Python.

Thank you for reading my blog, please share your valuable feedback so that I can improve myself because I’m a absolute beginner in writing bogs.

Passionate about ML