3. Python abs()
The abs() method returns the absolute
value of the given number. If the number
is a complex number, abs() returns its
magnitude.
The syntax of abs() method is:
abs(num)
4. Python abs()
integer = -20print('Absolute value of -20 is:',
abs(integer))
#random floating number
floating = -30.33
print('Absolute value of -30.33 is:',
abs(floating))
When you run the program, the output will be:
Absolute value of -20 is: 20
Absolute value of -30.33 is: 30.33
5. Python all()
The all() method returns True when all
elements in the given iterable are true. If not, it
returns False.
The syntax of all() method is:
all(iterable)
all() Parameters
The all() method takes a single parameter:
iterable - any iterable (list, tuple, dictionary,
etc.) which contains the elements
6. Python all()
Return Value from all()
The all() method returns:
True - If all elements in an iterable are true
False - If any element in an iterable is false
7. Python all()
l = [1, 3, 4, 5]
print(all(l))
# all values false
l = [0, False]
print(all(l))
# one false value
l = [1, 3, 4, 0]
print(all(l))# one true value
l = [0, False, 5]
print(all(l))
# empty iterable
l = []
print(all(l))
8. Python all()
When you run the program, the output will be:
True
False
False
False
True
9. Python any()
The any() method returns True if any element of an
iterable is True. If not, any() returns False.
The syntax of any() is:
any(iterable)
any() Parameters
The any() method takes an iterable (list, string,
dictionary etc.) in Python.
10. Python any()
Return Value from any()
any() returns:
True: if at least one element of an iterable is true
False: if all elements are false or if an iterable is
empty
11. Python any()
l = [1, 3, 4, 0]
print(any(l))
l = [0, False]
print(any(l))
l = [0, False, 5]
print(any(l))
l = []print(any(l))
13. Python complex()
The complex() method returns a complex number
when real and imaginary parts are provided, or it
converts a string to a complex number.
The syntax of complex() is:
complex([real[, imag]])
14. Python complex()
complex() Parameters
In general, the complex() method takes two
parameters:
real - real part. If real is omitted, it defaults to 0.
imag - imaginary part. If imag is omitted, it default to
0.
If the first parameter passed to this method is a
string, it will be interpreted as a complex number. In
this case, second parameter shouldn't be passed.
15. Python complex()
Return Value from complex()
As suggested by the name, the complex() method
returns a complex number.
If the string passed to this method is not a valid
complex number, ValueError exception is raised.
16. Python complex()
z = complex(2, -3)
print(z)z = complex(1)
print(z)z = complex()
print(z)
z = complex('5-9j')
print(z)
17. Python float()
The float() method returns a floating point number
from a number or a string.
The syntax for float() is:
float([x])
float() Parameters
The float() method takes a single parameter:
x (Optional) - number or string that needs to be
converted to floating point number
If it's a string, the string should contain decimal
points
19. Python float()
Return value from float()
The float() method returns:
Equivalent floating point number if an argument is
passed
0.0 if no arguments passed
OverflowError exception if the argument is outside
the range of Python float
20. Python float()
# for integers
print(float(10))
# for floats
print(float(11.22))
# for string floats
print(float("-13.33"))
# for string floats with whitespaces
print(float(" -24.45n"))
# string float error
print(float("abc"))
21. Python float()
When you run the program, the output will be:
10.0
11.22
-13.33
-24.45
ValueError: could not convert string to float: 'abc'
22. Python help()
The help() method calls the built-in Python help
system.
The syntax of help() is:
help(object)
help() Parameters
The help() method takes maximum of one
parameter.
object (optional) - you want to generate the help of
the given object
23. Python help()
How help() works in Python?
The help() method is used for interactive use. It's
recommenced to try it in your interpreter when you
need help to write Python program and use
Python modules.
24. Python help()
object is passed to help() (not a string)
Try these on Python shell.
>>> help(list)
>>> help(dict)
>>> help(print)
>>> help([1, 2, 3])
25. Python help()
object is passed to help() (not a string)
Try these on Python shell.
>>> help(list)
>>> help(dict)
>>> help(print)
>>> help([1, 2, 3])
26. Python help()
To quit the help utility and return to the interpreter,
you need to type quit and press enter.
help > quit
27. Python hex()
The hex() function converts an integer number to the
corresponding hexadecimal string.
The syntax of hex() is:
hex(x)
hex() Parameters
The hex() function takes a single argument.
x - integer number (int object or it has to
define __index__() method that returns an integer)
28. Python hex()
Return Value from hex()
The hex() function converts an integer to the
corresponding hexadecimal number in string form
and returns it.
The returned hexadecimal string starts with prefix
"0x" indicating it's in hexadecimal form.
29. Python hex()
number = 435
print(number, 'in hex =', hex(number))
number = 0print(number, 'in hex =', hex(number))
number = -34print(number, 'in hex =', hex(number))
returnType = type(hex(number))
print('Return type from hex() is', returnType)
30. Python hex()
When you run the program, the output will be:
435 in hex = 0x1b3
0 in hex = 0x0
-34 in hex = -0x22
Return type from hex() is <class 'str'>
31. Python int()
The int() method returns an integer object from any
number or string.
The syntax of int() method is:
int(x=0, base=10)
32. Python int()
int() Parameters
The int() method takes two arguments:
x - Number or string to be converted to integer
object.
Default argument is zero.
base - Base of the number in x.
Can be 0 (code literal) or 2-36.
33. Python int()
Return value from int()
The int() method returns:
an integer object from the given number or string,
treats default base as 10
(No parameters) returns 0
(If base given) treats the string in the given base (0,
2, 8, 10, 16)
35. Python int()
When you run the program, the output will be:
int(123) is: 123
int(123.23) is: 123
int('123') is: 123
36. Python list() Function
When you run the program, the output will be:
int(123) is: 123
int(123.23) is: 123
int('123') is: 123
The syntax of list() constructor is:
list([iterable])
37. Python list() Function
list() Parameters
Python list() constructor takes a single argument:
iterable (Optional) - an object that could be a
sequence (string, tuples) or collection
(set, dictionary) or iterator object
38. Python list() Function
Return value from list()
The list() constructor returns a mutable sequence list
of elements.
If no parameters are passed, it creates an empty list
If iterable is passed as parameter, it creates a list of
elements in the iterable
40. Python list() Function
# vowel list
vowelList = ['a', 'e', 'i', 'o', 'u']
print(list(vowelList))
41. Python list() Function
When you run the program, the output will be:
[]
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']
42. Python max()
The max() method returns the largest element in an
iterable or largest of two or more parameters.
Differnt syntaxes of max() are:
max(iterable, *iterables[,key, default])
max(arg1, arg2, *args[, key])
43. Python max()
max() Parameters
max() has two forms of arguments it can work with.
max(iterable, *iterables[, key, default])
iterable - sequence (tuple, string), collection
(set, dictionary) or an iterator object whose largest
element is to be found
*iterables (Optional) - any number of iterables whose
largest is to be found
key (Optional) - key function where the iterables are
passed and comparison is performed based on its return
value
default (Optional) - default value if the given iterable is
empty
44. Python max()
max(arg1, arg2, *args[, key])
arg1 - mandatory first object for comparison (could be
number, string or other object)
arg2 - mandatory second object for comparison (could be
number, string or other object)
*args(Optional) - other objects for comparison
key - key function where each argument is passed, and
comparison is performed based on its return value
45. Python max()
max(arg1, arg2, *args[, key])
arg1 - mandatory first object for comparison (could be
number, string or other object)
arg2 - mandatory second object for comparison (could be
number, string or other object)
*args(Optional) - other objects for comparison
key - key function where each argument is passed, and
comparison is performed based on its return value
46. Python max()
# using max(arg1, arg2, *args)
print('Maximum is:', max(1, 3, 2, 5, 4))
# using max(iterable)
num = [1, 3, 2, 8, 5, 10, 6]
print('Maximum is:', max(num))
47. Python max()
When you run the program, the output will be:
Maximum is: 5
Maximum is: 10