Python Cheatsheet




Introduction

Python is an interpreted, high-level, dynamic, free, and open-source programming language. Both procedural and object-oriented programming are supported by it.


  • Free and Open Source
  • Object-Oriented Language
  • High-Level Language
  • Extensible feature
  • Interpreted Language:
  • Dynamically Typed Language
  • Allocating Memory Dynamically


Install

Download and Install Python IDE


Basics


Variables


Variable scope


Datatypes


List vs Tuples

# List

  • List are mutable
  • Iterations are time-consuming
  • Lists consume more memory
  • Inserting and deleting items is easier with a list.


# Tuple

  • Tuples are immutable
  • Iterations are comparatively Faster
  • Accessing the elements is best accomplished with a tuple data type.
  • Tuple consumes less memory than the list


Type casting

Sometimes we may need to convert one datatype to another, termed "type casting."


Operators


Strings


Boolean



Tuple


List


Sets


Dictionary


Control flow statements


Loops


Exception handling


Functions


Modules

A file with Python definitions and statements is known as a module. Variables, classes, and functions can all be defined in a module. Runnable code may also be included in a module. Code that has been grouped together into modules is simpler to read and utilise.

eg: os, sys

Packages
Python packages can contain several modules. A package is a directory of Python modules that differs from a directory intended to hold numerous Python scripts by including an additional __init .py file. Providing that each relevant directory has its own __init .py file, packages can be nested in various depths.

eg: pandas


Functional Programming
  • For the same input, this function always returns the same result.
  • It will not modify any argument and global variables.
  • Functional languages use recursion to implement iteration.
  • Functions can be Higher-Order and First-Class.
  • We can add new variables, but we cannot change any already existing variables.

If a programming language treats functions as first-class objects, then it is said to support first-class functions.
Higher-order functions are those that either return a function as their result or take one or more functions as arguments.


Class

A class in Python is a blueprint for creating objects, encapsulating both data (attributes) and behavior (methods) that are associated with those objects. It is a fundamental concept in object-oriented programming (OOP) that allows you to model and structure your code in a way that reflects the real-world entities and relationships within your program, making it more organized, reusable, and maintainable.


Inheritance

Inheritance in Python is a fundamental concept of object-oriented programming (OOP) that allows one class (the child class or subclass) to derive attributes and methods from another class (the parent class or superclass). This mechanism enables code reuse and the creation of a hierarchical structure of classes, where child classes inherit the characteristics of parent classes while also having the flexibility to add their own attributes and methods. Inheritance is a core principle in OOP, facilitating the organization and extension of code, as well as promoting modularity and reusability.


Constructor overriding

In Python, constructor overriding refers to the ability of a subclass (child class) to provide its own implementation of the constructor (__init__ method) inherited from its superclass (parent class). This allows the child class to customize the initialization process by defining its own attributes or behavior specific to the child class, while still having the option to call the constructor of the parent class using the super() function. Constructor overriding is a fundamental aspect of object-oriented programming (OOP) in Python, facilitating code reusability and extensibility in class hierarchies.





Comments

Post a Comment

Popular Posts