🧠 Day-1: Python Basics — MIT-Level Notes
🔢 1. Variables & Data Types
✅ What is a Variable?
A variable is a name that refers to a value. In Python, you don’t need to
declare the data type explicitly.
x=5 # int
pi = 3.14 # float
name = "Dhruv" # str
is_cool = True # bool
✅ Check the Data Type:
print(type(x)) # <class 'int'>
print(type(name)) # <class 'str'>
🧾 2. Input & Output
✅ Getting Input:
user_input = input("Enter your name: ")
✅ Printing Output:
print("Hello", "World", sep="--", end="!\n")
✅ Format Output:
age = 20
print(f"{name} is {age} years old.") # f-string
➕ 3. Operators
✅ Arithmetic Operators:
a, b = 10, 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3 (floor division)
print(a % b) # 1 (modulo)
print(a ** b) # 1000 (exponent)
✅ Comparison Operators:
print(a == b) # False
print(a != b) # True
print(a < b) # False
✅ Logical Operators:
x=5
print(x > 2 and x < 10) # True
print(x < 2 or x > 1) # True
print(not(x > 1)) # False
🔁 4. Control Structures (if, elif, else)
x = int(input("Enter a number: "))
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
🔂 5. Loops
✅ For Loop:
for i in range(5):
print(i) # 0 to 4
# Nested pattern
for i in range(3):
for j in range(3):
print("*", end="")
print()
✅ While Loop:
count = 0
while count < 5:
print(count)
count += 1
🔧 6. Functions
def greet(name):
"""This function greets the user."""
return f"Hello, {name}!"
print(greet("Dhruv"))
● def: defines a function
● return: sends result back
● """Docstring""": describes what the function does
📚 7. Lists
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
print(fruits[-1]) # cherry
print(fruits[0:2]) # ['apple', 'banana']
fruits.append("orange") # add
fruits.insert(1, "grape")# insert at index
fruits.pop() # remove last
fruits.remove("banana") # remove specific
fruits.sort()
fruits.clear()
print(fruits[::-1]) # reverse
📦 8. Tuples
t = (1, 2, 3)
print(t[0]) #1
t_list = list(t) # convert to list
t_list.sort()
t = tuple(t_list)
● Immutable (can't modify)
● Indexing/slicing supported
🔥 9. Sets
s = {1, 2, 3, 1}
print(s) # {1, 2, 3}
s.add(4)
s.discard(2)
print(3 in s) # membership
● No duplicates
● Unordered
🧠 10. Dictionaries
person = {"name": "Dhruv", "age": 21}
print(person["name"]) # Access
person["age"] = 22 # Update
person["city"] = "Delhi" # Add
del person["city"] # Delete
print(person.keys())
print(person.values())
print(person.items())