Computer Science

Variables in Python Class 11 CBSE Computer Science: Variables, Naming Rules, Assignment and Dynamic Typing

Class 11 · Computer Science

Variables in Python (CBSE Class 11 Computer Science)

A variable is one of the most important concepts in programming. Whenever a program needs to store information such as a student's name, marks, salary, or age, it uses variables. Variables allow programs to store data temporarily in the computer's memory so that it can be processed whenever required.

Learning Objectives

  • Understand variables.
  • Learn variable assignment.
  • Know the rules for naming variables.
  • Understand dynamic typing.
  • Learn multiple assignment.
  • Learn swapping of variables.

What is a Variable?

A variable is a named memory location used to store data. The value stored in a variable can change during program execution.

Definition: A variable is a name given to a memory location that stores data and whose value can change during program execution.

Variable Representation


Variable Name
      │
      ▼
+-------------+
|   marks     |
+-------------+
|     95      |
+-------------+

Here, marks is the variable name and 95 is the value stored in it.


Creating Variables

In Python, a variable is created automatically when a value is assigned to it. No separate declaration is required.

Syntax


variable_name = value

Examples


name = "Aman"

age = 17

marks = 92.5

passed = True

Assignment Operator (=)

The assignment operator (=) assigns the value on the right side to the variable on the left side.


x = 50

Here, the value 50 is assigned to the variable x.


Difference Between Assignment Operator (=) and Comparison Operator (==)

Beginners often confuse the assignment operator (=) with the comparison operator (==). Although they look similar, they perform completely different tasks.

Assignment Operator (=) Comparison Operator (==)
Assigns a value to a variable. Compares two values.
Used to store data. Used to check equality.
Does not return True or False. Returns either True or False.
Example: x = 10 Example: x == 10

Example 1: Assignment Operator


marks = 95

print(marks)

Output


95

Here, the value 95 is assigned to the variable marks.


Example 2: Comparison Operator


marks = 95

print(marks == 95)

print(marks == 80)

Output


True

False

The comparison operator checks whether two values are equal and returns a Boolean value (True or False).


Think Like a Programmer

Predict the output of the following program:


x = 15

print(x == 15)

print(x == 20)

x = 20

print(x)

Answer


True

False

20

The first two statements compare values, while the third statement changes the value stored in the variable.


Remember:
  • = means Assign a Value.
  • == means Compare Two Values.

A simple memory trick is:

  • One "=" → Store
  • Two "==" → Compare

Rules for Naming Variables

  • Must begin with a letter or underscore (_).
  • Cannot begin with a digit.
  • Can contain letters, digits, and underscores.
  • Cannot contain spaces.
  • Cannot use Python keywords.
  • Python is case-sensitive.

Valid Variable Names

Variable Name Valid?
student ✔ Yes
roll_no ✔ Yes
_salary ✔ Yes
marks2026 ✔ Yes

Invalid Variable Names

Variable Name Reason
2marks Starts with a digit
student name Contains a space
class Python keyword
total-marks Contains hyphen (-)

Python is Case-Sensitive

Python treats uppercase and lowercase letters as different.


name = "Riya"

Name = "Aman"

print(name)

print(Name)

Output


Riya

Aman

Dynamic Typing

Python is a dynamically typed language. The data type of a variable is determined automatically based on the value assigned to it.


x = 25

x = "Python"

x = 45.6

The same variable x stores an integer, then a string, and finally a floating-point value.


Multiple Assignment

Python allows multiple variables to be assigned values in a single statement.

Example 1


a = b = c = 100

All three variables store the value 100.

Example 2


x, y, z = 10, 20, 30
Variable Value
x 10
y 20
z 30

Swapping Variables

Python provides a simple way to exchange the values of two variables without using a third variable.


a = 10

b = 20

a, b = b, a

print(a)

print(b)

Output


20

10

Variable Naming Conventions

Although Python allows many styles of naming variables, following standard naming conventions improves code readability.

Convention Example
Use meaningful names student_name
Use lowercase letters total_marks
Separate words using underscore annual_salary
Avoid single-letter names (except loop variables) student_age

Think Like a Programmer

Predict the value stored in each variable after executing the following code:


x = 10

y = x

x = 20
Variable Final Value
x 20
y 10

Changing x after assigning its value to y does not affect y.


Real-Life Example

Imagine a student locker.

  • The locker number is the variable name.
  • The books kept inside the locker are the variable value.
  • The books can be replaced, but the locker number remains the same.

Common Errors

  • Using keywords as variable names.
  • Starting variable names with digits.
  • Using spaces in variable names.
  • Confusing = with ==.
  • Ignoring Python's case sensitivity.

Exam Tips

  • Remember that variables are created automatically when a value is assigned.
  • Learn all variable naming rules.
  • Understand dynamic typing.
  • Practice multiple assignment and swapping.
  • Use meaningful variable names in programs.

Frequently Asked Questions (FAQs)

1. What is a variable?

A variable is a named memory location used to store data.

2. Does Python require variable declaration?

No. Variables are created automatically when values are assigned.

3. Is Python case-sensitive?

Yes. Variables such as name and Name are different.

4. What is dynamic typing?

Dynamic typing means the data type of a variable is determined automatically during execution.

5. Can multiple variables be assigned in one statement?

Yes. Python supports multiple assignment.


Summary

  • Variables store data in memory.
  • Variables are created automatically during assignment.
  • Variable names must follow Python naming rules.
  • Python is a dynamically typed language.
  • Multiple assignment and variable swapping are simple in Python.
  • Meaningful variable names improve program readability and maintenance.