Style:
Original
font-size
columns
#

Recommended

Variables and Data Types
Conditional Statements
Loops
Functions
Lists and Tuples
Dictionaries
Classes and Objects
File Handling
Exception Handling
Modules Packages
Regular Expressions
Pip & Virtual Environments
The Python Standard Library
terms and conditions
privacy policy
contact

Python

Author:fivver
Column 1

Creating a list

To create a new list in Python, use square brackets [] and separate the elements with commas. For example: my_list = [1, 2, 'hello', True]. Lists can contain different data types such as integers, strings or booleans.

Creating lists in python



List Functions

Here are some common list functions and methods in Python that you can use to manipulate and work with lists:

  1. len(list): Returns the number of elements in the list.
  2. sorted(list): Returns a new sorted list (does not modify the original list).
  3. sum(list): Returns the sum of all elements in the list (if they are numeric).
  4. max(list): Returns the largest element in the list.
  5. min(list): Returns the smallest element in the list.
  6. any(list): Returns True if at least one element in the list is True.
  7. all(list): Returns True if all elements in the list are True.


List Methods:

  1. list.append(item): Adds an item to the end of the list.
  2. list.extend(iterable): Adds all elements from an iterable to the end of the list.
  3. list.insert(index, item): Inserts an item at a specific index.
  4. list.remove(item): Removes the first occurrence of the specified item.
  5. list.pop([index]): Removes and returns the element at the specified index (last element if index is not provided).
  6. list.index(item): Returns the index of the first occurrence of the specified item.
  7. list.count(item): Returns the number of times the specified item appears in the list.
  8. list.sort(): Sorts the elements of the list in ascending order.
  9. list.reverse(): Reverses the order of the elements in the list.
  10. list.copy(): Returns a shallow copy of the list.
  11. list.clear(): Removes all elements from the list.


List Comprehensions:

List comprehensions are concise ways to create lists using a single line of code. They often involve iterating over an iterable and applying an expression to each element.

Example

squares = [x ** 2 for x in range(10)]

# Creates a list of squares from 0 to 9: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

These functions and methods are fundamental for working with lists in Python. Experimenting with them will help you become more comfortable and proficient in using lists effectively in your code.

Selecting List Elements

In Python, you can select or access elements from a list using indexing. Lists are zero-indexed, which means that the first element has an index of 0, the second element has an index of 1, and so on. Here are a few ways to select list elements in Python:

Remember that attempting to access an index that is out of the range of the list will result in an "IndexError". Always ensure that your indices are within the valid range of the list.

Column 2

Operators in Python

Python provides a variety of operators for performing different operations. Some common types of operators include arithmetic, assignment, comparison, logical and bitwise operators. Arithmetic operators are used to perform mathematical calculations such as addition (+), subtraction (-), multiplication (*), division (/) etc. Assignment operator (=) is used to assign values to variables. Comparison operators (==, !=, >,< ,>= ,<= ) are used for comparing two values or expressions and return True or False based on the result.

Assignment Operators

Assignment operators are used to assign values to variables. They combine the assignment operator (=) with another arithmetic or logical operator, such as += (add and assign), -= (subtract and assign), *= (multiply and assign), /= (divide and assign). These operators allow you to perform an operation on a variable while assigning the result back into the same variable in one step.

Logical operators

Logical operators in Python are used to combine multiple conditions and evaluate the result as either True or False. The three logical operators in Python are 'and', 'or', and 'not'.


- The 'and' operator returns True if both conditions on its left and right side are true.

- The 'or' operator returns True if at least one of the conditions on its left or right side is true.

- The 'not' operator negates a condition, returning False if it was originally true, and vice versa.

Numeric comparison operators

Numeric comparison operators are used to compare two numeric values in Python. Some commonly used numeric comparison operators include:


- Equal to (==): Returns True if the operands on both sides of the operator are equal.

- Not equal to (!=): Returns True if the operands on both sides of the operator are not equal.

- Greater than (>), Less than (<), Greater than or equal to (>=), and Less than or equal to (<=) : These operators return True or False based on whether one operand is greater, less, greater/equal, or less/equal compared with another.

Column 3

Open-source Python packages are important tools. Their powers expand the Python language. Install new packages, such as pandas, by typing pip install pandas in your command terminal. After installing a package, import it.

The Working Directory

The working directory is where Python reads and saves files by default. An example of a working directory is C://file/path. The os library sets and retrieves the working directory.

COMMONLY USED DATA TYPES

  • Variables hold data. No need to declare types.
  • Common data types: int, float, str, bool.



Input and Output:

  • print("Hello, world!") to display output.
  • input("Prompt: ") to get user input.


Conditional Statements:

  • if condition:
  • elif another_condition:
  • else:


Python Definition

Python is a high-level, interpreted programming language known for its simplicity and readability. It was created by Guido van Rossum in 1991. Python supports multiple paradigms including procedural, object-oriented, and functional programming. It has a large standard library that provides tools for various tasks such as web development, data analysis, machine learning, and more.

Some Basics Syntax

  • Statements end with a newline character (no semicolons).
  • Comments are preceded by a # symbol.
  • python is a case sensitive
  • python index starts from 0
  • python uses whitespaces (tab or spaces) to indent code instead of using braces

HELP

Module (Library)

python module is simply a '.py' file

https://www.cheatrepo.com/sheet/Python-ea7c48
Last Updated: Wed Oct 01 2025

Press space bar to start a drag. When dragging you can use the arrow keys to move the item around and escape to cancel. Some screen readers may require you to be in focus mode or to use your pass through key