0% found this document useful (0 votes)
3 views4 pages

Mutable vs Immutable in Python

The document explains the concepts of mutable and immutable objects in Python, illustrating with examples. It also defines Python functions, their purpose, and provides a recursive function to calculate the factorial of numbers in a list. The document includes code snippets demonstrating these concepts.

Uploaded by

amiramir hacker
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Mutable vs Immutable in Python

The document explains the concepts of mutable and immutable objects in Python, illustrating with examples. It also defines Python functions, their purpose, and provides a recursive function to calculate the factorial of numbers in a list. The document includes code snippets demonstrating these concepts.

Uploaded by

amiramir hacker
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Mutable" and "Immutable

 Mutable: Something that can be changed after it is created.


 Immutable: Something that cannot be changed after it is created.
 Mutable Program
 a = [1,2,3,4,]
 [Link](5);
 print(a)
 Immutable Program
x=5
 print(“before”,x)
 x = x+1
 print(“after”,x)
✅ What are Python Functions?
 A function in Python is a block of code that runs only when you call it. It is used to
organize code, avoid repetition, and make the program clean and reusable.
 Why Use Functions?
 To reuse code
 To break problems into smaller steps
 To make code easier to understand
 Program
 # Function definition
 def greet():
 print("Hello! Welcome to Python.")
 # Calling the function
 greet()
Q3:Write a Python program that takes a list of numbers and
finds the factorial of each number using a recursive function.

 What is Recursion?
 A function that calls itself again and again is called recursion.
 ❗ What is Factorial?
 The factorial of a number means:
 n × (n-1) × (n-2) × ... × 1
 Example: 4! = 4 × 3 × 2 × 1 = 24
 def factorial(n):
 if n == 0 or n == 1:
 return 1
 else:
 return n * factorial(n - 1)
 numbers = [2, 3, 4]
 for num in numbers:
 print("Factorial of", num, "is", factorial(num))
 Result Display
 Factorial of 2 is 2
 Factorial of 3 is 6
 Factorial of 4 is 24

You might also like