
Learning outcomes
By the end of this chapter, you should be able to:- define a variable in simple terms
- write valid variable names
- assign and reassign values
- explain dynamic typing in Python
What is a variable?
A variable is a name that refers to a value stored in memory.age and name are variable names.
Why do we need variables?
Without variables, you would repeat raw values many times. Variables make programs readable and maintainable.Assignment statement
Python uses= for assignment.
= means “store value in variable”. It does not mean “equals” in mathematics.
Variable naming rules
- use letters, digits, and
_ - must not start with a digit
- no spaces in names
- names are case-sensitive (
marksandMarksare different) - do not use keywords (
if,for,class, …)
total_marks, student_name, is_passed
Dynamic typing in Python
In Python, variable type is determined at runtime.Exam-focused points
- variable: named memory location/reference
- assignment operator is
= - equality comparison uses
== - Python is dynamically typed
Practice questions
- Write 5 valid and 5 invalid variable names.
- Predict output:
- Correct the errors:
