PYTHON LAB MANNUALstudent
PYTHON LAB MANNUALstudent
PYTHON LAB MANNUALstudent
L DEPARTMENT OF INFORMATION
TECHNOLOGY
GE8161
A
PROBLEM SOLVING AND PYTHON
B PROGRAMMING LABORATORY
LAB MANUAL
M
A
I YEAR
N AUGUST 2018 TO DECEMBER 2018
PREPARED BY APPROVED BY
U Dr.R.Priyatharshini
Mrs.M.Sowmiya
Mrs.P.Deepika
Mrs.K.Kausalya
A Mr. K. Kumaran
Ms. Suganthi HOD
L
EASWARI ENGINEERING COLLEGE
Vision
To impart Quality Education towards the holistic development of students and be a strategic
partner in the Industrial Advancement arena and emerge as a 'Center of Excellence for Higher
Studies' in the specialization of Information Technology.
Mission
Create, select, and apply appropriate techniques, resources, modern engineering and IT tools
including prediction and modelling to complex engineering activities with an understanding of the
limitations.
Manage complex IT projects with consideration of the human, financial, ethical and environmental
factors and an understanding of risk management processes, and operational and policy implications.
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY LTPC
0042
OBJECTIVES
To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.
LIST OF PROGRAMS
PLATFORM NEEDED
Python 3 interpreter for Windows/Linux
OUTCOMES
Regulation – 2017
COURSE OBJECTIVES:
1. Introduction to Python 4
7. Selection sort 4
8. Insertion sort 4
9. Merge sort 4
10. First n prime numbers 4
13. Find the most frequent words in a text read from a file 4
Total Hours 60
Justification of mapping:
PO1 strongly maps since it needs engineering knowledge to solve python programs. PO2
C107.1 and PO3 weakly maps to this CO since it deals with simple problems. Since Python
supports lifelong learning ability to create solutions this CO maps PO12
PO1 strongly maps since it needs engineering knowledge to solve python programs. PO3
weakly maps to this CO since it deals with simple problems. Since Python supports life long
C107.2 learning ability to create solutions this CO maps PO12
PO1, PO2,PO3 strongly maps to this CO since it deals with development of solutions by
applying engineering knowledge and problem analysis.PO5 maps with this CO since it uses
C107.3
Python platform to solve problems . Since Python supports life long learning ability to
create solutions this CO maps PO12
PO1 and PO3 strongly maps to since it needs engineering knowledge design and
C107.4 development of solutions. PO4 and PO5 maps with this CO since it uses Python platform to
solve complex problems
PO1 strongly maps to this CO since it needs detailed knowledge for handling files using
C107.5
Python.
PO4 and PO5 strongly maps. Since it uses multiple tools for creating solutions to complex
CBS1 problems. Since developing real time solutions for problems uses Python and MYSQL
supports life long learning ability so PO12 strongly maps.
PO4 and PO5 strongly maps. Since PIP is a modern tools for managing Python Add on
CBS2 softwares.PO12 strongly maps since this is used for managing all the software packages
used in Python projects.
TABLE OF CONTENTS
Record Sign
EX.NO DATE TITLE
Mark
1 Introduction to Python
6a Linear search
6b Binary search
7 Selection sort
8 Insertion sort
9 Merge Sort
11 Multiply matrices
Introduction
Python is ahigh-level, general-purpose, interpreted, interactive, object-oriented
dynamic programming language. It was created by Guido van Rossum during 1985- 1990.
Like Perl, Python source code is also available under the GNU General Public License
(GPL). Python is named after a TV Show called ‘Monty Python’s Flying Circus’ and not after
Python-the snake.
Python 3.0 was released in 2008. Although this version is supposed to be backward
incompatibles, later on many of its important features have been back ported to be
compatible with version 2.7
Installing Python
Virtually all modern programming languages make us of an IDE, or Integrated
Development Environment, which allows the creation, editing, testing, and saving of
programs and modules.
For Python programming you need a working Python installation and a text editor.
Python comes with its own editor, IDLE, which is quite nice and totally sufficient for the
beginning.
The Python download page is http://www.python.org/download. The most recent
version is Python 3.5.3 (as of January 2017);
Windows users
Download the appropriate Windows installer (the x86 MSI installer, if you do not
have a 64-bit AMD or Intel chip). Start the installer by double-clicking it and follow the
prompts.
See https://docs.python.org/3/using/windows.html#installing-python for information.
Mac users
Starting from Mac OS X Tiger, Python ships by default with the operating system,
but you will need to update to Python 3 until OS X starts including Python 3 (check the
version by starting python3 in a command line terminal). Also IDLE (the Python editor)
might be missing in the standard installation. If you want to (re-)install Python, get the
MacOS installer from the Python download site.
You are probably lucky and Python is already installed on your machine. To test it
type python3 on a command line. If you see something like what is shown in the following
section, you are set.
IDLE may need to be installed separately, from its own package such as idle3 or as
part of python-tools.
If you have to install Python, first try to use the operating system's package
manager or go to the repository where your packages are available and get Python 3.
Python 3.0 was released in December 2008; all distributions should have Python 3
available, so you may not need to compile it from scratch. Ubuntu and Fedora do have
Python 3 binary packages available, but they are not yet the default, so they need to be
installed specially.
Roughly, here are the steps to compile Python from source code in Unix (If these
totally don't make sense, you may want to read another introduction to *nix, such
as Introduction to Linux):
Download the .tgz file (use your Web browser to get the gzipped tar file
from https://www.python.org/downloads/release/python-343)
Uncompress the tar file (put in the correct path to where you downloaded it):
Change to the directory and tell the computer to compile and install the program
cd Python-3.4/
$ ./configure --prefix=$HOME/python3_install
... lots of output. Watch for error messages here ...
$ make
... even more output. Hopefully no error messages ...
$ make install
Add Python 3 to your path. You can test it first by specifying the full path. You
should add $HOME/python3_install/bin to your PATH bash variable.
$ ~/python3_install/bin/python3
Python 3.4.3 (... size and date information ...)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The above commands will install Python 3 to your home directory, which is probably
what you want, but if you skip the --prefix=$HOME/python3_install, it will install it
to /usr/local. If you want to use the IDLE graphical code editor, you need to make sure that
the tk and tcl libraries, together with their development files, are installed on the system.
You will get a warning during the make phase if these are not available.
echo %PATH%
On Windows XP:
Right-click the My Computer icon on your desktop and select Properties. Select
the Advanced tab, then click the Environment Variables... button.
Once you've brought up the environment variable editor, you'll do the same thing
regardless of which version of Windows you're running. Under System Variables in the
bottom half of the editor, find a variable called PATH. If there is is one, select it and
click Edit.... Assuming your Python root is C:\Python34, add these two folders to your path
(and make sure you get the semicolons right; there should be a semicolon between each
folder in the list):
:\Python34
C:\Python34\Scripts
$ python
Python 3.0b3 (r30b3:66303, Sep 8 2008, 14:01:02) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> is Python's way of telling you that you are in interactive mode. In interactive mode
what you type is immediately run. Try typing 1+1 in. Python will respond with 2.
Interactive mode allows you to test out and see what Python will do. If you ever feel the
need to play with new Python statements, go into interactive mode and try them out.
>>> 5
5
>>> print(5*7)
35
>>> "hello" * 4
'hellohellohellohello'
>>> "hello".__class__
<type 'str'>
if 1:
print("True")
print("Done")
If you try to enter this as written in the interactive environment, you might be surprised by
the result:
>>> if 1:
... print("True")
... print("Done")
File "<stdin>", line 3
print("Done")
^
SyntaxError: invalid syntax
What the interpreter is saying is that the indentation of the second print was
unexpected. You should have entered a blank line to end the first (i.e., "if") statement,
before you started writing the next print statement. For example, you should have entered
the statements as though they were written:
if 1:
print("True")
print("Done")
>>> if 1:
... print("True")
...
True
>>> print("Done")
Done
>>>
Interactive mode
Instead of Python exiting when the program is finished, you can use the -i flag to
start an interactive session. This can be very useful for debugging and prototyping.
python -i hello.py
Ex.No.1 Introduction to Python
Introduction
Python is ahigh-level, general-purpose, interpreted, interactive, object-oriented
dynamic programming language. It was created by Guido van Rossum during 1985- 1990.
Like Perl, Python source code is also available under the GNU General Public License
(GPL). Python is named after a TV Show called ‘Monty Python’s Flying Circus’ and not after
Python-the snake.
Python 3.0 was released in 2008. Although this version is supposed to be backward
incompatibles, later on many of its important features have been back ported to be
compatible with version 2.7
Installing Python
Virtually all modern programming languages make us of an IDE, or Integrated
Development Environment, which allows the creation, editing, testing, and saving of
programs and modules.
For Python programming you need a working Python installation and a text editor.
Python comes with its own editor, IDLE, which is quite nice and totally sufficient for the
beginning.
The Python download page is http://www.python.org/download. The most recent
version is Python 3.5.3 (as of January 2017);
Windows users
Download the appropriate Windows installer (the x86 MSI installer, if you do not
have a 64-bit AMD or Intel chip). Start the installer by double-clicking it and follow the
prompts.
See https://docs.python.org/3/using/windows.html#installing-python for information.
Mac users
Starting from Mac OS X Tiger, Python ships by default with the operating system,
but you will need to update to Python 3 until OS X starts including Python 3 (check the
version by starting python3 in a command line terminal). Also IDLE (the Python editor)
might be missing in the standard installation. If you want to (re-)install Python, get the
MacOS installer from the Python download site.
Linux, BSD, and UNIX users
You are probably lucky and Python is already installed on your machine. To test it
type python3 on a command line. If you see something like what is shown in the following
section, you are set.
IDLE may need to be installed separately, from its own package such as idle3 or as
part of python-tools.
If you have to install Python, first try to use the operating system's package
manager or go to the repository where your packages are available and get Python 3.
Python 3.0 was released in December 2008; all distributions should have Python 3
available, so you may not need to compile it from scratch. Ubuntu and Fedora do have
Python 3 binary packages available, but they are not yet the default, so they need to be
installed specially.
Roughly, here are the steps to compile Python from source code in Unix (If these
totally don't make sense, you may want to read another introduction to *nix, such
as Introduction to Linux):
Download the .tgz file (use your Web browser to get the gzipped tar file
from https://www.python.org/downloads/release/python-343)
Uncompress the tar file (put in the correct path to where you downloaded it):
Change to the directory and tell the computer to compile and install the program
cd Python-3.4/
$ ./configure --prefix=$HOME/python3_install
... lots of output. Watch for error messages here ...
$ make
... even more output. Hopefully no error messages ...
$ make install
Add Python 3 to your path. You can test it first by specifying the full path. You
should add $HOME/python3_install/bin to your PATH bash variable.
$ ~/python3_install/bin/python3
Python 3.4.3 (... size and date information ...)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The above commands will install Python 3 to your home directory, which is probably
what you want, but if you skip the --prefix=$HOME/python3_install, it will install it
to /usr/local. If you want to use the IDLE graphical code editor, you need to make sure that
the tk and tcl libraries, together with their development files, are installed on the system.
You will get a warning during the make phase if these are not available.
echo %PATH%
On Windows XP:
:\Python34
C:\Python34\Scripts
$ python
Python 3.0b3 (r30b3:66303, Sep 8 2008, 14:01:02) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> is Python's way of telling you that you are in interactive mode. In interactive mode
what you type is immediately run. Try typing 1+1 in. Python will respond with 2.
Interactive mode allows you to test out and see what Python will do. If you ever feel the
need to play with new Python statements, go into interactive mode and try them out.
>>> 5
5
>>> print(5*7)
35
>>> "hello" * 4
'hellohellohellohello'
>>> "hello".__class__
<type 'str'>
if 1:
print("True")
print("Done")
If you try to enter this as written in the interactive environment, you might be surprised by
the result:
>>> if 1:
... print("True")
... print("Done")
File "<stdin>", line 3
print("Done")
^
SyntaxError: invalid syntax
What the interpreter is saying is that the indentation of the second print was
unexpected. You should have entered a blank line to end the first (i.e., "if") statement,
before you started writing the next print statement. For example, you should have entered
the statements as though they were written:
if 1:
print("True")
print("Done")
>>> if 1:
... print("True")
...
True
>>> print("Done")
Done
>>>
Interactive mode
Instead of Python exiting when the program is finished, you can use the -i flag to
start an interactive session. This can be very useful for debugging and prototyping.
python -i hello.py
Ex.No.2 Compute GCD of two numbers
Aim:
Algorithm:
Step 1: Start
Step 5: d1=d2
Step 6: d2=remainder
Step 7: remainder=d1 % d2
Step 9: Stop
Output:
Thus the Python program to compute the GCD of two numbers was created and
executed successfully.
Ex.N0.3 Find the square root of Number(Newtons Method)
Aim:
To Write a Python program to find the square root of a number (Newton’s method)
Algorithm:
Step 1: Start
Step 2: The first parameter is the value whose square root will be approximated.
Step 3: The second is the number of times to iterate the calculation yielding a better
result from the user.
Step 6: Stop
Output:
>>> my_sqrt(16)
4.0000006366929393
Result:
Thus the Python program to find the square root of a number (Newton’s method)
was created and executed successfuly
Ex.N0.4 Exponentiation (Power of a Number)
Aim:
Algorithm:
Step 1: Start
Step 2: r->n
Step 7: Stop
Output:
Enter base: 2
Enter exponential value: 5
Result: 32
Result:
Thus the a Python program to find the exponentiation (Power of a number) was
created and executed successfully
Ex.N0.5 Find the maximum of a list of numbers
Aim:
Algorithm:
Step 1: Start
Step 6: Stop
Output:
Enter element:23
Enter element:567
Enter element:3
Thus a Python program to find the maximum of a list of numbers was created and
executed successfully.
Ex.N0.6a Linear Search
Aim:
Algorithm:
Step 1: Start
Step 4: Set i to 1
Step 8: Go to Step 2
Output:
20 found at position 3
Result:
Thus a python program to search an element was created using linear search
technique and executed successfully
Ex.N0.6b Binary Search
Aim:
Algorithm:
Step 1:Start
Step 4: Found<-False
if List[Midpoint]=ItemSought Then
ItemFound=True
SearchFailed=True
Elif List[Midpoint]>ItemSought Then
Last=Midpoint-1
Else
First=Midpoint+1
End if
End While
Step 7: Stop
Output:
Enter the list of elements: 10 15 20 24 98
20 found at position 3
Result:
Thus a python program to search an element using Binary search technique was
created and executed successfully
Ex.N0.7 Selection Sort
Aim:
Algorithm:
Step 1: Start
Step 8: Stop
Output:
Enter the elements: [55, 20, 54, 17, 31, 93, 44, 26, 77]
Thus a Python programs to sort elements using selection sort method was created
and executed successfully.
Ex.N0.8 Insertion Sort
Aim:
Algorithm:
Step 1: Start
Step 3a: The second element in the list is compared with the elements that appear
before it (only first element in this case).
Step 3b: If the second element is smaller than first element, second element is
inserted in the position of first element. After first step, first two elements of an
array will be sorted.
Step 8: Repeat step 3 until all the elements in the list is sorted
Step 9: Stop
Output:
Enter the elements: [55, 20, 54, 17, 31, 93, 44, 26, 77]
Thus a Python program to sort the elements using selection sort method was
created and executed successfully
Ex.N0.9 Merge Sort
Aim:
Algorithm:
Step 1: Start
MERGE-SORT(A,p,r)
Step 2: if r>p
Step 3: Find the middle point to divide the array into two halves:
Step 7: Stop
Output:
[1, 2, 5, 7, 8, 10]
Result:
Thus a Python programs to sort the element using merge sort method was created
and executed successfully
Ex.N0.10 Find n Prime numbers
Aim:
Algorithm:
Step 1: Start
Step 4: Let the for loop range from 2 to half number (excluding 1 and the number
itself).
Step 5: Then find the number of divisors using the if statement and increment the
count variable each time.
Step 6: If the number of divisors is lesser than or equal to 0, the number is prime.
Step 8: Stop
Output:
11
13
15
17
Result:
Thus a Python program to find the first n prime numbers was created and executed
successfully.
Ex.N0.11 Multiply Matrices
Aim:
Algorithm:
Step 1: Start
Step 4: Check the numbers of rows and column of first and second matrices
Step 5: If number of rows of first matrix is equal to the number of columns of second
matrix, go to Otherwise, print matrix multiplications are not possible and go to step
3
Step 8: Stop
Output:
Thus a Python program to multiply two matrices was created and executed
successfully
Ex.N0.12 Programs that take command line arguments (word count)
Aim:
Algorithm:
Step 1: Start
Step 3: Read each line from a file and split a lines to form a list of words
Step 5: Stop
Output:
Number of words: 12
Result:
Thus a Python program for word count was created and executed successfully
Ex.N0.13 Find the most frequent words in a text read from a file
Aim:
To write a Python program to find the most frequent words in a text read from a file
Algorithm:
Step 1: Start
Step 2: Take the file name and letter to be counted from the user
Step 2: Read each line from the file and split the line to form a list of words.
Step 3: Use a for loop to traverse through the words in the list and another for loop
to traverse through the letters in the word
Step 4: Check if the letter provided by the user and the letter encountered over
iteration is equal and if they are, increment the letter count.
Step 5: stop
Output:
Thus a Python program to find the most frequent words in a text read from a file
was created and successfully
Ex.N0.14 Simulate elliptical orbits in Pygame
Aim:
Algorithm:
Step 1: Start
Step 6:Plot the points using Scatter plot functions and draw orbit
Step 7: Stop
Output:
Result:
Thus a Python program to simulate elliptical orbits in pygame was created and
executed successfully.
Ex.N0.15 Simulate bouncing ball using Pygame
Aim:
Algorithm:
Step 1: Start
Step 4: Assign various colors for the ball and set the base color
Step 6: Write a function to move the base according to the ball position
Step 7: Stop
Output:
Result:
Thus a Python program to simulate bouncing ball using pygame was simulated
successfully
Ex.N0.16.a Introduction to PIP(Packet Manager)
pip is a package management system used to install and manage software packages written in
Python. Many packages can be found in the default source for packages and their dependencies
— Python Package Index (PyPI).[4]
Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include pip (pip3 for
Python 3) by default.[5]Starting with pip 10.0.0, python 2.6 is no longer supported. Users willing
to use pip on python 2.6 should stay on version 9.[6]
pip is a recursive acronym that can stand for either "Pip Installs Packages" or "Pip Installs
Python".[7][8] Alternatively, pip stands for "preferred installer program".
Command-line interface
Most distributions of Python come with pip preinstalled. If pip is missing, it can be installed
through the system package manager or by invoking cURL, a client-side data transfer tool:
One major advantage of pip is the ease of its command-line interface, which makes installing
Python software packages as easy as issuing one command:
Most importantly pip has a feature to manage full lists of packages and corresponding version
numbers, possible through a "requirements" file. [4] This permits the efficient re-creation of an
entire group of packages in a separate environment (e.g. another computer) or virtual
environment. This can be achieved with a properly formatted requirements.txt file and the
following command:
Install some package for a specific version python, where ${version} is replaced for 2, 3, 3.4, etc.:
Result:
Tkinter basics
Tkinter provides us with a variety of common GUI elements which we can use to build our
interface – such as buttons, menus and various kinds of entry fields and display areas. We call
these elements widgets. We are going to construct a tree of widgets for our GUI – each widget
will have a parent widget, all the way up to the root window of our application. For example, a
button or a text field needs to be inside some kind of containing window.
The widget classes provide us with a lot of default functionality. They have methods for
configuring the GUI’s appearance – for example, arranging the elements according to some kind
of layout – and for handling various kinds of user-driven events. Once we have constructed the
backbone of our GUI, we will need to customise it by integrating it with our internal application
class.
Our first GUI will be a window with a label and two buttons:
class MyFirstGUI:
def __init__(self, master):
self.master = master
master.title("A simple GUI")
def greet(self):
print("Greetings!")
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
Try executing this code for yourself. You should be able to see a window with a title, a text label
and two buttons – one which prints a message in the console, and one which closes the window.
The window should have all the normal properties of any other window you encounter in your
window manager – you are probably able to drag it around by the titlebar, resize it by dragging
the frame, and maximise, minimise or close it using buttons on the titlebar.
There are many ways in which we could organise our application class. In this example, our class
doesn’t inherit from any tkinter objects – we use composition to associate our tree of widgets with
our class. We could also use inheritance to extend one of the widgets in the tree with our custom
functions.
root.mainloop()is a method on the main window which we execute when we want to run our
application. This method will loop forever, waiting for events from the user, until the user exits
the program – either by closing the window, or by terminating the program with a keyboard
interrupt in the console.
Widget classes
There are many different widget classes built into tkinter – they should be familiar to you from
other GUIs:
A Frame is a container widget which is placed inside a window, which can have its own border
and background – it is used to group related widgets together in an application’s layout.
Toplevel is a container widget which is displayed as a separate window.
Canvas is a widget for drawing graphics. In advanced usage, it can also be used to create custom
widgets – because we can draw anything we like inside it, and make it interactive.
Text displays formatted text, which can be editable and can have embedded images.
A Button usually maps directly onto a user action – when the user clicks on a button, something
should happen.
A Label is a simple widget which displays a short piece of text or an image, but usually isn’t
interactive.
A Message is similar to a Label, but is designed for longer bodies of text which need to be
wrapped.
A Scrollbar allows the user to scroll through content which is too large to be visible all at once.
Checkbutton, Radiobutton, Listbox, Entry and Scale are different kinds of input widgets – they allow
the user to enter information into the program.
Layout options
The GUI in the previous example has a relatively simple layout: we arranged the three widgets in
a single column inside the window. To do this, we used the pack method, which is one of the
three different geometry managers available in tkinter. We have to use one of the available
geometry managers to specify a position for each of our widgets, otherwise the widget will not
appear in our window.
By default, pack arranges widgets vertically inside their parent container, from the top down, but
we can change the alignment to the bottom, left or right by using the optional side parameter. We
can mix different alignments in the same container, but this may not work very well for complex
layouts. It should work reasonably well in our simple case, however:
from tkinter import LEFT, RIGHT
# (...)
self.label.pack()
self.greet_button.pack(side=LEFT)
self.close_button.pack(side=RIGHT)
We can create quite complicated layouts with pack by grouping widgets together in frames and
aligning the groups to our liking – but we can avoid a lot of this complexity by using the grid
method instead. It allows us to position widgets in a more flexible way, using a grid layout. This
is the geometry manager recommended for complex interfaces:
# (...)
self.label.grid(columnspan=2, sticky=W)
self.greet_button.grid(row=1)
self.close_button.grid(row=1, column=1)
We place each widget in a cell inside a table by specifying a row and a column – the default row
is the first available empty row, and the default column is 0.
If a widget is smaller than its cell, we can customise how it is aligned using the sticky parameter –
the possible values are the cardinal directions (N, S, E and W), which we can combine through
addition. By default, the widget is centered both vertically and horizontally, but we can make it
stick to a particular side by including it in the sticky parameter. For example, sticky=W will cause
the widget to be left-aligned horizontally, and sticky=W+E will cause it to be stretched to fill the
whole cell horizontally. We can also specify corners using NE, SW, etc..
To make a widget span multiple columns or rows, we can use the columnspan and rowspan options
– in the example above, we have made the label span two columns so that it takes up the same
space horizontally as both of the buttons underneath it.
Custom events
So far we have only bound event handlers to events which are defined in tkinter by default – the
Button class already knows about button clicks, since clicking is an expected part of normal
button behaviour. We are not restricted to these particular events, however – we can make
widgets listen for other events and bind handlers to them, using the bind method which we can
find on every widget class.
Events are uniquely identified by a sequence name in string format – the format is described by a
mini-language which is not specific to Python. Here are a few examples of common events:
"<Button-1>", "<Button-2>" and "<Button-3>" are events which signal that a particular mouse button
has been pressed while the mouse cursor is positioned over the widget in question. Button 1 is the
left mouse button, Button 3 is the right, and Button 2 the middle button – but remember that not
all mice have a middle button.
"<ButtonRelease-1>" indicates that the left button has been released.
"<B1-Motion>" indicates that the mouse was moved while the left button was pressed (we can use
B2 or B3 for the other buttons).
"<Enter>" and "<Leave>" tell us that the mouse curson has entered or left the widget.
"<Key>" means that any key on the keyboard was pressed. We can also listen for specific key
presses, for example "<Return>" (the enter key), or combinations like "<Shift-Up>" (shift-up-
arrow). Key presses of most printable characters are expressed as the bare characters, without
brackets – for example, the letter a is just "a".