0% found this document useful (0 votes)
4 views

Python Functions

Python

Uploaded by

gayatriksh25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Functions

Python

Uploaded by

gayatriksh25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

type()
It return the type(Datatype) of parameter/value.

Example-

int = 10

str = "Hello, Python!"

dict = {'name': 'Alice', 'age': 25}

print("Type of integer", type(int))

print("Type of string:", type(str))

print("Type of dict:", type(dict))

Output-

Type of integer <class 'int'>

Type of string: <class 'str'>

Type of dict: <class 'dict'>

2.abs()
The abs() function in Python is used to return the absolute value of a number.

Example-

a = -5

b = -3.7

print(abs(a))

print(abs(b))

Output-

3.7

3.all()
This function takes a sequence as an parameter and returns True if all values in para,eter
sequence(List,Tuple,String,etc) have a Boolean value True; otherwise False.

Example-

list1 = [1, 2, 3, 4, 5]

list2 = ["a", "", ""]

list3 = [True,True]

list4 = [True,False]
print(all(list1))

print(all(list2))

print(all(list3))

print(all(list4))

Output-

True

False

True

False

4.any()
This function returns True if any one values in parameter sequence(List, Tuple, String, etc) have a
Boolean value True; otherwise returns false.

Example-

list1 = ["","",""]

list2 = ["a", "", ""]

list3 = [True,False]

print(any(list1))

print(any(list2))

print(any(list3))

Output-

False

True

True

5.callable()
This function checks if an object appears callable (i.e., if it can be called as a function or method). It
returns True if the object can be called, and False otherwise.

Example-

print(callable(type))

print(callable([10,20,20]))

Output-
True

False

6.isinstance()
This function checks if an object is an instance of a specified class or a subclass. It returns True if the
object is an instance of the class, and False otherwise.

Example-

x = 10

print(isinstance(x, int))

print(isinstance(x, str))

Output-

True

False

7.id()
The id() function in Python returns the unique identifier (memory address) for an object.

Example-

x = 10

y = 10

print("id of x:", id(x))

print("id of y:", id(y))

Output-

id of x: 1719180526096

id of y: 1719180526096

8.hasattr()
The hasattr() function in Python checks if an object has a specified attribute. It returns True if the
object has the attribute, and False otherwise.

Example-

class MyClass:

name = "Python"

obj = MyClass()

print(hasattr(obj, 'name'))
print(hasattr(obj, 'age'))

Output-

True

False

9.bin()
This function returns binary conversion of specified integer parameter.

Example-

x = 10

print(bin(x))

Output-

0b1010

10.hex()
This function returns hexa-decimal conversion of specified integer parameter.

Example-

x = 10

print(hex(x))

Output-

0xa

11.oct()
This function returns octal conversion of specified integer parameter.

Example-

x = 10

print(oct(x))

Output-

0o12

12.bool()
The bool() function in Python converts a value to a boolean (True or False) based on its truthiness. It
returns False for falsy values like None, 0, "", and False, and True for all other values.

Example-

x=0

print(bool(x))
y = "Hello"

print(bool(y))

Output-

False

True

13.float()
The float() function in Python converts a number or a string into a floating-point number.

Example-

x = "3.14"

print(float(x))

y=5

print(float(y))

Output-

3.14

5.0

14.int()
The int() function in Python converts a number or a string into an integer.

Example-

x = "42"

print(int(x))

y = 3.99

print(int(y))

Output-

42

15.list()
The list() function converts specified data-structure into list and returns.

Example-
x = "hello"

print(list(x))

y = (1, 2, 3)

print(list(y))

Output-

['h', 'e', 'l', 'l', 'o']

[1, 2, 3]

16.tuple()
The tuple() function converts specified data-structure into tuple and returns.

Example-

x = [1, 2, 3]

print(tuple(x))

y = "hello"

print(tuple(y))

Output-

(1, 2, 3)

('h', 'e', 'l', 'l', 'o')

17.str()
This function converts and returns parameter value into string type.

Example-

a = 15+15

b = str(15) + str(15)

print(a)

print(b)

Output-

30

1515

18.ord()
This function returns ASCII value of specified character as parameter.

Example-
char = 'A'

print(ord(char))

sy = '+'

print(ord(sy))

num = '7'

print(ord(num))

Output-

65

43

55

19.chr()
This function returns a related character based on provided ASCII value as parameter.

Example-

num = 65

print(chr(num))

num = 122

print(chr(num))

Output-

20.input()
Accepts input from the user.

Example-

name = input("Enter your name: ")

print("Hello, " + name)

Output-

Enter your name: adt

Hello, adt
21.print()
This function prints to the standard output device.

Example-

print("Hello")

print("Welcome to the Jupiter")

Output-

Hello

Welcome to the Jupiter

22.open()
Opens a file and returns an object of type class_io.TextIOWrapper.

Example-

f = open(“D:\\example1.txt”)

print(type(f)

Output-

Type of f is: <class’_io.TextWrapper’>

23.complex()
Returns a complex number of specified parameter.

Example-

a = complex(3)

b = complex(2,7)

print(a)

print(b)

Output-

(3+0j)

(2+7j)

24.eval()
Evaluates and executes an expression provided as parameter string.

Example-

x=5

result = eval("x * 2")


print(result)

Output-

10

25. @staticmethod()
This function can externally convert a non-static member-function into a static member function.

Example-

class MyClass:

@static method

def greet(name):

return f"Hello, {name}!"

print(MyClass.greet("Alice"))

Output-

Hello, Alice!

26.issubclass()
Example-

class Animal:

pass

class Dog(Animal):

pass

print(issubclass(Dog, Animal))

print(issubclass(Dog, object))

Output-

True

True

27.sum()
This function takes a sequence as a parameter and returns sum of all elements of specified sequence.

Example-

numbers = [1, 2, 3, 4]

print(sum(numbers))
print(sum(numbers, 5))

Output-

10

15

28.max()
This function returns the largest item from specified sequence.

Example-

numbers = [10, 92, 30, 43]

print("Largest element is: ", max(numbers))

Output-

Largest element is: 92

29.min()
This function returns the smallest item from specified sequence.

Example-

numbers = [10, 92, 30, 43]

print("Smallest element is: ", min(numbers))

Output- Smallest element is: 10

30.pow()
This function obtains and returns the value of base raised to power.

Example-

print("2 raised to 3 is: ",pow(2, 3))

print("7 raised to -1 is:",pow(7,-1))

Output-

2 raised to 3 is: 8

7 raised to -1 is: 0.14285714285714285

31.range()
This function returns a sequence of numbers, based on specific start_value to end_value – 1 by
processing on specified step.

Example-

print("range(1,10) will result as:", list(range(1,10)))

print("range(1,10,+2) will result as:", list(range(1,10,+2)))


Output-

range(1,10) will result as: [1, 2, 3, 4, 5, 6, 7, 8, 9]

range(1,10,+2) will result as: [1, 3, 5, 7, 9]

32.help()

Starts built-in help system.

Example-

help()

Output-

Welcome to Python 3.10's help utility!

If this is your first time using Python, you should definitely check out

the tutorial on the internet at https://docs.python.org/3.10/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing

Python programs and using Python modules. To quit this help utility and

return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type

"modules", "keywords", "symbols", or "topics". Each module also comes

with a one-line summary of what it does; to list the modules whose name

or summary contain a given string such as "spam", type "modules spam".

You might also like