Python_unit3_Answers
Python_unit3_Answers
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"
7. Output:
Reversed number: 56789
9. Bitwise Even/Odd:
n=7
if n & 1:
print("Odd")
else:
print("Even")
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
15. Output:
rev = 4321
nums = [1, 2, 3, 4, 5, 6]
print(sum_evens(nums))
Example:
class Animal:
def speak(self): pass
class Dog(Animal):
def speak(self): print("Bark")