VIVEKANAND SCHOOL D-BLOCK ANAND VIHAR CLASS-11
PYTHON NOTES
What is python?
Features of Python
· High Level
. Free and Open Source
. Case Sensitive
. Interpreted
. Plateform Independent
. Rich library of functions
· Dynamic
. General Purpose
Execution Mode
We can use Python Interpreter in two ways:
. Interactive mode
· Script mode
Interactive Mode
. Instant execution of individual statement
· Convenient for testing single line of code
. We cannot save statements for future use
Script Mode
. Allows us to write and execute more than one Instruction together.
. We can save programs (python script) for future use
. Python scripts are saved as file with extension ".py"
What is the print() Statement in Python?
The print() function is used to display output on the screen.
📌 Syntax:
print(object(s), sep=' ', end='\n')
Some Special Parameters in print()
sep – Separator between multiple values:
print("Hello", "World", sep="-")
👉 Output:
Hello-World
end – What to print at the end of the line:
print("Hello", end=" ")
print("World")
👉 Output:
Hello World
🔹 Key Points
print() is used to display text, numbers, and variables.
Strings are written in quotes (" " or ' ').
You can print multiple values by separating them with commas.
print() automatically goes to the next line unless you change the end parameter.
Tokens:
Tokens are the smallest individual unit in a program. It is further divided into 5 parts:
1. Keyword
2. Identifier
3. Operator
4. Literal
5. Punctuator
1. Python Keywords
. These are predefined words which a specific meaning to Python Interpreter.
. These are reserve keywords
2. Identifiers
These are the name given to any value.
Rules for naming Identifier
. The name should begin with an alphabet or and underscore sign and can be followed by any combination of
characters a-z, A-Z, 0-9 or underscore.
. It can be of any length but we should keep it simple, short and meaningful.
. it should not be a python keyword or reserved word.
. We cannot use special symbols like!, @, #, $, % etc. in identifiers.
3. Operators
These are special symbols used to perform specific operation on values. Different types of operator
supported in python are given below:
. Arithmetic operator
. Relational operator
·Assignment operator
. Logical operator
•Membership operator
1. Arithmetic Operators in Python
Arithmetic operators are used to perform mathematical operations.
➤ List of Arithmetic Operators
Operator Name Example Meaning
+ Addition a + b Adds two numbers
- Subtraction a - b Subtracts b from a
* Multiplication a * b Multiplies a and b
/ Divisiona / b Divides a by b (float result)
// Floor Division a // b Division result without decimal
% Modulus a % b Remainder of a divided by b
** Exponentiation a ** b a raised to power of b
✅ Explanation with Examples a = 10
b=3
print(a + b) # 13 → Addition print(a - b) # 7 → Subtraction print(a * b) # 30 → Multiplication
print(a / b) # 3.333... → Normal division (float) print(a // b) # 3 → Floor division (integer result) print(a % b) #
1 → Remainder (modulo)
print(a ** b) # 1000 → 10 to the power 3
📌 Key Notes:
/ always gives a float (decimal) result, even if numbers divide exactly. 6 / 2 → 3.0
// gives the integer part only (floor value). 7 // 2 → 3
-7 // 2 → -4 (because it rounds down)
% gives the remainder of division. 7 % 3 → 1
10 % 2 → 0
** is used for power/exponentiation. 2 ** 3 → 8
5 ** 2 → 25
Solving Expressions with Multiple Arithmetic Operators
When an expression has more than one operator, Python follows a specific order to solve it. This order is
called operator precedence.
✅ Operator Precedence (Highest to Lowest)
Precedence Operators Description
1 ** Exponentiation
2 *, /, //, % Multiplication, Division, Floor Division, Modulus
3 +, - Addition, Subtraction
4 () Parentheses – evaluated first
Rule:
Always solve:
Parentheses first Then Exponents
Then Multiplication/Division/Modulus Lastly Addition/Subtraction
If operators have same precedence, Python solves from left to right (except for **, which is right to left).
📌 Example 1:
result = 5 + 3 * 2 Step-by-step:
Multiplication first → 3 * 2 = 6
Then addition → 5 + 6 = 11
✅ Output: 11
📌 Example 2:
result = (5 + 3) * 2 Step-by-step:
Parentheses first → 5 + 3 = 8
Then multiplication → 8 * 2 = 16
✅ Output: 16
📌 Example 3:
result = 2 + 3 * 4 ** 2 Step-by-step:
Exponentiation first → 4 ** 2 = 16
Then multiplication → 3 * 16 = 48
Then addition → 2 + 48 = 50
✅ Output: 50
📌 Example 4:
result = (10 - 2) ** 2 + 5 * 2 Step-by-step:
Parentheses → 10 - 2 = 8
Exponent → 8 ** 2 = 64
Multiplication → 5 * 2 = 10
Addition → 64 + 10 = 74
✅ Output: 74
2. Relational Operators
Relational operators are used to compare two values. The result of a relational operation is always either:
True (if the condition is correct), or False (if the condition is incorrect)
📘 List of Relational Operators in Python
Operator Description Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7 > 4 True
< Less than 3 < 8 True
>= Greater than or equal to 6 >= 6 True
<= Less than or equal to 2 <= 5 True
✅ Examples in Python
a = 10
b = 20
print(a == b) # False print(a != b) # True print(a > b) # False print(a < b) # True print(a >= 10) # True print(b <=
15) # False
Examples with Strings
In Python, strings are compared lexicographically (i.e., like dictionary order), based on Unicode values of
characters.
name1 = "Apple"
name2 = "Banana"
print(name1 == name2) # False
print(name1 != name2) # True
print(name1 > name2) # False ("A" comes before "B")
print(name1 < name2) # True
👉 'A' < 'B' because the Unicode value of 'A' is 65 and 'B' is 66.
________________________________________
🔍 More String Comparison Examples
print("cat" == "cat") # True
print("dog" != "Dog") # True ('d' and 'D' are different)
print("apple" < "bat") # True ('a' comes before 'b')
print("Zoo" > "apple") # False ('Z' has lower Unicode than 'a')
________________________________________
✅ Summary
• Relational operators return True or False.
• They work on both numbers and strings.
• String comparisons are case-sensitive and follow alphabetical order (A–Z < a–z).
3. Logical operator
Logical operators are used to combine two or more conditions. They return True or False based on Boolean
logic.
🔗 List of Logical Operators in Python
Operator Name Description Example Result
and Logical AND True only if both conditions are True (5 > 3) and (10 > 2) True
or Logical OR True if at least one condition is True (5 > 3) or (10 < 2) True
not Logical NOT Reverses the result (True ↔ False) not (5 > 3) False
Logical Operator Precedence (Order of Evaluation)
1. not (highest)
2. and
3. or (lowest)
👉 Parentheses () can be used to force order.
Q Example Without Parentheses
x=5
y = 10
z = 15
print(x > 3 and y < 20 or z < 10) Step-by-step:
x > 3 → True
y < 20 → True
z < 10 → False Now:
True and True → True True or False → ✅ True
Q Example With Parentheses
print(x > 3 and (y > 20 or z < 20)) x > 3 → True
Inside ():
y > 20 → False
z < 20 → True
False or True → True Now:
True and True → ✅ True
📌 Example with not print(not (x < 2 or y > 5)) x < 2 → False
y > 5 → True
Inside () → False or True → True not True → ✅ False
4. Membership operator
Membership operators are used to test whether a value or variable is present in a sequence (like a string, list,
tuple, or dictionary).
They return a Boolean value: True or False.
✅ Types of Membership Operators
Operator Description Example Result
in Returns True if value is found 'a' in 'apple' True
not in Returns True if value is not found 'x' not in 'apple' True
📘 Examples in Python
▶️ With Strings:
fruit = "apple"
print('a' in fruit) # True print('x' in fruit) # False print('app' in fruit) # True
print('App' in fruit) # False (case-sensitive)
5. Shorthand operator
Shorthand operators are used to update the value of a variable in a shorter way. Instead of writing:
x=x+5
You can write:
x += 5
✅ This is cleaner and faster.
🧮 List of Shorthand Operators in Python
Operator Meaning Equivalent To
+= Add and assign x += 2 → x = x + 2
-= Subtract and assign x -= 3 → x = x - 3
*= Multiply and assign x *= 4 → x = x * 4
/= Divide and assign x /= 5 → x = x / 5
//= Floor divide and assign x //= 2 → x = x // 2
%= Modulus and assign x %= 3 → x = x % 3
**= Power and assign x **= 2 → x = x ** 2
✅ Examples in Python x = 10
x += 5 # x = 10 + 5 → 15
x -= 2 # x = 15 - 2 → 13
x *= 2 # x = 13 * 2 → 26
x /= 4 # x = 26 / 4 → 6.5
x //= 2 # x = 6.5 // 2 → 3.0 (float)
x %= 2 # x = 3.0 % 2 → 1.0
x **= 3 # x = 1.0 ** 3 → 1.0
6. Identity Operators in Python
Identity operators are used to compare the memory locations of two objects, i.e., whether two variables
point to the same object in memory.
✅ The two identity operators are:
Operator Meaning Example
is True if both refer to same object a is b
is not True if they refer to different objects a is not b