PYTHON NOTES:
1. PYTHON INTERPRETER
What is a Python Interpreter?
• The Python interpreter is a program that reads and
executes Python code.
• It reads your Python program line by line and executes it
immediately.
• This makes it easier to find and fix errors, especially
for beginners.
Two Ways to Use Python:
Interactive Mode:
• Like a calculator. You type a command and see the result
immediately.
• Used for quick testing or learning.
>>> print("Hello, World!")
Hello, World!
Script Mode:
• You write your code in a .py file (text file), save it,
and run it later.
• Best for writing longer programs.
Example:
# file: greet.py
print("Welcome to Python!")
Run it: python greet.py
2. DATA TYPES IN PYTHON
Everything in Python is an object, and each object has a type.
2.1 Numbers
Integer (int)
• Whole numbers without decimals.
x = 10
print(type(x)) # <class 'int'>
Float (float)
• Numbers with decimals.
y = 3.14
print(type(y)) # <class 'float'>
Complex (complex)
• Numbers with a real and imaginary part.
z = 2 + 3j
print(type(z)) # <class 'complex'>
2.2 String (str)
• A sequence of characters (letters, numbers, symbols).
name = "Alice"
print(name)
print(name.upper()) # Convert to uppercase
• Strings can be written using 'single', "double", or
'''triple''' quotes.
2.3 Boolean (bool)
• Used to represent True or False values.
is_student = True
has_passed = False
print(type(is_student)) # <class 'bool'>
2.4 List
• Ordered collection of items. Changeable (mutable).
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # banana
fruits.append("mango")
2.5 Tuple
• Ordered collection, but unchangeable (immutable).
dimensions = (20, 30, 40)
print(dimensions[0]) # 20
2.6 Set
• Unordered collection, no duplicate items allowed.
numbers = {1, 2, 3, 2}
print(numbers) # Output: {1, 2, 3}
2.7 Dictionary (dict)
• Collection of key-value pairs.
student = {"name": "Ali", "age": 20}
print(student["name"]) # Ali
3. INPUT & OUTPUT IN PYTHON
print() for Output
• Used to show messages or values on the screen.
print("Hello World")
x = 5
print("The value of x is:", x)
input() for Input
• Used to take input from the user.
• It always returns the input as a string.
name = input("Enter your name: ")
print("Hello", name)
# convert input to integer
age = int(input("Enter your age: "))
print("You will be", age + 1, "next year.")
4. PYTHON OPERATORS
Operators are symbols used to perform operations on variables
and values.
4.1 Arithmetic Operators
Operator Meaning Example Output
+ Addition 2 + 3 5
- Subtraction 5 - 1 4
* Multiplication 3 * 4 12
/ Division 10 / 2 5.0
// Floor Division 7 // 2 3
% Modulus 7 % 2 1
Operator Meaning Example Output
** Exponent 2 ** 3 8
4.2 Comparison Operators
Used to compare two values and return True or False.
a = 10
b = 5
print(a > b) # True
print(a == b) # False
4.3 Logical Operators
Used to combine multiple conditions.
x = 10
print(x > 5 and x < 15) # True
print(x < 5 or x > 15) # False
print(not(x > 5)) # False
4.4 Assignment Operators
Used to assign values and update them.
x = 10
x += 5 # x = x + 5
print(x) # 15
Other operators: -=, *=, /=, %=, **=
4.5 Membership Operators
Check if a value exists in a sequence.
colors = ["red", "blue"]
print("red" in colors) # True
print("green" not in colors) # True
4.6 Identity Operators
Check whether two variables refer to the same object in
memory.
a = [1, 2]
b = a
c = [1, 2]
print(a is b) # True
print(a is c) # False
5. CONTROL STRUCTURES (Decision Making & Loops)
5.1 If-Else Statements
Used to make decisions in a program.
Syntax:
if condition:
# do something
elif another_condition:
# do something else
else:
# fallback
Example:
age = int(input("Enter your age: "))
if age < 18:
print("You are a minor.")
elif age == 18:
print("You just became an adult!")
else:
print("You are an adult.")
5.2 Loops
For Loop – Used to repeat actions a known number of times.
for i in range(1, 6):
print("i =", i)
While Loop – Used when the number of repetitions is
unknown.
count = 1
while count <= 5:
print("Count is", count)
count += 1
5.3 Loop Control Statements
Statement Meaning
break Stop the loop
continue Skip the current iteration
pass Do nothing (placeholder)
Example of break:
for i in range(5):
if i == 3:
break
print(i)
Example of continue:
for i in range(5):
if i == 2:
continue
print(i)
Summary for Students
• Python is beginner-friendly and very readable.
• Learn the basic data types: int, float, str, list, tuple,
set, dict.
• Practice writing input/output programs using input() and
print().
• Master the basic operators and understand how to use them
in conditions.
• Practice control structures: if-else, for loops, while
loops, and loop controls.