#PROGRAM FOR FIBONACCI SERIES
def fibonacci(n):
a, b = 0, 1 # Step 1: Initialize the first two Fibonacci numbers
series = [] # Step 2: Create an empty list to store the Fibonacci series
for _ in range(n): # Step 3: Loop n times (to generate n numbers)
series.append(a) # Step 4: Add the current number 'a' to the series list
a, b = b, a + b # Step 5: Update 'a' to be 'b' and 'b' to be 'a + b' (next Fibonacci numbers)
return series # Step 6: Return the completed Fibonacci series list
What happens when you call fibonacci(10)?
Initially: a = 0, b = 1, series = []
Iteration 1: Append 0 → series = [0]; Update → a = 1, b = 1
Iteration 2: Append 1 → series = [0, 1]; Update → a = 1, b = 2
Iteration 3: Append 1 → series = [0, 1, 1]; Update → a = 2, b = 3
Iteration 4: Append 2 → series = [0, 1, 1, 2]; Update → a = 3, b = 5
Iteration 5: Append 3 → series = [0, 1, 1, 2, 3]; Update → a = 5, b = 8
Iteration 6: Append 5 → series = [0, 1, 1, 2, 3, 5]; Update → a = 8, b = 13
Iteration 7: Append 8 → series = [0, 1, 1, 2, 3, 5, 8]; Update → a = 13, b = 21
Iteration 8: Append 13 → series = [0, 1, 1, 2, 3, 5, 8, 13]; Update → a = 21, b = 34
Iteration 9: Append 21 → series = [0, 1, 1, 2, 3, 5, 8, 13, 21]; Update → a = 34, b = 55
Iteration 10: Append 34 → series = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]; Update → a = 55, b = 89
The function then returns the list of the first 10 Fibonacci numbers:
OUTPUT
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
🟢 1. Print a Message
python
CopyEdit
print("Welcome to Python!")
🟢 2. Ask for the User's Name
python
CopyEdit
name = input("What is your name? ")
print("Hello, " + name + "!")
🟢 3. Add Two Numbers
python
CopyEdit
a = input("Enter first number: ")
b = input("Enter second number: ")
sum = int(a) + int(b)
print("The sum is:", sum)
🟢 4. Convert Celsius to Fahrenheit
python
CopyEdit
c = float(input("Enter temperature in Celsius: "))
f = (c * 9/5) + 32
print("Temperature in Fahrenheit:", f)
🟢 5. Square of a Number
python
CopyEdit
num = int(input("Enter a number: "))
print("Square is:", num * num)
🟢 6. Say Good Morning
python
CopyEdit
print("Good Morning!")
🟢 7. Print Numbers 1 to 5
python
CopyEdit
for i in range(1, 6):
print(i
🔢 1. Fibonacci Series (Up to N Terms)
python
CopyEdit
n = int(input("How many terms? "))
a, b = 0, 1
for _ in range(n):
print(a)
a, b = b, a + b
🔍 2. Check if a Number is Prime
python
CopyEdit
num = int(input("Enter a number: "))
if num < 2:
print("Not Prime")
else:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
🔁 3. Print All Prime Numbers in a Range
python
CopyEdit
start = int(input("Start of range: "))
end = int(input("End of range: "))
for num in range(start, end + 1):
if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num)
🔄 4. Check for Palindrome Number
python
CopyEdit
num = input("Enter a number: ")
if num == num[::-1]:
print("Palindrome")
else:
print("Not a palindrome")
🔢 5. Factorial of a Number
python
CopyEdit
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)
✅ 1. Check if a Number is Positive or Negative
python
CopyEdit
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
✅ 2. Find the Largest of Two Numbers
python
CopyEdit
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print("The larger number is", a)
elif b > a:
print("The larger number is", b)
else:
print("Both numbers are equal")
✅ 3. Check if a Number is Even or Odd
python
CopyEdit
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
✅ 4. Check if a Year is a Leap Year
python
CopyEdit
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
✅ 5. Grade Based on Marks
python
CopyEdit
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
elif marks >= 40:
print("Grade D")
else:
print("Fail")
✅ 6. Check Voting Eligibility
python
CopyEdit
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")