Table of Contents

Python Cheatsheet

Basic Syntax

```python

  x = 5
  name = "John"
  ```

```python

  # This is a single-line comment
  """
  This is a
  multi-line comment
  """
  ```

Data Types

Control Flow

```python

  if x > 5:
      print("x is greater than 5")
  elif x == 5:
      print("x is 5")
  else:
      print("x is less than 5")
  ```

```python

    for i in range(5):
        print(i)
    ```

```python

    count = 0
    while count < 5:
        print(count)
        count += 1
    ```

Functions

```python

  def greet(name):
      return f"Hello, {name}"
  ```

```python

  add = lambda x, y: x + y
  print(add(2, 3))
  ```

Data Structures

```python

  lst = [1, 2, 3]
  lst.append(4)
  lst.remove(2)
  print(lst)
  ```

```python

  d = {"name": "Alice", "age": 25}
  print(d["name"])
  d["age"] = 26
  ```

Exceptions

```python

  try:
      result = 10 / 0
  except ZeroDivisionError:
      print("Cannot divide by zero")
  finally:
      print("End of try-except block")
  ```

File Handling

```python

  with open("file.txt", "r") as file:
      content = file.read()
  print(content)
  ```

```python

  with open("file.txt", "w") as file:
      file.write("Hello, World!")
  ```

Modules

```python

  import math
  print(math.sqrt(16))
  ```

```python

  # In mymodule.py
  def add(x, y):
      return x + y
  # In main file
  from mymodule import add
  print(add(3, 4))
  ```

Classes & Objects

```python

  class Person:
      def __init__(self, name, age):
          self.name = name
          self.age = age
      def greet(self):
          print(f"Hello, my name is {self.name}")
  p = Person("Alice", 25)
  p.greet()
  ```

Common Built-in Functions

Useful Tips

```python

  squares = [x**2 for x in range(5)]
  print(squares)
  ```

```python

  a, b = 1, 2
  print(a, b)
  ```

Conclusion

This cheatsheet covers the basics of Python syntax and essential concepts for quick reference. For more details, refer to the official Python documentation.