2. Integers
Purpose:
Digital computer can only process data that
has been expressed in binary format. That is
to say 0 or 1, which can be expressed
electronically as On/Off.
For Information technology applications, the
amount of information being processed is an
important design consideration.
The term bit-length is a technical shorthand
for this measure.
bit_length() method in python tell us the
Number of bits necessary to represent an
integer in binary number.
.bit_length()
Use Cases
3. Integers
Purpose:
Not only real numbers, Python can also
handle complex numbers and its associated
functions.
Complex numbers have their uses in many
applications related to mathematics and
python provides useful tools to handle and
manipulate them.
Complex numbers are expressed as
combination of real and imaginary part.
Complex number = a + bi
Where ‘a’ is the real part and ‘bi’ is the
imaginary part.
Since integers are real numbers only, they
will always return 0 when using .imag().
.real() and .imag()
Use Cases
4. Integers
Purpose:
Whenever we are dealing with complex
numbers we can use this function.
This function basically removes the
parenthesis from an integer.
.conjugate()
Use Cases
.numerator()
.denominator()
Purpose:
These function return the numerator and
denominator from an integer.
Since integers are without any decimals, so
the denominator will always be 1.
5. Strings
Purpose:
At some point, we may need to beak a large
string into smaller chunks or strings. This is
opposite of concatenation which merges or
combines string into one.
To do this, we use the split function. It breaks
the given string by the specified separator
and then returns the output in form of a list.
We need to specify a delimiter as the
separator. If we do not define a delimiter,
then python will split the string assuming
whitespace as a separator.
We can also define how many times we
need python to split the string. By default
there is no limit.
.split()
Use Cases
6. Strings
Purpose:
This function returns the capitalized version
of a string. We need to note that it only
capitalizes the first letter of the string.
.capitalize()
Use Cases
.lower() and .upper()
Purpose:
.lower() function returns the entire string in a
lower case.
.upper() function returns the entire string in
upper case.
7. Strings
Purpose:
This method formats the specified value and
inserts them inside the string’s placeholder.
A placeholder is defined using curly brackets:
{}. Placeholder tell the format method the
location where we need to insert a specific
value.
We can have multiple placeholder in a string
in which we can have multiple values.
This method is particularly useful when we
need to pass something inside a string which
is changing or is calculated.
.format()
Use Cases
8. Float
Purpose:
This function returns a pair of integer whose
ratio is exactly equal to the original float and
with a positive denominator. Note that is
returns the result in tuple.
.as_integer_ratio()
Use Cases
.hex() and .fromhex()
Purpose:
.hex() function returns the hexadecimal
representation of a floating point number.
.fromhex() creates a floating point number
from a hexadecimal string.
9. Float
Purpose:
This function checks whether the value
stored is an integer of not. Returns true if it
is.
.is_integer()
Use Cases
.imag() and .real()
Purpose:
Complex numbers are expressed as
combination of real and imaginary part.
Complex number = a + bi
Where ‘a’ is the real part and ‘bi’ is the
imaginary part.
Since floats are real numbers only, they will
always return 0 when using .imag().
.
10. Lists
Purpose:
This function adds a single item to the end of
the list. Accordingly the length of the string
increases by 1.
.append()
Use Cases
.copy()
Purpose:
A list can be copied by using the ‘=‘
operator. But the problem with this is,
whenever we change the old list the new list
will also change.
To remedy this we can use the fuction
.copy() to just clone the old list. We can save
this cloned list as a new list and work on it.
11. Lists
Purpose:
This function is different from .append() as
by use of this function we can add more
than one element to our list.
Basically we can add an iterable objects.
.extend()
Use Cases
.index()
Purpose:
This function finds the given element in the
list and returns its position.
Note: If the same element is present more
than once, this function returns the index of
the first occurrence of the element.
Note: Index in python starts from 0, not 1.
We need to provide the element of which we
want to find the index.
12. Lists
Purpose:
This function can used to sort list of integers,
floats, strings and others.
The sorting is done in ascending,
descending or user defined order.
.sort()
Use Cases
.pop()
Purpose:
This function removes the item at the given
index from the list and returns the removed
item.
By default if we do not specify the item, then
the function will remove the last items on list.
It does not work when the index is out of
bounds or out of range.