What is a Variable?
A variable is a name used to store data in Python.
Example:
name = "Raj"
age = 25
print(name)
print(age)
Output:
Raj
25
Explanation:
Here, "name" and "age" are variables. They store different types of data.
What is a Function?
A function is a block of reusable code that performs a specific task.
Example:
`def greet():
print("Hello World")
greet()
Output:
Hello World`
Explanation:
- "def" is a keyword used to define a function.
- "greet" is the function name.
- "greet()" is used to call the function.
Functions help us reuse code and avoid writing the same code again and again.
**
What is a User-Defined Function?**
A user-defined function is a function created by the programmer to perform a specific task.
Example:
`def addition():
a = 10
b = 20
print(a + b)
addition()
Output:
30
`
Explanation:
we create our own function using the "def" keyword.
What is a Parameter?
A parameter is a variable that is written inside the function definition to receive data.
Example:
`def addition(a, b):
print(a + b)
addition(10, 20)
Output:
30`
Explanation:
Here, "a" and "b" are parameters. They receive values when the function is called.
What is an Argument?
An argument is a value passed to a function when calling it.
Example:
`def greet(name):
print("Hello", name)
greet("Raj")
Output:
Hello Raj`
Explanation:
- "name" is a parameter.
- ""Raj"" is an argument.
Important Difference:
Parameter = Variable in function definition.
Argument = Actual value passed during function call.
What is a Positional Argument?
A positional argument is an argument passed to a function based on its position or order.
Example:
`def student(name, age):
print(name)
print(age)
student("Raj", 25)
Output:
Raj
25`
Explanation:
- First argument ""Raj"" goes to "name".
- Second argument "25" goes to "age".
Positional arguments are used when we pass values according to the parameter order.
What is a Keyword Argument?
A keyword argument is an argument passed using the parameter name.
Example:
`def student(name, age):
print(name)
print(age)
student(age=25, name="Raj")
Output:
Raj
25`
Explanation:
we pass values using parameter names.
The order does not matter because we specify the parameter names.
Keyword arguments make code more readable and flexible.
What is a Default Argument?
A default argument is a parameter that has a default value.
If we do not pass a value, Python uses the default value.
Example:
`def greet(name="Student"):
print("Hello", name)
greet()
greet("Raj")
Output:
Hello Student
Hello Raj`
Explanation:
If we do not provide a name, Python uses ""Student"" automatically.
What is a Function?
A function is a reusable block of code that performs a specific task.
Example:
`
def square(number):
return number * number
print(square(5))
Output:
25`
Explanation:
The function calculates the square of a number and returns the result.
What is a Method?
A method is a function that belongs to an object or a class.
Example:
`name = "python"
print(name.upper())
Output:
PYTHON`
Explanation:
the "upper()" is a string method. It converts lowercase letters into uppercase letters.
What is the Difference Between Function and Method?
Function:
A function can be called independently.
Example:
print("Hello")
Method:
A method is called using an object or class.
Example:
name = "python"
print(name.upper())
Difference:
- Function is called directly by its name.
- Method is called through an object or class.
- Every method is a function-like callable, but not every function is a method.
What is Mutable?
Mutable means an object can be changed after it is created.
Example:
`
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
Output:
[10, 20, 30, 40]`
Explanation:
A list is mutable because we can add, remove, or change its values after creating it.
Other Mutable Data Types:
- List
- Dictionary
- Set
What is Immutable?
Immutable means an object cannot be changed after it is created.
Example:
`name = "Python"
name = name + " Programming"
print(name)
Output:
Python Programming`
Explanation:
Strings are immutable. The original string is not modified; a new string is created and assigned to the variable.
Other Immutable Data Types:
- String
- Integer
- Float
- Tuple
What is a Pure Function?
A pure function always returns the same output for the same input and does not modify external data or produce side effects.
Example:
`def addition(a, b):
return a + b
print(addition(10, 20))
print(addition(10, 20))
Output:
30
30`
Explanation:
The same inputs always produce the same output. The function does not change external data.
Top comments (0)