Module 1to6 key notes
Module 1to6 key notes
Key Concepts:
Creativity & Motivation: Python enables creative
thinking through its simplicity and wide applicability.
Comments:
o Single-line: # comment
functions.
Parameters & Arguments:
o Parameters are placeholders; arguments are
actual values.
Loops:
ofor i in range(5):
o while condition:
Loop Control:
o break: exits loop
Tuples:
o Immutable.
o (a, b) vs [a, b]
Dictionaries:
o Key-value pairs: {"name": "Sachin"}
Command-line Arguments:
o Use sys.argv to accept input from command line.
Exceptions:
o Try-except block:
python
CopyEdit
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
Common Modules:
o datetime, time, os, calendar, math
Packages:
o Directory with __init__.py
✅ Module-Wise Notes
(Already provided above — keep that part as-is in your
PDF)
string)
6. Program to swap numbers:
python
CopyEdit
a=5
b = 10
temp = a
a=b
b = temp
print(a, b)
7. If-Elif-Else Example:
python
CopyEdit
x = 10
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")
8. For vs While:
o For: Known iterations
python
CopyEdit
for i in range(5): print(i)```
o While: Runs until condition is False
python
CopyEdit
while x < 5: x += 1```
9. Predict:
python
CopyEdit
for i in range(2, 10, 2):
print(i, end=" ")
Output: 2 4 6 8
10. Break and Continue:
python
CopyEdit
for i in range(5):
if i == 3: break
print(i)
11. Largest of three numbers:
python
CopyEdit
a = int(input())
b = int(input())
c = int(input())
if a >= b and a >= c:
print(a)
elif b >= c:
print(b)
else:
print(c)
12. Flow of execution:
python
CopyEdit
x=5
while x > 0:
print(x, end=" ")
x=2
Output: 5 2
13. Prime check function:
python
CopyEdit
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
14. Reverse string:
python
CopyEdit
s = "hello"
reversed_str = ""
for char in s:
reversed_str = char + reversed_str
print(reversed_str)
15. Divisible by 3 and 5:
python
CopyEdit
n = int(input())
if n % 3 == 0 and n % 5 == 0:
print("Yes")
else:
print("No")
def sum_even(numbers):
return sum(x for x in numbers if x % 2 == 0)
3. Local vs Global:
o Local: Inside function
4. Recursion (factorial):
python
def fact(n):
if n == 0:
return 1
return n * fact(n-1)
5. String slicing:
python
s = "Python"
print(s[0:3]) # 'Pyt'
6. Output of:
python
x = [1, 2, 3]
y=x
y.append(4)
print(x)
Output: [1, 2, 3, 4] (Lists are mutable)
7. List vs Tuple:
o List: Mutable, []
o Tuple: Immutable, ()
8. Max in list:
python
lst = [1, 4, 2]
max_num = lst[0]
for i in lst:
if i > max_num:
max_num = i
print(max_num)
9. File handling:
python
with open("file.txt") as f:
print(len(f.read().split()))
11. Calculator function:
Python