Python Programming

Introduction to Python & Setup

5 sections AI-powered notes
GET THE FULL EXPERIENCE

This is the chapter notes. Students get the interactive version.

  • Ask Aarav Sir anything — instant voice + chat doubts
  • Interactive lessons with audio narration + visual diagrams
  • Study Lab — paste any photo, PDF, or YouTube link to get it explained

What is Python?

What is Python?

Welcome to the exciting world of Python programming! If you're new to coding, you've chosen an incredible language to start with. If you're an experienced developer looking to expand your toolkit, you're about to discover why Python is one of the most in-demand and versatile languages today.

This page will introduce you to Python: what it is, why it's so incredibly popular, and the vast array of problems it helps solve in the real world. By the end, you'll have a clear understanding of Python's power and potential.

So, What Exactly Is Python?

At its core, Python is a high-level, interpreted, general-purpose programming language. Let's break down what that means:

  • High-Level: You don't have to worry about complex machine code or memory management. Python handles many underlying complexities for you, allowing you to focus on solving problems with code that's closer to human language.
  • Interpreted: Unlike some languages that require a separate "compilation" step before running, Python code is executed line by line by an interpreter. This makes the development process faster and more interactive, as you can test small changes immediately.
  • General-Purpose: Python isn't designed for one specific task. It's a Swiss Army knife of programming languages, capable of handling a vast range of applications across many domains.
  • Developed by Guido van Rossum in the late 1980s and first released in 1991, Python was designed with an emphasis on code readability, using significant indentation to delineate code blocks. This design philosophy is one of its most celebrated features, making it easier to learn and work with.

Why Python's Popularity Keeps Soaring

Python has consistently ranked as one of the most popular programming languages for years, and for good reason. Its widespread adoption isn't just a trend; it's a testament to its practical advantages.

1. Simplicity and Readability

Python's syntax is famously clean and concise, often described as being close to plain English. This focus on readability significantly lowers the barrier to entry for beginners and makes collaboration easier for teams. You'll spend less time deciphering cryptic code and more time building solutions.

{{VISUAL: diagram: a side-by-side comparison of "Hello, World!" code in Python vs. a more verbose language like Java or C++ to highlight Python's simplicity.}}

2. Incredible Versatility

As a general-purpose language, Python excels in a multitude of fields. Whether you're building websites, crunching data, developing AI, or automating system tasks, Python likely has a solution. This versatility means that learning Python opens doors to many different career paths and project types.

{{VISUAL: diagram: an infographic showcasing various Python application domains like web development, data science, AI, automation, and game development, represented by distinct icons.}}

3. Massive Ecosystem and Community Support

Python boasts a colossal ecosystem of libraries, frameworks, and tools. From web development frameworks like Django and Flask, to powerful data science libraries like Pandas and NumPy, and machine learning powerhouses like TensorFlow and PyTorch – chances are, someone has already built a tool to help you with your task.

Beyond the tools, Python has one of the largest and most active communities in programming. This means abundant resources, tutorials, forums, and open-source contributions, making it easy to find help and learn from others.

4. Cross-Platform Compatibility

Python code can run on various operating systems—Windows, macOS, Linux, and more—with minimal or no changes. This "write once, run anywhere" capability is a huge advantage for developers, ensuring their applications reach a broader audience without extensive re-coding.

5. High Demand in the Job Market

The demand for Python developers continues to grow across industries. Companies of all sizes, from startups to tech giants, rely on Python for critical operations. Learning Python equips you with a highly sought-after skill, enhancing your career prospects significantly.

Python in Action: Real-World Applications

So, where exactly is Python being used out there in the wild? Everywhere! Here are just a few key areas where Python shines:

  • Web Development: Powering the backend of popular websites and web applications like Instagram, Spotify, Dropbox, and Reddit. Frameworks like Django and Flask are staples in the web development world.
  • Data Science & Machine Learning: Python is the undisputed champion in data analysis, visualization, artificial intelligence, and machine learning. Libraries such as NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, and PyTorch enable cutting-edge research and product development.
  • Automation & Scripting: From automating repetitive tasks on your computer to managing complex network systems or even controlling IoT devices, Python's simplicity makes it ideal for scripting.
  • Game Development: While not typically used for AAA titles, Python (with libraries like Pygame) is excellent for rapid prototyping, creating indie games, or developing game logic.
  • Desktop Applications: Creating graphical user interface (GUI) applications for desktops using libraries like Tkinter or PyQt.
  • Education: Due to its simplicity and readability, Python is a common introductory language in universities and coding bootcamps worldwide.

{{VISUAL: photo: a collage of logos representing popular Python libraries and frameworks (e.g., Django, Flask, Pandas, NumPy, TensorFlow, PyTorch, Selenium, Pygame) arranged around a central Python logo.}}

You've now got a solid foundation for understanding what Python is and why it's such a pivotal language in today's tech landscape. It's a powerful, flexible, and approachable tool that opens up a world of possibilities.

Next, we'll guide you through getting Python set up on your own computer, so you can start writing your very first lines of code!


Install Python Now

Install Python Now

Welcome back, future Pythonista! In the previous lesson, we learned what Python is and why it's a fantastic language to learn. Now, it's time to get our hands dirty and bring Python to life on your own computer. This page will guide you through the essential steps to download and install the Python interpreter, setting the stage for you to write your very first lines of code.

Installing Python isn't just about getting a program; it's about empowering your machine with the ability to understand and execute Python instructions. Think of it as teaching your computer a new language!

🚀 Getting Ready for Installation

Before we dive into the installation process, let's take a moment to prepare.

Python 3 is Your Friend

There are two major versions of Python: Python 2 and Python 3. While Python 2 was widely used for many years, it officially reached its "End of Life" in 2020. This means it no longer receives updates or support.

For all new learning and development, we will exclusively use Python 3. Make sure you always download a Python 3 version.

Check for Existing Python (Optional)

It's possible you already have Python installed on your system, especially if you're on macOS or Linux, or if you've installed other development tools. Let's quickly check:

  1. Open your Terminal (macOS/Linux) or Command Prompt (Windows).
    • Windows: Search for "cmd" or "Command Prompt" in the Start menu.
    • macOS: Search for "Terminal" in Spotlight (Cmd + Space).
    • Linux: Look for "Terminal" in your applications menu.
  2. Type the following command and press Enter:
    python3 --version
    
    Or, if that doesn't work:
    python --version
    

If you see an output like Python 3.x.x (e.g., Python 3.9.7), great! You might already have a suitable version. If you see Python 2.x.x or an error message like "command not found," then you definitely need to install Python 3.

Even if you have an older Python 3 version, it's a good practice to install the latest stable release to ensure you have all the newest features and security updates.

⬇️ Downloading Python

The official and safest place to download Python is from its official website.

  1. Visit the official Python website: Navigate your web browser to www.python.org.
  2. Go to the Downloads section: Hover over the "Downloads" menu item. The website is usually smart enough to detect your operating system and suggest the appropriate download.
  3. Choose the latest stable Python 3 release: Click the prominent button that says "Download Python 3.x.x" (where 3.x.x is the current latest stable version). This will typically download the 64-bit installer, which is suitable for most modern computers.

{{VISUAL: photo: screenshot of the Python downloads page on python.org, highlighting the latest stable release for download}}

Once the download is complete, locate the installer file in your "Downloads" folder. It will typically be named something like python-3.x.x-amd64.exe for Windows or python-3.x.x-macos.pkg for macOS.

💻 Installing on Windows

This is where many new Python users face their first hurdle, but don't worry, we'll walk through it step-by-step.

  1. Run the Installer: Double-click the downloaded .exe file.
  2. Crucial Step: Add Python to PATH!
    • On the very first installer screen, you will see a checkbox at the bottom labeled "Add python.exe to PATH". MAKE SURE TO CHECK THIS BOX.
    • Why is this important? The PATH environment variable tells your operating system where to look for executable programs. If Python isn't added to PATH, you'll have to navigate to Python's installation directory every time you want to run a script from the command prompt, which is cumbersome. Checking this box allows you to run python commands from any directory.
  3. Choose Installation Type:
    • For beginners, it's recommended to select "Install Now". This performs a standard installation with recommended features.
    • If you choose "Customize installation," ensure that "pip" and "Add Python to environment variables" are selected.
  4. Complete the Installation: Follow the on-screen prompts. The installation process might take a few minutes. Once it's complete, you'll see a "Setup was successful" message.
  5. Close the Installer: Click "Close."

{{VISUAL: diagram: annotated screenshot of the Python installer for Windows, specifically highlighting the "Add Python to PATH" checkbox and "Install Now" option}}

🍎 Installing on macOS

macOS typically comes with an older version of Python 2 pre-installed. We need to install Python 3 alongside it, and don't worry, they can coexist peacefully.

  1. Run the Installer: Double-click the downloaded .pkg file.
  2. Follow the Prompts: The installer will guide you through standard installation steps.
    • Click "Continue" multiple times to read the Read Me and License information.
    • Agree to the license terms.
    • Choose "Install" to perform a standard installation. You might be prompted for your administrator password.
  3. Complete Installation: Once the installation is finished, you'll see a "The installation was successful" message. Click "Close."
  4. Move to Trash (Optional): You can safely move the installer file to the trash after installation.

Important Note for macOS: Due to the pre-installed Python 2, you will generally use the command python3 instead of python in your terminal to explicitly invoke Python 3. We'll verify this in the next step.

🐧 Installing on Linux

Many Linux distributions come with Python 3 pre-installed. You can check this by opening a terminal and typing python3 --version.

If Python 3 is not installed or you want a newer version, the easiest way is usually through your distribution's package manager.

  • Debian/Ubuntu:
    sudo apt update
    sudo apt install python3
    sudo apt install python3-pip # Install pip for Python 3
    
  • Fedora/RHEL/CentOS:
    sudo dnf install python3
    sudo dnf install python3-pip
    
    (Or sudo yum install python3 on older RHEL/CentOS versions)

Linux users often have more control over their Python installations and might use tools like pyenv for managing multiple Python versions, but for a basic installation, the package manager is sufficient.

✅ Verifying Your Installation

Regardless of your operating system, this crucial step confirms that Python is correctly installed and accessible from your command line.

  1. Open a new Terminal or Command Prompt window. If you had one open during the installation, close it and open a fresh one to ensure environment variables are refreshed.
  2. Check Python version:
    • Type the following and press Enter:
      python3 --version
      
    • You should see output similar to Python 3.x.x (e.g., Python 3.10.2).
    • On Windows, python --version should also work and show Python 3.x.x. On macOS/Linux, python --version might still show Python 2 if it's present, which is why python3 is preferred.
  3. Check Pip version:
    • Pip is Python's package installer, which we'll use extensively later. Let's make sure it's also installed correctly:
      pip3 --version
      
    • You should see output like pip 22.x.x from ...python3.x.... If pip3 doesn't work, try pip --version.
  4. Enter the Python Interactive Shell:
    • Type:
      python3
      
      (Or python on Windows if python3 doesn't work).
    • You should see a prompt like >>>. This is the Python interactive shell, a fantastic place to test small snippets of code!
    • Type your very first Python command:
      print("Hello, Python!")
      
    • Press Enter. You should see Hello, Python! printed below. Congratulations!
    • To exit the interactive shell, type exit() and press Enter.

{{VISUAL: photo: screenshot of a command prompt/terminal window showing the output of python3 --version and python3 interactive shell with a simple print("Hello, Python!") statement}}

If all these checks pass, you've successfully installed Python! Take a moment to celebrate this achievement – you're officially ready to start coding.

In the next lesson, we'll dive into writing and executing your very first Python script, putting this installation to good use!


Your Python Workspace

Your Python Workspace: Where Code Comes Alive

Welcome back, future Pythonista! In our previous session, we explored the fascinating world of Python and successfully installed it on your machine. Now that Python is ready to go, the next crucial step is to set up your workspace – the digital environment where you'll write, edit, and execute your code.

Think of it like a craftsperson's workshop. You've got your raw materials (Python itself), but you need the right tools and a dedicated space to turn those materials into something amazing. For a programmer, these tools primarily consist of a code editor (or an Integrated Development Environment) and the command line (or terminal).

Let's dive in and get acquainted with these essential components.

The Code Editor: Your Creative Canvas

At its core, a code editor is a specialized text editor designed specifically for writing programming code. While you could technically write Python code in a simple text editor like Notepad or TextEdit, dedicated code editors offer a plethora of features that make coding significantly easier, faster, and more enjoyable.

These features often include:

  • Syntax Highlighting: Different parts of your code (keywords, strings, comments) are colored differently, dramatically improving readability and helping you spot errors.
  • Auto-completion/IntelliSense: The editor intelligently suggests code completions as you type, reducing typos and speeding up development.
  • Error Checking: Real-time feedback on potential syntax errors or logical issues, guiding you to correct problems as you write.
  • Code Formatting: Tools to automatically tidy up your code's appearance, ensuring consistency and adherence to style guides.
  • Extensions/Plugins: Add-ons that extend the editor's functionality for specific languages, frameworks, or tasks, tailoring it to your needs.

For this course, and for a vast majority of professional developers, we highly recommend using Visual Studio Code (VS Code). It's a free, open-source, and incredibly powerful code editor developed by Microsoft, compatible with Windows, macOS, and Linux. It strikes an excellent balance between being lightweight enough for beginners and powerful enough for complex projects.

{{VISUAL: photo: The Visual Studio Code interface showing a Python script with syntax highlighting, a file explorer sidebar, and an integrated terminal panel at the bottom.}}

While VS Code is often called a "code editor," it blurs the lines with an Integrated Development Environment (IDE). An IDE typically bundles an editor with more comprehensive tools like a debugger, compiler, and version control integration all in one application. For most Python development, VS Code, with its extensive extensions for Python, provides a powerful, near-IDE experience.

Opening VS Code

If you followed the installation guide in the previous page, you should have VS Code installed.

  • Windows: Search for "Visual Studio Code" in your Start menu and click to open.
  • macOS: Find "Visual Studio Code" in your Applications folder or search for it using Spotlight (Cmd + Space and type "VS Code").
  • Linux: Depending on your distribution, you might find it in your applications launcher or run code from the terminal if it's in your system's PATH.

Once opened, you'll usually see a welcome screen. You can close this and go to File > New Text File to create an empty file, or File > Open Folder... to open an existing project directory. We'll explore file management more in the next page.

The Command Line (Terminal): Your Direct Control Panel

While your code editor is where you write your code, the command line interface (CLI) – often called the terminal on macOS and Linux, or Command Prompt/PowerShell on Windows – is where you'll often execute your Python scripts and interact directly with your operating system and Python installation.

It's a text-based interface where you type commands instead of clicking graphical icons. Don't be intimidated by its stark appearance; it's an incredibly powerful tool that gives you fine-grained control over your computer. For Python, you'll use it to:

  • Run Python scripts: Execute your saved .py files using the Python interpreter (e.g., python your_script.py).
  • Install and manage Python packages: Use pip (Python's package installer) to add external libraries to your projects (e.g., pip install package_name).
  • Navigate through your file system: Move between directories to locate your Python files.
  • Start interactive Python sessions: Experiment with Python code directly in a live interpreter.

{{VISUAL: photo: A dark-themed command line (terminal) window open on a computer screen, displaying directory navigation commands and the result of a simple Python execution command.}}

Opening Your Command Line Interface

  • Windows:
    • Command Prompt: Search for "cmd" in your Start menu.
    • PowerShell: Search for "PowerShell" in your Start menu. PowerShell is generally more modern and powerful, offering more features, but cmd works perfectly fine for basic Python execution.
  • macOS: Search for "Terminal" in Spotlight (Cmd + Space and type "Terminal") or find it in Applications > Utilities.
  • Linux: Typically found in your applications menu under "Terminal" or similar. The keyboard shortcut Ctrl + Alt + T often opens it directly.

Once opened, you'll see a prompt, usually showing your current directory (e.g., C:\Users\YourName> on Windows, YourComputerName:~ YourName$ on macOS/Linux), waiting for your commands.

Stuck on something here?
Aarav Sir explains any part — voice or chat — 24/7.

The Dynamic Duo: Editor and Terminal

So, how do these two essential tools work together? It's a beautiful and efficient synergy that forms the core of your Python development workflow:

  1. Write and Save: You write your Python code in your code editor (VS Code) and save it as a file with a .py extension (e.g., hello_world.py).
  2. Navigate: You then open your command line/terminal and use commands to navigate to the exact directory where you saved your Python file.
  3. Execute: Finally, you use the terminal to tell your operating system to run that Python file using the Python interpreter you installed earlier. The Python interpreter then reads and executes your code, displaying any output back in the terminal.

{{VISUAL: diagram: A simplified workflow diagram showing "Code Editor (VS Code)" -> "Write & Save .py file" -> "Terminal/Command Line" -> "Navigate to directory" -> "Execute 'python filename.py'" -> "Output displayed in Terminal."}}

This workflow forms the backbone of how you'll develop Python programs. You'll spend most of your time typing away in your editor, but you'll frequently switch to your terminal to test, run, and interact with your code.

Your First Step: Open Them Up!

Before moving on, take a moment to launch both VS Code and your preferred command line tool (Terminal/Command Prompt/PowerShell). Get a feel for their interfaces. Don't worry if it feels a bit unfamiliar or overwhelming right now; familiarity will come quickly with practice.

In the next page, we'll put these tools to immediate use by creating our very first Python program and executing it. Get ready to make some magic happen!


Write Your First Script

Write Your First Python Script

Welcome back, future programmer! You've successfully navigated the basics of Python, understood why it's a fantastic language, and set up your development environment. Now, it's time for the moment you've been waiting for: writing your very first Python program!

This is a pivotal step. For centuries, humans have communicated through language. Today, you're learning to communicate with machines. That single line of code you're about to write is a profound act of creation, opening doors to endless possibilities.

The Time-Honored Tradition: "Hello, World!"

In the world of programming, there's a sacred ritual for beginners: writing a program that simply outputs "Hello, World!" to the screen. This tradition dates back to the early days of the C programming language in the 1970s and serves a few crucial purposes:

  1. Simplicity: It's the simplest possible program that still demonstrates a complete execution path.
  2. Environment Check: If you can run "Hello, World!", it confirms your setup (Python installation, text editor/IDE) is working correctly.
  3. Foundation: It introduces fundamental concepts like output, functions, and string literals without overwhelming you.
  4. Rite of Passage: Every programmer, from novice to expert, started here. It's a shared experience.

So, let's join the ranks!

Step 1: Open Your Text Editor/IDE

You've already installed a text editor or Integrated Development Environment (IDE) like VS Code in the previous chapter. Now, open it up!

  • If you're using VS Code:
    • Open VS Code.
    • Go to File > New Text File (or press Ctrl+N / Cmd+N).
    • You'll see a blank canvas, ready for your genius.

{{VISUAL: photo: VS Code editor with an empty file open, ready for typing.}}

This blank page is where your ideas will come to life. Take a moment to appreciate the clean slate.

Step 2: Write the Code

Now for the magic! Type the following exact line of code into your editor:

print("Hello, World!")

Make sure every character is identical, including the capitalization, the parentheses, and especially the double quotation marks.

{{VISUAL: photo: VS Code editor displaying the "print('Hello, World!')" code typed within a new file.}}

Let's break down what's happening in this single, powerful line:

  • print(): This is a function. In Python, a function is a named block of code that performs a specific task. The print() function's job is to display whatever you put inside its parentheses to the screen (or, more accurately, to the standard output, which is usually your terminal or command prompt).
  • ( and ): These are parentheses. They are crucial for calling a function. Whatever you want the print function to display goes inside these parentheses.
  • "Hello, World!": This is a string literal. In programming, a "string" is a sequence of characters (letters, numbers, symbols) treated as plain text. The quotation marks (" or ' can be used) tell Python that everything enclosed within them is a string and should be treated as literal text, not as a command or variable. Without the quotes, Python would try to interpret Hello and World as commands, which would lead to an error!

Congratulations, you've just written your first piece of Python code! It might be short, but it's perfectly formed.

Step 3: Save Your Script

A Python program isn't just typed into an editor; it needs to be saved as a file so that the Python interpreter can find and execute it.

  1. Go to File > Save As... (or press Ctrl+S / Cmd+S).
  2. Choose a location: It's a good practice to create a dedicated folder for your Python projects. For this course, you might make a folder called python_projects and then a subfolder called chapter_1.
  3. Name your file: Give your script a meaningful name. For this program, hello.py is perfect.
    • Crucially, ensure the file name ends with the .py extension. This tells your operating system and the Python interpreter that this file contains Python code. Without .py, your computer won't know it's a Python script!
  4. Click Save.

{{VISUAL: photo: A file save dialog box showing the filename "hello.py" and the file type selected as Python.}}

You now have a bona fide Python script on your computer! It's a text file, but it carries the instructions for the Python interpreter to follow.

A Note on File Naming:

  • Keep filenames lowercase.
  • Use underscores _ instead of spaces if you need multiple words (e.g., my_first_script.py).
  • Avoid special characters or symbols.
  • Never name your script the same as a built-in Python module (e.g., don't name it print.py or math.py). This can cause conflicts.

You've taken a significant step. You've gone from simply understanding concepts to actively creating with them. Your hello.py file is now a dormant giant, waiting for the command to awaken and perform its task.

What's next? The exciting part: running your script and seeing "Hello, World!" appear on your screen! That's what we'll cover in the final page of this chapter. Get ready to experience the immediate gratification of programming.


Run Code, Practice More

Run Code, Practice More

Welcome to the thrilling conclusion of our introductory chapter! So far, you've learned what Python is, why it's so popular, and you've successfully set up your development environment. You even wrote your very first Python program: hello_world.py.

Now, it's time for the moment of truth: seeing your code come to life! On this page, we'll walk through exactly how to execute your Python script from the command line. Then, we'll empower you to modify your code and run new experiments, solidifying your understanding with hands-on practice.


The Command Line: Your Control Center

The "command line" (also known as the "terminal" on macOS/Linux, or "Command Prompt"/"PowerShell" on Windows) is a text-based interface used to interact with your operating system. It's where you'll tell Python to run your scripts.

Don't be intimidated! While it might look a little old-school, it's an incredibly powerful tool that every programmer uses daily.

Step-by-Step: Executing Your hello_world.py Script

Let's get that "Hello, World!" message printed!

  1. Open Your Terminal/Command Prompt:

    • Windows: Search for "Command Prompt" or "PowerShell" in the Start menu.
    • macOS: Search for "Terminal" in Spotlight (Cmd + Space).
    • Linux: Look for "Terminal" in your applications menu.

    You'll see a window with a blinking cursor, likely showing your current directory (e.g., C:\Users\YourName> on Windows, or YourMac:~ YourName$ on macOS).

  2. Navigate to Your Script's Directory: Remember where you saved your hello_world.py file? Let's assume you created a folder called python_projects on your Desktop.

    You need to use the cd (change directory) command to tell the command line where your script is.

    • If your script is on your Desktop in a folder called python_projects:

      cd Desktop/python_projects
      

      (On Windows, it might be cd C:\Users\YourName\Desktop\python_projects or similar. You can usually cd Desktop first, then cd python_projects.)

    • If you're unsure of your current directory:

      • Windows: Type dir and press Enter.
      • macOS/Linux: Type ls and press Enter. This will list the contents of your current directory. Use it to help you navigate!

    Once you're in the correct directory, you should see your prompt reflect that change (e.g., C:\Users\YourName\Desktop\python_projects>).

  3. Run Your Python Script: With your terminal in the same directory as hello_world.py, type the following command and press Enter:

    python hello_world.py
    

    What happens next? Python springs into action! It reads your hello_world.py file line by line, executes the print() function, and displays the output directly in your terminal.

    {{VISUAL: photo: A screenshot of a command line terminal showing the cd command to navigate to a directory and then the python hello_world.py command executing, displaying "Hello, World!" as output.}}

Congratulations! You've just run your very first Python program. This fundamental action is the gateway to all your future coding endeavors.

Common Execution Issues & Quick Fixes

  • python: command not found (or similar): This usually means Python isn't correctly added to your system's PATH environment variable, or the installation wasn't completed successfully. Revisit Page 3: "Install Python" and ensure Python is accessible globally.
  • No such file or directory: 'hello_world.py': This is the most common error! It means you're not in the correct directory where your script is saved, or you've mistyped the filename.
    • Double-check your cd command.
    • Use ls (macOS/Linux) or dir (Windows) to verify the file exists in your current terminal location.
    • Ensure the filename (hello_world.py) is spelled exactly right, including the .py extension.
  • Syntax Errors: If you accidentally made a typo in your script (e.g., prnt("Hello")), Python will stop with an error message indicating the line number. This is Python's way of telling you something is wrong with your code's grammar. Don't worry, you'll learn to love (or at least tolerate) these messages as they guide you to fix your code.

Beyond "Hello, World!": Introducing Variables

"Hello, World!" is great for a start, but real programs do more. Let's introduce a fundamental concept: variables.

A variable is like a named container that holds a piece of data. You can put data into it, retrieve data from it, and even change the data it holds.

Let's modify our script to use a variable.

  1. Open hello_world.py in your text editor (VS Code, Sublime Text, etc.).

  2. Change the content to this:

    # This is our first variable!
    greeting = "Hello, Pythonista!"
    print(greeting)
    
    • greeting = "Hello, Pythonista!" creates a variable named greeting and assigns the text Hello, Pythonista! to it. The text inside quotes is called a string.
    • print(greeting) now prints the content of the greeting variable, not the word "greeting" itself.
  3. Save the file.

  4. Run it again from your terminal, just as before:

    python hello_world.py
    

    You should now see Hello, Pythonista! printed.

    {{VISUAL: diagram: A side-by-side comparison showing the original hello_world.py code next to the modified version that introduces a greeting variable, highlighting the change.}}

This is a small step, but a crucial one! You've just learned how to:

  • Store information in a variable.
  • Use that variable in your program.
  • Modify an existing script and see the updated output.

Quick Exploration: Python's Interactive Mode

For quick tests or simple calculations, you don't always need to create a .py file. Python has an interactive mode (also known as a REPL: Read-Eval-Print Loop).

  1. Open your terminal.

  2. Type python and press Enter (without any filename).

    You'll see a >>> prompt. This means Python is waiting for your commands!

    Try this:

    >>> print("Hello from interactive mode!")
    Hello from interactive mode!
    >>> 10 + 5
    15
    >>> message = "I love Python!"
    >>> print(message)
    I love Python!
    >>>
    

    {{VISUAL: photo: A screenshot of a command line terminal showing Python's interactive mode (REPL) with the >>> prompt, demonstrating a print() statement, a simple arithmetic operation, and variable assignment followed by printing the variable.}}

This mode is fantastic for quickly testing syntax, trying out new functions, or debugging small snippets of code without saving a file. To exit interactive mode, type exit() and press Enter, or press Ctrl+D (macOS/Linux) or Ctrl+Z then Enter (Windows).


Your Turn: Practice Exercises!

The best way to learn programming is by doing. Try these challenges:

  1. Modify Your Message: Open hello_world.py again. Change the text inside the greeting variable to something personal, like "Hello, [Your Name]! It's great to be here." Save and run it.
  2. Multiple Greetings: Add a second variable called farewell and assign it a different message (e.g., "See you in the next chapter!"). Then, use two separate print() statements to print both greeting and farewell messages.
    # Example snippet for guidance
    greeting = "..."
    farewell = "..."
    
    print(greeting)
    print(farewell)
    
  3. Basic Math: Create a new file named math_operations.py. In this file, write Python code that:
    • Creates a variable num1 and assigns it the value 25.
    • Creates a variable num2 and assigns it the value 7.
    • Creates a variable sum_result that stores the sum of num1 and num2 (num1 + num2).
    • Prints the sum_result. Save math_operations.py and run it from your terminal.

What's Next?

You've just completed your first major milestone! You've set up your environment, written your first code, and executed it. This foundation is solid.

In the next chapter, we'll dive deeper into the core building blocks of Python: data types. You'll learn about different kinds of information Python can handle (like numbers, text, and true/false values) and how to work with them effectively.

Keep practicing, keep exploring, and get ready to build amazing things!

In this chapter

  • 1.What is Python?
  • 2.Install Python Now
  • 3.Your Python Workspace
  • 4.Write Your First Script
  • 5.Run Code, Practice More

Frequently asked questions

What is Python?

Welcome to the exciting world of Python programming! If you're new to coding, you've chosen an incredible language to start with. If you're an experienced developer looking to expand your toolkit, you're about to discover why Python is one of the most in-demand and versatile languages today.

What is Install Python Now?

Welcome back, future Pythonista! In the previous lesson, we learned what Python is and why it's a fantastic language to learn. Now, it's time to get our hands dirty and bring Python to life on your own computer. This page will guide you through the essential steps to download and install the Python interpreter, setting

What is Your Python Workspace?

Welcome back, future Pythonista! In our previous session, we explored the fascinating world of Python and successfully installed it on your machine. Now that Python is ready to go, the next crucial step is to set up your *workspace* – the digital environment where you'll write, edit, and execute your code.

What is Write Your First Script?

Welcome back, future programmer! You've successfully navigated the basics of Python, understood why it's a fantastic language, and set up your development environment. Now, it's time for the moment you've been waiting for: **writing your very first Python program!**

What is Run Code, Practice More?

Welcome to the thrilling conclusion of our introductory chapter! So far, you've learned what Python is, why it's so popular, and you've successfully set up your development environment. You even wrote your very first Python program: `hello_world.py`.

More chapters in Python Programming

Want the full Python Programming experience?

Every chapter. Interactive lessons. AI teacher on tap. Study Lab for any photo or PDF. 3-day free trial — no credit card.

1000s of students
100% NCERT-aligned
Powered by AI

Install Learn Skill

Add to home screen for the best experience