0% found this document useful (0 votes)
16 views5 pages

PG Notes - Functions in Python

The document provides an overview of functions in Python, including how to define, call, and use parameters and return values. It lists common built-in functions with examples and outputs, such as print(), input(), len(), and others. Additionally, it includes sample code demonstrating the use of these functions in various scenarios.

Uploaded by

Sree Hari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

PG Notes - Functions in Python

The document provides an overview of functions in Python, including how to define, call, and use parameters and return values. It lists common built-in functions with examples and outputs, such as print(), input(), len(), and others. Additionally, it includes sample code demonstrating the use of these functions in various scenarios.

Uploaded by

Sree Hari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Functions in Python

Functions in Python are reusable blocks of code that perform a


specific task. They help organize code, improve readability, and
allow for reuse. Python provides built-in functions like print(),
len(), and sum(), and also allows you to define custom functions.
1. Defining a Function
A function is defined using the def keyword:
def greet():
print("Hello, World!")
2. Calling a Function
Invoke a function by using its name followed by parentheses:
greet() # Output: Hello, World!
3. Function with Parameters
Parameters allow passing values into a function:
def greet(name):
print(f" Hello, {name}!")
greet("Alice") # Output: Hello, Alice!
4. Function with Return Value
Functions can return values using the return keyword:
def add(a, b):
return a + b
result = add(3, 5)
print(result) # Output: 8
Common Python Built-in Functions

Function
[Link] Purpose / Explanation Example Code Output
Name
Displays output on the
1 print() print("Hello") Hello
screen

name = input("Enter
2 input() Takes input from the user User input
name: ")
Returns the length of a
3 len() len("Python") 6
string, list, or tuple

Returns the data type of a


4 type() type(10) <class 'int'>
value

5 int() Converts value to integer int("25") 25


6 float() Converts value to float float("3.5") 3.5
7 str() Converts value to string str(100) "100"
8 list() Converts data into a list list((1,2,3)) [1, 2, 3]
9 tuple() Converts data into a tuple tuple([1,2,3]) (1, 2, 3)
10 set() Converts data into a set set([1,1,2]) {1, 2}
Returns the sum of
11 sum() sum([1,2,3]) 6
elements

12 max() Returns the largest value max(10, 20, 5) 20


Returns the smallest
13 min() min(10, 20, 5) 5
value

Returns absolute
14 abs() abs(-7) 7
(positive) value

Raises a number to a
15 pow() pow(2,3) 8
power

16 round() Rounds a number round(3.6) 4


17 sorted() Sorts elements sorted([3,1,2]) [1, 2, 3]

Sample Coding’s in Python Functions:

1. Program using print()


print("Welcome to Python Programming")

2. Program using input() and print()


name = input("Enter your name: ")
print("Hello", name)

3. Program using len()


word = "Python"
print("Length of word:", len(word))

4. Program using type()


a = 10
b = 3.5
c = "Python"

print(type(a))
print(type(b))
print(type(c))

5. Program using int(), float(), and str()


num1 = int("20")
num2 = float("5.5")
num3 = str(100)

print(num1)
print(num2)
print(num3)

6. Program using list(), tuple(), and set()


data = [1, 2, 2, 3]
print(list(data))
print(tuple(data))
print(set(data))

7. Program using sum(), max(), and min()


marks = [75, 80, 90, 65]

print("Total:", sum(marks))
print("Highest:", max(marks))
print("Lowest:", min(marks))

8. Program using abs()


num = -25
print("Absolute value:", abs(num))

9. Program using pow()


print("Power value:", pow(2, 4))

10. Program using round()


num = 3.76
print("Rounded value:", round(num))

11. Program using sorted()


numbers = [4, 1, 3, 2]
print("Sorted list:", sorted(numbers))

You might also like