1.Operators in Python
Operators are special symbols or keywords used to perform operations on variables and values.
Python has different types of operators.
A.Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
B.Comparison Operators
Comparison operators are used to compare two values. They return either True or False.
C.Logical Operators
Logical operators are used to combine multiple conditions.
Python has three logical operators:
1.and – Returns True if both conditions are true.
2.or – Returns True if at least one condition is true.
3.not – Reverses the result.
Example:
age = 20
print(age > 18 and age < 30)
print(age < 18 or age == 20)
print(not(age > 18))
Output:
True
True
False
D.Assignment Operators
Assignment operators are used to assign values to variables.
E.Membership Operators
Membership operators are used to check whether a value exists in a sequence.
1.in – Checks whether a value exists.
2.not in – Checks whether a value does not exist.
Example:
fruits = ["apple", "banana", "mango"]
print("apple" in fruits)
print("orange" not in fruits)
Output:
True
True
F.Identity Operators
Identity operators are used to check whether two variables refer to the same object.
1.is – Returns True if both refer to the same object.
2.is not – Returns True if they refer to different objects.
Example:
a = [1, 2, 3]
b = a
print(a is b)
Output:
True
G.Bitwise Operators
2.Functions in Python
A function is a block of reusable code that performs a specific task.
Functions help us avoid writing the same code repeatedly and make programs easier to understand.
Python provides two main ways to work with functions:
- Built-in functions
- User-defined functions
A.Built-in Functions
Built-in functions are already available in Python. We can use them directly without defining them.
Some common built-in functions are:
B.User-Defined Functions
User-defined functions are functions created by programmers to perform specific tasks.
We use the def keyword to create a function.
Syntax:
def function_name():
# Statements
Example:
def greet():
print("Hello, Welcome to Python!")
greet()
Output:
Hello, Welcome to Python!
Here:
- def is used to define a function.
- greet is the function name.
- greet() is used to call the function.
3.Function Arguments in Python
Function arguments are values passed to a function when calling it. They allow us to send information to a function.
Let's understand the different types of function arguments.
A.Positional Arguments
In positional arguments, values are passed according to the order of parameters.
Example:
def student(name, age):
print("Name:", name)
print("Age:", age)
student("Gowtham", 20)
Output:
Name: Gowtham
Age: 20
Here, "Gowtham" is passed to name, and 20 is passed to age.
B.Keyword Arguments
In keyword arguments, we pass values using parameter names. The order does not matter.
Example:
def student(name, age):
print("Name:", name)
print("Age:", age)
student(age=20, name="Gowtham")
Output:
Name: Gowtham
Age: 20
C.Default Arguments
Default arguments have predefined values. If we don't pass a value, Python uses the default value.
Example:
def greet(name="Friend"):
print("Hello", name)
greet()
greet("Gowtham")
Output:
Hello Friend
Hello Gowtham
D.Variable-Length Arguments
Sometimes, we don't know how many arguments will be passed to a function. Python supports variable-length arguments.
1.*args
*args allows us to pass multiple positional arguments to a function.
def add(*numbers):
print(sum(numbers))
add(10, 20)
add(10, 20, 30, 40)
Output:
30
100
2.**kwargs
**kwargs allows us to pass multiple keyword arguments to a function. These arguments are stored as a dictionary.
def student(**details):
print(details)
student(name="Gowtham", age=20, city="Chennai")
Output:
{'name': 'Gowtham', 'age': 20, 'city': 'Chennai'}





Top comments (0)