Python (Beginner) - Lesson 2: Python Basics – Syntax, Data Types, and Basic Operations

Welcome to Lesson 2! In this lesson, we will build on your introductory knowledge of Python by exploring its core syntax, diving into various data types, and performing basic arithmetic operations. This lesson is essential as it introduces fundamental concepts that you will use in every Python program you write. We will work exclusively in Visual Studio Code, reinforcing the habit of writing and running your code in a professional development environment.

Python Syntax

Python's syntax is one of its most celebrated features because it emphasizes readability and simplicity. Let’s explore some key elements of Python syntax:

  • Indentation:
    Python uses indentation (usually 4 spaces) to define blocks of code rather than using curly braces {} as in some other languages.

    • Why is indentation important?

      • It visually represents the structure of your code.

      • It separates code into distinct blocks, such as the body of loops, conditionals, or function definitions.

      • Improper indentation can lead to syntax errors.

  • Comments:
    Comments are portions of your code that Python ignores when executing. They are used to describe what your code does, making it easier for others (and your future self) to understand.

    • How to write a comment:

      • A comment begins with the # symbol. Everything on that line after the # is considered a comment.

    • Example:

# This is a comment and will not be executed by Python.

Understanding these syntax rules is critical for writing clear, error-free Python code.

Data Types

In Python, data types define the kind of value a variable can hold. We will focus on four primary data types:

  • Integers and Floats:

    • Integers:

      • Represent whole numbers without any decimal point (e.g., 10, -3, 0).

    • Floats:

      • Represent numbers with a fractional part, using a decimal point (e.g., 3.14, -0.001).

    • Usage:

      • These numeric types are used for arithmetic operations such as addition, subtraction, multiplication, and division.

  • Strings:

    • A string is a sequence of characters enclosed in single (' ') or double (" ") quotes.

    • Usage:

      • Used to store text or a sequence of characters.

      • Strings are immutable, meaning once a string is created, it cannot be modified directly.

  • Booleans:

    • Booleans represent one of two values: True or False.

    • Usage:

      • They are commonly used in conditional statements to determine the flow of execution in a program.

Grasping these data types is essential as they form the basis of most operations and manipulations in Python.

Example Code

Let’s look at a comprehensive example that demonstrates Python syntax, the declaration of various data types, and performing a basic arithmetic operation. Open Visual Studio Code, create a new Python file (e.g., lesson2_basics.py), and type the following code:

# This example demonstrates Python syntax and basic data types.

# Variable assignment using the '=' operator:
number = 10          # 'number' is an integer.
pi = 3.14            # 'pi' is a float, representing a decimal number.
message = "Hello"    # 'message' is a string, a sequence of characters.
is_active = True     # 'is_active' is a boolean, representing a true/false value.

# Performing a basic arithmetic operation:
result = number + 5

# Outputting the result using the print() function:
print("The result is:", result)

Detailed Explanation:

  • Variable Assignments:

    • number = 10: We assign the integer value 10 to the variable number.

    • pi = 3.14: We assign the float value 3.14 to the variable pi.

    • message = "Hello": We store a string "Hello" in the variable message.

    • is_active = True: We set the boolean variable is_active to True.

  • Arithmetic Operation:

    • result = number + 5: Here, we perform an addition operation by adding 5 to the value stored in number (which is 10). The result, 15, is stored in the variable result.

  • Output:

    • print("The result is:", result): This line uses the print() function to display the text "The result is:" followed by the value of result (i.e., 15) in the integrated terminal of Visual Studio Code.

Take a moment to run this code in VS Code’s integrated terminal. Notice how the output is displayed, reinforcing the concept of using print() for debugging and interaction.

Tasks

Now it’s your turn to practice what you’ve learned. Follow these practical tasks using Visual Studio Code:

  1. Create a New Python File:

    • Open Visual Studio Code.

    • Create a new file and save it with a .py extension, for example, basic_operations.py.

  2. Declare Variables of Different Types:

    • Declare at least one variable for each of the following data types:

      • Integer: Assign a whole number.

      • Float: Assign a decimal number.

      • String: Assign a text value.

      • Boolean: Assign either True or False.

    • Use clear and descriptive variable names to reflect their content.

  3. Perform a Basic Arithmetic Operation:

    • Choose one of the numeric variables (integer or float).

    • Perform an arithmetic operation (e.g., addition, subtraction, multiplication, or division) with a constant value.

    • Store the result in a new variable.

  4. Comment Your Code:

    • Add comments above or next to your code lines explaining what each section does. This practice improves readability and helps others understand your logic.

  5. Run Your Code:

    • Use VS Code’s integrated terminal to run your Python file.

    • Observe the output and ensure that the arithmetic operation is correctly performed and displayed.

Recall Questions

Review the following questions to reinforce the key concepts covered in this lesson:

  • Why is indentation important in Python?
    Consider how indentation is used to structure code blocks and why it’s crucial for avoiding syntax errors.

  • What are the primary data types used in Python?
    List and describe integers, floats, strings, and booleans, and explain their roles in programming.

  • How do you perform arithmetic operations in Python?
    Reflect on the use of the = operator for assignment and how arithmetic expressions are evaluated and stored in variables.

By the end of this lesson, you should have a solid understanding of Python’s syntax, how to work with various data types, and how to perform basic arithmetic operations. These skills are essential as you progress to more complex topics in future lessons. Continue practicing in Visual Studio Code to build your confidence and familiarity with Python programming.

Previous
Previous

Python (Beginner) - Lesson 1: Introduction to Python and Visual Studio Code Setup

Next
Next

Python (Beginner) - Lesson 3: Variables, User Input, and Basic I/O