Python Cheat Sheet

This cheat sheet covers some of the most commonly used Python syntax and functions, providing a quick reference for key concepts as you write and debug your programs.

Variables and Data Types

  • Integer: A whole number, e.g., x = 10

  • Float: A decimal number, e.g., y = 3.14

  • String: A sequence of characters, e.g., name = "Alice"

  • Boolean: A true or false value, e.g., is_valid = True

Basic Operations

  • Addition: x + y

  • Subtraction: x - y

  • Multiplication: x * y

  • Division: x / y

  • Exponentiation: x ** y (x raised to the power of y)

  • Modulo: x % y (remainder of x divided by y)

String Operations

  • Concatenation: "Hello" + "World""HelloWorld"

  • Length of a String: len("Hello")5

  • String Methods:

    • lower(): "Hello".lower()"hello"

    • upper(): "Hello".upper()"HELLO"

    • replace(): "Hello".replace("H", "J")"Jello"

Lists

  • Creating a List: my_list = [1, 2, 3]

  • Accessing Items: my_list[0]1

  • Appending an Item: my_list.append(4)[1, 2, 3, 4]

  • Removing an Item: my_list.remove(2)[1, 3, 4]

  • List Length: len(my_list)3

Dictionaries

  • Creating a Dictionary: my_dict = {"name": "Alice", "age": 25}

  • Accessing Values by Key: my_dict["name"]"Alice"

  • Adding a Key-Value Pair: my_dict["email"] = "[email protected]"

  • Removing a Key-Value Pair: my_dict.pop("age"){"name": "Alice", "email": "[email protected]"}

Control Flow

  • If Statement: if x > 10: print("x is greater than 10") elif x == 10: print("x is equal to 10") else: print("x is less than 10")

  • For Loop: for item in my_list: print(item)

  • While Loop: while x < 5: print(x) x += 1

Functions

  • Defining a Function: def greet(name): return "Hello, " + name

  • Calling a Function: print(greet("Alice")) # Outputs: Hello, Alice

Input and Output

  • Printing to the Console: print("Hello, World!")

  • Getting User Input: name = input("What is your name? ")

Exception Handling

  • Try-Except Block: try: result = 10 / 0 except ZeroDivisionError: print("You can't divide by zero!")

Importing Modules

  • Importing a Module: import math print(math.sqrt(16)) # Outputs: 4.0

  • Installing and Using External Libraries (with pip): pip install requests

File Handling

  • Reading from a File: with open("file.txt", "r") as file: contents = file.read()

  • Writing to a File: with open("file.txt", "w") as file: file.write("Hello, World!")

Common Built-in Functions

  • Range: range(5) → Generates numbers from 0 to 4.

  • Type: type(x) → Returns the data type of x.

  • len(): len(my_list) → Returns the number of items in a list, string, etc.

  • max() / min(): max(my_list) → Returns the largest element; min(my_list) → Returns the smallest.

List Comprehension

  • Basic List Comprehension: squares = [x**2 for x in range(5)] # Outputs: [0, 1, 4, 9, 16]

Conclusion

This cheat sheet provides a quick reference to some of the most common Python concepts and syntax. As you continue learning, you’ll discover many more powerful features that Python offers. Keep this handy as you write code and explore new Python concepts!

Some content generated with AI

Last updated

Was this helpful?