Chapter 1
Chapter 1
Python is a high-level, interpreted programming language known for its simplicity and
readability.
Keywords: Reserved words like if, elif, while, import, def, class, etc.
Identifiers: Variable, function, and class names. Must start with a letter or _, and
cannot be a keyword.
Type Conversion:
python
Copy
x = int(3.7) # Converts float to int → 3
y = float(10) # Converts int to float → 10.0
z = str(25) # Converts int to string → "25"
Example:
python
Copy
a, b = 10, 20
print(a + b) # 30
print(a > b and b > 15) # False
python
Copy
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
For Loop:
python
Copy
for i in range(5):
print(i) # Prints 0 to 4
While Loop:
python
Copy
x = 5
while x > 0:
print(x)
x -= 1
python
Copy
def greet(name):
return "Hello, " + name
python
Copy
square = lambda x: x * x
print(square(5)) # 25
python
Copy
f = open("file.txt", "r") # Open in read mode
data = f.read() # Read the content
f.close() # Close the file
python
Copy
f = open("file.txt", "w")
f.write("Hello, World!")
f.close()
Mode Description
r Read mode
w Write mode (Overwrites existing file)
a Append mode (Adds to the existing file)
rb Read binary mode
wb Write binary mode
Summary of Chapter 1