SlideShare a Scribd company logo
Python Built-in Functions by A Technologies
Python Built-in Functions
Python Built-in Functions
By:
By:
Prof. Amitav Biswas
Prof. Amitav Biswas
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)
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
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
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
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))
Python all()
When you run the program, the output will be:
True
False
False
False
True
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.
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
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))
Python any()
When you run the program, the output will be:
True
False
True
False
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]])
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.
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.
Python complex()
z = complex(2, -3)
print(z)z = complex(1)
print(z)z = complex()
print(z)
z = complex('5-9j')
print(z)
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
Python float()
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
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"))
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'
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
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.
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])
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])
Python help()
To quit the help utility and return to the interpreter,
you need to type quit and press enter.
help > quit
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)
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.
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)
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'>
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)
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.
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)
Python int()
# integerprint("int(123) is:",
int(123))
# floatprint("int(123.23) is:",
int(123.23))
# stringprint("int('123') is:",
int('123'))
Python int()
When you run the program, the output will be:
int(123) is: 123
int(123.23) is: 123
int('123') is: 123
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])
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
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
Python list() Function
# empty list
print(list())
# vowel string
vowelString = 'aeiou‘
print(list(vowelString))
# vowel tuple
vowelTuple = ('a', 'e', 'i', 'o', 'u')
print(list(vowelTuple))
Python list() Function
# vowel list
vowelList = ['a', 'e', 'i', 'o', 'u']
print(list(vowelList))
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']
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])
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
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
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
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))
Python max()
When you run the program, the output will be:
Maximum is: 5
Maximum is: 10
Ad

More Related Content

Similar to Python Built-in Functions by A Technologies (20)

PYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptxPYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptx
georgejustymirobi1
 
Day2
Day2Day2
Day2
Karin Lagesen
 
Data Handling
Data Handling Data Handling
Data Handling
bharath916489
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
Mohd Sajjad
 
Array
ArrayArray
Array
Malainine Zaid
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
AnishaJ7
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings Methods
Mr Examples
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
Abdul Haseeb
 
Functions.docx
Functions.docxFunctions.docx
Functions.docx
VandanaGoyal21
 
Data types in python
Data types in pythonData types in python
Data types in python
RaginiJain21
 
funadamentals of python programming language (right from scratch)
funadamentals of python programming language (right from scratch)funadamentals of python programming language (right from scratch)
funadamentals of python programming language (right from scratch)
MdFurquan7
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
UadAccount
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
types.pdf
types.pdftypes.pdf
types.pdf
Venkateswara Babu Ravipati
 
Arrays
ArraysArrays
Arrays
Komal Singh
 
Parsing swiftly-Cocoaheads-2015-02-12
Parsing swiftly-Cocoaheads-2015-02-12Parsing swiftly-Cocoaheads-2015-02-12
Parsing swiftly-Cocoaheads-2015-02-12
griotspeak
 
Unit-5-Part1 Array in Python programming.pdf
Unit-5-Part1 Array in Python programming.pdfUnit-5-Part1 Array in Python programming.pdf
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
object oriented programing in python and pip
object oriented programing in python and pipobject oriented programing in python and pip
object oriented programing in python and pip
LakshmiMarineni
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
Logan Chien
 
PYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptxPYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptx
georgejustymirobi1
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
Mohd Sajjad
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
AnishaJ7
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings Methods
Mr Examples
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
Abdul Haseeb
 
Data types in python
Data types in pythonData types in python
Data types in python
RaginiJain21
 
funadamentals of python programming language (right from scratch)
funadamentals of python programming language (right from scratch)funadamentals of python programming language (right from scratch)
funadamentals of python programming language (right from scratch)
MdFurquan7
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
UadAccount
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
Parsing swiftly-Cocoaheads-2015-02-12
Parsing swiftly-Cocoaheads-2015-02-12Parsing swiftly-Cocoaheads-2015-02-12
Parsing swiftly-Cocoaheads-2015-02-12
griotspeak
 
Unit-5-Part1 Array in Python programming.pdf
Unit-5-Part1 Array in Python programming.pdfUnit-5-Part1 Array in Python programming.pdf
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
object oriented programing in python and pip
object oriented programing in python and pipobject oriented programing in python and pip
object oriented programing in python and pip
LakshmiMarineni
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
Logan Chien
 

Recently uploaded (20)

Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Ad

Python Built-in Functions by A Technologies

  • 2. Python Built-in Functions Python Built-in Functions By: By: Prof. Amitav Biswas Prof. Amitav Biswas
  • 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))
  • 12. Python any() When you run the program, the output will be: True False True False
  • 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)
  • 34. Python int() # integerprint("int(123) is:", int(123)) # floatprint("int(123.23) is:", int(123.23)) # stringprint("int('123') is:", int('123'))
  • 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
  • 39. Python list() Function # empty list print(list()) # vowel string vowelString = 'aeiou‘ print(list(vowelString)) # vowel tuple vowelTuple = ('a', 'e', 'i', 'o', 'u') print(list(vowelTuple))
  • 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