Python has tons of Built-in modules that can be read from above link. In this notebook file, we will be discussing a short but important subset of it:
. py
extension containing Python code (variables, functions, classes etc) that can be imported inside another Python Program.
- Modularity: We use modules to break down large programs into small manageable and organized files.
- Simplicity: Rather than focusing the entire problem at hand, a module typically focuses on one relatively small portion of the problem.
- Maintainability: Modules are typically designed so that they enforce logical boundries between different problem domains.
- Reusability: Functionality defined in a single module can be easily reused (through an appropriately defined interface) by other parts of the application. This eliminates the need to duplicate code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs.
- Scoping: Modules typically define a separate namespace, which helps avoid collisions between identifiers in different areas of a program. The key benefit of using modules is namespaces: you must import the module to use its functions within a Python script or notebook. Namespaces provide encapsulation and avoid naming conflicts between your code and a module or across modules.
Note: - A module is a single file of Python code that is meant to be imported, while a Python package is a simple directory having collections of Python modules under a common namespace.
import math
¶
- We can use the
import
keyword to import a module, and later using the module name we can access its functions using the dot . operator, likemath.ceil()
# We have seen the use of dir() function. When called without argument it displays symbols of current module
print(dir())
['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'quit']
import math
print(dir())
math.ceil(2.3)
math.factorial(10)
['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'math', 'quit']
3628800
import math as m
¶
- We can also import a module by using a short alias, thus saving typing time in some cases. Note that in this case, the name
math
will not be recognized in our scope. Hence,math.ceil()
is invalid andm.ceil()
is the correct implementation.
import math as m
print(dir())
m.ceil(2.3)
['In', 'Out', '_', '_2', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'm', 'math', 'quit']
3
from math import ceil
OR from math import ceil, floor
¶
- We can use the
from
keyword to import specific name(s) from a module instead of importing the entire contents of a module. This way we don't have to use the dot operator and can access the function directly by its name
from math import ceil
print(dir())
ceil(2.3)
['In', 'Out', '_', '_2', '_3', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_i4', '_ih', '_ii', '_iii', '_oh', 'ceil', 'exit', 'get_ipython', 'm', 'math', 'quit']
3
from mymath import *
¶
- We can import all the attributes from a module using asterik
*
construct. The difference betweenimport math
andfrom math import *
is that in the later case you can don't have to use the dot operator and can directly use the functions, e.g.,ceil()
from math import *
print(dir())
ceil(2.3)
['In', 'Out', '_', '_2', '_3', '_4', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_i4', '_i5', '_ih', '_ii', '_iii', '_oh', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exit', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'get_ipython', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'm', 'math', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'quit', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
3
math
Module¶math
module¶math.pi
3.141592653589793
math.tau
6.283185307179586
e
is also an irrational number with approximated value of 2.718math.e
2.718281828459045
math.inf
(added to Python 3.5) is a special data type equivalent to a floatmath.inf
inf
type(math.inf)
float
# Proof of concept: Positive infinity is greater than any highest known number
math.inf > 99993999999999929999999999456748884839999883
True
# Proof of concept: Negative infinity is smaller than any smallest known number
-math.inf < -91876999999999999999999999954309873211234
True
# Proof of concept: Whatever number is added/subtracted to positive infinity, the result is positive infinity
math.inf + 324987699999999543
inf
# Proof of concept: Whatever number is subtracted/added from negative infinity, the result is negative infinity
-math.inf - 324999999999876543
-inf
NaN
value can be due to invalid inputs, or it can indicate that a variable that should be numerical has been corrupted by text characters or symbolsmath.nan
nan
type(math.nan)
float
def fact_loop(num):
if num < 0:
return 0
if num == 0:
return 1
factorial = 1
for i in range(1, num + 1):
factorial = factorial * i
return factorial
fact_loop(50)
30414093201713378043612608166064768844377641568960512000000000000
def fact_recursion(num):
if num < 0:
return 0
if num == 0:
return 1
return num * fact_recursion(num - 1)
fact_loop(50)
30414093201713378043612608166064768844377641568960512000000000000
import math
math.factorial(50)
30414093201713378043612608166064768844377641568960512000000000000
Lets compare the execution time of calculating factorial using above three ways, using the timeit()
method which returns the time taken to execute the statements a specified number of times
timeit.timeit(stmt, setup, globals, number)
Where
stmt
: Code statement(s) whose execution time is to be measured.(Use ; for multiple statements)setup
: Used to import some modules or declare some necessary variables. (Use ; for multiple statements)globals
: You can simplay pass globals()
to the globals parameter, which will cause the code to be executed within your current global namespacenumber
: It specifies the number of times stmt will be executed. (Default is 1 million times)import timeit
timeit.timeit("fact_loop(10)", globals=globals(), number = 1000000)
0.7165604919999993
timeit.timeit("fact_recursion(10)", globals=globals(), number = 1000000)
1.374110029999997
timeit.timeit("math.factorial(10)", setup = "import math", number = 1000000)
0.1095546269999943
import math
math.ceil(20.222), math.ceil(-11.85)
(21, -11)
import math
math.floor(20.99), math.floor(-13.1)
(20, -14)
import math
math.trunc(20.99), math.trunc(-13.1)
(20, -13)
# perm(n,k) = n!/(n-k)!
import math
math.perm(3,2)
#All permutations made by with letters a, b, c by taking two at a time are six (ab, ba, ac, ca, bc, cb)
6
# comb(n,k) = n!/k!(n-k)!
import math
math.comb(3,2)
#All combinations made by with letters a, b, c by taking two at a time are three (ab, ac, bc)
3
import math
math.gcd(39,27), math.gcd(100,50)
(3, 50)
import math
math.lcm(20,30) # 60. Available on Python 3.9, I have currently Python3.8 :(
60
# Example: The power(a,b) function returns a**b. Available in the math module as well as Python built-in function
# The pow() function in the math module is computationally faster
import math
a = 2
b = 5
a**b , pow(a,b), math.pow(a,b)
(32, 32.0, 32.0)
# Example: The sqrt(x) function returns a number y such that y² = x;
import math
math.sqrt(25)
5.0
# Example: The exp(x) function returns e**x, where e is Euler's number (2.718281828459045)
import math
x = 3
math.e ** x, math.exp(x)
(20.085536923187664, 20.085536923187668)
# Example: The log(x, base) function return the logarithm of x to the mentioned base. Default base is e
# Logarithm is the inverse function to exponentiation
math.log(8), math.log(8, math.e), math.log(8, 2), math.log(8, 10)
(2.0794415416798357, 2.0794415416798357, 3.0, 0.9030899869919434)
# Six functions of an angle: sin(), cos(), tan(), asin(), acos(), atan().
# The angle given to these functions should be in radians. A circle has 360 degrees and 2pi radians
import math
math.sin(0), math.sin(3.14)
(0.0, 0.0015926529164868282)
# Examples of Hyperbolic Functions: sinh(), cosh(), tanh(), asinh(), acosh(), atanh()
import math
math.sinh(3.14)
11.53029203041011
random
Module¶random
module¶#import random module
import random
# use dir() to get the list of complete functions in random module
print("Existing functions in Random module: \n\n", dir(random))
Existing functions in Random module: ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
random.random()
Function¶random.random()
returns a random float value in the interval [0,1), i.e., 0 is inclusive while 1 is notrv = random.random()
rv
0.3315107182746464
random.uniform()
Function¶random.uniform(a, b)
returns a random float value in the interval a and brv = random.uniform(0, 100)
rv
29.74603093979552
random.randint(1,100)
4
random.randint()
and random.randrange()
Functions¶range()
function that provides a range object, using which you can generate a list of numbers in that specific rangerandom.randint(start, stop)
returns one random integer (with start and stop both inclusive).random.randrange(start, stop=None, step=1)
returns one random integer (with stop NOT inclusive).# Note the stop value is also inclusive, which is unlike what we expect in Python
import random
random.randint(0, 5)
5
# This fixes the problem and does not include the endpoint
random.randrange(0, 5)
4
random.choice()
Function¶random.choice(seq)
import random
# select a random element from a list
list1 = ['Arif', 'Rauf', 'Mujahid']
print("Random element from list: ", random.choice(list1))
# select a random character from a string
string = "HappyLearning"
print("Fetching Random item from string: ", random.choice(string))
# select a random item from a tuple
tuple1 = (1, 2, 3, 4, 5)
print("Fetching Random element from Tuple: ",random.choice(tuple1))
Random element from list: Arif Fetching Random item from string: y Fetching Random element from Tuple: 1
With this background, one should be able explore other methods as and when required
time
, datetime
, and calendar
modules, let me put the stage right by having a brief discussion on the concept of time and time zones:!ls /usr/share/zoneinfo/
+VERSION Canada GB Iran NZ-CHAT UCT Africa Chile GB-Eire Israel Navajo US America Cuba GMT Jamaica PRC UTC Antarctica EET GMT+0 Japan PST8PDT Universal Arctic EST GMT-0 Kwajalein Pacific W-SU Asia EST5EDT GMT0 Libya Poland WET Atlantic Egypt Greenwich MET Portugal Zulu Australia Eire HST MST ROC iso3166.tab Brazil Etc Hongkong MST7MDT ROK leapseconds CET Europe Iceland Mexico Singapore posixrules CST6CDT Factory Indian NZ Turkey zone.tab
!ls /usr/share/zoneinfo/Asia
Aden Chongqing Jerusalem Novokuznetsk Tashkent Almaty Chungking Kabul Novosibirsk Tbilisi Amman Colombo Kamchatka Omsk Tehran Anadyr Dacca Karachi Oral Tel_Aviv Aqtau Damascus Kashgar Phnom_Penh Thimbu Aqtobe Dhaka Kathmandu Pontianak Thimphu Ashgabat Dili Katmandu Pyongyang Tokyo Ashkhabad Dubai Khandyga Qatar Tomsk Atyrau Dushanbe Kolkata Qostanay Ujung_Pandang Baghdad Famagusta Krasnoyarsk Qyzylorda Ulaanbaatar Bahrain Gaza Kuala_Lumpur Rangoon Ulan_Bator Baku Harbin Kuching Riyadh Urumqi Bangkok Hebron Kuwait Saigon Ust-Nera Barnaul Ho_Chi_Minh Macao Sakhalin Vientiane Beirut Hong_Kong Macau Samarkand Vladivostok Bishkek Hovd Magadan Seoul Yakutsk Brunei Irkutsk Makassar Shanghai Yangon Calcutta Istanbul Manila Singapore Yekaterinburg Chita Jakarta Muscat Srednekolymsk Yerevan Choibalsan Jayapura Nicosia Taipei
On all UNIX based systems (Mac, Linux), TZ
is an environment variable that can be set to any of the above files to get the date of that appropriate zone. By default the system is configured to set it to the local time of the country
! date
Sun Dec 26 11:36:12 PKT 2021
! TZ=Asia/Karachi date
Sun Dec 26 11:36:13 PKT 2021
! TZ=Asia/Calcutta date
Sun Dec 26 12:06:13 IST 2021
! TZ=America/Los-Angeles date
Sun Dec 26 06:36:14 UTC 2021
time
Module¶time
module¶import time
# use dir() to get the list of complete functions in time module
print("Existing functions in time module: \n\n", dir(time))
Existing functions in time module: ['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'monotonic_ns', 'perf_counter', 'perf_counter_ns', 'process_time', 'process_time_ns', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'time_ns', 'timezone', 'tzname', 'tzset']
(i) The time.sleep(seconds)
method is used to delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision.
import time
print("This is printed immediately.")
time.sleep(4)
print("This is printed after 4 seconds.")
This is printed immediately. This is printed after 4 seconds.
(ii) The time.time()
method returns the current time in seconds since UNIX Epoch (00:00:00 UTC on 1 January 1970).
import time
seconds = time.time()
seconds
1640500598.971924
#Get time using shell command
!date
Sun Dec 26 11:36:39 PKT 2021
!date +%s
1640500600
(iii) The time.ctime(seconds)
method takes seconds passed since epoch as argument and returns a string representing local time.
import time
dtg1 = time.ctime(0)
dtg1
'Thu Jan 1 05:00:00 1970'
import time
seconds = time.time()
dtg2 = time.ctime(seconds)
dtg2
'Sun Dec 26 11:36:46 2021'
With this background, one should be able explore other methods as and when required
datetime
Module¶datetime
module can support many of the same operations as time
module, but provides a more object oriented set of types, and also has some limited support for time zones.datetime
module¶# import datetime module
import datetime
# use dir() to get the list of complete functions in datetime module
print("Existing functions in datetime module: \n\n", dir(datetime))
Existing functions in datetime module: ['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']
#The smallest year number allowed in a date or datetime object. MINYEAR is 1.
datetime.MINYEAR
1
#The largest year number allowed in a date or datetime object. MAXYEAR is 9999.
datetime.MAXYEAR
9999
(i) The datetime.datetime.today()
and datetime.datetime.now()
methods return a datetime object as per the time zone of the system
dtg = datetime.datetime.today()
dtg
datetime.datetime(2021, 12, 26, 11, 36, 59, 584624)
dtg = datetime.datetime.now()
dtg
datetime.datetime(2021, 12, 26, 11, 36, 59, 921566)
(ii) Let us explore some commonly used attributes related with the datetime
object.
dtg.year:
returns the yeardtg.month:
returns the monthdtg.day:
returns the datedtg.hour:
returns the hourdtg.minute:
returns the minutesdtg.second:
returns the secondsdtg.microsecond:
returns the microsecondsdtg.year
2021
dtg.day
26
dtg.hour
11
dtg.microsecond
921566
(iii) The datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
method is used to create a datetime
object
import datetime
dtg = datetime.datetime(2021,12,31)
print(dtg)
print(type(dtg))
2021-12-31 00:00:00 <class 'datetime.datetime'>
dtg = datetime.datetime(2021, 12, 31, 4, 30, 54, 678)
dtg
datetime.datetime(2021, 12, 31, 4, 30, 54, 678)
(iv) The datetime.time([hour[, minute[, second[, microsecond[, tzinfo]]]]])
methods returns a time
object.
t1 = datetime.time(10, 15, 54, 247)
print(t1)
print(type(t1))
10:15:54.000247 <class 'datetime.time'>
With this background, one should be able explore other methods as and when required
calendar
Module¶cal
program, and provides additional useful functions related to the calendar. By default, these calendars have Monday as the first day of the week, and Sunday as the lastcalendar
module¶import calendar
# use dir() to get the list of complete functions in calendar module
print("Existing functions in calendar module: \n\n", dir(calendar))
Existing functions in calendar module: ['Calendar', 'EPOCH', 'FRIDAY', 'February', 'HTMLCalendar', 'IllegalMonthError', 'IllegalWeekdayError', 'January', 'LocaleHTMLCalendar', 'LocaleTextCalendar', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'TextCalendar', 'WEDNESDAY', '_EPOCH_ORD', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_colwidth', '_locale', '_localized_day', '_localized_month', '_monthlen', '_nextmonth', '_prevmonth', '_spacing', 'c', 'calendar', 'datetime', 'day_abbr', 'day_name', 'different_locale', 'error', 'firstweekday', 'format', 'formatstring', 'isleap', 'leapdays', 'main', 'mdays', 'month', 'month_abbr', 'month_name', 'monthcalendar', 'monthrange', 'prcal', 'prmonth', 'prweek', 'repeat', 'setfirstweekday', 'sys', 'timegm', 'week', 'weekday', 'weekheader']
# calendar() method to print the calendar of whole year
cy = calendar.calendar(2021)
print(cy)
2021 January February March Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7 4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14 11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21 18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28 25 26 27 28 29 30 31 29 30 31 April May June Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 1 2 1 2 3 4 5 6 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 31 July August September Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 1 1 2 3 4 5 5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12 12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19 19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26 26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30 30 31 October November December Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26 25 26 27 28 29 30 31 29 30 27 28 29 30 31
import calendar
# month() method is used to print calendar of specific month
#print calendar of November 2021
c = calendar.month(2021,11)
print(c)
November 2021 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import calendar
# can check wether the year is leap year or not
print("2021 is leap year: ", calendar.isleap(2021))
print("2020 was be leap year: ", calendar.isleap(2020))
2021 is leap year: False 2020 was be leap year: True
With this background, one should be able explore other methods as and when required
os
Module¶os
module¶import os
# to get the list of complete functions in OS module
print("Existing functions in OS module: \n\n", dir(os))
Existing functions in OS module: ['CLD_CONTINUED', 'CLD_DUMPED', 'CLD_EXITED', 'CLD_TRAPPED', 'DirEntry', 'EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_LOCK', 'F_OK', 'F_TEST', 'F_TLOCK', 'F_ULOCK', 'MutableMapping', 'NGROUPS_MAX', 'O_ACCMODE', 'O_APPEND', 'O_ASYNC', 'O_CLOEXEC', 'O_CREAT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_EXLOCK', 'O_NDELAY', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_SHLOCK', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'POSIX_SPAWN_CLOSE', 'POSIX_SPAWN_DUP2', 'POSIX_SPAWN_OPEN', 'PRIO_PGRP', 'PRIO_PROCESS', 'PRIO_USER', 'P_ALL', 'P_NOWAIT', 'P_NOWAITO', 'P_PGID', 'P_PID', 'P_WAIT', 'PathLike', 'RTLD_GLOBAL', 'RTLD_LAZY', 'RTLD_LOCAL', 'RTLD_NODELETE', 'RTLD_NOLOAD', 'RTLD_NOW', 'R_OK', 'SCHED_FIFO', 'SCHED_OTHER', 'SCHED_RR', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'ST_NOSUID', 'ST_RDONLY', 'TMP_MAX', 'WCONTINUED', 'WCOREDUMP', 'WEXITED', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WNOWAIT', 'WSTOPPED', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_spawnvef', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chflags', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'cpu_count', 'ctermid', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'environb', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'get_blocking', 'get_exec_path', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getegid', 'getenv', 'getenvb', 'geteuid', 'getgid', 'getgrouplist', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getpriority', 'getsid', 'getuid', 'initgroups', 'isatty', 'kill', 'killpg', 'lchflags', 'lchmod', 'lchown', 'linesep', 'link', 'listdir', 'lockf', 'lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'popen', 'posix_spawn', 'posix_spawnp', 'pread', 'putenv', 'pwrite', 'read', 'readlink', 'readv', 'register_at_fork', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sched_get_priority_max', 'sched_get_priority_min', 'sched_yield', 'sendfile', 'sep', 'set_blocking', 'set_inheritable', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setpriority', 'setregid', 'setreuid', 'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'st', 'stat', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sync', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'terminal_size', 'times', 'times_result', 'truncate', 'ttyname', 'umask', 'uname', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'walk', 'write', 'writev']
os.getcwd()
and os.listdir()
Function¶os.getcwd()
return a unicode string representing the current working directory.os.listdir(path=None)
return a list containing the names of the files in the pwd in arbitrary order. Does not display '.' and '..' directories. An optional path can be specifiedimport os
# getcwd() function is used to return the current working directory
cwd = os.getcwd()
print("Current working directory:\n", cwd )
# lisdir() function is used to return the contents of current working directory
mylist = os.listdir(os.getcwd())
print("\nContents of directory: \n", mylist )
os.chdir()
Function¶os.chdir(path)
function is used to change the current working directory to the specified path.import os
print("Get current working directory:\n", os.getcwd())
os.chdir('/Users/arif/')
#os.chdir('C:\\Users\Arif\Desktop')
print("Get current working directory again:\n", os.getcwd())
os.mkdir()
and os.rmdir()
Function¶os.mkdir(path)
funcion creates a new directoryos.rmdir(path)
funcion removes a directoryimport os
os.chdir('/Users/arif/Documents/')
list1 = os.listdir(os.getcwd())
print("Contents of directory: ", list1)
os.mkdir("ANewDir")
list2 = os.listdir(os.getcwd())
print("Contents of directory: ", list2)
os.rmdir("ANewDir")
list3 = os.listdir(os.getcwd())
print("Contents of directory: ", list3)
help(os.system)
os.system()
Function¶os.system(command)
method is used to execute the command in a subshellimport os
os.system('ls -l /Users/')
print("\n")
os.system('echo "This is getting more and more interesting"')
print("\n")
os.system('date')
Students should explore other functions like chmod()
, chown()
, fstat()
, getpid()
, getuid()
threading
Module¶threading
module¶# Python program to illustrate the concept of threading
import threading
import os
import time
def display1():
for i in range(4):
print("\nI am Thread1 and my PID is {}". format(os.getpid()))
time.sleep(2)
def display2():
for i in range(4):
print("\nI am Thread2 and my PID is {}". format(os.getpid()))
time.sleep(2)
# creating thread
t1 = threading.Thread(target=display1)
t2 = threading.Thread(target=display2)
t1.start()
t2.start()
# wait until thread 1 is completely executed
t1.join()
# wait until thread 2 is completely executed
t2.join()
# both threads completely executed
print("Done!")
I am Thread1 and my PID is 28244 I am Thread2 and my PID is 28244 I am Thread1 and my PID is 28244 I am Thread2 and my PID is 28244 I am Thread2 and my PID is 28244 I am Thread1 and my PID is 28244 I am Thread2 and my PID is 28244 I am Thread1 and my PID is 28244 Done!
urllib
Package¶urllib
package in Python 3 is a collection of following Python modules used for working with Uniform Resource Locators:urllib.request
for opening and reading URLs, using variety of protocolsurllib.error
containing the exceptions raised by urllib.requesturllib.parse
for parsing URLsurllib.robotparser
for parsing robots.txt filesurllib
package¶
The
urllib.request.urlopen()
, may return a URLError sayingSSL: CERTIFICATE_VERIFY_FAILED
. To handle this error set the_create_default_https_context
attribute ofssl
to_create_unverified_context
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
urllib.request.urlretrieve()
Function¶urllib.request.urlretrieve(url, filename=None)
method is used to retrieve a remote file into a temporary location on disk.import urllib
#Get the raw data url from your github gist account
myurl = 'https://gist.githubusercontent.com/arifpucit/bbcb0bba0b5c245585b375f273f17876/raw/6c64ac3e1d2ce9d91c5dfcc652387ad9c3fb6293/family.csv'
urllib.request.urlretrieve(myurl, './downloads/family.csv')
('./downloads/family.csv', <http.client.HTTPMessage at 0x7fc94692d460>)
import os
os.listdir('./downloads')
['.DS_Store', 'family.csv']
os.system('cat ./downloads/family.csv')
Names,Age,Addr Arif,50,Lahore Rauf,52,Islamabad Maaz,27,Peshawer Hadeed,22,Islamabad Mujahid,18,Karachi
0
Try answering the following questions to test your understanding of the topics covered in this notebook:
os
module in Python?os.path.exists
.os
module?os
and os.path
modules.