Python Course

website·Indexed 30 days ago

https://codedocs.netlify.app/

A comprehensive guide to Python programming covering installation on Windows, PIP package management, basic concepts, data structures, methods, typing, standard libraries, and Object-Oriented Programming (POO).

Tokens
6.3K
Snippets
72
Records
77
Agent score
75%

What's inside Python

  1. Overview of Python, Java, and C++ use cases

    Different languages are suited for different development tasks:

    • Python: A general-purpose language used for data science, machine learning, web development, game creation, cross-platform apps, and scripting.
    • Java: An object-oriented, portable language used for enterprise software, web and mobile applications, cross-platform apps, and games (e.g., Minecraft).
    • C++: An extension of C used for high-performance software, operating systems (Windows, Linux), graphics engines (Unreal Engine), and the creation of other high-level languages.
  2. Understand Python execution and indentation rules

    Python executes code in a cascade (top-to-bottom). It uses indentation (typically 4 spaces) to define code blocks. Incorrect indentation will result in errors.
    # Correct indentation
    variable = 10
    if variable == 10:
        print('variable is equal to 10')
  3. Create anonymous functions with Lambda

    Lambdas allow the creation of anonymous functions (functions without an identifier). They consist of the lambda keyword, a comma-separated list of parameters, and a single expression representing the process/return value.
    # Create a lambda function that adds two numbers
    x = lambda a, b : a + b 
    
    # Invoke the lambda function
    print(x(10, 10))
  4. Define constants using Enum

    Enums are created by defining a class that inherits from Enum in the standard library. They are used to create sets of constants or absolute data. Access the actual value of an enum member using the .value property.
    from enum import Enum
    
    class x(Enum):
      a : int = 1
      b : int = 2
      c : int = 3
    
    # Access the value of an element
    print(x.a.value)
  5. Import functions and modules in Python

    Python allows importing standard libraries, third-party packages (via pip), or custom local modules.

    • To import a specific function: from module import xfunction
    • To import everything from a module: from module import *
    # module.py
    def fib(n : int) -> None:
      a, b = 0, 1
      while a < n:
        print(a, end=' ')
        a, b = b, a+b
      print()
    
    # main.py
    from module import fib
    fib(20)
  6. Handle runtime errors with try-except-finally

    Exceptions are errors that occur during execution. Use try and except blocks to catch specific errors (like ZeroDivisionError) or general exceptions to prevent the program from crashing. The finally block always executes regardless of whether an error occurred.
    try:
      0 / 0 # Triggers ZeroDivisionError
    except ZeroDivisionError:
      print('0 entre 0 es indefinido.')
    except Exception:
      # Catch-all for any other exception
      ...
    except:
      # Another way to catch any exception
      ...
    finally:
      # This code ALWAYS executes
      print('Execution finished')
  7. Implement asynchronous functions with async and await

    The async keyword defines a coroutine (an asynchronous function). The await keyword is used inside an async function to pause execution until a task completes. To execute an asynchronous function from a synchronous context, use asyncio.run() from the asyncio module.
    import asyncio
    
    async def coroutine(x : int) -> int:
      # Wait for x seconds before returning
      await asyncio.sleep(x)
      return x
    
    # Execute the asynchronous function
    asyncio.run(coroutine(10))
  8. Implement conditional logic with if-elif-else

    Conditional structures (if, elif, else) execute blocks of code based on whether a condition evaluates to True. If a condition is False or None, the block is skipped.
    hi = 'hi'
    
    if hi == 'Hi':
      print('Son iguales')
    elif hi != 'HI':
      print('Son diferentes')
    else:
      # Executes if no previous conditions are met
      ...