Python Cheat Sheet Part - 4
Sandeep
Posted on May 5, 2022
File Handling
File handling refers to reading or writing data from files. Python provides some functions that allow us to manipulate data in the files.
open() function
var_name = open("file name", "opening mode")
close() function
var_name.close()
read () function
The read functions contains different methods, read(),readline() and readlines()
read() #return one big string
It returns a list of lines
readlines() #returns a list
It returns one line at a time
readline #returns one line at a time
write function
This function writes a sequence of strings to the file.
write() #Used to write a fixed sequence of characters to a file
It is used to write a list of strings
writelines()
append() function
The append function is used to append to the file instead of overwriting it. To append to an existing file, simply open the file in append mode by using 'a' as the second argument of open() as follows:
file = open("Hello.txt", "a")
Exception Handling
An exception is an unusual condition that results in an interruption in the flow of a program.
try and except
A basic try-catch block in python. When the try block throws an error, the control goes to the except block.
try:
[Statement body block]
raise Exception()
except Exception as e:
[Error processing block]
Object Oriented Programming (OOPS)
It is a programming approach that primarily focuses on using objects and classes. The objects can be any real-world entities.
class
The syntax for writing a class in python
class class_name:
pass #statements
class with a constructor
The syntax for writing a class with the constructor in python
class Example:
# Default constructor
def __init__(self):
self.name = "Example"
# A method for printing data members
def print_me(self):
print(self.name)
Creating an object
Instantiating an object can be done as follows:
<object-name> = <class-name>(<arguments>)
filter function
The filter function allows you to process an iterable and extract those items that satisfy a given condition
filter(function, iterable)
issubclass function
Used to find whether a class is a subclass of a given class or not as follows
issubclass(obj, classinfo) # returns true if obj is a subclass of classinfo
Iterators and Generators
Here are some of the advanced topics of the Python programming language like iterators and generators
Iterator
Used to create an iterator over an iterable
iter_list = iter(['Harry', 'Aakash', 'Rohan'])
print(next(iter_list))
print(next(iter_list))
print(next(iter_list))
Generator
Used to generate values on the fly
# A simple generator function
def my_gen():
n = 1
print('This is printed first')
# Generator function contains yield statements
yield n
n += 1
print('This is printed second')
yield n
n += 1
print('This is printed at last')
yield n
Decorators
Decorators are used to modifying the behavior of a function or a class. They are usually called before the definition of a function you want to decorate.
property Decorator (getter)
@property
def name(self):
return self.__name
setter Decorator
It is used to set the property 'name'
@name.setter
def name(self, value):
self.__name=value
deleter Decorator
It is used to delete the property 'name'
@name.deleter #property-name.deleter decorator
def name(self, value):
print('Deleting..')
del self.__name
Posted on May 5, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
June 19, 2024