{ "cells": [ { "cell_type": "markdown", "id": "0ae4c48e", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "id": "55e48c41", "metadata": {}, "source": [ "# Python for Beginners using ChatGPT & Claude" ] }, { "cell_type": "markdown", "id": "a94c36ff-d17f-4828-adaf-5d624db2e11d", "metadata": {}, "source": [ "## Basics\n", "By the end of this section, you will be able to:\n", "1. Understand the concept of **Integrated Development Environments (IDEs)**\n", "2. Know how **AI tools** can help you learn faster but also realize their limitations\n", "3. Differentiate between Python **scripts** and Jupyter **Notebooks**\n", "4. Write and execute a basic **\"Hello World\"** program\n", "5. Recognize the importance of **code readability and PEP 8** guidelines\n", "\n", "### Comparison of Python, MATLAB, C++, and R\n", "\n", "| **Aspect** | Python | MATLAB | C++ | R |\n", "|------------|--------|--------|-----|---|\n", "| **Learning Curve** | Easy, clean syntax **(Most beginner-friendly)** | Moderate, good for math background | Steep, requires low-level understanding | Moderate, can be challenging for beginners |\n", "| **Performance** | Slower than C++, optimizable with NumPy | Good for matrix operations | Typically fastest | Can be slow for large datasets, optimized for stats |\n", "| **Use Cases** | General-purpose, web dev, data science, AI/ML **(Most Versatile)** | Engineering, scientific computing, signal processing | System/software dev, games, resource-intensive apps | Statistical computing, data analysis, bioinformatics |\n", "| **Data Analysis & Visualization** | Strong libraries (Pandas, Matplotlib) | Excellent built-in capabilities | Limited built-in, needs external libraries | Excellent tools (e.g., ggplot2) |\n", "| **Community & Ecosystem** | Large, active, vast library ecosystem **(Largest community)** | Smaller, strong in academia/engineering | Large, extensive domain libraries | Strong in statistics and data science |\n", "| **Cost** | **Free**, open-source | Proprietary, licensed (expensive) | Free compilers, some paid IDEs | Free, open-source |\n", "| **Language Integration** | Easy with C/C++ and others **(Excellent interoperability)** | Can integrate with C/C++, Java, Python | Integrates with most languages | Can integrate with C/C++ and Python |\n", "| **ML & AI Support** | Excellent (TensorFlow, PyTorch, scikit-learn) **(Leader in AI/ML tools)** | Good support, less extensive than Python | Used for low-level ML and optimization | Good for statistical learning, less for deep learning |\n", "\n", "I focus mainly on the science and engineering applications of Python but e.g. \n", "\n", "- **Netflix** uses Python extensively for its recommendation engine, data analysis, and backend services.\n", "- **YouTube** uses Python for video sharing and viewing functionality\n", "- **Instagram** uses Python (Django framework) for its backend\n", "- **BitTorrent** used Python for the original BitTorrent client\n", "\n", "### Integrated Development Environments\n", "\n", "I recommend either using Jupyter Lab Desktop (available for all operating systems) or Anaconda Cloud which doesn't require installation.\n", "Jupyterlab Desktop will run faster, however Anaconda Cloud has Anaconda AI Assistant built in for free. And if you are on a university computer where you can't install anything Anaconda Cloud is a good choice.\n", "\n", "If you want to know about the other options for IDEs and why I decided to choose Jupyterlab for this and my other courses check out these two videos:\n", "\n", "+ [13 Beginner-Friendly Python IDEs Compared in 2024: Jupyter Lab, VS Code, PyCharm, Wing, Zed and More](https://youtu.be/6lj-Mv25eWs)\n", "
\n", "\n", "+ [Choosing the Best Beginner Friendly Python IDE in 2024: VS Code vs. JupyterLab vs. Anaconda Cloud](https://youtu.be/U41WhFaggtA)\n", "\n", "\n", "\n", "### Jupyter Lab\n", "For a detailed video about the installation of JupyterLab Desktop check out this video:\n", " + [Jupyter Lab Desktop: Installation, Configuration, and Best Practices for Windows & Mac](https://youtu.be/Q5li7FMUKEk)\n", "" ] }, { "attachments": {}, "cell_type": "markdown", "id": "99b5e10f-c532-4594-a8d8-3e96a975c96f", "metadata": {}, "source": [ "### Line Width and Limiter Lines (PEP8)\n", "\n", "In this notebook, you'll notice two limiter lines: one at 80 characters and another at 100 characters. These lines relate to an important aspect of Python coding style.\n", "\n", "**PEP 8: The Style Guide for Python Code**\n", "\n", "PEP 8 is the official style guide for Python code. It provides guidelines to improve code readability and consistency across the Python community. One key recommendation concerns line length:\n", "\n", "> 🔍 **Guideline**: Keep lines of code between 79-99 characters long.\n", "\n", "**Why Limit Line Length?**\n", "\n", "1. **Readability**: Shorter lines are easier to read and understand.\n", "2. **Side-by-Side Viewing**: Allows multiple files to be open side-by-side.\n", "3. **Printing**: Ensures code prints well on standard paper or small screens.\n", "\n", "**Example**\n", "```python\n", "# This is a very long line of code that exceeds the recommended 79-character limit and might be hard to read\n", "result = some_long_function_name(first_long_parameter_name, second_long_parameter_name, third_long_parameter_name)\n", "\n", "# Better: Split into multiple lines\n", "result = some_long_function_name(\n", " first_long_parameter_name,\n", " second_long_parameter_name,\n", " third_long_parameter_name\n", ")\n", "```\n", "**More on Pep8** and good programming principles in the **advanced courses**:\n", "+ [Python Basics](https://training-scientists.com/python-basics-course/)\n", "+ [Python for Scientists & Engineers](https://training-scientists.com/python-for-scientists-and-engineers/)\n", "+ [Python for Biologists](https://training-scientists.com/python-for-biologists/)\n", "\n", " > You can **download** this Jupyter Notebook from the video description (as PDF or as a Jupyter Notebook).\n", "\n", " > There are exercises that you can get on my course website https://training-scientists.com (Python Beginner Course using AI)" ] }, { "cell_type": "markdown", "id": "0ae1a03e-8dae-45e5-ac38-b9f8edcd7fd8", "metadata": {}, "source": [ "### Anaconda Cloud\n", "For Anaconda Cloud \n", "> https://anaconda.cloud\n", "\n", "no installation is necessary, you can just create an Account on their website and start coding.\n", "\n", "While Jupyter Notebooks run perfectly, running Python scripts with graphical output does not work." ] }, { "cell_type": "markdown", "id": "53867bc4-c5f5-4ae1-8499-053a5ee7eb69", "metadata": {}, "source": [ "### AI Tools\n", "\n", "We will use ChatGPT, Claude and Anaconda Assistant in this course to help you learn programming faster.\n", "\n", "AI Tools are great at explaining code and concepts so you can 2X your learning curve.\n", "\n", "> To use Claude go to https://claude.ai create an account and start chatting with it.\n", "\n", "> To use ChatGPT do the same on: https://chatgpt.com\n", "\n", "E.g. Ask Claude 🤖💬:\n", "> 1. `Can you tell me how Python compares to Matlab, C++ and R?`\n", "> \n", "> 2. `Can you reformat that into a visually appealing table that I can copy paste into a Jupyter Notebook markdown cell?`\n", "\n", "Using AI tools is not cheating. Cars will look like cheating for someone who sells horse carriages. Or dinosaurs who don't want to learn something new.\n", "\n", "The code AI tools generate is not always working so we still need to learn programming ourselves.\n", "If you want to know more check out:\n", "\n", "+ [Can Claude 3.5 | ChatGPT 4o | GitHub Copilot build Snake & Electron Cloud simulation in Python? #GPT](https://youtu.be/m8YKaG4-_x8)\n", "\n", "\n", "\n", "+ [GitHub Copilot: Accelerating Coding or False Hope? | Reaction Video](https://youtu.be/vgsNdaxnXlE)\n", "\n", "\n", "+ [Debunking AI Myths: My Reaction to 'Why is everyone LYING?](https://youtu.be/KVLNA2231U4)\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "6413de94-9c67-4826-8834-2185b5d99360", "metadata": {}, "source": [ "### Python Scripts vs Jupyter Notebooks\n", "\n", "Scripts always run completely top to bottom, so if there is an error somewhere in the end you will need to change the code and run everything again. \n", "\n", "Whereas Notebooks you can run code cell by cell (line by line if you want to).\n", "This makes debugging and overall development a lot faster.\n", "+ You can show multiple plots, add text like this and structure the Notebook with a Table of Contents\n", "\n", "+ Jupyter Notebooks allow you to structure your code with markdown cells, headings etc.\n", "\n", "+ Scripts are better though if you want to run games (like snake) or simulations with a video like output\n", "\n", "+ Jupyter Notebooks have a lot of advantages but also some pitfalls like cell state we will look at later" ] }, { "cell_type": "markdown", "id": "a81c3b33-16a8-488b-a1aa-fe93127f49da", "metadata": {}, "source": [ "### Hello World (Jupyter) 👋\n", "This would not be a programming tutorial without a Hello World script" ] }, { "cell_type": "code", "execution_count": 1, "id": "fbd1ebd3-9d8f-4f2a-9ec6-c61593315156", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print(\"Hello World\")" ] }, { "cell_type": "markdown", "id": "59cc6c0f-15f9-41d4-acc6-4f321b058c45", "metadata": {}, "source": [ "### Hello World (Script)" ] }, { "cell_type": "markdown", "id": "70203aa5", "metadata": {}, "source": [ "## Variables & Data Types 🏷️\n", "By the end of this section, you will be able to:\n", "1. Define and use **variables** in Python\n", "2. Identify and work with different **data types** (int, float, string, boolean)\n", "3. Understand and use **f-strings** for string formatting\n", "4. Create and manipulate **lists, tuples, and dictionaries**\n", "5. Recognize the **appropriate use cases** for different data structures" ] }, { "cell_type": "code", "execution_count": 2, "id": "4f4f950d-8737-4774-9a75-041ec5517549", "metadata": { "ExecuteTime": { "end_time": "2022-03-08T14:59:03.154556Z", "start_time": "2022-03-08T14:59:03.140558Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n", "16\n", "1.3\n", "(5+3j)\n", "True\n" ] } ], "source": [ "z = \"Hello World\" # string\n", "x = 16 # integer\n", "u = 1.3 # float\n", "complex_number = 5+3j # complex\n", "on_or_off = True # boolean\n", "\n", "print(z)\n", "print(x)\n", "print(u)\n", "print(complex_number)\n", "print(on_or_off)" ] }, { "cell_type": "code", "execution_count": 3, "id": "a18384a6-e21f-4f79-8956-78d2111fe940", "metadata": { "ExecuteTime": { "end_time": "2022-03-08T14:59:03.200558Z", "start_time": "2022-03-08T14:59:03.188556Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "