PYTHON CONCEPTS
CONTENTS
LIST COPREHENSIONS
LAMBDA FUNCTIONS
FILTER FUNCTIONS
MAP FUNCTIONS
OOPS FEATURES (object oriented programming structures)
FILE HANDLING
EXCEPTION HANDLING
LIST COMPREHENSION
List comprehension is an easy to read, compact, and elegant way of creating a list from any
existing iterable object
it offers a shorter syntax when you want to create a new list based on the values of an existing list.
Advantages:
Conciseness and Readability.
Efficiency
Filters and maps the items in a list
Versatility
Example:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
LAMBDA FUNCTIONS
Lambda functions are also known as anonymous functions or nameless function.
Lambda function take one or more functions as arguments and returns only one expression.
They are used very often within higher-order functions like map() and filter() .
Keyword: ‘’lambda’’
Importance of lambda in python:
Simplicity
Use with other functions
Function programing
Example
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, numbers))
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(squared) # [1, 4, 9, 16]
print(evens) # [2, 4]
FILTER FUNCTIONS
The filter() function returns an iterator where the items are filtered through a function
to test if the item is accepted or not.
Syntax : filter(function, iterable)
EXAMPLE:
def even(x):
return x%2==0
Num=[1,2,3,4,5,6,7,8,9,10]
Check even=(list(filter(even , Num)))
Print(check even)
MAP FUNCTION
map() function returns a map object(which is an iterator) of the results after
applying the given function to each item of a given iterable (list, tuple etc.)
Map has a more efficient memory utilization than conventional loops
We can pass one or more iterable to the map() function.
EXAMPLE:
def addition(n):
return n + n
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
OUTPUT:[2, 4, 6, 8]
OOPs FEATURES
In Python, Object-Oriented Programming Structures (OOPs) is a programming
paradigm that uses objects and classes in programming. It aims to implement real-
world entities like inheritance, polymorphisms, encapsulation, etc..
Types of OOPs concepts:
1.Class
2.Objects
3.Encapsulation
4.Polymorphisim
5.Inheritance
6.Abstaction
CLASSES AND OBJECTS:
Python Class & Object:
A class is a code template for creating
objects.
Created using the keyword – class.
Objects have member variables and
have behavior associated with them.
An object is created using the
constructor of the class. This object will
then be called the instance of the class.
ENCAPSULATION:
Encapsulation describes the idea of wrapping data and the methods that work on data within one
unit.
We can Access and Modify data using Setters and Getters Methods.
To put restrictions on accessing variables and methods directly and can prevent the accidental
modification of data.
INHERITANCE:
It is a mechanism that allows you to create a hierarchy of classes that share a set of properties and
methods by deriving a class from parent class.
• It represents real-world relationships well.
• It provides the reusability of a code.
• It allows us to add more features to a class without modifying it.
Polymorphism:
The word polymorphism means having many forms.
In programming, polymorphism means the same function name (but different
signatures) being used for different types.
The key difference is the data types and number of arguments used in function.
Abstraction:
Abstraction is used to hide irrelevant details from the user and show the details that are
relevant to the users.
we can achieve data abstraction by using abstract classes and abstract classes can be
created using abstract base class module and abstract method of a b c module.
FILE HANDLING
File handling in Python is an important part of any web application.
File handling helps in storing a massive collection of data across various types of files.
Python supports a wide range of functions to create, read, update, and delete files.
Modes in File Handling:
• ‘R' – Read Mode: To read data from the file.
• ‘w' – Write Mode: To write data into the file or modify it.
overwrites the data
• ‘A' – Append Mode: To append data to the file.
• ‘R+' – Read or Write Mode: To write or read the data from the same file.
• Write and Read (‘w+’) : Open the file for reading and writing. truncated and over-written.
How File Handling modes work:
• Read a file in python:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read())
Output:
Hello World Hello Python Good Morning
Write a file in python
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.write(“Hello World”)
The above code writes the String ‘Hello World’ into the ‘test.txt’ file
Append in a Python File
my_file = open(“C:/Documents/Python/test.txt”, “a+”)
my_file.write (“Strawberry”)
The above code appends the string ‘Strawberry’ at the end of the‘test.txt’ file
Read and Write in a Python File
my_file= open((“C:/Documents/Python/test.txt”, 'r+') as file:
content = file. Read()
print(content)
file. Write('Appending new content’)
Write and Read in a Python File
my_file = open(“C:/Documents/Python/test.txt”'w+') as file:
my_file.write('This will overwrite existing content')
content = my_file.read()
EXCEPTION HANDLING
Exception handling is a mechanism that allows you to handle errors that occur during
the execution of your program. In Python, exceptions are handled using the try and
except blocks.
The try block contains the code that may raise an exception. The except block contains
the code that will be executed if an exception is raised.
Here is an example of exception handling in Python:
Blocks in Exception Handling:-
TRY block:
This is the block where we write the suspicious code of the programme i.e, the code which
might raise a exception.
EXCEPT block:
This the block where is suspicious code’s exception is handled.
This block gets executed only when try block raises an exception.
ELSE block:
Code that runs if the try block does not raise an exception.
FINALLY block:
The finally block gets executed regardless of whether the try block raises an exception or not.
This can be useful for cleaning up resources, such as closing files or database connections.
THANK YOU