Your First Python Lab

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will embark on your Python journey by exploring fundamental concepts. You will learn how to use the Python interpreter, work with variables and assignments, utilize the print() function for output, interact with users using the input() function, and properly exit the Python environment. This hands-on experience will provide a solid foundation for your future Python programming endeavors.

Click the Continue button below to start the lab.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-270256{{"`Your First Python Lab`"}} python/numeric_types -.-> lab-270256{{"`Your First Python Lab`"}} python/strings -.-> lab-270256{{"`Your First Python Lab`"}} python/python_shell -.-> lab-270256{{"`Your First Python Lab`"}} python/build_in_functions -.-> lab-270256{{"`Your First Python Lab`"}} end

Launching the Python Interpreter

The Python interpreter is a powerful tool that allows you to execute Python code interactively. Let's start by opening the Python interpreter:

  1. Open your terminal. You should see a command prompt, typically ending with a $ symbol.

    alt text
  2. Type the following command and press Enter:

    python
  3. You should now see the Python interpreter prompt, which looks like this:

    Python 3.x.x (default, Mon DD YYYY, HH:MM:SS)
    [GCC x.x.x] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

    The >>> symbol indicates that the Python interpreter is ready to accept your commands.

    lab-your-first-python-lab-1-1

Now that you're in the Python interpreter, let's try a simple operation:

>>> 2 + 3
5

The interpreter immediately calculates and displays the result.

You can also perform more complex calculations:

>>> (10 * 5) + (20 / 4)
55.0

Notice how the interpreter handles the order of operations correctly.

alt text

This interactive environment is excellent for testing small code snippets and learning Python basics.

Working with Variables and Assignments

Variables in Python are like containers that store data. Let's explore how to create and use variables:

  1. In the Python interpreter, type the following:

    >>> x = 42
    >>> x
    42

    Here, we've created a variable named x and assigned it the value 42. When we type x and press Enter, the interpreter shows us the value stored in x.

  2. We can change the value of a variable:

    >>> x = 100
    >>> x
    100

    Now x holds the value 100.

  3. We can also use one variable to set the value of another:

    >>> y = x
    >>> y
    100

    y now has the same value as x.

  4. Variables can store different types of data, not just numbers:

    >>> name = "Alice"
    >>> name
    'Alice'

    Here, name is a variable that stores a string (text) value.

Remember, variable names in Python:

  • Cannot start with a number
  • Cannot contain spaces
  • Are case-sensitive (name and Name are different variables)
  • Should be descriptive of what they store

Using the print() Function

The print() function is used to display output in Python. Let's explore how to use it:

  1. In the Python interpreter, type:

    >>> print("Hello, LabEx!")
    Hello, LabEx!

    The text inside the quotation marks is printed to the screen.

  2. We can print the values of variables:

    >>> x = 42
    >>> print(x)
    42
  3. We can combine text and variables in a single print statement:

    >>> name = "Alice"
    >>> print("My name is", name)
    My name is Alice

    Notice how print() automatically adds a space between the items.

Using the input() Function

The input() function allows us to get input from the user. Let's see how it works:

  1. In the Python interpreter, type:

    >>> name = input("What is your name? ")
    What is your name?

    The interpreter will wait for you to type something and press Enter.

  2. Enter your name when prompted. For example:

    What is your name? Alice
    alt text
  3. Now, let's print a greeting using the name we just input:

    >>> print("Hello,", name)
    Hello, Alice

Remember, input() always returns a string. If you need a number, you must convert it using int() for integers or float() for decimal numbers.

Exiting the Python Interpreter

When you're done working in the Python interpreter, it's important to know how to exit properly:

  1. To exit the Python interpreter, type:

    >>> exit()

    This function call will close the Python interpreter and return you to your regular terminal prompt.

  2. Alternatively, you can use the keyboard shortcut:

    • On most systems: Press Ctrl + D
    • On Windows: Press Ctrl + Z followed by Enter
  3. After exiting, you should see your regular terminal prompt (usually ending with $).

Remember, any variables or functions you defined in the Python interpreter session will be lost when you exit. If you need to save your work, make sure to write it in a Python file (with a .py extension) using a text editor before closing the interpreter.

Summary

In this lab, you've taken your first steps into the world of Python programming. You've learned how to:

  1. Launch the Python interpreter
  2. Work with variables and assignments
  3. Use the print() function to display output
  4. Interact with users using the input() function
  5. Properly exit the Python interpreter

These fundamental skills form the foundation of Python programming and will be essential as you continue your journey in software development.

To learn more about LabEx and how to use it, visit our Support Center or watch this video.

Remember, programming is a skill that improves with practice. Don't hesitate to experiment with the concepts you've learned here. The more you code, the more comfortable and proficient you'll become. Good luck with your Python journey!

Other Python Tutorials you may like