Python Basic Syntax: Variables, Operators, Expressions, and Statements

Published on May 15, 2025 by Aman K Sahu

1. Variables

πŸ”Ή A variable is like a labeled box in your computer’s memory. You can store different types of data inside it β€” like numbers, words, or lists β€” and use them later in your program.

πŸ”Ή In Python, you don't have to mention the type of data you're storing. Python automatically understands it based on the value.

βœ… For example:

name = "Alice"
age = 25

🟒 Here, name is storing the text "Alice", which is called a string.
🟒 age is storing the number 25, which is an integer.

πŸ”Έ You can use these variables later in your program like this:

print("Name:", name)
print("Age:", age)

This will output:
Name: Alice
Age: 25

2. Operators

Operators are symbols used to perform actions on variables and values β€” like adding numbers or comparing them.

  • Arithmetic Operators (math): + - * / % ** //
  • Assignment Operators: = += -= etc.
  • Comparison Operators: == != > < >= <=
  • Logical Operators: and or not
  • Bitwise Operators: & | ^ ~ << >>

βœ… Example: Add two numbers and store the result:

sum = 10 + 5

Now sum will hold the value 15.

3. Expressions

An expression is a combination of variables, values, and operators that Python evaluates (solves) to produce a result.

βœ… Example: Calculating the total cost:

total = price * quantity + tax

Here, the expression on the right side of = is solved first, and the result is stored in total.

4. Statements

A statement is a complete instruction that Python can understand and run. Examples include:

  • Assignment statement (e.g., x = 5)
  • Conditional statement (e.g., if)
  • Loop statement (e.g., for, while)
  • Function definition (e.g., def my_function():)

βœ… Example: Checking a score:

if score >= 90:
    print("Excellent")

This is an if statement that prints "Excellent" if the score is 90 or more.

Next: Python Data Types