You are on page 1of 22

OPERATING SYSTEM INTERFACE

OS module in python provides a way of establishing interface with underlying


operating systems such as UNIX,Linux,MacOS,Windows etc for utilizing the
services

Methods in OS module

Method Description
os.mkdir(path) Creates new directory
os.getcwd() Returns the current working directory
os.listdir(path) Displays the list of contents for the
given path
os.rename(source,destination) Renames source directory to
destination directory
os.remove(path) Removes the file from the given path
os.rmdir(path) Removes the existing directory
os.chdir(path) Move from existing directory to
specified path
os.uname() Returns the information about the
operating system
os.system(command) Executes shell command i.e.,
command=”cls” for clearing the screen
Program explanation of os module

Output

re module (pattern matching)

Regular expression(re) module is a powerful tool used for various kinds of string
manipulation.

They are used for describing search pattern to extract information from text such
as code, files or documents etc. Python allows the users to specify meta
characters such as +,*,$,^,(),[] etc in regular expressions.
Metacharacters

Metacharacter Description
^ Matches at the beginning of the line
Eg:^welcome
Matches welcome at the start of the text
$ Matches at the end of the line
Eg:welcome$
Matches welcome at the end of the text
[] Matches any single character in brackets
Eg: [Ww]elcome
Matches Welcome or welcome
[^] Matches any single character not in brackets
Eg: [^aeiou] matches anything other than vowels
* Matches 0 or more occurrences of expression
Eg: [A-Z]*-matches 0 or more occurrences of
uppercase characters from A to Z
+ Matches 1 or more occurrences of expression
[A-Z]+-matches 1 or more occurrences of uppercase
characters from A to Z
\w Matches only alphanumeric characters
\d Matches only digits
\d{n} Matches exactly n digits
\W Matches special characters
\d{n,} Matches n or more digits
\d{n,m} Matches n and at most m digits
Methods

Method Description Example


re.match() It matches pattern at the 1)
beginning of the string. If match import re
is found it returns a match object text="Hai welcome to
otherwise None is returned everyone"
pattern="Hai"
if re.match(pattern,text):
print("Match found")
else:
print("Match not found")
Output
Match found
2)
import re
text="Hai welcome to
everyone"
pattern="welcome"
if re.match(pattern,text):
print("Match found")
else:
print("Match not found")
Output
Match not found

re.search() It searches for pattern anywhere import re


in the string. If search is text="Hai welcome to
successful it returns a match everyone"
object otherwise None is pattern="welcome"
returned if re.search(pattern,text):
print("Match found")

else:
print("Match not found")
Output
Match found
re.sub() It is used to search a pattern and import re
replace it with another pattern. It text="Hai welcome to ece
returns updated string guys.ece guys are good"
pattern="ece"
replacement="cse"
modifiedstring=re.sub(pattern
,replacement,text)
print(modifiedstring)
Output
Hai welcome to cse guys.cse
guys are good
re.findall() It is used to search a string and import re
returns a list of matches of text="Hai welcome to cse 123
pattern in string. If no match is hain 456 welcome"
found,empty list is returned pattern="[a-zA-Z]+" #matches
1 or more occurences of
either lowercase or uppercase
characters
print(re.findall(pattern,text))

Output
['Hai', 'welcome', 'to', 'cse',
'hain', 'welcome']
Sample Programs on Pattern matching

1) Write a python program to extract only the digits from the given string

Output

2) Write a python program to extract alphabets from the given string

Output
3) Write a python program to extract dates from given string

Output

4) Write a python program that extract special characters from the given
string

Output
5) Write a python program to check email address is valid or not

Output

6) Write a python program to extract words from given string

Output
math module
It is the standard module available in python which performs mathematical
operations
Method Description
math.sqrt(x) Returns the square root of x
math.pow(x,y) Returns x**y
math.sin(x) Returns the sine of x
math.cos(x) Returns the cosine of x
math.tan(x) Returns the tangent of x
math.trunc(x) Returns the truncated value of x
math.exp(x) Returns the exponent of x
log10(x) Returns the base-10 logarithm of x
factorial(x) Returns the factorial of x

Internet Access
urllib.request is a module which defines the functions and classes in
fetching the URL’s by offering a simple interface using urlopen() function.
Downloading webpage from URL
Extracting only links and images from website
BeautifulSoup is a library which is used for scraping the information from
the web pages.

Output
Data compression

It is the process of modifying or encoding the data in such a way that it consumes
less space on disk. The most commonly used modules in python for data
compression are bz2,gzip,tarfile,zipfile,zlib etc

Compression & Decompression of data using zlib module

zlib module provides compress() and decompress() for handling compression and
decompression(restoring original data)

Output

zipfile module

It is used for manipulating ZIP archived files.

Methods

Method Description
write() Adding files to archive
namelist() Listing the contents of the archive
Write a program to create zip file by adding files using zipfile module

Output

Write a program to list the contents of the zip file using zipfile module

Output
Tkinter programming

It is the standard GUI library for python which provides fast and easy way to
create GUI application.

Steps for creating GUI application

1) Import tkinter module


2) Create GUI application window.
3) Add one or more widgets to the GUI application
4) Enter the main event loop to take action against each event triggered by
the user.

Tkinter has many GUI elements(called widgets) such as buttons,


labels,radiobuttons,entry etc which can be used to build the interface.

Widgets

They are the building blocks of GUI which are used to display information or get
input from the user.

Widget Description
Label Used to display text or images
Button Used to perform an operation when
clicked on it.
Entry Used for single line text entry which can
be edited
Radiobutton Used for creating standard radio
buttons which can be used to select
only one option at a time
Checkbutton Used for selecting multiple options at a
time
Text Used to display text in multiple lines
Menubutton Used to display menus in application
Menu Used to provide various commands to
the user which are contained inside
Menubutton
Frame Used as container widget for organizing
other widgets
Canvas Used to draw shapes such as lines,
ovals, polygons and rectangles in your
application.
Listbox Used for providing list of options to the
user
Message used to display multiline text fields for
accepting values from a user.
Scale Used to provide slider widget
Scrollbar Used to create horizontal or vertical
scroll bar
Toplevel Used to provide separate window
container
Spinbox Used to select from fixed number of
values
PanedWindow container widget that may contain any
number of panes, arranged horizontally
or vertically.
Messagebox Used to display message boxes in
application
OptionMenu Creates drop down menu
1) Write a python program to set the title for tkinter window
2) Write a program to take input from the user and display the data when
button is clicked
3) Write a python program to demonstrate Radiobutton widget
4) Write a program to demonstrate OptionMenu widget
Graphics using Turtle

turtle is module which allows to draw intricate shapes and pictures all over the
screen

Methods

Methods Description
turtle.forward() Moves turtle forward by the specified
steps
turtle.backward() Moves turtle backward by the specified
steps
turtle.shape() Changes the shape of the turtle
turtle.color() Sets the color of the turtle
turtle.exitonclick() Closes the window when clicking on the
turtle
turtle.showturtle() Shows the turtle on the screen
turtle.hideturtle() Hide the turtle from the screen
turtle.pendown() Draws line while moving the turtle
turtle.penup() Does not draw line while moving the
turtle
turtle.left() Turns the turtle left by specified
number of degrees
turtle.right() Turns the turtle right by specified
number of degrees
turtle.circle() Draws circle by accepting radius as the
argument
turtle.bgcolor() Changes background color of the
graphics window
turtle.fill() Fill closed shape with currently set color
turtle.pensize() Changes the thickness of turtle’s pen
1) Write a program to change the shape of turtle and moves left by 90 and
forward by 190
2) Write a program to draw rectangle
3) Write a program to draw a circle and fill it with blue color

You might also like