Detailed Python Notes for Beginners
1. Python Interpreter
The Python Interpreter is a program that reads and executes Python code line by line. When
a Python program is run, the interpreter first converts it into bytecode and then the Python
Virtual Machine (PVM) executes the bytecode.
Advantages:
- Easier debugging due to line-by-line execution.
- Platform-independent execution.
Example:
print("Welcome to Python!")
2. Data Types in Python
Python has several built-in data types used to store values of different kinds.
Common Data Types:
- int: Integer values (e.g., x = 10)
- float: Decimal values (e.g., pi = 3.14)
- str: Textual data (e.g., name = 'Alice')
- bool: Boolean values (True or False)
- list: Ordered, mutable collection (e.g., nums = [1, 2, 3])
- tuple: Ordered, immutable collection (e.g., t = (1, 2))
- set: Unordered collection of unique items (e.g., s = {1, 2, 3})
- dict: Collection of key-value pairs (e.g., d = {'a': 1})
a = 5 # int
b = 3.14 # float
name = "Alice" # str
flag = True # bool
nums = [1, 2, 3] # list
t = (4, 5) # tuple
s = {1, 2, 3} # set
d = {"a": 1, "b": 2} # dict
3. Input/Output Statements
Python uses the input() function to take input from the user and the print() function to
display output.
You can convert input into the desired data type using int(), float(), etc.
Examples:
name = input("Enter your name: ")
print("Hello", name)
num = int(input("Enter a number: "))
print("Double is:", num * 2)
a = input("Enter first number: ")
b = input("Enter second number: ")
print("Sum is:", int(a) + int(b))
4. Python Operators
Operators in Python are used to perform operations on variables and values.
Types of Operators:
1. Arithmetic: +, -, *, /, %, //, **
2. Comparison: ==, !=, >, <, >=, <=
3. Logical: and, or, not
4. Assignment: =, +=, -=, *=, /=
5. Bitwise: &, |, ^, ~, <<, >>
6. Membership: in, not in
7. Identity: is, is not
Examples:
a = 10
b = 3
print("Addition:", a + b)
print("Power:", a ** b)
print("Floor Division:", a // b)
print("a > b:", a > b)
print("a == b:", a == b)
x = True
y = False
print(x and y)
print(x or y)
print(not x)
lst = [1, 2, 3]
print(2 in lst)
print(4 not in lst)
x = [1, 2]
y = [1, 2]
z = x
print(x is y) # False
print(x is z) # True
5. Decision Structures in Python
Python uses conditional statements to execute blocks of code based on conditions.
1. if statement
2. if-else statement
3. if-elif-else ladder
Syntax:
if condition:
statement
elif another_condition:
statement
else:
statement
Examples:
age = int(input("Enter your age: "))
if age >= 18:
print("Adult")
else:
print("Minor")
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")