0% found this document useful (0 votes)
3 views3 pages

Chapter 4 - Using Python Standard Library

Chapter 4 of the Computer Science curriculum focuses on using Python's extensive standard library, which includes built-in modules for various functionalities such as mathematical operations and random number generation. It covers built-in functions, mathematical and string functions, and provides examples of how to use them in programming. The chapter also includes exercises for converting numbers and manipulating strings using these functions.

Uploaded by

Sanjeev Bora
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Chapter 4 - Using Python Standard Library

Chapter 4 of the Computer Science curriculum focuses on using Python's extensive standard library, which includes built-in modules for various functionalities such as mathematical operations and random number generation. It covers built-in functions, mathematical and string functions, and provides examples of how to use them in programming. The chapter also includes exercises for converting numbers and manipulating strings using these functions.

Uploaded by

Sanjeev Bora
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Class: XII, Sub: Computer Science, Class: 2020-21

Chapter 4 – Using Python libraries

Topic: Using Python standard Library’s functions and modules

Python’s standard library is very extensive, offering a wide range of modules and
functions. The library contains built-in modules (written in C) that provide access to
system functionality such as file I/O, that would otherwise be inaccessible to Python
programmers, as well as modules written in Python that provide standardized
solutions for many problems that occur in everyday programming. Some of the
module/libraries are math (for mathematical functions), random (for pseudo-random
number generation), urllib(for adding and suing websites’ address from within
program), etc.

Using Python built-in functions

The Python interpreter has a number of functions built into it that are always
available; we need not import any module. The built in functions are part of current
namespace of Python interpreter. To use built – in functions of Python directly as:

<function – name>( )

Ex: some built in functions are input( ), int( ), float( ), type( ), len( ), etc.

Mathematical and string functions

oct(<integer>) : returns octal string for given number

hex(<integer>) : returns hex string for a given numbers

Ex 1: Write a program that reads number, then converts it into octal and hexadecimal
equivalent numbers using built-in functions of Python.

The output produced by above program would be:


Enter a number : 17
Number Entered = 17
Octal Conversion yields 0o21
Hexadecimal Conversion yields 0x11
Ex 2: Write a program that inputs a real number and converts it it nearest integer using
two different built-in functions. It also displays the given number rounded off to 3
places after decimal.

The behaviour of rund( ) can be surprising for floats, e.g., round(0.5) and round(-0.5)
are 0, and round(1.5) is 2.

String Functions

<str>.join()

(i) If the string based iterator is a string then the <str> is inserted after every
character of the string. E.g.,

(ii) If the string based iterator is a list or tuple of strings then, the given
string/character is joined with each member of the list or tuple, but the tuple
or list must have all member as strings otherwise Python will raise an error.

<str>.split( )

(i) If we do not provide any argument to split then by default it will split the
given string considering whitespaces as a separator, e.g.,
(ii) If we provide a string or a character as an argument to split(), then the given
string is divided into parts considering the given string/character as
separator and separator character is not included in the split strings e.g.,

The given string is divided from positions containing “o”

<str>.replace( )

Word ‘Python’ has been replaced with ‘Programming’ in given string

Ex 3: Write a program that inputs a main string and then creates an encrypted string
by embedding a short symbol based string after each character. The program should
also be able to produce the dectyped string from encrypted string.

The sample run of the above program is as shown below:

You might also like