1.
Output:
x=0
if x:
print("A")
else:
print("B")
Output: B (0 is False in Python)
2. Output:
Fibonacci Series (first 10 terms):
0 1 1 2 3 5 8 13 21 34
3. range() function:
Used to generate a sequence of numbers.
Example: range(1, 5) generates 1, 2, 3, 4
4. Decision-Making:
if, elif, else statements
Example:
x=5
if x > 0:
print("Positive")
5. OOP Example:
class Student:
def __init__(self, name, roll, marks):
self.name = name
self.roll = roll
self.__marks = marks
def check_pass(self):
return "Pass" if self.__marks >= 40 else "Fail"
s = Student("John", 101, 45)
print(s.check_pass())
6. Pyramid Pattern (n=5):
for i in range(1, 6):
print(" " * (5 - i) + "* " * i)
7. Output:
Reversed number: 56789
8. List Comprehension (Uppercase):
s = ["hello", "world"]
uppercase = [word.upper() for word in s]
print(uppercase)
9. Bitwise Even/Odd:
n=7
if n & 1:
print("Odd")
else:
print("Even")
10. Conditional Statements:
if-elif-else for decision-making
x = 10
if x < 0:
print("Negative")
elif x == 0:
print("Zero")
else:
print("Positive")
11. for vs while:
for - fixed iteration
while - runs while condition is True
12. Prime Palindromes:
def is_prime(n):
return n > 1 and all(n % i != 0 for i in range(2, int(n**0.5)+1))
def is_palindrome(n):
return str(n) == str(n)[::-1]
count = 0
n=2
while count < 10:
if is_prime(n) and is_palindrome(n):
print(n, end=" ")
count += 1
n += 1
13. Count Characters:
s = "Hello@123"
digits = letters = specials = 0
for ch in s:
if ch.isdigit(): digits += 1
elif ch.isalpha(): letters += 1
else: specials += 1
print(digits, letters, specials)
14. Even Numbers (1 to 10):
for i in range(1, 11):
if i % 2 == 0:
print(i)
15. Output:
rev = 4321
16. Squares using List Comprehension:
squares = [x**2 for x in range(1, 11)]
print(squares)
17. Reverse String using while:
s = "hello"
rev = ""
i = len(s) - 1
while i >= 0:
rev += s[i]
i -= 1
print(rev)
18. Sum of Even Numbers:
def sum_evens(lst):
return sum([x for x in lst if x % 2 == 0])
nums = [1, 2, 3, 4, 5, 6]
print(sum_evens(nums))
19. OOP Concepts:
- Class & Object: Template & instance
- Encapsulation: Private data (__var)
- Inheritance: Child inherits from Parent
- Polymorphism: Same method different behavior
- Abstraction: Hides details, shows essential
Example:
class Animal:
def speak(self): pass
class Dog(Animal):
def speak(self): print("Bark")