Output:
------
25
5.8
Sam
True
These rules are enforced by Python. If you break them, your code can produce a syntax error.
# ✅ Valid ----
age = 25
_name = "Sam"
student_name = "John"
2name = "Sam" # ❌ Invalid
# ✅ Valid ----
student1 = "Sam"
student_1 = "Sam"
student_name = "Sam"
# ❌ Invalid ----
student-name = "Sam" # - is not allowed
student name = "Sam" # space is not allowed
age = 20
Age = 30
AGE = 40
print(age) # Output: 20
print(Age) # Output: 30
print(AGE) # Output: 40
Python prevents you from using these as variable names and will throw error
False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield
Code Example:
❌ Invalid ----
True = 5
Output:
-----
ERROR!
Traceback (most recent call last):
File "<main.py>", line 1
True = 5
^^^^
SyntaxError: cannot assign to True
Python may not throw an error when you do this, but your variable can shadow the built-in name, which may change the behavior of your code or cause errors later.
Constants: True, False, None, Ellipsis, NotImplemented
Exceptions / Warnings: ArithmeticError, AssertionError, AttributeError, BaseException, BaseExceptionGroup, BlockingIOError, BrokenPipeError, BufferError, BytesWarning, ChildProcessError, ConnectionAbortedError, ConnectionError, ConnectionRefusedError, ConnectionResetError, DeprecationWarning, EOFError, EncodingWarning, EnvironmentError, Exception, ExceptionGroup, FileExistsError, FileNotFoundError, FloatingPointError, FutureWarning, GeneratorExit, IOError, ImportError, ImportWarning, IndentationError, IndexError, InterruptedError, IsADirectoryError, KeyError, KeyboardInterrupt, LookupError, MemoryError, ModuleNotFoundError, NameError, NotADirectoryError, NotImplementedError, OSError, OverflowError, PendingDeprecationWarning, PermissionError, ProcessLookupError, RecursionError, ReferenceError, ResourceWarning, RuntimeError, RuntimeWarning, StopAsyncIteration, StopIteration, SyntaxError, SyntaxWarning, SystemError, SystemExit, TabError, TimeoutError, TypeError, UnboundLocalError, UnicodeDecodeError, UnicodeEncodeError, UnicodeError, UnicodeTranslateError, UnicodeWarning, UserWarning, ValueError, Warning, ZeroDivisionError
Built-in Functions: abs, aiter, all, anext, any, ascii, bin, breakpoint, callable, chr, compile, delattr, dir, divmod, enumerate, eval, exec, filter, format, getattr, globals, hasattr, hash, help, hex, id, input, isinstance, issubclass, iter, len, locals, map, max, min, next, oct, open, ord, pow, print, quit, repr, reversed, round, setattr, sorted, sum, vars, zip
Built-in Types / Classes: bool, bytearray, bytes, classmethod, complex, dict, float, frozenset, int, list, memoryview, object, property, range, set, slice, staticmethod, str, super, tuple, type
Special `__...__` Names:__build_class__, __debug__, __doc__, __import__, __loader__, __name__, __package__, __spec__
Code Example:
❌ Invalid ----
print = 5 # This line of code doesn't throw error
print("Hello") # This causes error
Output:
-----
ERROR!
Traceback (most recent call last):
File "<main.py>", line 2, in <module>
TypeError: 'int' object is not callable
Naming conventions are recommended ways of naming variables. They help maintain readability, consistency, and good coding practices across your programs. They are not rules, so not following them will generally not cause an error in Python.
name = "Alice"
age = 10
score = 95
first_name = "Alice"
student_age = 10
total_score = 95
MAX_SCORE = 100
PI = 3.14159
student_name = "Alice" # ✅ Clear
sn = "Alice" # 🤔 Less descriptive
# use names that clearly indicate True/False
is_active = True
has_permission = False
can_edit = True
One value is assigned to one variable at a time.
name = "Alice"
age = 10
score = 95
Multiple variables are assigned their respective values at once.
name, age, score, is_student = "Alice", 10, 95, True
x = y = z = 0
a = 10
b = 20
c = a
print(c) # Output: 10
# Swapping values
a, b = b, a
c = a
print(a) # Output: 20
print(b) # Output: 10
print(c) # Output: 20