Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
39 views
Chapter 4
Uploaded by
shrishailmahajan81
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save chapter 4 For Later
Download
Save
Save chapter 4 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
39 views
Chapter 4
Uploaded by
shrishailmahajan81
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save chapter 4 For Later
Carousel Previous
Carousel Next
Save
Save chapter 4 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 18
Search
Fullscreen
‘ag Whats a Library ? ‘45 importing Modules in a Python Program ‘44. Using Python Standard Library's Functions and Modules 45 creating a Python Library INTRODUCTION ‘You all must have read and enjoyed a lot of book: books etc. And, if you recall, all these book type Don’t be ~all these book types are further divided into chapters. Can you tell, why is this done ? Yeah, you are right. Putting all the pages of one book or novel together, with no chapters, will make the book boring and difficult to comprehend. So, dividing a bigger unit into smaller manageable units is a good strategy. Similarly, in programming, if we create smaller handleable gyre units, these are called modules, A related term is library here, A story books, novels, course-books, reference have one thing in common. Confused ? Q library refers to a collection of modules that together cater to A nese i type of needs or applications ¢.g., NumPy library of sone font Ee caters to scientific computing needs, In this chapter, applications epi We shall talk about using some Python libraries as well as iting own libraries/modules.ANCE wiry . ;
) based on given string or character split strings as members word> ) ~ replaces a word or part of the Letus understand and use these string functions practically. Carefully go through the examples Of these as given below joint ) SSP) I the string based iterator is a string then the
is inserted after every character of the s points. I ndgits is not given, it returns nearest inte to its input ym, “converted to integer in2 ways as", tnun, “and”, ra) (rum, 3) loff to 3 places after decimal is, rnum2) mamber eg, “1295 can be converted to munber 12355COMPUTER SCENCE YH rng, = “este. splitQ) | @ If you do not provide any argument to split hen bby default it will split the pi ‘considering whitespace as a separator, <8 iy ‘>>>"T Love Python” .split() ‘ Wh or without whtpce. th Love’, "Python ne _ sete te at contig i th, Love Python”. split("") mg | 5 “Python | (@ If you provide. string or character a8 an argument © . | ee eats cation tie sven sneer (oF Oe Sing isnot i lit strings © para symbol based string after eac ‘Sing from encrypted Sing. “4-2. Wie a progam that inpss 2 main sing and chen hy character. The program shot ssaseasrasiasnasset Dooce Nae ot cer oma rantort ) will abwrays be les "eel sone Neb fora ba i pesies:. i Peers any eed ee tom rng, nmr the ouput “Topmentes random loting point number between 0.0 to 1.0, simply use random) 99> import random Teer peed re rn 1010) random fotng point amber been rngelocer to upper wsng nde) andor) with diference of upper limit with lower Limit i, (pper= is inportant for learning how to se Ubraries. At is generated via some algorithm or procedure and hence detest | number in the ranges 11.55 or 111_55, after "port statement, you may write stement, you may write = ‘s repeated four times. What could be the possible set of outputs y combination of digits)? are the correct possible outputs because ber N between range 0.0.<= N«< 1.0. the range becomes 0.0 to <5 range becomes 15 to < 20 yndition of range 15 to < 20. 1 possible and maximum possible numbers by following code ? -3) Solution. — minimum possible number =O i ‘maximum possible number =?mm * lle by simply giving their name $s savy ZABCDEFGHTIKLMNOPQRS TUVRY2 Boose onesie te ; 7 © hen —Swoukd mabe 7 panned ae ot rer sme and passing the string name as its Pea nodules be ote that ike other Pie ering mele, you must import pmport string = math and random. rent modules i.e, .py files) to be also be stored in the folder, evenCOMPUTER SCIENCE wy hoy and spac yon PTHON UBEARES one or more PACKIES "brary in Python ‘ brary can B3ve ate your own It om a ow you can eens 7 e shall be creating simple brag 2 apace + Coa — Natnpeone Pnettoy Dey ies xp Devcon iets Oy semana [Pyuestey pe > iseanooucage Without the Ose Saar ae Jook for subr [2 Mods.py aa 2 itp A package vs. folder | 4.5.2 Procedure For Creating Packoges In order to create a package, you need to follow a certain procedure as discussed below, Major steps to create a package are = 1 Decide about the basie structure of your package. It means that you should have a dear idea about what will be your package's name (ie, a directory/folder tunderscore-sepaated don't The Many ‘ther sys. ;portant information about PYTHONPATH, which tsps el thes) oa Python interpreter will look in when importing instance, we are going to create the ee package shown on the right | pie tof print(sys.path) command, you will find that the paseeniry Tt means that the Python interpreter will frst look in curren when trying to do an import. If the asked module/package is not rectory, then it check the directory listed in PYTHONPATH.ne PTHON UBPARES 175 Scere ge or a fle named iafOF8 {vert ris variable wri! Foy ‘aiable ayspaths TMs Tor input script (or the curr specified) le IPATH (a list of directory names: with the | © variable PATH). by iondependent defaul 5. Ater copying yun package folder in sit on become a Python libra ly Subjects List( ist() and CSX1 module "The site-packages is (p To imp import command as an one eeeare wel for documentation purposes sei be pei ren inn eae Ou Type Questions 1 OT related to a particular task and can CREATING AND 1G PA (documenta specific type of applications or (@) documentation mntaining documentation related (@) docstrings Let Us REVISE ’ — hic = oe. Fython community, Pacape son weCOMPUTER Sci : aflder to be used as 2 Python package ? 1. Which fle mast bea part of afer EET kage py (4) mate py peer LT re extension. 10. A Python ha ory @ mpy (a) mod @) imp Fai ge putt enn ry eo ” treated as importable package. : lt ater ert a bles, class definitions program. 44 Any folder having py files i a Python package | 5 Aflderhaving py files along witha special file ie, _init_py initis NOTE ¢ Answers for 0TOs are given at the end of the book. Solved Problems 1. What ise module, package and a library ? an importable Python pag. 2. What isa Python module ? What is its significance ? Solution. A “module” isa chunk of Python code that exists in its own ( intendedtabe tath-sqrt() function only “quoted string that isthe firs thing in a function rem ? What would happen there i ‘he same program ? the following ted "ow available tothe program that same name as tha ofthe module Program isignoted by Python. the range 0.0 to 1.0 and rant, b) erate random numbers beeen range ato b look) integers from the same range ? Why ?COMPUTER SCIENCE wiry, pean La 94 (whens et)! we between range 2.4, which is then ra Bet ee anyone you ike =) ree: 4, 9.06 16, om print (e102) of exceution ofthe program fom the each ofthe variables FROM and TO. det 2160 seoltings put scolding is not good 3-7" " 1 soseme print set Lost") | count 30 ssessople”) or scolding 15 ai } er ‘is matale, The code tsi SLY note program Test fe le functions) to this package folder om? Can't we use it without doing so? the package and its contents must be the default place from where Python “And printsimple count) statement causes OUP 10 ould be the outpat produced by the following code import math import random print(wath.ceil(randon.randon())) . 9. What number in the range [0.0, 1.0) but matical) this range. Thus _print(ste( int( math. pow( random. ’ (str( int( math. pow( random.COMPUTER Science CE wm ~~ ee sa situation when two different engi * Solution. There isa nameciash. A name dash © a cea par of the same Since both the modules have the Same function nth coatcee oy 15. What the pole in the ling ef de? ; Fronnath seport factorsal print (ath. factorial(S)) ; foum of impor, the pore identifier (in thi Solution. In the “froarimport” Beats ed's races are fang “Thus the statement should be Pest “ with the module name. prime(factorial(5)) GLOSSARY A) (@ baste. floordiv(7.0, 7) -mul(3, 5) () print (basic.square(3.5)) () Compute floor division of 1000.01 with 100.23 (Hint use a function multiple times). eesodue, we define vdon() = rondon-random) result if you now evaluate 7. What are the possible outcome minimum values that STRING = "CBSEONL ‘unser = random. neo hile STRING|? print (STRING{N] + STRING[NUMBER a) rw", end = '*) GP ESNEMOF (DLENOIONE (G)NSITEFLOF (ir) ECINBHISE |& Consider the following code Anport randow print(int( 20 + random. random (20 + random .randon( print(int( 22 + random. ran Find the suggested output options ae 3 in range, PICK) ata print(I, er ) nu bm pears () DELHIDELHT MUMBAIMUMBAT CCHENNATCHENNAT KOLKATAKOLKATA 19, Consider the following code = (ii) DEL MuneAr CCHENNAT KOLKATA 2 iable PICKER, Top-Level package Subpackage for File Format Subpackage For sound effects Subpackage for Filters mats wavwrite, How will formats import wavwrite. How following code ? Also specify the maximum and [case D 206) (i) DeLAT HIMUMBAL DELHIMIMBATCHENNAT (io) OeLAT MMBAIMUMBAT KOLKATAKOLKATAKOLKATA,COMPUTER Scitnice Ew Pm Mg oy Type C : Programming Practice/Knowledge based Questions 1. Write a Python program having following, functions (A function with the following, signature remove_letter(sentence, letter) This function should take a string and a letter (as a single-character strin returning a copy of that string with every instance of the indicated lette reat a a remove letter(“Hello there!”, “e”) should return the string “Hilo thy For. ea Try implementing it using,
.split( ) function. (ii) Write a function to do the following Try implementing the capwords() functionality using other functions, ie, spy and join( ). Compare the result with the capwords( ) function’s result » eta, 2 Create a module lengthconversion py that stores functions for various lengths conversion, to convert miles to kilometer “@, + miletokm( ) 4 kantomile( ) to convert kilometers to miles 4 feettoinches{ ) + inchestofeet( ) It should also store constant values such as value of (mile in kilometers and vice versa), [1 mile ~ 1.609344 kilometer ; 1 feet = 12 inches] Help( ) function should display proper information ersion eg, 3. Create a module MassConversion.py that stores function for mass conv 4 kgtotonne( ) to convert kg to tonnes 4 tonnetokg( ) to convert tonne to kg 4 kgtopound( ) to convert kg to pound 4 poundtokg( ) to convert pound to kg (Also store constants 1 kg = 0.001 tonne, 1 kg = 2.20462 pound) Help()) function should give proper information about the module. 4. Create a package from above two modules as this Conversion Length Lengthconversion. py Mass | massconversion.py Make sure that above package meets the requirements of being a Python package. Also, you shad ® able to import above package andjor its modules using import command. 3
You might also like
Using Python Libraries Class 12 Cs Python Notes
PDF
71% (7)
Using Python Libraries Class 12 Cs Python Notes
7 pages
Library 12
PDF
No ratings yet
Library 12
19 pages
Using Python Libraries
PDF
No ratings yet
Using Python Libraries
18 pages
PYHTON LIBRARIES
PDF
No ratings yet
PYHTON LIBRARIES
18 pages
Using Python Libraries
PDF
50% (2)
Using Python Libraries
101 pages
Python-Library & Modules
PDF
No ratings yet
Python-Library & Modules
4 pages
Using Python Libraries
PDF
No ratings yet
Using Python Libraries
101 pages
Using Python Libraries
PDF
No ratings yet
Using Python Libraries
15 pages
Using Python Libraries
PDF
100% (1)
Using Python Libraries
15 pages
Unit - Iii
PDF
No ratings yet
Unit - Iii
50 pages
PPS Paper Solution
PDF
No ratings yet
PPS Paper Solution
21 pages
Online Python Libraries
PDF
No ratings yet
Online Python Libraries
36 pages
Python For Data Science (Slideshare)
PDF
No ratings yet
Python For Data Science (Slideshare)
54 pages
Unit 5 Modules
PDF
No ratings yet
Unit 5 Modules
50 pages
Introduction to Python Modules
PDF
No ratings yet
Introduction to Python Modules
8 pages
Basic Python Programming
PDF
No ratings yet
Basic Python Programming
38 pages
Chapter 7
PDF
No ratings yet
Chapter 7
15 pages
RemoveWatermark PYTHON+MID2
PDF
No ratings yet
RemoveWatermark PYTHON+MID2
8 pages
3. Functions, Modules and Packages in Python
PDF
No ratings yet
3. Functions, Modules and Packages in Python
12 pages
Revised Python Modules XI CSc (1)
PDF
No ratings yet
Revised Python Modules XI CSc (1)
29 pages
Functions Ch2
PDF
No ratings yet
Functions Ch2
11 pages
Python Libraries PDF
PDF
No ratings yet
Python Libraries PDF
26 pages
Python_1_merged
PDF
No ratings yet
Python_1_merged
101 pages
Python Module
PDF
No ratings yet
Python Module
41 pages
CP Presentation
PDF
No ratings yet
CP Presentation
15 pages
CO - Lab - 6 (A)
PDF
No ratings yet
CO - Lab - 6 (A)
21 pages
M-4 (1)
PDF
No ratings yet
M-4 (1)
53 pages
CIT 101 Py Lesson 6 Final
PDF
No ratings yet
CIT 101 Py Lesson 6 Final
9 pages
Chapter 9 - Modules in Python
PDF
No ratings yet
Chapter 9 - Modules in Python
26 pages
Python Unit 04
PDF
No ratings yet
Python Unit 04
48 pages
Python
PDF
No ratings yet
Python
372 pages
2 - 3 Functions 2
PDF
No ratings yet
2 - 3 Functions 2
34 pages
Module 7 - Modules and File handling
PDF
No ratings yet
Module 7 - Modules and File handling
21 pages
Python Handwritten Notes - 1 To 2
PDF
No ratings yet
Python Handwritten Notes - 1 To 2
36 pages
Unit 2
PDF
No ratings yet
Unit 2
16 pages
Introduction To Python: 6th School On LHC Physics
PDF
No ratings yet
Introduction To Python: 6th School On LHC Physics
24 pages
Unit 4
PDF
No ratings yet
Unit 4
18 pages
04 Fundamental Jul 24
PDF
No ratings yet
04 Fundamental Jul 24
131 pages
Full Speed Python PDF
PDF
No ratings yet
Full Speed Python PDF
39 pages
chapter 6 modules
PDF
No ratings yet
chapter 6 modules
27 pages
Py QB
PDF
No ratings yet
Py QB
19 pages
Python Programming Notes
PDF
No ratings yet
Python Programming Notes
33 pages
Python Notes
PDF
No ratings yet
Python Notes
32 pages
7_Functions and Modules
PDF
No ratings yet
7_Functions and Modules
38 pages
Python U 5 One Shot Notes - 080fb705 335c 4bee Afc8 Ee7a01d3a11e
PDF
No ratings yet
Python U 5 One Shot Notes - 080fb705 335c 4bee Afc8 Ee7a01d3a11e
122 pages
Using Python Libraries-1
PDF
No ratings yet
Using Python Libraries-1
61 pages
Python
PDF
No ratings yet
Python
23 pages
DocScanner 18 May 2023 12-40 PM
PDF
No ratings yet
DocScanner 18 May 2023 12-40 PM
18 pages
Modules in Python
PDF
No ratings yet
Modules in Python
71 pages
Tokens and Data Types Notes
PDF
No ratings yet
Tokens and Data Types Notes
11 pages
Python Tutorial
PDF
No ratings yet
Python Tutorial
114 pages
Module in Python Notes
PDF
No ratings yet
Module in Python Notes
11 pages
Class 12 Chapter 4_073304
PDF
No ratings yet
Class 12 Chapter 4_073304
16 pages
Python Quick Reference
PDF
100% (1)
Python Quick Reference
3 pages
Comprehensive Guide to Python Modules and Usage
PDF
No ratings yet
Comprehensive Guide to Python Modules and Usage
16 pages
A Modular Approach To Program Organization
PDF
No ratings yet
A Modular Approach To Program Organization
51 pages
06_20241021_LibraryModules
PDF
No ratings yet
06_20241021_LibraryModules
26 pages
XII-CS-UNIT-I-Part4-PYTHON-LIBRARIES-2020-21
PDF
No ratings yet
XII-CS-UNIT-I-Part4-PYTHON-LIBRARIES-2020-21
7 pages
Chapter 3 Solved Problem
PDF
No ratings yet
Chapter 3 Solved Problem
8 pages
Current Electriciry
PDF
No ratings yet
Current Electriciry
54 pages
Unit 3 Computer Networks
PDF
No ratings yet
Unit 3 Computer Networks
98 pages
SQLTABLE2223
PDF
No ratings yet
SQLTABLE2223
49 pages
PGG GEHI COMP XI L
PDF
No ratings yet
PGG GEHI COMP XI L
22 pages
Pysql 2223
PDF
No ratings yet
Pysql 2223
12 pages
Project of Shri
PDF
No ratings yet
Project of Shri
30 pages
Jaat
PDF
No ratings yet
Jaat
1 page
Chapter 3 Class11 (2023-24) Motion in Straight Line
PDF
No ratings yet
Chapter 3 Class11 (2023-24) Motion in Straight Line
45 pages