Python Cheat Sheet - Studyopedia

Python Cheat Sheet

Python Cheat Sheet will guide you to work on Python with basics and advanced topics. Cheat Sheet for students, engineers, and professionals.


Python is a powerful, interpreted, object-oriented programming language. It is used in many areas for development and is considered a perfect language for scripting.

Features of Python

  • Cross-platform
  • Open Source
  • Multiple Programming-paradigms
  • Fewer lines of code

Install and Run Python

Python can be easily installed on any OS. You can also use any of the below platforms to run Python:


Variables in Python are reserved memory locations. You only need to assign a value in Python, for variable declaration and that’s it!

Scope of Variables

The existence of a variable in a region is called its scope, or the accessibility of a variable defines its scope:

  • Local Scope: Whenever a variable is defined in a function, it can be used only in that function.
  • Global Scope: A variable has Global Scope, if it’s created outside the function i.e. the code body, and is accessible from anywhere.
  • GLOBAL keyword: To change the value of a global variable inside the function, use the GLOBAL keyword


Python Tokens are individual units in a program. Following are the types of Tokens:

  • Keywords: Keywords are reserved words used in programming languages, for example, if, else, not, continue, return, try, etc.
  • Identifiers: Identifiers in Python are used for naming variables, functions, and arrays. Do not use keywords as identifiers.
  • Literals: Literals are the values assigned to each constant variable:
    • String Literals: Enclose the text in quotes. Can use both single as well as double quotes. Use triple quotes for multi-line strings.
    • Numeric Literals: Includes int, long, float, and complex.
    • Boolean Literals: Can have either True or False values


Python Operators perform operations by taking one or more values, to give another value. The following are the operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison/ Relational operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators

Type Conversion

Type Conversion in Python allows users to convert int type to another, for example, float to int, int to complex, int to float, etc. To convert a number from one type to another, for example, int to float, we have the following methods in Python:

  • float(num): To convert from num int to float type.
  • int(num): To convert num to int type.
  • long(num): To convert num to long type.
  • complex(num): To convert from num int to complex type.
  • Complex (Num1, num2): To convert num1 and num2 to complex type, wherein num1 will be the real part, whereas num2 will be the imaginary part with j.

Get User Input

With Python, easily get user input, instead of hard coding the values. Similar to Java, C++, and C, Python also supports the concept of user input. The input() method in Python is used to get input from the user.


Decision-Making Statements

Take decisions based on different conditions in Python:

  • if statement
  • if-else statement
  • if…elif…else statement
  • break statement: The current loop is terminated and the execution of the program is aborted. After termination, the control reaches the next line after the loop.
  • continue statement: The continue statement in Python transfers control to the conditional expression and jumps to the next iteration of the loop.


Loops execute a block of code and this code executes while the condition is true:

  • while loop
  • for loop


To store Number types, such as integers, floats, etc., In Python3, Numbers include int, float, and complex datatypes,



Strings in Python are sequences of characters that can be easily created:

  • Create a String:
  • Slicing to access substrings:
  • Accessing a character:

  • Concatenate Strings:

  • String Operators:String Operators in Python


A function in Python is an organized block of reusable code, that avoids repeating the tasks again and again. Functions in Python begin with the keyword def and are followed by the name of the function and parentheses.

  • Create a Function:
  • Function Parameters:

Lambda Functions

A lambda function is a function without a name i.e. an anonymous function. The keyword lambda is used to define a lambda function in Python and has only a single expression.

Multiply a number by an argument with lambda:

Classes & Objects

A class is the basis of object-oriented programming in Python. A class is a template for an object, whereas an object is an instance of a class. Example of a Class:

Example of Objects:



Tuple is a sequence in Python, a collection of objects. Also, Python Tuples are immutable i.e. you cannot update a tuple or any of its elements.

  • Create a Tuple with string elements:
  • Create a Tuple with int elements:
  • Access values in a Tuple:
  • Fetch a specific element with indexing in Tuples:
  • Length of Tuple:
  • Get the maximum value from a Tuple:
  • Get the minimum value from a Tuple:


The dictionary represents the key-value pair in Python, enclosed in curly braces. Keys are unique and a colon separates it from value, whereas comma separates the items.

  • Create a Dictionary:

    Python Dictionary
  • Access values in a Dictionary:

  • Print all the keys of a Dictionary:

  • Print all the values of a Dictionary:

  • Get the length of a Dictionary:


Lists in Python are ordered. It is modifiable and changeable, unlike Tuples.

Python Tuples vs Python Lists

  • Create a List with int elements:
  • Create a List with string elements:
  • Access values from a List:
  • Indexing in Lists:
  • Get the length of a List:

  • Get the maximum from a List:
  • Get the minimum from a List:


A Set is a collection in Python. Set items i.e. elements are placed inside curly braces {}:

  • Create a Set with int elements
  • Create a Set with string elements

  • Get the length of a Set

  • Return the difference between the two sets:

  • Keep only the duplicate items in the two sets:

  • Keep all the items in the two sets except the duplicates:

  • Union of Sets


The modules in Python are used to organize the code. We can easily import a module in Python using the import statement:


Types of modules:

  • math: The math module in Python is used to perform mathematical tasks, with functions, including, trigonometric, logarithmic, etc:
  • statistics:
    The statistics module in Python is used to perform statistical tasks, with statistical functions, including, mean, median, mode, stdev(), etc.
  • random
    The random module in Python is used to form random numbers. Here, we will see how to import a random module in a Python program.

Learn Python via video course (English)

Learn Python via video course (Hindi)

What next?

After completing Python, follow the below tutorials and learn Python Libraries:
Python Programming Examples
Python Online Quiz
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment