Monday, April 7, 2025

7.15 Uses of Library functions : String Functions , Numeric and mathematical | Grade 9 New Curriculum Computer Science 2082

 


7.15 Uses of Library functions : String Functions (center, upper, lower, Len), Numeric and mathematical (sum, pow, round, abs, sqrt, Int)

 

Function

A function is a block of reusable code that performs a specific task.

 

Library function

Library functions are part of Python's standard library or third-party libraries that come with a set of predefined functions.

 

 

String Function

·       A string function is a built-in function that is used to manipulate or process string data.

·       These functions perform various operations like changing the case of letters, finding the length of a string, concatenating strings, and more.

 

center(width, fillchar): Centers the string within the given width, padding it with a specified character (default is a space).

Example:

text = "Hello"

print(text.center(10, "*"))  # Output: '**Hello***'

 

upper( ): Converts all characters in the string to uppercase.

Example:

text = "hello"

print(text.upper( ))  # Output: 'HELLO'

 

lower( ): Converts all characters in the string to lowercase.

Example:

text = "hello"

print(text.lower( ))  # Output: 'hello'

 

len(): Returns the length of the string (i.e., the number of characters).

Example:

text = "Hello"

print(len(text))  # Output: 5

 

Numeric Functions

These functions are part of Python's built-in capabilities or from the math module to manipulate numeric values.

 

sum(iterable): Returns the sum of all elements in an iterable (e.g., a list).

Example:

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

print(sum(numbers))  # Output: 15

 

abs(x): Returns the absolute value of a number.

Example:

print(abs(-5))  # Output: 5

print(abs(3.5))  # Output: 3.5

 

round(x, n): Rounds a number x to n decimal places (default is 0).

Example:

print(round(3.14159, 2))  # Output: 3.14

print(round(3.5))  # Output: 4

 

pow(x, y): Returns x raised to the power of y (i.e., x^y).

Example:

print(pow(2, 3))  # Output: 8 (2^3)

 

int(x): Converts a number or string to an integer.

Example:

print(int(3.7))  # Output: 3

print(int("42"))  # Output: 42

 

min( ) and max( )Function: Returns the smallest (minimum) or largest(maximum) value from the given iterable or from multiple values passed as arguments.

Example:

numbers = [3, 1, 4, 1, 5, 9, 2, 6]

print(min(numbers))  # Output: 1

 

Mathematical Functions (from math module)

These functions are provided by the math module and perform more advanced mathematical operations.

 

math.sqrt(x): Returns the square root of x.

Example:

import math

print(math.sqrt(16))  # Output: 4.0

 

math.factorial(x) : Returns the factorial of a number x (i.e., x! = x * (x - 1) * (x - 2) * ... * 1).

Example:

import math

print(math.factorial(5))  # Output: 120 (5! = 5 * 4 * 3 * 2 * 1)

 

math.pi : Provides the mathematical constant π (approximately 3.14159).

Example:

import math

print(math.pi)  # Output: 3.141592653589793

 

math.sin(x) and math.cos(x): Return the sine and cosine of x (where x is in radians).

Example:

import math

print(math.sin(math.radians(30)))  # Output: 0.5

print(math.cos(math.radians(60)))  # Output: 0.5

 

math.log(x, base): Returns the logarithm of x to the given base. If the base is not specified, it returns the natural logarithm (base e).

Example:

import math

print(math.log(10))  # Output: 2.302585092994046 (natural log of 10)

print(math.log(100, 10))  # Output: 2.0 (logarithm base 10 of 100)

No comments:

Post a Comment