Unique words in a file sorted alphabetically
# Unique words in a file
fp = None
try:
fp = open("fileipt.txt","r")
except IOError as e:
print("File opening error : ", e)
except:
print("unknown error")
else:
fdata = fp.read()
fdata = fdata.replace('.',"")
fdata = fdata.replace('-',"")
fdata = fdata.replace(',',"")
fdata = fdata.lower()
Words = fdata.split()
UniqueWords = list(set(Words))
list.sort(UniqueWords)
print("Number of Unique Words = ",len(UniqueWords))
print("Unique Words:")
print(UniqueWords)
finally:
if(fp):
fp.close()
"""
Sample input
Input file: fileipt.txt
File handling in Python has several functions for creating, reading, updating, and deleting files. Read - Default value. Opens a file for reading, error if the file does not exist. Append - Opens a file for appending, creates the file if it does not exist. Write - Opens a file for writing, creates the file if it does not exist. Create - Creates the specified file, returns an error if the file exists.
>python File9.py
Number of Unique Words = 36
Unique Words:
['a', 'an', 'and', 'append', 'appending', 'create', 'creates', 'creating', 'default', 'deleting', 'does', 'error', 'exist', 'exists', 'file', 'files', 'for', 'functions', 'handling', 'has', 'if', 'in', 'it', 'not', 'opens', 'python', 'read', 'reading', 'returns', 'several', 'specified', 'the', 'updating', 'value', 'write', 'writing']
"""
Modules - Arithmetic operations
# Creating modules
# Arithmetic operations
# arithopns.py
def fnadd(a,b):
return a+b
def fnsub(a,b):
return a-b
def fnmul(a,b):
return a*b
def fndiv(a,b):
return a/b
def fnmod(a,b):
return a%b
def fntruncdiv(a,b):
return a//b
# Main program
# importing modules
from arithopns import *
a = eval(input("Enter value: "))
b = eval(input("Enter value: "))
print("Sum =",fnadd(a,b))
print("Difference =",fnsub(a,b))
print("Product =",fnmul(a,b))
print("Division =",round(fndiv(a,b),4))
print("Modulo reminder =",fnmod(a,b))
print("Truncation division =",fntruncdiv(a,b))
"""
Sample output
>python modpgmAO.py
Enter value: 7
Enter value: 6
Sum = 13
Difference = 1
Product = 42
Division = 1.1667
Modulo reminder = 1
Truncation division = 1
"""
Retail shop billing
Pygame tool
PYGAME
Aim: To study about Pygame tool
Introduction to Pygame
Python is the most popular programming language and has vast libraries
for various fields such as Machine Learning (Numpy,
Pandas, Matplotlib), Artificial intelligence (Pytorch, TensorFlow), and Game
development (Pygame, Pyglet).
Pygame was officially written by Pete Shinners. Pygame is a cross-platform
set of Python modules designed for writing video games. Pygame adds
functionality on top of the SDL library. Simple DirectMedia
Layer is a cross-platform development library designed to provide low level
access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL
and Direct3D. Pygame, together with SDL, allows users to create fully featured games
and multimedia programs in the python language. Pygame is highly portable and
runs on nearly every platform and operating system.
Pygame core modules
Pygame modules provide
abstract access to specific hardware on user system, as well as uniform methods
to work with that hardware. Important Pygame Modules include
cursors |
load
cursor images, includes standard cursors |
display |
control
the display window or screen |
draw |
draw
simple shapes onto a Surface |
event |
manage
events and the event queue |
font |
create
and render TrueType fonts |
image |
save
and load images |
joystick |
manage
joystick devices |
key |
manage
the keyboard |
mouse |
manage
the mouse |
sndarray |
manipulate
sounds with numpy |
surfarray |
manipulate
images with numpy |
time |
control
timing |
transform |
scale,
rotate, and flip images |
General steps to
creating a Pygame based program
1.
Importing and Initializing PyGame
1.
import the pygame
library and initialize all the functions in
the pygame
modules. This allows pygame
to
connect its abstractions to user specific hardware.
2.
Setting Up the
Display
1.
Create a screen /
surface object on which the graphical operations will be performed.
3. Draw objects
/ Load images on the surface object
4. Create a clock and set the
framerate
5.
Sound Effects
1.
pygame
provides mixer
to handle all sound-related
activities. classes and methods in mixer provides
background music and sound effects for various actions. music
sub-module is used to stream individual
sound files in a variety of formats.
6.
Using .blit()
and .flip()
1.
blit
stands for Block Transfer. blit is the process to render the game object onto the surface. The .blit()
takes two
arguments – The Surface
to draw and the location (coordinates) at which to draw it on the
source Surface.
2.
flip(
)
updates the entire screen with everything that’s been
drawn since the last flip.
7. Setting
Up the Game Loop – The game loop performs four very important functions:
- Processing Events.
- Process user input (Key
presses, mouse movements, joystick movements) using the
pygame
event system. - All events in
pygame
are placed in the event queue, which can then be accessed and manipulated. - Updates the state of all
game objects in response to the user input
- Updates the display and
audio output
- Maintains the speed of the
game
- Every cycle of the game
loop is called a frame. The number of frames
handled each second is called the frame rate.
- Frames continue to occur
until some condition to exit the game is met. Example – The player
closes the window.
- Terminate the program.