Python comments explain why code exists, what a tricky line means, or which decision a future reader should not miss.
Python variables are names that point to values. A variable can point to a number now, a string later, or a list after that, so clear names matter more than type declarations.
On this page, focus on writing code that another beginner can read: helpful comments, meaningful variable names, and simple assignments.
Comments are lines that Python ignores during execution. They are used to explain code, make it more readable, or temporarily disable code.
Use the # symbol. Everything after # on that line is a comment.
Python doesn't have a dedicated multi-line comment syntax. Use multiple # lines, or use triple-quoted strings (which are technically string literals, not comments).
# This is a single-line comment
print("Hello") # inline comment
# Comments are ignored by Python
# print("This line won't run")
# Consecutive comments explain nearby code.
# Use them for context that is not obvious from the statement itself.
def greet(name: str) -> str:
"""Return a greeting for one person.
Args:
name: Person to greet.
"""
return f"Hello, {name}!"
print(greet("Maya"))
Hello, Maya!
A variable is a named container that stores a value in memory. In Python, you don't need to declare a variable's type - it's inferred automatically.
name = "Alice" # str
age = 25 # int
height = 5.7 # float
is_student = True # bool
print(name) # Alice
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
# Multiple assignment
x = y = z = 0 # all three equal 0
# Tuple unpacking
a, b, c = 1, 2, 3
print(a, b, c) # 1 2 3
# Swap variables (Python style)
a, b = b, a
print(a, b) # 2 1
# Valid variable names
my_name = "Alice"
_private = 42
camelCase = "ok but not Pythonic"
snake_case = "preferred in Python"
value2 = 100
# Invalid variable names (will cause SyntaxError)
# 2value = 10 # starts with digit
# my-name = "Bob" # hyphen not allowed
# class = "Python" # reserved keyword
# Python naming conventions (PEP 8)
user_name = "alice" # variables: snake_case
MAX_SIZE = 100 # constants: UPPER_CASE
class MyClass: # classes: PascalCase
pass
Python is dynamically typed - a variable can hold different types at different times. Use type() to check the current type.
x = 10
print(type(x)) # <class 'int'>
x = "hello"
print(type(x)) # <class 'str'>
x = 3.14
print(type(x)) # <class 'float'>
x = [1, 2, 3]
print(type(x)) # <class 'list'>
# Type hints (Python 3.5+) - optional but recommended
def add(a: int, b: int) -> int:
return a + b
0 of 2 checked
Try this next
0 of 3 completed
No. Comment only when the reason is not obvious from the code. Clear variable names usually remove the need for many comments.
Yes. A name can point to a string first and an integer later, but changing types too often can make code harder to read.
A good name explains the value or purpose, such as total_price, user_name, or is_active. Avoid vague names like x unless the code is very small.
Explore 500+ free tutorials across 20+ languages and frameworks.