0% found this document useful (0 votes)
3 views5 pages

3 Variables And Operators In Python

The document provides an overview of variables and operators in Python, explaining how to create variables, the rules for naming them, and the use of assignment and arithmetic operators. It also covers type conversion and type casting, as well as Boolean, logical, and comparison operators. Additionally, the document includes examples and exercises for practical understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

3 Variables And Operators In Python

The document provides an overview of variables and operators in Python, explaining how to create variables, the rules for naming them, and the use of assignment and arithmetic operators. It also covers type conversion and type casting, as well as Boolean, logical, and comparison operators. Additionally, the document includes examples and exercises for practical understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VARIABLES & OPERATORS IN PYTHON

3.1 Variables in Python


A variable is a name given to a memory location that stores a value. In Python, variables are created when
a value is assigned to them.

Example:

x = 10
name = "Ali"
pi = 3.14

Python is dynamically typed, which means you do not need to declare the data type explicitly.

3.2 Rules and Guidelines for Creating a Variable


Rules: - Variable names must start with a letter (a–z, A–Z) or underscore (_) - Cannot start with a number -
Cannot use Python keywords (if, for, while, etc.) - No spaces allowed

Guidelines (Best Practices): - Use meaningful names (e.g., total_marks instead of tm ) - Use lowercase
letters with underscores - Avoid single-character names except for counters

Valid Variables:

age = 20
_total = 100
student_name = "Sara"

Invalid Variables:

2marks = 90
student-name = "Ali"

3.3 Assignment Operator


The assignment operator (=) is used to assign a value to a variable.

1
Example:

x = 5
y = x

Compound Assignment Operators:

x += 2 # x = x + 2
x -= 1 # x = x - 1
x *= 3 # x = x * 3
x /= 2 # x = x / 2

3.4 Multiple Assignments


Python allows assigning multiple values in a single line.

Example:

a, b, c = 10, 20, 30

Same Value Assignment:

x = y = z = 0

3.5 Use of Built-in Function (type)


The type() function is used to check the data type of a variable.

Example:

x = 10
print(type(x))

y = 3.14
print(type(y))

2
3.6 Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.

Operator Description Example

+ Addition 5+2=7

- Subtraction 5-2=3

* Multiplication 5 * 2 = 10

/ Division 5 / 2 = 2.5

** Exponent 2 ** 3 = 8

Example Code:

a = 10
b = 3
print(a + b)
print(a ** b)

3.7 Type Conversion vs Type Casting

Type Conversion (Implicit)

Automatically done by Python.

Example:

x = 5
y = 2.5
z = x + y
print(type(z))

Type Casting (Explicit)

Done by the programmer using functions.

Example:

3
x = "10"
y = int(x)

Common casting functions: - int() - float() - str()

3.8 Boolean Operator


Boolean values represent True or False.

Example:

is_pass = True
print(type(is_pass))

Relational expressions also return Boolean values.

3.9 Logical & Comparison Operators

Comparison Operators

Used to compare two values.

Operator Meaning

== Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

Example:

x = 10
y = 5
print(x > y)

4
Logical Operators

Used to combine conditional statements.

Operator Description

and True if both conditions are true

or True if any one condition is true

not Reverses the result

Example:

x = 10
y = 5
print(x > 5 and y < 10)

3.10 Exercise
1. Define a variable and give two examples.
2. Write rules for naming variables in Python.
3. Write a Python program to add two numbers using variables.
4. What is the use of type() function?
5. Differentiate between type conversion and type casting.
6. Write a program to demonstrate arithmetic operators.
7. Write examples of logical and comparison operators.

You might also like