0% found this document useful (0 votes)
2 views2 pages

Python Functions

The document provides a collection of Python function examples for various operations including arithmetic, data type checking, boolean comparisons, list manipulations, mathematical functions, grade checking, and string utilities. Each function is defined with specific parameters and returns relevant results or data structures. These examples serve as practical references for performing common programming tasks in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Functions

The document provides a collection of Python function examples for various operations including arithmetic, data type checking, boolean comparisons, list manipulations, mathematical functions, grade checking, and string utilities. Each function is defined with specific parameters and returns relevant results or data structures. These examples serve as practical references for performing common programming tasks in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Code Examples as Functions

1. Arithmetic Operations

def arithmetic_operations(x, y):


return {
"add": x + y,
"subtract": x - y,
"multiply": x * y,
"divide": x / y if y != 0 else "undefined"
}

2. Data Type Checker

def check_data_types(val):
return type(val)

3. Boolean Comparison

def boolean_comparison(a, b):


return {
"a_less_b": a < b,
"b_less_a": b < a,
"a_equals_b": a == b,
"a_not_equal_b": a != b
}

4. List Operations

def manipulate_list():
lst = [25, 23, 98, 78]
[Link](45)
[Link](2, 100)
return lst

5. Math Functions

import math
def math_functions(val):
return {
"sqrt": [Link](val),
"floor": [Link](val),
Python Code Examples as Functions

"ceil": [Link](val),
"power": [Link](val, 2),
"pi": [Link]
}

6. Grade Checker

def grade_checker(percent):
if percent >= 80:
return "A Grade"
elif percent >= 60:
return "B Grade"
elif percent >= 50:
return "C Grade"
elif percent >= 33:
return "D Grade"
else:
return "Fail"

7. String Utilities

def string_utilities(s):
return {
"upper": [Link](),
"lower": [Link](),
"length": len(s),
"replace_best": [Link]("best", "good")
}

You might also like