Variables Data Types and Operators#

โณ Loading Pyodideโ€ฆ

Variables#

Python uses variables to store information that can be used later in a program. Think of a variable as a name tag attached to an object in memory โ€” not the box itself, but the label stuck onto it.


๐Ÿงฑ Creating Variables#

A variable is created when you assign a value to a name using the = sign.

`

You can later use these variables anywhere in your program:


๐ŸŽฏ What Really Happens in Memory#

When you write:

Python creates an object (10) in memory, and x is just a reference (a label) that points to that object.

If you do this:

Now both x and y point to the same object in memory.

โœ… The id() function shows that both have the same ID, meaning they point to the same memory location.

Variable

Points To

Object Value

x

๐Ÿ‘‰

10

y

๐Ÿ‘‰

10 (same object as x)

Now, if you change the object:

โ— y still points to the old object (10), because you changed what x points to, not the object itself.


๐Ÿ’ก Analogy#

Think of x and y as name tags stuck to a coffee mug. Both can point to the same mug โ˜•, but if x changes its tag to a new mug, y still remains attached to the old one.

Variables#

A variable is created when you assign a value to a name using the = sign.

`

You can later use these variables anywhere in your program.

โœ… Naming Rules

  • Must start with a letter or underscore (_)

  • Can contain letters, numbers, and underscores

  • Case-sensitive (name โ‰  Name)

  • Avoid using keywords like for, if, class, etc.


๐Ÿงฎ Data Types#

Every value in Python has a type.

Data Type

Example

Description

int

10

Whole numbers

float

3.14

Decimal numbers

str

"Python"

Text or characters

bool

True, False

Logical values

list

[1, 2, 3]

Ordered, changeable collection

tuple

(1, 2, 3)

Ordered, unchangeable collection

dict

{"name": "Alex", "sales": 200}

Key-value pairs

You can check a variableโ€™s type with the built-in type() function.


โš™๏ธ Operators in Python#

Operators perform operations on variables and values.

Arithmetic Operators#

Operator

Description

Example

+

Addition

10 + 5 โ†’ 15

-

Subtraction

10 - 3 โ†’ 7

*

Multiplication

4 * 2 โ†’ 8

/

Division

8 / 2 โ†’ 4.0

%

Modulus (Remainder)

9 % 2 โ†’ 1

**

Exponent

2 ** 3 โ†’ 8

Comparison Operators#

Operator

Meaning

Example

==

Equal to

5 == 5 โ†’ True

!=

Not equal to

5 != 3 โ†’ True

>

Greater than

10 > 5 โ†’ True

<

Less than

2 < 8 โ†’ True

Logical Operators#

Operator

Description

Example

and

True if both are True

(5 > 2 and 4 < 10) โ†’ True

or

True if one is True

(5 > 2 or 4 > 10) โ†’ True

not

Reverses result

not (5 > 2) โ†’ False


๐Ÿ” Type Conversion#

You can change one data type to another using:


Why Naming Conventions Exist#

Good naming makes code easier to read, debug, and share. Python follows certain rules and best practices:

  • Variable names cannot be Python keywords (like class, if, for).

  • They must start with a letter or underscore (_), not a number.

  • They are case-sensitive (Name โ‰  name).

These conventions help both humans and computers understand the code clearly!

Why You Can't Use Words Like class or if

These are reserved keywords โ€” Python uses them for its own syntax. For example:

`

โŒ This gives a SyntaxError because class is already used to define classes in Python.

โœ… Instead, use:


๐Ÿง  Quick Practice Questions#

  • What is the difference between a variable and an object in Python?

  • What will this code print and why?

  • Predict the output:

  • Create variables to store:

  • Your name, age, monthly income, and whether you are a student. Then print them in one line using:

  • Write a Python program that:

  1. Takes two numbers a and b.

  2. Prints their sum, difference, and product.

Example:

  • Predict the output without running the code:

  • Which of the following are valid variable names? a) 2priceโ€ƒโ€ƒb) _priceโ€ƒโ€ƒc) classโ€ƒโ€ƒd) price2

  • Predict the output:

  • Identify the data types:

  • Find a reserved keyword using keyword.kwlist and try using it as a variable. What error do you get? Replace it with a meaningful name.

  • Write a short Python snippet that calculates profit margin using variables with clear business-style names (cost_price, selling_price, profit_margin).

# Your code here