Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Sandeep
Sandeep

Posted on • Edited on

     

Python Cheat Sheet Part - 4

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")
Enter fullscreen modeExit fullscreen mode

close() function

var_name.close()
Enter fullscreen modeExit fullscreen mode

read () function
The read functions contains different methods, read(),readline() and readlines()

read() #return one big string
Enter fullscreen modeExit fullscreen mode

It returns a list of lines

readlines() #returns a list
Enter fullscreen modeExit fullscreen mode

It returns one line at a time

readline #returns one line at a time
Enter fullscreen modeExit fullscreen mode

write function
This function writes a sequence of strings to the file.

write() #Used to write a fixed sequence of characters to a file
Enter fullscreen modeExit fullscreen mode

It is used to write a list of strings

writelines()
Enter fullscreen modeExit fullscreen mode

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")
Enter fullscreen modeExit fullscreen mode

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]
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

Creating an object
Instantiating an object can be done as follows:

<object-name> = <class-name>(<arguments>)
Enter fullscreen modeExit fullscreen mode

filter function
The filter function allows you to process an iterable and extract those items that satisfy a given condition

filter(function, iterable)
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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))
Enter fullscreen modeExit fullscreen mode

Generator
Used to generate values on the fly

# A simple generator functiondef 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
Enter fullscreen modeExit fullscreen mode

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)

@propertydef name(self):    return self.__name
Enter fullscreen modeExit fullscreen mode

setter Decorator
It is used to set the property 'name'

@name.setterdef name(self, value):    self.__name=value
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

Python cheat sheet part-1

Python cheat sheet part-2

Python cheat sheet part-3

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Developer 🧑‍🎓 Passionate about Web Development, Python Development and open-source. Book lover 📔 || Music Lover 🎶 || 💻 Internet surfer 🏄
  • Location
    Hyderabad, India
  • Education
    Master's in Computer Applications
  • Work
    Data Engineer
  • Joined

More fromSandeep

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp