Python Fundamentals - II Cs Class 11
Python Fundamentals - II Cs Class 11
Operators are special symbols that perform operations on variables and values. They are the
foundation of any programming language for performing calculations, comparisons, logical
operations, and more. Python provides several types of operators, categorized based on the type of
operations they perform.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition,
subtraction, multiplication, etc.
a = 10
b=3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a % b) # Output: 1
print(a ** b) # Output: 1000
print(a // b) # Output: 3
Comparison operators are used to compare two values or variables and return a Boolean (True or
False).
3. Assignment Operators:-
Assignment operators are used to assign values to variables. Python supports a wide range of
compound assignment operators for performing operations and updating values simultaneously.
Operator Description Example
= Simple assignment x = 5 assigns 5 to x
Example:
x=5
x += 3 # Same as x = x + 3
print(x) # Output: 8
4. Logical Operators:-
Logical operators are used to combine conditional statements and return Boolean values.
Operator Description Example
and Returns True if both statements are True True and False → False
not Reverses the result; returns False if the result is True not True → False
Example:
Python code
x=5
y = 10
print(x > 3 and y < 20) # Output: True
print(x > 6 or y > 10) # Output: False
print(not(x > 3)) # Output: False
5. Identity Operators: -
Identity operators are used to compare objects and check if they reference the same memory
location (i.e., if they are the same object).
Description Example
Operator
is Returns True if both variables point to the same object x is y
is not Returns True if variables do not point to the same object x is not y
Example:
Python code
x = [1, 2, 3]
y = [1, 2, 3]
z=x
print(x is y) # Output: False (Different objects)
print(x is z) # Output: True (Same object)
print(x is not y) # Output: True (Different objects)
7. Membership Operators
Membership operators are used to check whether a value or variable exists within a sequence (e.g.,
string, list, tuple, etc.).
not in Returns True if a value does not exist in the sequence 'b' not in 'apple' → True
Example:
Python code
x = [1, 2, 3, 4, 5]
print(3 in x) # Output: True
print(6 not in x) # Output: True
Comment: A comment is text in the code ignored by the interpreter, used to explain or annotate the
code. Comments start with #. Example: # This is a comment.
Function: A function is a reusable block of code that performs a specific task and can be called with
arguments. Defined using the def keyword. Example:
Python code
def add(a, b):
return a + b
Indentation: Indentation is the use of spaces or tabs to define the structure of code blocks. In
Python, it indicates the start and end of code blocks like those in functions or loops. Example:
Python code
if x > 0:
print("Positive")
Block: A block is a group of statements that are executed together, defined by consistent indentation.
Example:
def greet(name):
print("Welcome!")
Errors in Programming:
In programming, errors can happen at different stages. Some errors stop your program before it even
runs, while others appear when your program is running. We can classify these errors into two
categories: Compile-Time Errors and Run-Time Errors.
1. Compile-Time Errors
Compile-time errors occur when you're writing the code, before the program even runs. These errors
are caught by the compiler, which checks if your code is following the rules of the programming
language. If there are any compile-time errors, the program won’t run until they're fixed.
a) Syntax Errors
These happen when the code doesn’t follow the grammar or rules of the programming language.
- How to Spot: The compiler will flag these errors and show where the mistake is. Fixing them is
usually simple once you know the problem.
b) Semantic Errors
These occur when the code is written correctly in terms of grammar, but the meaning is wrong.
- Example: Trying to use a variable before giving it a value or calling a function with the wrong
number of arguments.
- How to Spot: The compiler will catch this and show an error message that points to the incorrect
part of the code.
2. Run-Time Errors
Run-time errors happen after the program has been successfully compiled and is running. These
errors occur due to actions that happen only when the program is actually being executed.
- Runtime Errors
These occur when the program tries to perform an operation that isn’t allowed or possible.
- How to Spot: The program will crash or stop working when it hits a runtime error. You’ll need to fix
the code logic that leads to the problem.
- Logic Errors
These errors don’t stop the program from running, but they make the program behave incorrectly
or give wrong results. These happen when your logic is flawed.
- Example: Using the wrong formula for a calculation or setting incorrect conditions for a loop.
- How to Spot: The program runs, but the output is wrong. Logic errors are tricky and are usually
found through careful testing and debugging.
try:
print(10 / number)
except ZeroDivisionError:
- How to Spot: Without proper error handling (like using `try-catch` blocks), these errors will crash
your program.