Question 43: Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. Download hole Program / Project code, by clicking following link: What is the output of the following program? num = 1
def func():
num = 3
print(num)
func()
print(num)
In this Python code, you have a global variable named num set to 1, and you define a function called func. Inside the function, there is a local variable also named num set to 3. You then print the local variable within the function and also print the global variable after calling the function. Here's what happens:
num = 1 # This is a global variable
def func():
num = 3 # This is a local variable, separate from the global 'num'
print(num) # This prints the local 'num'
func() # Calling the function
print(num) # This prints the global 'num'
Output:
3 1
Here's the breakdown: - The num variable inside the func function is a local variable and is distinct from the global num variable. The local num is set to 3 within the function and is printed within the function, resulting in "3" being printed.
- The global num variable remains unaffected by the local variable defined inside the function. After the function call, it is printed, resulting in "1" being printed.
This demonstrates the concept of variable scope in Python. Local variables defined within a function do not affect the values of global variables with the same name. Programming Code: Following code write in: BP_P43.py # Python Function
# Calculate Factorial of a number
def cal_fact(num):
if num == 1:
return 1
elif num == 0:
return 0
else:
fact = 1
while num > 0:
fact *= num
num -= 1
return fact
num = int(input("Enter a non-negative Integer number: "))
print("Given number is: ", num)
print(f" {num} Factorial is: ", cal_fact(num))
# Thanks for Reading.
Output:
def func():
num = 3
print(num)
func()
print(num)
In this Python code, you have a global variable named num set to 1, and you define a function called func. Inside the function, there is a local variable also named num set to 3. You then print the local variable within the function and also print the global variable after calling the function.
num = 1 # This is a global variable
def func():
num = 3 # This is a local variable, separate from the global 'num'
print(num) # This prints the local 'num'
func() # Calling the function
print(num) # This prints the global 'num'
Output:
3
- The num variable inside the func function is a local variable and is distinct from the global num variable. The local num is set to 3 within the function and is printed within the function, resulting in "3" being printed.
- The global num variable remains unaffected by the local variable defined inside the function. After the function call, it is printed, resulting in "1" being printed.
# Python Function # Calculate Factorial of a number def cal_fact(num): if num == 1: return 1 elif num == 0: return 0 else: fact = 1 while num > 0: fact *= num num -= 1 return fact num = int(input("Enter a non-negative Integer number: ")) print("Given number is: ", num) print(f" {num} Factorial is: ", cal_fact(num)) # Thanks for Reading.
Output:
0 Comments