Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python Fundamentals - II Cs Class 11

Cs class 11 notes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Fundamentals - II Cs Class 11

Cs class 11 notes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

OPERATORS:-

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

2. Comparison (Relational) Operators

Comparison operators are used to compare two values or variables and return a Boolean (True or
False).

Code for python


x = 10
y = 20
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: False
print(x < y) # Output: True

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

+= Add and assign x += 3 → x = x + 3


-= Subtract and assign x -= 3 → x = x - 3

*= Multiply and assign x *= 3 → x = x * 3

/= Divide and assign x /= 3 → x = x / 3

//= Floor divide and assign x //= 3 → x = x // 3

%= Modulus and assign x %= 3 → x = x % 3

**= Exponentiate and assign x **= 3 → x = x ** 3

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

or Returns True if one of the statements is True True or False → True

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.).

Operator Description Example


in Returns True if a value exists in the sequence 'a' in 'apple' → True

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

for General understanding rvalue and lvalue:-


In Python, L-value refers to a variable that can appear on the left side of an assignment and
can store a value, representing a modifiable memory location. R-value refers to the value or
data that is assigned to an L-value, typically appearing on the right side of the assignment. L-
values are variables, while R-values are constants or expressions. For example, in x = 5, x is
the L-value, and 5 is the R-value. R-values cannot appear on the left side of an assignment.
Punctuators in Python
1. Parentheses (): Group expressions, call functions, define tuples.
Example: print("Hello")

2. Square Brackets []: Indexing, slicing, define lists.


Example: my_list[0]

3. Curly Braces {}: Define dictionaries and sets.


Example: {"key": "value"}

4. Colon : : Indicate block start, dictionary key-value pairs, slicing.


Example: def func():, my_dict["key"] = "value"

5. Comma , : Separate items in lists, tuples, and arguments.


Example: 1, 2, 3
Barebone of python programme
Statement: A statement is a single line of code that performs an action, such as an assignment or a
print operation. Example: x = 5 or print(x).

Expression: An expression is a combination of values, variables, and operators that evaluates to a


single value. Example: 3 + 4 or x * 2.

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("Hello, " + 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.

- Example: print("Hello, World" # Missing closing parenthesis

- 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.

- Example: number = int(input("Enter a number: "))

print(10 / number) # May cause a runtime error if number = 0

- 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.

3) Exceptions (Unhandled Errors)


Exceptions are special types of errors that occur when something unexpected happens during
execution, like trying to open a file that doesn’t exist.
- Example: A file not found error when trying to read a file that isn’t available.

try:

number = int(input("Enter a number: "))

print(10 / number)

except ZeroDivisionError:

print("Cannot divide by zero!")

- How to Spot: Without proper error handling (like using `try-catch` blocks), these errors will crash
your program.

You might also like