Python 3 cheat sheet

Hi, I’ve created this Python 3 cheat sheet to help beginners to learn python. I’ll be adding new content over the next few days.

Basics

The print() function displays the given message on the screen.

Variables

In programming, a variable is a value that can be changed. All variables will have a storage location and a name. The variable name is usually used to refer to the value that is stored in a variable.

In Python, a variable is declared when you set a value to the variable. Unlike some other programming languages, Python does not have special keywords for declaring a variable.

Read more about Python variables.

Type conversion

We perform type conversions to convert a variable from one data type to another.

Generally, there are two types of type conversions.

  • Implicit conversions – This conversion is type-safe and requires no special code. It is performed by the compiler and no data loss occurs.
  • Explicit conversions – These conversions require a cast operator. Explicit conversion cannot be made without the risk of losing information. A cast is a way of informing the compiler that you wish to make a conversion and you are aware of the possible data loss.

Conditional statements

These statements are used to make decisions based on one or more conditions. If the condition specified in a conditional statement is found true, then a set of instructions will be executed and otherwise, something else will be executed.

Ternary operator

It is a special conditional operator that compares two values and determines the third value based on the comparison.

Loops

With the help of a loop or looping statements, you can execute a statement or a set of statements until an expression is evaluated to false.

Functions

A function contains a set of statements that creates output based on the given input or parameters.

List

In Python, a list is an ordered collection of items of different data types. The items of a list are enclosed in [ ] brackets. Each list item is separated by a comma (,).

Create a list

Add elements

Remove elements

Modify elements

Sorting

Tuple

A list is an ordered collection of items of various data types. The items of a tuple are enclosed in () brackets. Each list item is separated by a comma (,). Unlike a list, the elements of a tuple cannot be modified.

Dictionary

A dictionary is a collection of data as key-value pairs. Each item of a dictionary is contained in {} brackets.

Create a dictionary

Add elements

Remove elements

Sets

Similar to a dictionary, the elements of a set are contained in {} brackets. The main difference between sets and other datatypes like list, tuple, and dictionary is that sets do not have an index.

Create a set

Add / remove items

Comprehension

Comprehensions in Python provide us with a short and succinct way to create new lists, set, or dictionary using iterable objects that have been already defined. Python supports the following comprehensions:

  • List Comprehensions
  • Dictionary Comprehensions
  • Set Comprehensions

Generator expression

Similar to comprehensions, generator expressions are also a convenient way to create iterable objects.

Unpacking

Unpacking is a method by which we extract the values of an iterable object to separate variables.

Lambda

lambda expression is a special syntax to create functions without names.

Datetime module

Python DateTime module comes with everything you need to access date and time.

Exception handling

Exception handling is the mechanism by which errors in the application are captured and handled. It is one of the most important features of a programming language.

In Python, Exception handling is done with the help of the following keywords:

  • try
  • except
  • finally
  • raise

Class

A class is a blueprint of an object. An object in the real world will have properties like shape, color, weight, etc. Likewise, in object-oriented programming, a class defines certain properties, events, and functionalities that an object can have.

Single inheritance

Single inheritance enables a derived class to inherit properties and behavior from a single parent class.

Multiple inheritance

Single inheritance enables a derived class to inherit properties and behavior from multiple classes.

Multilevel inheritance

Multiple inheritance means that a class is inheriting the properties of a derived class.

Abstract classes

Generally, an abstract class is a class that contains abstract methods and cannot be instantiated. It serves as a blueprint for other classes expecting its child classes to implement its abstract methods.

Python by default does not provide abstract classes. But it has a module named ABC which provides the base for defining Abstract Base classes(ABC).

Attribute methods

These methods allow you to get or check the properties of an object.

Class methods

Class methods are not specific to any instances. So, they can be accessed without creating an object. It can access and modify the state of a class.

Class methods can access and modify the state of an instance.

Static methods

Like class methods, static methods can also be called without creating an object. But it cannot access or modify the state of a class. Also, it does not accept the class parameter and is created only once.

Private methods

Private methods are methods that cannot be accessed outside the class and by child classes using inheritance.


Leave a Reply

Your email address will not be published.Required fields are marked *