# Python 3 Ramp-Up Guide Python ramp-up guyide: List of topics to google for, with some inline help in the form of code snippets or links. Test online before having your CLI ready at: https://www.programiz.com/python-programming/online-compiler/ ## Introduction to Python - What is Python? - [Installing Python](https://www.python.org/) - Running Python code (using IDLE, command line, or integrated development environments like PyCharm) ## Basic Syntax - Comments - Variables and data types (integers, floats, strings, booleans) - Basic operations (arithmetic, comparison, logical) - Basic input/output (print(), input()) ```python= # Example of basic syntax # Comments # This is a comment # Variables and data types num = 10 name = "John" is_valid = True # Basic operations result = 10 + 5 ``` ## Control Flow - Conditional statements (if, elif, else) - Loops (for, while) - Loop control statements (break, continue) ```Python= # Example of control flow # Conditional statements x = 10 if x > 5: print("x is greater than 5") # Loops for i in range(5): print(i) # While loop num = 0 while num < 5: print(num) num += 1 # Exception Handling with try-except block try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero") ``` - Exception Handling (try-except blocks) ```python= # Example of exception handling try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero") finally: print("Always run when exiting the block.") ``` ## Data Structures - Lists - Tuples - Dictionaries - Sets ```python= # Example of data structures # Lists my_list = [1, 2, 3, 4, 5] # Tuples my_tuple = (1, 2, 3) # Dictionaries my_dict = {"key1": "value1", "key2": "value2"} # Sets my_set = {1, 2, 3} ``` ### Iterating over your data sructures: ```python= # Sample list and dictionary objects sample_list = ['apple', 'banana', 'orange', 'grape'] sample_dict = {'name': 'John', 'age': 30, 'city': 'New York'} # Using while loop with a list print("Using while loop with a list:") index = 0 while index < len(sample_list): print(sample_list[index]) index += 1 # Using for loop with a dictionary print("\nUsing for loop with a dictionary:") for key in sample_dict: print(key, ":", sample_dict[key]) # Using for loop with enumerate on a list print("\nUsing for loop with enumerate on a list:") for index, fruit in enumerate(sample_list): print("Index:", index, "Fruit:", fruit) # Using 'in' keyword to check if a value exists in a list print("\nUsing 'in' keyword to check if a value exists in a list:") if 'banana' in sample_list: print("Yes, 'banana' exists in the list.") # Using 'in' keyword to check if a key exists in a dictionary print("\nUsing 'in' keyword to check if a key exists in a dictionary:") if 'age' in sample_dict: print("Yes, 'age' exists in the dictionary.") ``` ### Slicing / masks In this example, slice_mask represents the slice notation numbers[2:5:2], which selects elements from index 2 to 5 (exclusive) with a step of 2. The resulting masked_numbers2 contains the elements selected by this slicing mask. ```python= # Example of slicing with masks # Creating a list of numbers from 0 to 9 numbers = list(range(10)) # Using slicing with masks to select specific elements mask1 = [True, False, True, False, True, False, True, False, True, False] masked_numbers1 = [numbers[i] for i, m in enumerate(mask1) if m] # Using slicing with masks using slice notation # (start, end, step) slice_mask = slice(2, 5, 2) masked_numbers2 = numbers[slice_mask] # Equivalent mask masked_numbers3 = numbers[2:5:2] # For 2D arrays a mask looks like: subsampling = twoDList[2:5:2, 1:6:3] print(masked_numbers1) # Output: [0, 2, 4, 6, 8] print(masked_numbers2) # Output: [2, 4] print(masked_numbers3) # Output: [2, 4] ``` ## Functions - Defining functions - Parameters and arguments - Return statement - Scope of variables ```python= # Example of functions def add_numbers(x, y): return x + y result = add_numbers(5, 3) print(result) ``` ## Modules and Packages - Importing modules: `import math` at the top of files. - [Creating and using packages](https://packaging.python.org/en/latest/glossary/#term-Source-Distribution-or-sdist) ## File Handling - Opening and closing files - Reading from and writing to files - File modes (read, write, append) ```python= # Example of file handling # Writing to a file with open("file.txt", "w") as f: f.write("Hello, World!") # Reading from a file with open("file.txt", "r") as f: data = f.read() print(data) ``` ## Object-Oriented Programming (OOP) - Classes and objects - Attributes and methods - Inheritance - Polymorphism ```python= # Example of object-oriented programming from abc import ABC, abstractmethod class ParentClass: def __init__(self, arg): self.arg = arg @abstractmethod def method(self): pass @staticmethod def static_method(): print("Static method") class ChildClass(ParentClass): def __init__(self, arg1, arg2, arg3): super().__init__(arg1) self.arg2 = arg2 self.arg3 = arg3 def method(self): print("Method") # Factory class class Factory: @staticmethod def create_child(arg1, arg2, arg3): return ChildClass(arg1, arg2, arg3) ``` ## Advanced Topics (optional) - List comprehensions: `[x for x in range(10)]` - Lambda functions: `lambda x: x * 2` - Generators and iterators: `generator = (x for x in range(10))` - Decorators: `@decorator_function` - Virtual environments and packaging: `pip install virtualenv` ## Working with External Libraries - Installing external libraries (using pip) - Exploring popular libraries (e.g., NumPy, pandas, matplotlib) ## Practical Projects and Exercises - Solving coding challenges - Building simple applications - Participating in coding competitions or hackathons ## Project structuring: https://docs.python-guide.org/writing/structure/ or Google "Python3 project structure best practices". ## Awesome sample projects Stepper motor library featuring: * no UI! * multiprocess for real parallelism. * Well designed OOP collaborations for extensibility. https://github.com/juanmf/StepperMotors With that, Here's a list of some of the best Python repositories to read and learn from in terms of best practices and documentation: The Python Standard Library: The official Python documentation provides detailed explanations and examples for all modules in the standard library. It's an excellent resource for learning about Python's built-in functionality and best practices. You can find it at: https://docs.python.org/3/library/index.html Requests: A popular HTTP library for Python, Requests has well-documented code and is widely considered a model of clean, readable Python. It's great for learning about network programming and best practices in Python. Repository: https://github.com/psf/requests Django: A high-level Python web framework that encourages rapid development and clean, pragmatic design. Django's documentation is renowned for its thoroughness and clarity, making it an excellent resource for learning about web development in Python. Repository: https://github.com/django/django Flask: Another popular web framework for Python, Flask is lightweight and easy to learn. Its documentation is well-organized and provides clear examples, making it a great resource for beginners. Repository: https://github.com/pallets/flask Pandas: A powerful data manipulation and analysis library for Python, Pandas has extensive documentation with examples and best practices for working with data in Python. Repository: https://github.com/pandas-dev/pandas Numpy: A fundamental package for scientific computing with Python, Numpy provides support for arrays, matrices, and mathematical functions. Its documentation is comprehensive and includes examples of usage and best practices. Repository: https://github.com/numpy/numpy Scikit-learn: A machine learning library for Python, Scikit-learn has well-documented code and extensive tutorials covering a wide range of machine learning techniques and best practices. Repository: https://github.com/scikit-learn/scikit-learn PyTorch: An open-source machine learning framework that accelerates the path from research prototyping to production deployment. PyTorch has excellent documentation with tutorials, examples, and best practices for deep learning in Python. Repository: https://github.com/pytorch/pytorch FastAPI: A modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints. It's well-documented with clear examples and best practices. Repository: https://github.com/tiangolo/fastapi Tornado: A Python web framework and asynchronous networking library. Tornado's documentation covers its usage and best practices for building scalable and non-blocking web applications. Repository: https://github.com/tornadoweb/tornado These repositories cover a wide range of topics and provide excellent resources for learning Python programming, web development, data manipulation, machine learning, and more. You can explore their documentation, codebase, and examples to gain insights into best practices and improve your Python skills.