python functions and conditional statment
python functions and conditional statment
Jagtap Tejas
ROLL NO.: - 106
110
DIV :- A
ACTIVITY:- Python Functions and
Conditional
statment
What are Python Functions?
• Functions are reusable blocks of code
that perform specific tasks.
• Example:
• def greet(name):
• return f'Hello, {name}!'
Types of Functions
• 1. Built-in Functions (e.g., print(), len())
• 2. User-defined Functions
• Types:
• 1. Positional Arguments
• 2. Keyword Arguments
• 3. Default Arguments
• 4. Variable-length Arguments (*args,
Return Statement
• The return statement is used to send a result
back to the caller.
• Example:
• def add(a, b):
• return a + b
Lambda Functions
• Lambda functions are anonymous, one-line
functions.
• Syntax:
• lambda arguments: expression
• Example:
• square = lambda x: x ** 2
What are Conditional Statements?
• Conditional statements allow decision-making
in a program.
• Example:
• if x > 0:
• print('Positive number')
if-else Statement
• if condition:
• statement(s)
• else:
• statement(s)
• Example:
• if x % 2 == 0:
• print('Even')
• else:
if-elif-else Statement
• if condition1:
• statement(s)
• elif condition2:
• statement(s)
• else:
• statement(s)
• Example:
• if marks >= 90:
Nested if Statements
• An if statement inside another if statement.
• Example:
• if age > 18:
• if has_license:
• print('You can drive')
• else:
• print('Get a license first')
Logical Operators in Conditions
• Logical operators combine multiple
conditions:
• - and: Both conditions must be True.
• - or: At least one condition must be True.
• - not: Negates a condition.
• Example:
• if age > 18 and has_license:
• print('You can drive')
Ternary Conditional Statement
• A compact way to write if-else statements.
• Syntax:
• value_if_true if condition else value_if_false
• Example:
• result = 'Pass' if marks >= 50 else 'Fail'
Summary
• - Functions simplify code reuse and
modularity.
• - Conditional statements control the flow of
execution.
• - Practice writing functions and using if-else
logic in real problems.