Unit 2: Basic Syntax and Variables

Overview

Now that you have Python installed and understand how to run code, it’s time to explore Python’s syntax (the rules for writing Python code) and variables (used to store information).

Python is known for its clean and readable syntax. Unlike other programming languages, it does not require semicolons (;) at the end of statements or curly braces ({}) to define blocks of code. Instead, Python relies on indentation (spaces or tabs) to structure code properly.

1. Understanding Python Syntax

Indentation: The Structure of Python Code

In many programming languages, blocks of code are enclosed in curly brackets {}. However, in Python, indentation (spaces or tabs at the beginning of a line) determines the structure of the program.

✅ Tip: Always be consistent with indentation. The standard convention is 4 spaces per indentation level.

Comments: Writing Notes in Your Code

Comments are lines of text in your code that Python ignores. They are useful for explaining what your code does.

       •      Single-line comment: Starts with #

       •      Multi-line comment: Enclosed in triple quotes ”’ or “””

2. Variables and Data Types

What Are Variables?

A variable is a name that stores a value. You can think of it as a box that holds data, which can be changed later.

📌 Rules for Naming Variables:

✔️ Must start with a letter or underscore _

✔️ Can contain letters, numbers, and underscores

❌ Cannot start with a number (1name is invalid)

❌ Cannot use Python keywords (e.g., if, else, print)

3. Data Types in Python

Python has several built-in data types, including:

Data Type     Example

String (str)     “Hello”

Integer (int)   42

Float (float)   3.14

Boolean (bool)     True, False

4. Basic Operators in Python

Operators in Python allow us to perform operations on variables and values.

Arithmetic Operators (Used for mathematical calculations)

Operator       Description   Example

+     Addition 5 + 3  → 8

–     Subtraction   10 – 2  → 8

*      Multiplication       4 * 3  → 12

/      Division  8 / 2  → 4.0

//    Floor Division       9 // 2  → 4

%     Modulus (Remainder) 10 % 3  → 1

**    Exponentiation    2 ** 3  → 8

Comparison Operators (Used for comparing values)

Operator       Meaning Example

==  Equal to 5 == 5 → True

!=    Not equal to 5 != 3 → True

>     Greater than 10 > 5 → True

<     Less than       3 < 8 → True

>=  Greater than or equal to    5 >= 5 → True

<=  Less than or equal to  7 <= 10 → True

5. Writing and Using Variables in Code

Let’s combine what we’ve learned to create a simple Python program.

📌 Explanation:

       •      We create variables for name, age, and is_student.

       •      We use print() to display their values.

       •      We calculate birth_year using subtraction.

Conclusion

In this unit, you learned about:

       •      Python syntax (indentation, comments)

       •      Variables (naming rules, assigning values)

       •      Data types (str, int, float, bool)

       •      Basic operators (arithmetic and comparison)

       •      Writing simple Python programs using variables

Now that you understand variables and data types, you’re ready to start making decisions in Python with if statements, which we’ll cover in Unit 3: Control Flow! 🚀

Overview

Now that you have Python installed and understand how to run code, it’s time to explore Python’s syntax (the rules for writing Python code) and variables (used to store information).

Python is known for its clean and readable syntax. Unlike other programming languages, it does not require semicolons (;) at the end of statements or curly braces ({}) to define blocks of code. Instead, Python relies on indentation (spaces or tabs) to structure code properly.

1. Understanding Python Syntax

Indentation: The Structure of Python Code

In many programming languages, blocks of code are enclosed in curly brackets {}. However, in Python, indentation (spaces or tabs at the beginning of a line) determines the structure of the program.

✅ Tip: Always be consistent with indentation. The standard convention is 4 spaces per indentation level.

Comments: Writing Notes in Your Code

Comments are lines of text in your code that Python ignores. They are useful for explaining what your code does.

       •      Single-line comment: Starts with #

       •      Multi-line comment: Enclosed in triple quotes ”’ or “””

2. Variables and Data Types

What Are Variables?

A variable is a name that stores a value. You can think of it as a box that holds data, which can be changed later.

📌 Rules for Naming Variables:

✔️ Must start with a letter or underscore _

✔️ Can contain letters, numbers, and underscores

❌ Cannot start with a number (1name is invalid)

❌ Cannot use Python keywords (e.g., if, else, print)

3. Data Types in Python

Python has several built-in data types, including:

Data Type     Example

String (str)     “Hello”

Integer (int)   42

Float (float)   3.14

Boolean (bool)     True, False

4. Basic Operators in Python

Operators in Python allow us to perform operations on variables and values.

Arithmetic Operators (Used for mathematical calculations)

Operator       Description   Example

+     Addition 5 + 3  → 8

–     Subtraction   10 – 2  → 8

*      Multiplication       4 * 3  → 12

/      Division  8 / 2  → 4.0

//    Floor Division       9 // 2  → 4

%     Modulus (Remainder) 10 % 3  → 1

**    Exponentiation    2 ** 3  → 8

Comparison Operators (Used for comparing values)

Operator       Meaning Example

==  Equal to 5 == 5 → True

!=    Not equal to 5 != 3 → True

>     Greater than 10 > 5 → True

<     Less than       3 < 8 → True

>=  Greater than or equal to    5 >= 5 → True

<=  Less than or equal to  7 <= 10 → True

5. Writing and Using Variables in Code

Let’s combine what we’ve learned to create a simple Python program.

📌 Explanation:

       •      We create variables for name, age, and is_student.

       •      We use print() to display their values.

       •      We calculate birth_year using subtraction.

Conclusion

In this unit, you learned about:

       •      Python syntax (indentation, comments)

       •      Variables (naming rules, assigning values)

       •      Data types (str, int, float, bool)

       •      Basic operators (arithmetic and comparison)

       •      Writing simple Python programs using variables

Now that you understand variables and data types, you’re ready to start making decisions in Python with if statements, which we’ll cover in Unit 3: Control Flow! 🚀

Tutorial Articles U1

Unit 1: Introduction to Python

What is Python?

Python is a high-level, easy-to-read programming language that allows you to write code in a simple and straightforward way. It’s popular for its clean syntax and versatility, making it ideal for beginners and experts alike. Python is used in web development, data science, automation, machine learning, and more.

Why Python is Great for Beginners

       1.    Readable and Simple Syntax:

Python’s syntax closely resembles the English language, making it easier to understand. It doesn’t require punctuation like semicolons or curly braces, which are common in other languages. Instead, Python uses indentation to organize code.

       2.    Versatility:

Python is used in a wide range of fields:

       •      Web Development (e.g., Django, Flask)

       •      Data Science (e.g., pandas, NumPy)

       •      Machine Learning (e.g., TensorFlow, scikit-learn)

       •      Automation (e.g., scripts to automate repetitive tasks)

       3.    Large Community:

Python has a huge community of developers. This means you’ll always find resources, tutorials, and forums to help you solve problems and improve your skills.

Setting Up Python

       1.    Installing Python:

       •      Go to python.org, download the installer for your operating system, and run it.

       •      Ensure to check the box to “Add Python to PATH” during installation. This allows you to run Python from the command line.

       •      After installation, open the terminal and type python –version to check if it installed correctly.

       2.    Choosing a Text Editor or IDE:

You can use any text editor to write Python code, but an IDE like VS Code, PyCharm, or Jupyter Notebook makes it easier to write, debug, and test your programs.

Basic Python Syntax

1.    Printing Output:

The print() function displays text or data on the screen.

2.    Variables:

Variables are used to store data. You can assign values to variables using the = sign.

3.    Data Types:

       •      Strings: Text wrapped in quotes (“Hello”).

       •      Integers: Whole numbers (10).

       •      Floats: Numbers with decimals (3.14).

       •      Booleans: True or False values.

 4.    Basic Arithmetic:

Python allows you to perform arithmetic operations like addition, subtraction, multiplication, and division.

Control Flow

 1.    If Statements:

Use if to make decisions. You can also use elif (else if) and else to check multiple conditions.

2.    Loops:

       •      For Loops: Iterates over a sequence (like a list).