0% found this document useful (0 votes)
4 views8 pages

Python Inbuilt Functions

The document provides an overview of various built-in functions in Python, including their definitions and examples of usage. Functions covered include abs(), chr(), ord(), int(), float(), input(), print(), len(), max(), min(), pow(), range(), round(), and several string manipulation methods. Each function is explained with sample code and expected output to illustrate its functionality.

Uploaded by

sapnayadav4832
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)
4 views8 pages

Python Inbuilt Functions

The document provides an overview of various built-in functions in Python, including their definitions and examples of usage. Functions covered include abs(), chr(), ord(), int(), float(), input(), print(), len(), max(), min(), pow(), range(), round(), and several string manipulation methods. Each function is explained with sample code and expected output to illustrate its functionality.

Uploaded by

sapnayadav4832
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
You are on page 1/ 8

J D EDUCATION CENTRE

BASIC CONCEPTS OF PYTHON

Python inbuilt functions : Built-in functions in Python are a collection of pre-defined


functions that are always available for use. Some common use functions are

1. abs() – this function return absolute value of a number by removing positive and
negative sign.
for example:
a=10
b= -20
print(“a=”,a)
print(“b=”,b)
output:
a=10
b=20

2. chr() : this function returns an ASCII character of the corresponding unicode pass to
the function. ASCII codes of A toZ are 65-90 and ASCII codes of a to z are 97-122
and ASCII codes of digits 0 to 9 are 48-57.

for example:
print(chr(65)) ---- A
print(chr(70)) ---- F
print(chr(105)) ---- i

3. ord() : this function returns Unicode of ASCII character pass to the function.

for example:
print(ord(“B”)) ---- 66
print(chr(“Z”)) ---- 90
print(chr(“5”)) ---- 53

4. int() – this function is used to convert any string , float value as integer.
for example:
a=25.56
print(int(a)) ------- 25
b=25.36
print(int(b)) ------- 25
c=25.50
print(int(a)) ------- 25
a=int(input(“enter a value:”))
print(a) ------- value input by user converted into integer

5. float() – this function is used to convert any string , integer value as float.
for example:
a=25
print(float(a)) ------- 25.0
b=float(input(“enter a value:”))
print(b) ------- value input by user converted into float

PAGE No.:1
J D EDUCATION CENTRE
BASIC CONCEPTS OF PYTHON

6. input() – this function is use to accept any value from user on console
for example:
a=input(“enter your city”)
print(a)

7. print() – this function is use to print any value on console. print function have sep and
end attributes. Print function have been discussed above.

8. len() – this function is used to find the length of ay string.


for example:
a=”ashish”
print(len(a)) --------- 6

9. max() – this function returns largest number from the list of values.

print(max(10,15,9,55,20) -------- 55

10. min() – this function returns smallest number from the list of values.

print(min(10,15,9,55,20) -------- 9

11. pow() – this function returns the value of x to the power value y

for example:
print(pow(10,3)) ------------- 1000

12. range(startvalue , stop value , increment / decrement) – this function is mostly used
to executes loops in python , loop starts from the given start value and stop as it reach
to the endvalue. By default increment value is 1.

for example:
for i in range(1,11,1):
print(“Kanpur”)

output : the above loop will print string “Kanpur” 10 times

13. round() – this function rounds up a floating value to an integer value.


for example :
print(round(25.63)) ----- 26
print(round(25.36))------25
print(round(25.50))------25

PAGE No.:2
J D EDUCATION CENTRE
BASIC CONCEPTS OF PYTHON

14. slice() - slice function is used to extract a selected part of string , list , tupple. You can
specify where to start the slicing, and where to end. You can also specify the step, which
allows you to slice.

for example:
“extract substring from string”
city="kanpur"
ex=city[slice(1,5,1)]
print("substring extracted from string=",ex)

output:
substring extracted from string= anpu

“extract substring from list”

city=["kanpur","lucknow","delhi","unnao","varanasi","merrut"]
ex=city[slice(1,5,1)]
print("substring extracted from list=",ex)

output:
substring extracted from list= ['lucknow', 'delhi', 'unnao', 'varanasi']

“extract substring from tupple”

city=("kanpur","lucknow","delhi","unnao","varanasi","merrut")
ex=city[slice(1,5,2)]
print("substring extracted from tuple=",ex)

output:
substring extracted from tuple= ('lucknow', 'unnao')

15. sorted() : this function returns the list of unsorted items in ascending or descending
order.
for example:
name=[‘amit’,’manish’,’dinesh’,’akhilesh’,’suresh’,’rajesh’]
sname=sorted(name)
print(sname)
output: in ascending order
['akhilesh', 'amit', 'dinesh', 'manish', 'suresh']
name=('amit','manish','dinesh','akhilesh','suresh')
sname=sorted(name,reverse=True)
print(sname)
output : in descending order
['suresh', 'manish', 'dinesh', 'amit', 'akhilesh']

PAGE No.:3
J D EDUCATION CENTRE
BASIC CONCEPTS OF PYTHON
16. str() – this function converts any value into a string value.
for example:
a=str(25.37)
print(a) -------- “25.37”
type(a)--------<class string>

17. type() – this function return the data type of a variable.


for example:
a = ('apple', 'banana', 'cherry')
b = "Hello World"
c = 33
pie=3.14
flag=True
print(type(a))
print(type(b))
print(type(c))
print(type(pie))
print(type(flag))
output:
<class 'tuple'>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

18. capitalize() – this function converts the first letter of string into uppercase the rest letters
will be in lower case.
for example:
st=’hello welcome to my world’
a=st.capitalize()
print(a)
output: “Hello welcome to my world”

st=’HELLO WELCOME TO MY WORLD’


a=st.capitalize()
print(a)
output: “Hello welcome to my world”

19. casefold() : this function converts the whole string into lower case

st=’HELLO WELCOME TO MY WORLD’


a=st.casefold()
print(a)
output: “hello welcome to my world”

st=’HELLO welcome TO my world’


a=st.casefold()
print(a)
output: “hello welcome to my world”

PAGE No.:4
J D EDUCATION CENTRE
BASIC CONCEPTS OF PYTHON

20. upper() – this function converts the whole string into upper case.
st=’HELLO welcome TO my world’
a=st.upper()
print(a)
output: “HELLO WELCOME TO MY WORLD”

21. title()- converts the first letter of each word in upper case.
st=’HELLO welcome TO my world’
a=st.title()
print(a)
output: “Hello Welcome To My World”

22. swapcase() – converts lower case to uppercase and vice versa.


st=’HELLO welcome TO my WORLD’
a=st.swapcase()
print(a)
output: “hello Welcome to My world”

23. strip() – this function removes extra space from string from the beginning and end of
string except others.
txt = " banana "
x = txt.strip()
print("of all fruits", x, "is my favorite")

24. startswith()- The startswith() method returns True if the string starts with the specified
value, otherwise False. It is a case sensitive
syntax of function:

string.startswith(“string”,start pos , end pos)


for example:
txt = "Hello, welcome to my world."
x = txt.startswith("Hello")
print(x)
output : True

txt = "Hello, welcome to my world."


x = txt.startswith("wel")
print(x)
output : False

txt = "Hello, welcome to my world."


x = txt.startswith("HELLO")
print(x)
output : False

PAGE No.:5
J D EDUCATION CENTRE
BASIC CONCEPTS OF PYTHON

25. replace()-The replace() method replaces a specified phrase with another specified
phrase.
for example:
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)
output: I like apples

26. count()-The count() method returns the number of times a specified value appears in the
string.

txt = "I love apples, apple are my favorite fruit"


x = txt.count("apple")
print(x)
output = 2
27. boo() – this function returns True when the variable or object is not empty ,NONE, 0 ,
blank else return False.
print(bool(10)) # Output: True
print(bool(0)) # Output: False
print(bool("hello")) # Output: True
print(bool("")) # Output: False
print(bool([1, 2])) # Output: True
print(bool([])) # Output: False
print(bool(None)) # Output: False

28. isalnum() - The isalnum() method returns True if all the characters are alphanumeric,
meaning alphabet letter (a-z) and numbers (0-9). Special characters are not allowed
txt = "Company 12"
x = txt.isalnum()
print(x)

output: False ( because space is a special character)

29. isdigit() - The isdigit() method returns True if all the characters in the string are (0-9)
digits.
txt = "123456"
x = txt.isdigit()
print(x)
output: True

30. isalpha() - The isalpha() method returns True if all the characters in the string are
alphabets.
txt = "abcdefg"
x = txt.isalpha()
print(x)
output: True

PAGE No.:6
J D EDUCATION CENTRE
BASIC CONCEPTS OF PYTHON
31. isdigit() - The isdigit() method returns True if all the characters in the string are integers.
txt = "123"
x = txt.isdigit()
print(x)

output: True

txt = "1234.35"
x = txt.isdigit()
print(x)
output: False

32. islower() – the islower() method returns True if all characters of string in lowercase
txt=”KANPUR”
x=txt.islower()
print(x)
output : False

txt=”kanpur”
x=txt.islower()
print(x)
output : True

33. isupper() – the isupper() method returns True if all characters of string in upper case
txt=”KANPUR”
x=txt.isupper()
print(x)
output : True

txt=”kanpur”
x=txt.isupper()
print(x)
output : False

34. istitle() – the istitle() method returns True if first character of each word in string is in
upper case

txt=”My City Is Kanpur”


x=txt.istitle()
print(x)
output : True

txt=”my city is KANPUR”


x=txt.istitle()
print(x)
output : False

PAGE No.:7
J D EDUCATION CENTRE
BASIC CONCEPTS OF PYTHON
35. isspace() – return true if the string is blank

x="KANPUR"
print(x.isspace())
output : False

x=" "
print(x.isspace())
output : True

*****************

PAGE No.:8

You might also like