Comprehensive Python Notes
This document contains a comprehensive overview of Python, covering its syntax, key
concepts, and examples to help you prepare for your exam.
1. Basics of Python
Python is an interpreted, high-level, general-purpose programming language.
Key features include simplicity, readability, and extensive library support.
Hello, World!
# Code Example:
print("Hello, World!")
Comments
# Single-line comment
# This is a comment
# Multi-line comment (no direct syntax, use multi-line strings)
'''
This is a multi-line comment
'''
2. Variables and Data Types
# Assigning Variables
x=5 # Integer
y = 3.14 # Float
z = "Hello" # String
# Dynamic Typing
a = 10
a = "Python" # 'a' now refers to a string
Data Types
# Examples of Python Data Types
int_var = 10 # Integer
float_var = 10.5 # Float
str_var = "Python" # String
bool_var = True # Boolean
list_var = [1, 2, 3] # List
tuple_var = (1, 2, 3) # Tuple
dict_var = {"key": "value"} # Dictionary
set_var = {1, 2, 3} # Set
3. Conditional Statements
# if-else Example
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
# if-elif-else Example
y = 20
if y < 10:
print("y is less than 10")
elif y == 20:
print("y is 20")
else:
print("y is greater than 10 and not 20")
4. Loops
For Loop
# For Loop Example
for i in range(5):
print(i) # Prints numbers from 0 to 4
While Loop
# While Loop Example
x=0
while x < 5:
print(x)
x += 1 # Increment x by 1
5. Functions
# Function Syntax
def function_name(parameters):
# Function body
return value
# Example:
def greet(name):
return f"Hello, {name}!"
print(greet("Ayush")) # Output: Hello, Ayush!
6. Object-Oriented Programming (OOP)
# Class and Object Example
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"My name is {self.name} and I am {self.age} years old."
# Create an object
person = Person("Ayush", 17)
print(person.introduce()) # Output: My name is Ayush and I am 17 years old.
7. Type Conversion
Python allows you to convert one data type to another. This can be done explicitly using
type conversion functions.
# Examples:
x = 10 # Integer
y = 3.14 # Float
# Convert int to float
x_float = float(x)
print(x_float) # Output: 10.0
# Convert float to int
y_int = int(y)
print(y_int) # Output: 3
# Convert string to int
s = "123"
s_int = int(s)
print(s_int) # Output: 123
# Convert list to tuple
list_var = [1, 2, 3]
tuple_var = tuple(list_var)
print(tuple_var) # Output: (1, 2, 3)
# Convert tuple to list
list_from_tuple = list(tuple_var)
print(list_from_tuple) # Output: [1, 2, 3]
8. Differences Between List, Dictionary, and Tuple
Feature List Dictionary Tuple
Definition Ordered, mutable Unordered, mutable Ordered, immutable
collection of items collection of key- collection of items
value pairs
Syntax [1, 2, 3] {"key": "value"} (1, 2, 3)
Mutable/Immutable Mutable Mutable Immutable
8. Differences Between List, Dictionary, and Tuple
Feature List Dictionary Tuple
Definition Ordered, mutable Unordered, mutable Ordered, immutable
collection of items collection of key- collection of items
value pairs
Syntax [1, 2, 3] {"key": "value"} (1, 2, 3)
Mutable/Immutable Mutable Mutable Immutable
Use Case Storing related Mapping keys to Fixed collection of
items values items