Beginner’s Guide to Python – Prerequisite for Mastering Machine Learning
About Lesson

Variables are used to store data in Python. A variable is created the first time a value is assigned to it. Variable names can contain letters, numbers, and underscores, but cannot start with a number.

Here’s how you can declare and use variables in Python:

python
# Integer variable
age = 25
# Float variable
height = 5.9
# String variable
name = "Bob"
# Boolean variable
is_valid = True

Python also supports various operators for performing arithmetic, comparison, and logical operations. Here are some examples:

python
# Arithmetic operators
x = 10
y = 5
# Addition
result = x + y
# Subtraction
result = x - y
# Multiplication
result = x * y
# Division
result = x / y
# Comparison operators
a = 10
b = 20
# Equal to
result = a == b
# Not equal to
result = a != b
# Greater than
result = a > b
# Less than
result = a < b
# Logical operators
p = True
q = False
# AND
result = p and q
# OR
result = p or q
# NOT
result = not p

Understanding variables and operators is essential for writing meaningful Python code. In the next chapter, we’ll explore control flow statements such as conditional statements and loops.

Join the conversation