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! 🚀