Your First Python Lab

PythonPythonBeginner
Practice Now

Introduction

Welcome to LabEx! In this first lab, you'll dive into Python basics, covering essentials like the Python interpreter, variables, assignment, input/output operations, and exiting the interpreter.

Achievements

  • Python Interpreter
  • Variables and Assignment
  • print() Function
  • input() Function

Click the Continue button below to start the lab.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/build_in_functions -.-> lab-270256{{"`Your First Python Lab`"}} end

Getting Started with the Python Interpreter

To begin, let's familiarize ourselves with the Python interpreter, a program that reads and executes Python code, commonly known as the Python shell. This tool is invaluable for testing and debugging Python code.

Open the Python interpreter by typing python3 in your terminal.

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

You'll see an interactive prompt >>>, indicating the interpreter is ready to receive your Python code. When you input code and press Enter, the interpreter executes it, showing the result.

For instance, entering a number will display it:

>>> 2
2

The Python Interpreter also handles basic arithmetic operations:

>>> 2 + 3
5
>>> 2 - 3
-1

lab-your-first-python-lab-1-2
This interactive environment allows you to experiment with Python code and receive immediate feedback.

Variables and Assignment

Understanding variables and assignments is fundamental in Python programming. Let's explore how to create variables and assign values in Python.

Basic Assignment

A variable is like a name for a value. For instance, x = 42 creates a variable named x representing the value 42. Later, we can use the variable name to access this value.

>>> x = 42
>>> x
42

Changing the value of a variable is simple. For example, if x = 42, then x = 44 updates the value of x to 44.

>>> x
42
>>> x = 44
>>> x
44

You can also use a variable to refer to another variable. If x = 42, then y = x makes y refer to the same value as x (which is 42).

>>> x
42
>>> y = x
>>> y
42

Variable Naming Rules

While variable names can be any sequence of characters, there are some rules to follow:

  • A variable name cannot start with a number.
  • A variable name cannot contain spaces.
  • A variable name cannot contain special characters such as !, @, #, $, %, ^, &, *, (, ), -, +, =, [, ], {, }, \, |, ;, :, ", ', <, >, ,, ., /, ?.

You will become familiar with these rules in the following exercises without needing to memorize them.

print() Function

The print() function in Python is a key tool for displaying output on the screen. It takes a value, known as the argument, inside the parentheses and prints it.

Basic Usage

Let's start with a simple example, In this example, we print the value "Hello LabEx" to the screen:

>>> print("Hello LabEx")
Hello LabEx

You can use print() to display the value of a variable:

>>> x = 3
>>> print(x)
3

Moreover, the print() function allows you to showcase multiple values on the screen by separating them with commas. In this case, print() automatically adds a space between the value of x and the string "is a number":

>>> x = 5
>>> print(x, "is a number")
5 is a number

print() also allows you to concatenate values using the plus operator:

>>> name = "LabEx"
>>> print("Hello " + name)
Hello LabEx

input() Function

The input() function in Python facilitates user interaction by prompting for input and capturing the entered value during program execution.

Basic Usage

Consider this example illustrating the basic usage of input():

>>> name = input("Enter your name: ")
Enter your name: John

>>> print("I am " + name)
I am John

In this scenario, input() prompts the user with "Enter your name: " and waits for input. The provided value is stored in the name variable. Subsequently, the print() function displays a personalized greeting containing the user's name.

Exit the Python Interpreter

To exit the Python Interpreter, type exit() and press Enter.

>>> exit()

This command effectively closes the Python Interpreter, providing a simple way to end your session.

Summary

Congratulations on completing your first Python lab!

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

Programming is a journey, but Next Lab is just a click away. Let's get started!

Other Python Tutorials you may like