Lesson 2: Variables, Data Types, and Basic Operations | Coding Class Series
Lesson 2: Variables, Data Types, and Basic Operations
Introduction:
Welcome to Lesson 2 of our Coding Class Series! In this lesson, we will learn the fundamentals of Variables, Data Types, and Basic Operations. These are the building blocks of any programming language and essential for writing your first programs.
1️⃣ Variables
A variable is like a container that stores data which can change during program execution.
Example in Python:
name = "Alice" # String variable
age = 20 # Integer variable
is_student = True # Boolean variable
Tips:
- Variable names should be meaningful (e.g.,
user_ageinstead ofx). - No spaces in variable names; use underscores
_instead.
2️⃣ Data Types
A data type defines what kind of value a variable holds. Common types include:
| Data Type | Example | Description |
|---|---|---|
| Integer | 10 | Whole numbers |
| Float | 3.14 | Decimal numbers |
| String | "Hello" | Text values |
| Boolean | True / False | Logical values |
3️⃣ Basic Operations
Operations allow you to work with variables and values.
Arithmetic Operations:
a = 10
b = 5
print(a + b) # 15
print(a - b) # 5
print(a * b) # 50
print(a / b) # 2.0
print(a % b) # 0 (remainder)
String Operations:
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name) # Alice Smith
Boolean Operations:
is_raining = True
has_umbrella = False
print(is_raining and has_umbrella) # False
print(is_raining or has_umbrella) # True
4️⃣ Practice Exercises
- Create a variable
cityand assign your city name to it. - Add two numbers using variables and print the result.
- Create a boolean variable
is_hungryand test it with logical operations. - Combine two strings using concatenation.
Remember: Practice makes coding easy and fun!