Pandas_Cheat_Sheet.pdf

  1. Functions: Functions are blocks of reusable code. They are defined using the def keyword. Here's a simple example:

    def greet(name):
        print("Hello, " + name + "!")
    
    greet("John")
    
    
  2. Lists: Lists are ordered, mutable sequences. They can contain elements of different types.

    pythonCopy code
    my_list = [1, 2, 3, "four", 5.0]
    print(my_list[0])  # Accessing elements
    my_list.append(6)  # Modifying the list
    
    
  3. Dictionaries: Dictionaries are collections of key-value pairs. They are unordered and mutable.

    pythonCopy code
    my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
    print(my_dict['name'])  # Accessing values
    my_dict['age'] = 26    # Modifying values
    
    
  4. Tuples: Tuples are ordered and immutable sequences. They are similar to lists but cannot be modified once created.

    pythonCopy code
    my_tuple = (1, 2, 3)
    print(my_tuple[0])  # Accessing elements
    
    
  5. Sets: Sets are unordered collections of unique elements.

    pythonCopy code
    my_set = {1, 2, 3, 3, 4}
    print(my_set)  # Outputs: {1, 2, 3, 4}
    
    
  6. Classes and Objects: Python is an object-oriented language. You can define classes and create objects from those classes.

    pythonCopy code
    class Dog:
        def __init__(self, name):
            self.name = name
    
        def bark(self):
            print("Woof!")
    
    my_dog = Dog("Buddy")
    my_dog.bark()
    
    
  7. Modules and Packages: Python code can be organized into modules, and modules can be grouped into packages. This helps in organizing and reusing code.

    pythonCopy code
    # module.py
    def say_hello():
        print("Hello from the module!")
    
    # main.py
    import module
    module.say_hello()
    
    
  8. File Handling: Python provides various functions for reading from and writing to files.

    pythonCopy code
    with open('example.txt', 'w') as file:
        file.write('Hello, World!')
    
    
  9. Exception Handling: Use try-except blocks to handle exceptions and prevent program crashes.

    pythonCopy code
    try:
        result = 10 / 0
    except ZeroDivisionError:
        print("Cannot divide by zero!")
    
    
  10. List Comprehensions: A concise way to create lists.

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

These are just a few of the essential features in Python. Familiarizing yourself with these concepts will provide a solid foundation for more advanced programming in Python.


  1. Generators and Iterators: Generators allow you to create iterators in a more concise way. They are defined using functions with the yield keyword.

    pythonCopy code
    def countdown(n):
        while n > 0:
            yield n
            n -= 1
    
    for i in countdown(5):
        print(i)
    
    
  2. Decorators: Decorators allow you to modify or extend the behavior of functions or methods.

    pythonCopy code
    def my_decorator(func):
        def wrapper():
            print("Something is happening before the function is called.")
            func()
            print("Something is happening after the function is called.")
        return wrapper
    
    @my_decorator
    def say_hello():
        print("Hello!")
    
    say_hello()
    
    
  3. Lambda Functions: Lambda functions are anonymous functions defined using the lambda keyword.

    pythonCopy code
    add = lambda x, y: x + y
    print(add(2, 3))
    
    
  4. List comprehensions with conditionals: You can include conditionals in list comprehensions for more complex filtering.

    pythonCopy code
    even_squares = [x**2 for x in range(10) if x % 2 == 0]
    
    
  5. Map, Filter, and Reduce: These are higher-order functions that operate on lists or other iterables.

    pythonCopy code
    numbers = [1, 2, 3, 4, 5]
    squared = list(map(lambda x: x**2, numbers))
    evens = list(filter(lambda x: x % 2 == 0, numbers))
    product = functools.reduce(lambda x, y: x * y, numbers)