
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialIn programming terminology, function is a separate, complete and reusable software component. Long and complex logic in a program is broken into smaller, independent and reusable blocks of instructions usually called a module, a subroutine or function. It is designed to perform a specific task that is a part of entire process. This approach towards software development is called modular programming.
Such a program has a main routine through which smaller independent modules (functions) are called upon. Each When called, a function performs a specified task and returns the control back to the calling routine, optionally along with result of its process.
Python interpreter has a number of built-in functions. They are always available for use in every interpreter session. Many of them have been discussed in previously. For example print() and input() for I/O, number conversion functions (int(), float(), complex()), data type conversions (list(), tuple(), set()) etc. Here is complete list of Python's built-in functions:
Built-in Functions | ||||
|---|---|---|---|---|
abs() | delattr() | hash() | memoryview() | set() |
all() | dict() | help() | min() | setattr() |
any() | dir() | hex() | next() | slice() |
ascii() | divmod() | id() | object() | sorted() |
bin() | enumerate() | input() | oct() | staticmethod() |
bool() | eval() | int() | open() | str() |
breakpoint() | exec() | isinstance() | ord() | sum() |
bytearray() | filter() | issubclass() | pow() | super() |
bytes() | float() | iter() | print() | tuple() |
callable() | format() | len() | property() | type() |
chr() | frozenset() | list() | range() | vars() |
classmethod() | getattr() | locals() | repr() | zip() |
compile() | globals() | map() | reversed() | __import__() |
complex() | hasattr() | max() |
Other functions will come up in later chapters of this tutorial.
In addition to built-in functions, a large number of pre-defined functions are also available as a part of libraries bundled with Python distributions. However they are not available for use automatically. These functions are defined in modules. A module is a file containing definition of functions, classes, variables, constants or any other Python object. Standard distribution of Python contains a large number of modules, generally known as built-in modules. Each built-in module contains resources for certain specific functionalities such as OS management, disk IO, networking, database connectivity etc.
Most of the times, built-in modules are written in C and integrated with Python interpreter.
A built-in module may be a Python script (with .py extension) containing useful utilities.
To display list of all available modules, use following command in Python console:
>>> help('modules')
Resources from other modules are loaded by import statement. The general format of using a function in any module is like this:
import module
module.function([arguments if any])
The sqrt() function in math module returns square root of a number.
>>> import math
>>> math.sqrt(100)
10.0
In this chapter we shall discuss some of the frequently used functions from certain built-in modules.
This module has functions to perform many tasks of operating system.
We can create a new directory using mkdir() function from os module.
>>> import os
>>> os.mkdir("d:\\tempdir")
A new directory corresponding to path in string argument in the function will be created. If we open D drive in Windows explorer we should notice tempdir folder created.
To change current working directory to use chdir() function.
>>> import os
>>> os.chdir("d:\\temp")
This function in returns name off current working directory.
>>> os.getcwd()
'd:\\temp'
Directory paths can also be relative. If current directory is set to D drive and then to temp without mentioning preceding path, then also current working directory will be changed to d:\temp
>>> os.chdir("d:\\")
>>> os.getcwd()
'd:\\'
>>> os.chdir("temp")
>>> os.getcwd()
'd:\\temp'
In order to set current directory to parent directory use ".." as the argument to chdir() function.
>>> os.chdir("d:\\temp")
>>> os.getcwd()
'd:\\temp'
>>> os.chdir("..")
>>> os.getcwd()
'd:\\'
The rmdir() function in os module removes a specified directory either with absolute or relative path. However it should not be the current working directory and it should be empty.
>>> os.chdir("tempdir")
>>> os.getcwd()
'd:\\tempdir'
>>> os.rmdir("d:\\temp")
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'd:\\temp'
>>> os.chdir("..")
>>> os.rmdir("temp")
The os module has listdir() function which returns list of all files in specified directory.
>>> os.listdir("c:\\Users")
['acer', 'All Users', 'Default', 'Default User', 'desktop.ini', 'Public']
Python’s standard library contains random module which defines various functions for handling randomization. Python uses a pseudo-random generator based upon Mersenne Twister algorithm that produces 53-bit precision floats. Functions in this module depend on pseudo-random number generator function random() which generates a random float number between 0.0 and 1.0.
random.random(): Returns a random float number between 0.0 to 1.0. The function doesn’t need any arguments
>>> import random
>>> random.random()
0.755173688207591
Other functions in random module are described here:
random.randint(): Returns a random integer between the specified integers
>>> import random
>>> random.randint(1,100)
58
>>> random.randint(1,100)
91
random.randrange(): Returns a random element from the range created by start, stop and step arguments. The start , stop and step parameters behave similar to range() function.
>>> random.randrange(1,10)
2
>>> random.randrange(1,10,2)
3
>>> random.randrange(0,101,10)
40
random.choice(): Returns a randomly selected element from a sequence object such as string, list or tuple. An empty sequence as argument raises IndexError
>>> import random
>>> random.choice('computer')
'o'
>>> random.choice([12,23,45,67,65,43])
65
>>> random.choice((12,23,45,67,65,43))
23
random.shuffle(): This functions randomly reorders elements in a list.
>>> numbers=[12,23,45,67,65,43]
>>> random.shuffle(numbers)
>>> numbers
[23, 12, 43, 65, 67, 45]
>>> random.shuffle(numbers)
>>> numbers
[23, 43, 65, 45, 12, 67]
This module presents commonly required mathematical functions.
In addition, two mathematical constants are also defined in this module.
Pie π which is defined as ratio of circumference to diameter of a circle and its value is 3.141592653589793, is available in math module.
>>> import math
>>> math.pi
3.141592653589793
Another mathematical constant in this module is e. It is called Euler’s number and is a base of natural logarithm. Its value is 2.718281828459045
>>> math.e
2.718281828459045
This module contains functions for calculating various trigonometric ratios for a given angle. The functions (sin, cos, tan etc.) need angle in radians as argument. We on the other hand are used to express angle in degrees. The math module presents two angle conversion functions (degrees() and radians()) to convert angle in degrees to radians and vice versa.
radians(): converts angle in degrees to radians.(Note: π radians is equivalent to 180 degrees)
>>> math.radians(30)
0.5235987755982988
degrees(): converts angle in radians to degree.
>>> math.degrees(math.pi/6)
29.999999999999996
Following statements show sin, cos and tan ratios for angle of 30 degrees (0.5235987755982988 radians)
>> math.sin(0.5235987755982988)
0.49999999999999994
>>> math.cos(0.5235987755982988)
0.8660254037844387
>>> math.tan(0.5235987755982988)
0.5773502691896257
sin(30) | 0.5 | 0.49999999999999994 |
cos(30) | 3/2 | 0.8660254037844387) |
tan(30) | 1/2 | 0. 5773502691896257 |
math.log(): returns natural logarithm of given number. Natural logarithm is calculated to the base e.
math.log10(): returns base-10 logarithm or standard logarithm of given number.
>>> math.log10(10)
1.0
math.exp(): returns a float number after raising e (math.e) to given number. exp(x) is equivalent to e**x
>>> math.log10(10)
1.0
>>> math.e**10
22026.465794806703
math.pow(): This function receives two float arguments, raises first to second and returns the result. pow(4,4) is equivalent to 4**4
>>> math.pow(4,4)
256.0
>>> 4**4
256
math.sqrt(): This function computes square root of given number
>>> math.sqrt(100)
10.0
>>> math.sqrt(3)
1.7320508075688772
The ceil() function approximates given number to smallest integer greater than or equal to given floating point number. The floor() function returns a largest integer less than or equal to given number
>>> math.ceil(4.5867)
5
>>> math.floor(4.5687)
4
This module provides functions and variables used to manipulate different parts of the Python runtime environment.
This return list of command line arguments passed to a Python script. Item at 0th index of this list is always the name of the script. Rest of the arguments are stored at subsequent indices.
Here is a Python script (test.py) consuming two arguments from command line.
import sys
print ("My name is {}. I am {} years old".format(sys.argv[1], sys.argv[2]))
This script is executed from command line as follows:
C:\python37>python tmp.py Anil 23
My name is Anil. I am 23 years old
This causes program to end and return to either Python console or command prompt. It is used to safely exit from program in case of exception.
It returns the largest integer a variable can take.
>>> import sys
>>> sys.maxsize
9223372036854775807
This is an environment variable that returns search path for all Python modules.
>>> sys.path
['', 'C:\\python37\\Lib\\idlelib', 'C:\\python37\\python36.zip', 'C:\\python37\\DLLs', 'C:\\python37\\lib', 'C:\\python37', 'C:\\Users\\acer\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\python37\\lib\\site-packages']
These are file objects used by the interpreter for standard input, output and errors. stdin is used for all interactive input (Python shell). stdout is used for the output of print() and of input(). The interpreter’s prompts and error messages go to stderr.
This attribute displays a string containing version number of current Python interpreter.
This module provides alternatives to built-in container data types such as list, tuple and dict.
This function is a factory function that returns object of a tuple subclass with named fields. Any valid Python identifier may be used for a field name except for names starting with an underscore.
collections.namedtuple(typename, field-list)
The typename parameter is the subclass of tuple. Its object has attributes mentioned in field list. These field attributes can be accessed by lookup as well as by its index.
Following statement declares a employee namedtuple having name, age and salary as fields
>>> import collections
>>> employee=collections.namedtuple('employee', [name, age, salary])
To create a new object of this namedtuple
>>> e1=employee("Ravi", 251, 20000)
Values of the field can be accessible by attribute lookup
>>> e1.name
'Ravi'
Or by index
>>> e1[0]
'Ravi'
Ordered dictionary is similar to a normal dictionary. However, normal dictionary the order of insertion of keys in it whereas ordered dictionary object remembers the same. The key-value pairs in normal dictionary object appear in arbitrary order.
>>> d1={}
>>> d1['A']=20
>>> d1['B']=30
>>> d1['C']=40
>>> d1['D']=50
We then traverse the dictionary by a for loop,
>>> for k,v in d1.items():
print (k,v)
A 20
B 30
D 50
C 40
But in case of OrderedDict object:
>>> import collections
>>> d2=collections.OrderedDict()
>>> d2['A']=20
>>> d2['B']=30
>>> d2['C']=40
>>> d2['D']=50
Key-value pairs will appear in the order of their insertion.
>>> for k,v in d2.items():
print (k,v)
A 20
B 30
C 40
D 50
A deque object supports append and pop operation from both ends of a list. It is more memory efficient than a normal list object because in a normal list, removing one of iem causes all items to its right to be shifted towards left. Hence it is very slow.
>>> q=collections.deque([10,20,30,40])
>>> q.appendleft(110)
>>> q
deque([110, 10, 20, 30, 40])
>>> q.append(41)
>>> q
deque([0, 10, 20, 30, 40, 41])
>>> q.pop()
40
>>> q
deque([0, 10, 20, 30, 40])
>>> q.popleft()
110
>>> q
deque([10, 20, 30, 40])
This module provides following statistical functions :
mean() : calculate arithmetic mean of numbers in a list
>>> import statistics
>>> statistics.mean([2,5,6,9])
5.5
median() : returns middle value of numeric data in a list. For odd items in list, it returns value at (n+1)/2 position. For even values, average of values at n/2 and (n/2)+1 positions is returned.
>>> import statistics
>>> statistics.median([1,2,3,8,9])
3
>>> statistics.median([1,2,3,7,8,9])
5.0
mode(): returns most repeated data point in the list.
>>> import statistics
>>> statistics.mode([2,5,3,2,8,3,9,4,2,5,6])
2
stdev() : calculates standard deviation on given sample in the form of list.
>>> import statistics
>>> statistics.stdev([1,1.5,2,2.5,3,3.5,4,4.5,5])
1.3693063937629153
This module has many time related functions.
time():
This function returns current system time in ticks. The ticks is number of seconds elapsed after epoch time i.e. 12.00 am, January 1, 1970.
>>> time.time()
1544348359.1183174
localtime():
This function translates time in ticks in a time tuple notation.
>>> tk=time.time()
>>> time.localtime(tk)
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=9, tm_hour=15, tm_min=11, tm_sec=25, tm_wday=6, tm_yday=343, tm_isdst=0)
This functions returns a readable format of local time
>>> tk=time.time()
>>> tp=time.localtime(tk)
>>> time.asctime(tp)
'Sun Dec 9 15:11:25 2018'
This function returns string representation of system's current time
>>> time.ctime()
'Sun Dec 9 15:17:40 2018'
This function halts current program execution for a specified duration in seconds.
>>> time.ctime()
'Sun Dec 9 15:19:14 2018'
>>> time.sleep(20)
>>> time.ctime()
'Sun Dec 9 15:19:34 2018'
Few more built-in modules will be discussed in separate subsequent chapters of this tutorial.