In Python, “operations on numbers and strings” refer to the various ways in which you can manipulate and perform actions on numerical and text-based data. Python provides numerous built-in functions which can be used to perform many complex operations. In this section, we will be exploring some methods of manipulating our numbers and strings.

**Operations on Numbers: **Python supports all the basic arithmetic operations on numbers, such as addition, subtraction, multiplication, division, and exponentiation. Additionally, Python also supports other numerical operations, such as modulo (remainder), floor division (integer division), and comparison operators (greater than, less than, equal to, etc.). For example:

```
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x % y) # Output: 1
print(x // y) # Output: 3 (floor division)
```

**Operations on Strings:** Python also supports several operations on strings, including concatenation (joining strings), slicing (retrieving specific parts of a string), and various methods for manipulating and formatting strings. For example:

```
string1 = "Hello"
string2 = "World"
print(string1 + string2) # Output: "HelloWorld"
print(string1[1:4]) # Output: "ell" (slice of string1)
print(string1.lower()) # Output: "hello"
print(string2.upper()) # Output: "WORLD"
```

In addition to these basic operations, there are many other built-in functions and libraries in Python that can be used to manipulate and process numbers and strings.

## Number functions:

In Python, number functions are built-in functions that can be used to perform various operations on numeric values. Here are some examples of commonly used number functions with code examples:

: returns the absolute value of a number**abs()**

```
print(abs(-10)) # Output: 10
print(abs(3.14)) # Output: 3.14
```

: rounds a number to a specified number of decimal places**round()**

```
print(round(3.141592, 2)) # Output: 3.14 (round to 2 decimal places)
print(round(2.5)) # Output: 2 (round to the nearest integer)
```

: returns the maximum value from a sequence of numbers**max()**

`print(max(1, 5, 3, 9, 2)) # Output: 9`

: returns the minimum value from a sequence of numbers**min()**

`print(min(1, 5, 3, 9, 2)) # Output: 1`

: returns the sum of a sequence of numbers**sum()**

`print(sum([1, 2, 3, 4, 5])) # Output: 15`

: returns the result of raising a number to a specified power**pow()**

```
print(pow(2, 3)) # Output: 8 (2 raised to the power of 3)
print(pow(4, 0.5)) # Output: 2.0 (square root of 4)
```

: converts a number to an integer**int()**

```
print(int(3.14)) # Output: 3
print(int("42")) # Output: 42 (convert string to integer)
```

: converts a number to a float**float()**

```
print(float(3)) # Output: 3.0
print(float("3.5")) # Output: 3.5 (convert string to float)
```

: returns the quotient and remainder of dividing two numbers**divmod()**

`print(divmod(10, 3)) # Output: (3, 1) (quotient of 3 and remainder of 1)`

: converts a number to a hexadecimal string**hex()**

`print(hex(255)) # Output: 0xff (hexadecimal representation of 255)`

: converts a number to a binary string**bin()**

`print(bin(7)) # Output: 0b111 (binary representation of 7)`

: converts a number to an octal string**oct()**

`print(oct(8)) # Output: 0o10 (octal representation of 8)`

: returns a complex number with the specified real and imaginary parts**complex()**

`print(complex(2, 3)) # Output: (2+3j) (complex number with real part 2 and imaginary part 3)`

: returns the absolute value of a complex number**abs()**

`print(abs(2 + 3j)) # Output: 3.605551275463989 (magnitude of complex number)`

Function: The**floor()**`floor()`

function is used to round a number down to the nearest integer. It returns the largest integer value that is less than or equal to the specified number.

```
import math
x = 3.5
print(math.floor(x)) # Output: 3
y = 4.8
print(math.floor(y)) # Output: 4
```

In the above example, the `math.floor()`

function is used to round the numbers `3.5`

and `4.8`

down to the nearest integer, which is `3`

and `4`

, respectively.

Function: The**ceil()**`ceil()`

function is used to round a number up to the nearest integer. It returns the smallest integer value that is greater than or equal to the specified number.

```
import math
x = 3.5
print(math.ceil(x)) # Output: 4
y = 4.8
print(math.ceil(y)) # Output: 5
```

In the above example, the `math.ceil()`

function is used to round the numbers `3.5`

and `4.8`

up to the nearest integer, which is `4`

and `5`

, respectively.

## Storing Data in Python

### Finding Log of Numbers in Python:

In Python, the `log()`

function is used to find the logarithm of a number with a specified base. The `math`

module provides various functions for working with logarithms.

Function: The**log()**`log()`

function is used to find the natural logarithm of a number, which is the logarithm to the base`e`

. If you want to find the logarithm with a different base, you can use the formula:

`log_a(x) = log_e(x) / log_e(a)`

where `x`

is the number and `a`

is the base.

```
import math
x = 10
print(math.log(x)) # Output: 2.302585092994046 (natural logarithm of 10)
y = 100
print(math.log(y, 10)) # Output: 2.0 (logarithm of 100 to the base 10)
```

In the above example, the `math.log()`

function is used to find the natural logarithm of the numbers `10`

and `100`

, which are `2.302585092994046`

and `2.0`

, respectively. The second argument specifies the base of the logarithm. If the base is not specified, it defaults to `e`

.

Function: The**log10()**`log10()`

function is used to find the logarithm of a number to the base 10.

```
import math
x = 100
print(math.log10(x)) # Output: 2.0 (logarithm of 100 to the base 10)
```

In the above example, the `math.log10()`

function is used to find the logarithm of the number `100`

to the base 10, which is `2.0`

.

These functions are useful in various scientific and mathematical calculations, such as calculating growth rates, calculating decibel levels, and many more.

### Finding the square root of numbers in Python

In Python, the `sqrt()`

function from the `math`

module is used to find the square root of a number.

```
import math
x = 16
print(math.sqrt(x)) # Output: 4.0 (square root of 16)
```

In the above example, the `math.sqrt()`

function is used to find the square root of the number `16`

, which is `4.0`

.

The `sqrt()`

function can also be used to find the square root of negative numbers, which results in a complex number:

```
import math
x = -16
print(math.sqrt(x)) # Output: (1.2246467991473532e-16+4j) (square root of -16)
```

In the above example, the `math.sqrt()`

function is used to find the square root of the number `-16`

, which results in a complex number with a real part of `1.2246467991473532e-16`

and an imaginary part of `4`

.

It is important to note that the `sqrt()`

function returns a floating-point number, which means that the result may not be exact for certain numbers. For example, the square root of `2`

cannot be represented exactly as a finite decimal or binary fraction.

## What is a string?

In computer programming, a string is a sequence of characters. In Python, a string is a sequence of Unicode characters. It is enclosed in quotation marks (`' '`

or `" "`

) to distinguish it from other types of data, such as numbers.

For example, the following are examples of strings:

```
my_string = "Hello, World!"
another_string = 'This is another string.'
```

In the above examples, `my_string`

and `another_string`

are strings that contain sequences of characters.

Strings can contain letters, numbers, symbols, and spaces, and can be of any length. They can be manipulated using various string methods to perform operations such as concatenation, searching, and replacing.

In Python, strings are immutable, which means that once a string is created, it cannot be modified. However, new strings can be created by concatenating or slicing existing strings.

Strings are widely used in programming for representing text data, such as user input, file contents, and website content.

## String functions:

In Python, there are many built-in string functions that allow you to manipulate and perform various operations on strings. Here are some of the most commonly used string functions:

`len()`

Function: The`len()`

function is used to find the length of a string.

```
my_string = "Hello, World!"
print(len(my_string)) # Output: 13
```

In the above example, the `len()`

function is used to find the length of the string `my_string`

, which is `13`

.

`lower()`

and`upper()`

Functions: The`lower()`

function is used to convert a string to lowercase, while the`upper()`

function is used to convert a string to uppercase.

```
my_string = "Hello, World!"
print(my_string.lower()) # Output: hello, world!
print(my_string.upper()) # Output: HELLO, WORLD!
```

In the above example, the `lower()`

and `upper()`

functions are used to convert the string `my_string`

to lowercase and uppercase, respectively.

`split()`

Function: The`split()`

function is used to split a string into a list of substrings based on a specified separator.

```
my_string = "Hello, World!"
print(my_string.split(",")) # Output: ['Hello', ' World!']
```

In the above example, the `split()`

function is used to split the string `my_string`

into a list of substrings based on the separator `,`

.

`strip()`

Function: The`strip()`

function is used to remove any leading or trailing whitespace from a string.

```
my_string = " Hello, World! "
print(my_string.strip()) # Output: Hello, World!
```

In the above example, the `strip()`

function is used to remove the leading and trailing whitespace from the string `my_string`

.

`replace()`

Function: The`replace()`

function is used to replace one or more occurrences of a substring in a string with a new substring.

```
my_string = "Hello, World!"
print(my_string.replace("World", "Universe")) # Output: Hello, Universe!
```

In the above example, the `replace()`

function is used to replace the substring `World`

in the string `my_string`

with the new substring `Universe`

.

These are just a few of the many string functions available in Python. They are widely used in programming for manipulating and performing various operations on strings.

These are just a few examples of number functions in Python. There are many more built-in functions and libraries available that can be used for various numerical computations. Read more About Number Functions.

In Python, strings are sequences of Unicode characters enclosed in quotation marks. There are many built-in string functions available in Python for manipulating and performing various operations on strings. Some of the commonly used string functions include `len()`

for finding the length of a string, `lower()`

and `upper()`

for converting a string to lowercase or uppercase, `split()`

for splitting a string into a list of substrings based on a specified separator, `strip()`

for removing leading or trailing whitespace from a string, and `replace()`

for replacing one or more occurrences of a substring in a string with a new substring. These functions are widely used in programming for manipulating and processing text data.

## Leave A Comment