Part 8- Library Function

 Library Function are also known as built in functions, they are functions already present in the Python library.

Advantages of Library Function

1.      Saves time

2.      Increase Productivity

3.      Improves code readability

4.      Easy to debug

 

1. pow()
pow() function is used to calculate the power of a given number.

Syntax:

pow(base, exponent)

Example:

result = pow(2, 3)

print(result)

 

2. sqrt()

sqrt() function is used to return the square root of a given number.
(It is available in the math module.)

Syntax:

import math

math.sqrt(number)

Example:

import math

result = math.sqrt(25)

print(result)

 

3. int()

int() function is used to convert a number or string into an integer value.

Syntax:

int(value)

Example:

num = int(5.8)

print(num)

 

4. abs()

abs() function is used to return the absolute (positive) value of a number.

Syntax:

abs(number)

Example:

result = abs(-10)

print(result)

 

5. round()

round() function is used to round a number to a specified number of decimal places.

Syntax:

round(number, digits)

number → the number to round

digits → number of decimal places (optional)

Example 1 (With decimal places):

result = round(5.6789, 2)

print(result)

 

Example 2 (Without decimal places):

result = round(5.4)

print(result)

 

Python String function

1. len()

len() function is used to count the number of characters in a string.

Syntax:

len(string)

Example:

text = "Python"

result = len(text)

print(result)

 

2. upper()

upper() function is used to convert all characters of a string into uppercase.

Syntax:

string.upper()

Example:

text = "hello"

result = text.upper()

print(result)

 

3. lower()

lower() function is used to convert all characters of a string into lowercase.

Syntax:

string.lower()

Example:

text = "PYTHON"

result = text.lower()

print(result)

 

4. center()

center() function is used to align a string at the center within a specified width.

Syntax:

string.center(width, fillchar)

width → Total width of the string

fillchar → Optional character to fill empty space

Example:

text = "Python"

result = text.center(12, "*")

print(result)

 

Work out example

1.      To calculate square root of a given number

import math

num = float(input("Enter a number: "))

result = math.sqrt(num)

print("Square root =", result)

 

2.      To count number of characters present in the given word

word = input("Enter a word: ")

print("Number of characters =", len(word))

 

3.      To convert given word in uppercase

word = input("Enter a word: ")

print("Uppercase word =", word.upper())

 

4.      To display longest word among two different words

word1 = input("Enter first word: ")

word2 = input("Enter second word: ")

if len(word1) > len(word2):

    print("Longest word =", word1)

else:

    print("Longest word =", word2)

 

5.      To display square and cube of the given number

num = int(input("Enter a number: "))

print("Square =", num ** 2)

print("Cube =", num ** 3)

 

6.      To count number of vowels present in a given word

word = input("Enter a word: ").lower()

count = 0

for ch in word:

    if ch in "aeiou":

        count += 1

print("Number of vowels =", count)

 

7.      To display the given word in reverse order

word = input("Enter a word: ")

print("Reversed word =", word[::-1])

 

8.      To check whether the given word is palindrome or not

word = input("Enter a word: ")

if word == word[::-1]:

    print("Palindrome")

else:

    print("Not Palindrome")

 

9.      To display alternate character of the given word

word = input("Enter a word: ")

print("Alternate characters =", word[::2])

 

10.  To count space in the given sentence

sentence = input("Enter a sentence: ")

print("Number of spaces =", sentence.count(" "))

 

11.  To count number of words in the given sentence

sentence = input("Enter a sentence: ")

words = sentence.split()

print("Number of words =", len(words))

 

Book exercise Page number 300

 

1. To calculate the square root of a given number.

import math

num = float(input("Enter a number: "))

result = math.sqrt(num)

print("Square root =", result)

 

2. To display the longest word among three different words.

word1 = input("Enter first word: ")

word2 = input("Enter second word: ")

word3 = input("Enter third word: ")

longest = word1

if len(word2) > len(longest):

    longest = word2

if len(word3) > len(longest):

    longest = word3

print("Longest word is:", longest)

 

3. To convert the given word into lower case.

word = input("Enter a word: ")

print("Lower case:", word.lower())

 

4. To count the number of characters present in the given word.

word = input("Enter a word: ")

print("Number of characters:", len(word))

 

5. To display the square root of all the even numbers from 2 to 20.

import math

for num in range(2, 21, 2):

    print("Square root of", num, "=", math.sqrt(num))

 

6. To display the cube of numbers from 1 to 10.

for num in range(1, 11):

    print("Cube of", num, "=", num**3)

 

7. To display the shortest word among two different words.

word1 = input("Enter first word: ")

word2 = input("Enter second word: ")

if len(word1) < len(word2):

    print("Shortest word is:", word1)

else:

    print("Shortest word is:", word2)

 

8. To count the number of consonants present in the given word.

word = input("Enter a word: ")

count = 0

for ch in word:

    if ch.isalpha() and ch.lower() not in "aeiou":

        count += 1

print("Number of consonants:", count)

 

9. To count the number of characters N/n present in the given word.

word = input("Enter a word: ")

count = 0

for ch in word:

    if ch == 'N' or ch == 'n':

        count += 1

print("Number of N/n:", count)

 

10. To display the string in the following pattern

 

PROGRAM

PROGRA

PROGR

PROG

PRO

PRO

PR

P

word = "PROGRAM"

for i in range(len(word), 0, -1):

    print(word[:i])

 

N

NE

NEP

NEPA

NEPAL

word = "NEPAL"

for i in range(1, len(word), + 1):

    print(word[:i])

Popular posts from this blog

Computer

Sequential Programs