You are on page 1of 12

21. What is a built-in function? What are the meanings of these built-in functions?

Give
an example for each one [https://docs.python.org/3/library/functions.html]
- Built-in functions are predefined functions that are designed to do specific jobs,
these functions are always ready to be used when called.
21.1. abs():
- Python abs() function return the absolute value of a number. The input can be an integer,
floating-point number, complex number. Then output the absolute value.
- Example:

21.2. any():
- Function any() returns True if any of the elements of the iterable(List, Dictionary, Tuple,
set, etc) are True else returns False
- Example:

21.3. ascii():
- Function ascii() return a readable version of any object. This function replace any non-
ascii characters with escape characters.
- Example:

21.4. enumerate()
- Function enumerate() adds a counter to an iterable and returns it in a form of
enumerating object. This enumerated object can then be used directly for loops or
converted into a list of tuples using the list() method.
- Syntax: enumerate(iterable, start=0), whereas we can change the start number to any
number
- Example:

21.5. help()
- Function help() is used to display the documentation of modules, functions, classes,
keywords,…
- Example:
21.6. len()
- Function len() can be use to find the length of an object(strings, lists, tuples,…)
- Example:

21.7. list()
- Function list() are arrays, a list is a collection of things, enclosed in [ ] and separated by
commas.
- Example:

21.8. max()
- Function max() return the largest item in an iterable or the largest of two or more
arguments.
- Example:

21.9. min()
- Function max() return the smallest item in an iterable or the smallest of two or more
arguments.
- Example:
21.10. reversed()
- Function reversed() returns an iterator that accesses the given sequence in the reverse
order.

21.11. round()
- Function round() float point number from the decimal value to the closest multiple of 10.
- Syntax: round(number, number of digits), we can optionally declare number of digits to
be rounded
- Example:

21.12. set()
- A Set is an unordered collection data type that is iterable, mutable and has no duplicate
elements. The major advantage of using a set, as opposed to a list, is that it has a highly
optimized method for checking whether a specific element is contained in the set.
- Example:

First run:

Second run:

21.13. sorted()
- Function sorted() returns a sorted list from the iterable object
- Example:
21.14. sum()
- Function sum() return the sum of all numbers in a list.
- Syntax: sum(iterable, start), declare start if we need to add the sum of start to the list
- Example:

21.15. tuple()
- Tuple is a collection of objects separated by commas. In some ways, a tuple is similar to
a list in terms of indexing, nested objects, and repetition but a tuple is immutable, unlike
lists which are mutable, also tuples use () rather than [] that used in list.
- Example:

21.16. zip()
- zip() function returns a zip object, which is an iterator of tuples where the first item in
each passed iterator is paired together, and then the second item in each passed iterator
are paired together, etc. If the length of two tuples is not the same, the one with least
items decide the length of the new iterator
- Example:

First run:

Second run:

22. What are the meanings of these built-in String functions? Give an
example for each one [https://www.tutorialspoint.com/built-in-
string-methods-in-python]
22.1. upper()
- Method upper() return the uppercase of all letters in a string
- Example:
22.2. lower()
- Method lower() return the lowercase of all letters in a string.

22.3. title()
- Method title() return a string whereas the fist character of each word is uppercase
- Example:

22.4. capitalize()
- Funxtion capitalize() return a string whereas the first character of the string is uppercase.
- Example:

22.5. decode()
- Method decode() convert the enncoded scheme to the desired scheme.
- Example:

22.6. encode()
- Method encode() encodes the string, using the specified encoding. If no encoding is
specified, UTF-8 will be used.
- Example:

22.7. endwith()
- Funciton endwith() return True if the string ends with the specified value, otherwise
return False
- Syntax: string.endwith(value, start, end). Optional start and end value can be declared to
- Example:
22.8. find()
- Method find() return the first position of a specified value and return -1 if not found
- Example:

22.9. index()
- Method index() return the index of the first occurrence of a element in a list. Unlike
find(), index() will raise a value error if the element cannot be found.
- Example:

22.10. isalnum()
- Method isalnum() checks whether all the characters in a given string are either alphabet
or numeric (alphanumeric) characters.
- Example:

22.11. isalpha()
- Method isalpha() is used to check whether all characters in the String is an alphabet
- Example:

22.12. isdigit()
- Method isdigit() returns “True” if all characters in the string are digits, Otherwise, It
returns “False”.
- Example:
22.13. isdecimal()
- Method isdecimal() return True if all characters are decimal, not number, otherwise
return False.
- Example:

22.14. islower()
- Method islower() returns True if all alphabets in a string are lowercase alphabets. If the
string contains at least one uppercase alphabet, it returns False.
- Example:

22.15. isnumeric()
- Method isnumeric() returns “True” if all characters in the string are numeric characters,
otherwise returns “False”.
- Example:

22.16. isspace()
- Method isspace() returns “True” if all characters in the string are whitespace characters,
Otherwise, It returns “False”. This Method is used to check if the argument contains all
whitespace characters, such as: ‘ ’, ‘\t’, ‘\n’, ‘\v’, ‘\f’, ‘\r’
- Example:

22.17. istitle()
- Method istitle() returns True if all words in a text start with a upper case letter, AND the
rest of the word are lower case letters, otherwise False.
- Example:
22.18. isupper()
- Method isupper() returns True if all the characters are in upper case, otherwise False.
Numbers, symbols and spaces are not checked, only alphabet characters.
- Example:

22.19. replace()
- Method replace() return a string where sbstrings are replaced with specified strings.
- Syntax: replace(old_string, new_string, number), we can optionally declare number to
replace the old with new string by number of times.
- Example

22.20. split()
- Method split() split a string into a list of strings after breaking the given string by the
specified separator.
- Example:

22.21. strip()
- Method strip() removes any leading (spaces at the beginning) and trailing (spaces at the
end) characters.
- Example:

22.22. swapcase()
- Method swapcase() swap the uppercase letter to lowercase and lowercase to uppercase.
- Example:

23. Give examples for string formatting with % operator, with .format() and
with string interpolation
[https://realpython.com/python-string-formatting/#2-new-style-string-
formatting-strformat]
24. What are the meanings of these built-in List functions? Give an
example for each one [https://www.tutorialspoint.com/built-in-
list-functions-and-methods-in-python]
24.1. append()
- Method append() adds an object to the existed one at the last position.
- Example:

24.2. count()
- Method count() return the number of times the object appear in list
- Example:

24.3. extend()
- Method extend() append a sequence to the end of the list
- Example:

24.4. index()
- Method index() return the first occurrence of the finding object.
- Example:
24.5. insert()
- Method insert() inserts a given element at at given index in a list
- Example:

24.6. pop()
- Method pop() removes and returns the last element of the list.
- Example:

24.7. remove()
- Method remove() remove a given object from the list
- Example:

24.8. reverse()
- Method reverse() reverses objects of the list in place.
- Example:
24.9. sort()
- Method sort() sort a list in ascending, descending, or user-defined order.
- Example:

38. What are the meanings of these built-in Dictionary functions? Give an example for each
one
38.1. clear()
- Method clear() removes all the items of a list
- Example:

38.2. copy()
- Method copy() return a shallow copy of a list (return a new list without copying the
original list)
- Example:

38.3. get()
- Method get() return the value of a given key in a dictionary, if not, then return None by
default value.
- Example:

38.4. has_key()
- Method has_key() return True if a specific key is presented, otherwise, return False
- Example:
38.5. items()
- Method items() is used to return the list with all dictionary keys with values as tuple
pairs
- Example:

38.6. keys()
- Method items() returns a list with all the key displayed in order.
- Example:

38.7. setdefault()
- Method setdefault() return the value of the key if the key exists, else insert a new key
with None value or specific value.
- Example:

38.8. values()
- Method values() returns the Key:Value of a dictionary as a list.
- Example:

38.9. update()
- Method update() returns an updated dictionary with the elements from another list, if it is
a new element, it will be add, or if it is an old key but new value, the old value is then
replaced.
- Example:

You might also like