Write a script in Python to create a tuple containing squares of first n natural numbers ?
Write a script in Python to create a tuple containing first n terms of Fibonacci series ?
Answers
Explanation:
Take the number of terms from the user and store it in a variable.
2. Pass the number as an argument to a recursive function named fibonacci.
3. Define the base condition as the number to be lesser than or equal to 1.
4. Otherwise call the function recursively with the argument as the number minus 1 added to the function called recursively with the argument as the number minus 2.
5. Use a for loop and print the returned value which is the fibonacci series.
Answer:
1.
n=int(input("Enter the number of natural numbers:"))
tup=()
for i in range(1,n+1):
tup+=(i^2,) # ^ means power
print("Tuple containing squares of first",n,"natural numbers is:",tup)
2.
n=int(input("Enter the number of terms"))
tup=()
a=0
b=1
for i in range(n):
a,b=a+b,a
tup+=(b,)
print("Tuple storing first",n," terms of fibonacci series is:",tup)
Explanation: