Python Notes
What is Python?
Python is a high-level, interpreted programming language known for its clear and
easy-to-understand syntax. It was created by Guido van Rossum and released in 1991.
Python is widely used in web development, data science, artificial intelligence, machine
learning, automation, and scientific research.
Example:
print("Welcome to Python Programming!")
Advantages of Python
1. Simple and Readable
2. Cross-Platform
3. Rich Library Support
4. Large Community
5. Supports Multiple Paradigms
6. Rapid Development
Disadvantages of Python
1. Slower Execution
2. High Memory Consumption
3. Mobile Development Limitations
4. Dynamic Typing Issues
Downloading and Installing
Python can be downloaded from the official website: https://www.python.org/downloads/
Installation steps (Windows):
1. Download the installer.
2. Run installer and check 'Add Python to PATH'.
3. Install.
Verify the python install using the command:
python –version
Which Version of Python?
Python 2 is outdated and not supported after 2020. It is not recommended for development
purpose.
Python 3 is current and recommended.
Always install the latest stable version of Python 3 which as of August 2025 is 3.13
Running Python Scripts
Python programs are saved in files ending with .py and can be run from the command line
using:
python filename.py
Using the Interpreter Interactively
Python provides an interactive shell for quick experiments.
Example:
>>> 5 + 3 → 8
>>> print('Hello') → Hello
Using Variables
Variables are containers for data values. Python uses dynamic typing so variable types need
not be declared.
Example:
x = 10
name = 'Arjun'
pi = 3.1416
String
In Python, a string is a fundamental data type representing a sequence of characters. Strings
are used to store and manipulate text data.
Creating Strings:
Strings can be created by enclosing characters within:
Single quotes: 'Hello'
Double quotes: "World"
Types of Strings:
Normal String – Processes escape characters like \n.
s = "Hello\nWorld"
print(s)
# Output:
# Hello
# World
Raw String: Prefixed with r, ignores escape sequences.
s = r"Hello\nWorld"
print(s)
# Hello\nWorld
Unicode String: Allows representation of international characters.
s = u"नमस्ते"
print(s)
# नमस्ते
String Operators and Expressions
Concatenation (+), Repetition (*), Membership (in, not in), Indexing, and Slicing.
Example:
a = 'Python'
print(a + ' Rocks') # Output :- a Rocks
print(a * 3) # Output :- PythonPythonPython
print('Py' in a)# Output :- True
print(a[0]) # Output :- P
print(a[0:3]) # Output :- Pyt
Writing to the Screen
The print() function is used to display output.
Example:
name = 'Meera'
score = 92
print('Hello,', name)
print('Your score is', score)
print(f'Welcome {name}, you scored {score}')
Operators and Expressions
Operators are symbols that perform specific operations on operands (values or variables).
Expressions are combinations of variables, values, and operators that the interpreter
evaluates to produce a result.
1. Arithmetic Operators
Used for performing mathematical calculations like addition, subtraction, multiplication,
division, modulus, floor division, and exponentiation.
Operator Meaning Example Result
+ Addition 5+3 8
- Subtraction 5-3 2
* Multiplication 5*3 15
/ Division (float) 5/2 2.5
// Floor Division 5 // 2 2
% Modulus 5%2 1
** Exponentiation 2 ** 3 8
Example Code:
a, b = 10, 3
print(a + b) # 13
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
2. Comparison (Relational) Operators
Used to compare two values. They return Boolean results (True or False).
Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5>3 True
< Less than 5<3 False
>= Greater than or 5 >= 5 True
equal to
<= Less than or equal 3 <= 5 True
to
Example Code:
x, y = 7, 10
print(x == y) # False
print(x != y) # True
print(x < y) # True
print(x >= y) # False
3. Logical Operators
Used to combine conditional statements.
Operator Meaning Example Result
and True if both (5 > 3) and (10 > 7) True
conditions are true
or True if at least one (5 > 3) or (10 < 7) True
condition is true
not Reverses the result not(5 > 3) False
Example Code:
a, b = True, False
print(a and b) # False
print(a or b) # True
print(not a) # False
4. Assignment Operators
Used to assign values to variables, often performing operations at the same time.
Operator Meaning Example Equivalent To
= Assign value x=5 x=5
+= Add and assign x += 3 x=x+3
-= Subtract and assign x -= 2 x=x-2
*= Multiply and assign x *= 4 x=x*4
/= Divide and assign x /= 2 x=x/2
//= Floor divide and x //= 2 x = x // 2
assign
%= Modulus and assign x %= 3 x=x%3
**= Power and assign x **= 2 x = x ** 2
Example Code:
x = 10
x += 5 # 15
x *= 2 # 30
print(x)
5. Bitwise Operators
Operate at the binary (bit) level.
Operator Meaning Example Result
& AND 5&3 1
| OR 5|3 7
^ XOR 5^3 6
~ NOT ~5 -6
<< Left Shift 5 << 1 10
>> Right Shift 5 >> 1 2
Example Code:
a, b = 5, 3
print(a & b) # 1
print(a | b) # 7
print(a ^ b) # 6
print(~a) # -6
print(a << 1) # 10
print(a >> 1) # 2
6. Identity Operators
Check whether two objects are stored at the same memory location.
Operator Meaning Example Result
is True if both point to x is y Depends
same object
is not True if not the same x is not y Depends
object
Example Code:
x = [1,2,3]
y = x
z = [1,2,3]
print(x is y) # True
print(x is z) # False
print(x is not z) # True
7. Membership Operators
Used to test whether a value exists in a sequence (string, list, tuple, etc.).
Operator Meaning Example Result
in True if value is 'a' in 'apple' True
found
not in True if value is not 'x' not in 'apple' True
found
Example Code:
fruits = ['apple','banana','cherry']
print('apple' in fruits) # True
print('grape' not in fruits) # True
Operator Precedence
When multiple operators are used in an expression, precedence decides the order of
evaluation. Highest to lowest order:
1. Parentheses ()
2. Exponentiation **
3. Unary operators (+, -, ~)
4. Multiplication, Division, Modulus (*, /, //, %)
5. Addition and Subtraction (+, -)
6. Bitwise operators (<<, >>, &, |, ^)
7. Comparison operators (==, !=, <, >, <=, >=)
8. Logical operators (not, and, or)
Example Code:
result = 2 + 3 * 4
print(result) # 14
result = (2 + 3) * 4
print(result) # 20