Introduction
Python is one of the most popular programming languages in the world, known for its simplicity, readability, and vast ecosystem. Whether you are starting your programming journey or switching from another language, understanding syntax, variables, and constants is the first step toward mastering Python.
In this guide, we will cover:
- What is Python syntax?
- How to create and use variables in Python
- How constants work in Python
- Best practices for writing professional Python code
- Practical examples for hands-on learning
1. Understanding Python Syntax
Syntax refers to the set of rules that define how Python code must be written so that the interpreter can understand and execute it.
Python’s syntax is clean and human-readable, making it easier for beginners to learn. Unlike many programming languages, Python uses indentation instead of curly braces {} to define code blocks.
Key Python Syntax Rules
- Indentation is mandatory to define code blocks.
- Statements are usually written one per line.
- Comments start with a # and are ignored by Python.
- Python is case-sensitive (name ≠ Name)
Example – Correct vs Incorrect Indentation:
# Correct syntax
if True:
print("Hello, World!") # Proper indentation
# Incorrect syntax (causes IndentationError)
if True:
print("Hello, World!"
2. Variables in Python
A variable is a named location in memory used to store data that can change during program execution.
Declaring Variables.
Python does not require explicit declaration of variables or specifying a type. The type is determined automatically based on the assigned value.
Example:
# Assigning values to variables
name = "Alice" # String
age = 25 # Integer
height = 5.6 # Float
is_student = True # Boolean
print(name, age, height, is_student)
Variable Naming Rules in Python
- Must start with a letter or underscore (_), not a number.
- They must not start with a number.
- Variable names can contain letters, numbers, and underscores.
- Cannot use reserved keywords (if, class, for, etc.).
- Use descriptive names for better readability.
Valid Examples:
user_name = "John"
totalAmount = 500
_score = 95
Invalid Examples (will cause errors):
2nd_place = "Silver" # Cannot start with a number
for = "loop" # 'for' is a keyword
Dynamic Typing in Python
Python is dynamically typed, meaning a variable’s type is determined at runtime and can change.
x = 10 # Initially integer
x = "Hello" # Now string
print(x)
3. Constants in Python
A constant is a value that should not be changed during program execution.
Python does not have a built-in const keyword, but by convention, constants are written in ALL UPPERCASE letters.
PI = 3.14159
MAX_USERS = 100
APP_NAME = "MyApplication"
While Python won’t prevent you from changing these values, it is a best practice to treat them as unchangeable.
4. Multiple Assignment in Python
Python allows assigning values to multiple variables in one line.
# Assigning different values
x, y, z = 10, 20, 30
# Assigning the same value
a = b = c = 50
print(x, y, z)
print(a, b, c)
5. Type Conversion in Variables
Sometimes, you may need to convert one data type to another. Python provides built-in functions like int(), float(), and str().
num_str = "100"
num_int = int(num_str) # Converts to integer
print(num_int, type(num_int))
6. Best Practices for Variables and Constants
Use meaningful names (e.g., user_age instead of ua).
Follow snake_case naming for variables and UPPERCASE for constants.
Avoid reassigning variables to values of unrelated types.
Group related constants together at the start of the file.
7. Complete Example
Here’s a small program that demonstrates Python syntax, variables, and constants:
# Constants
PI = 3.14159
APP_NAME = "Circle Area Calculator"
# Variables
radius = 7
area = PI * (radius ** 2)
# Output
print(APP_NAME)
print(f"The area of a circle with radius {radius} is {area}")
Output:
Circle Area Calculator
The area of a circle with radius 7 is 153.93791
Conclusion
Understanding Python’s syntax, variables, and constants is the foundation for writing clean, maintainable, and efficient code. With Python’s simplicity and readability, you can focus more on problem-solving rather than worrying about complex syntax rules.
By following best practices—like using descriptive variable names, consistent casing, and proper indentation—you can ensure your Python programs remain professional and easy to understand.