Python Programming: 4th Edition [4 ed.] 9798321458457

Welcome to the world of Python programming! Whether you are a seasoned developer or just starting your journey into the

149 6 21MB

English Pages 419 [423] Year 2024

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Python
Programming
AKNOWLEDGEMENT
PREFACE
Python Programming
CONTENTS
Chapter 1
Getting Started with Python
The difference between
"Python Implementation" vs. "Python distribution" vs. Python
Features/Advantages of Using Python Language
Limitations/Disadvantages of Python Language
1. Difficulty in Using Other Languages
2. Weak Language in Mobile Computing
3. Gets Slow in Speed
4. Run-time Errors
5. Underdeveloped Database Access Layers
Varieties of Python Language:
Some Jargon
Chapter 2
Installing Python
https://www.python.org/downloads/
python i
Download the latest version for Windows
How to Test the Python in Microsoft Windows
Chapter 3
Interacting with Python
1. Interactive Interpreter
2. Script from the Command-line
3. Integrated Development Environment
IDLE: Pythons Integrated Development Environment (IDE)
Recently added
|L IDLE (Python 3.9 64-bit)
|L Python 3.9 Module Docs (64-bit)
B? Python 3.9 Manuals (64-bit)
The Python Shell Window
Chapter 4
Python Syntax
Multiple Statements on a Single Line
Multi-line statements
The print Statement
The print() function
Multi-Line Output.
Important: Python 3.x
The raw_input() Function
raw_input(/prompt/)
The input() Function
Chapter 5
Variables and Data Types
Strings
1. Integer (int)
Octal literals (base 8)
Hexadecimal literals (base 16)
Binary literals (base 2)
Converting Integers to their String Representation
2. Floating-point numbers
3. Complex numbers
Conversion of Number Type
Numeric Conversion Functions
Numeric Function Definitions
int(x)
float(x)
long(x)
complex(reaZ, [imagf)
Collection Functions
Date and Time
Exercises
Chapter 6
Python Basic Operators
Arithmetic Operators
Assignment Operators
Multiple Assignment Statement
Relational or Comparison Operators
Logical Operators
not
Bit Manipulation Operators
Chapter 7
Pythons Built-in Functions
The input() Function
The range() function
The print() Function
abs()
max()
type()
len()
Chapter 8
Conditional Statements
pass
The assert Statement
The if-else
Ternary Operator
else:
Chapter 9
Loops
Using the range() Function with the for Loop
Using pass Statement
Using a break statement
Using continue Statement
Nested loops
Chapter 10
User-Defined Functions
function body/statements return [expression]
Function Varieties
Arguments
Returning values
The return Statement
Default arguments and keyword arguments
The global Statement
Functions and Namespaces
Chapter 11
Useful Modules
Importing modules
Changing module names
Location
Time module
sleep
Timing things
os module - Working with files and directories
Changing the directory
Getting the current directory
Getting the files in a directory
Changing and deleting files
Copying files
More with os.path
os.walk
Quitting your program
Zip files
For getting files from the internet
Sound
Finding Modules: The Path
The exec Statement
Chapter 12
Classes and Object-Oriented Programming
Classes in ^Python
Semantics
Class in Python
How to define Python classes
Creating and Using Objects
Garbage Collection
Python Constructors
Customizing Objects
init Method
del Method
repr Method
str Method
Further topics
Polymorphism
Built-in Functions
isinstance(oi/ect, type)
super(type)
Exercises
Chapter 13
Exceptions
Exceptions versus Syntax Errors
Raising an Exception
Exception Handling: The try and except Block
The else Clause
Cleaning Up After Using finally
Built-in Exceptions
exception AttributeError
exception NotlmplementedError
A Digression
Chapter 14
Multithreaded Programming
Starting a New Thread
Creating Thread Using Threading Module
Synchronizing Threads
Multithreaded Priority Queue
Chapter 15
Text Files in Python
File Semantics
File Organization and Structure
The open Function
file.closed
file.mode
file.name
The close() Method
The write() Method
The read() Method
File Positions
Renaming and Deleting Files
The remove() Method
Directories in Python
The mkdir() Method
The chdir() Method
The getcwd() Method
The rmdir() Method
File & Directory Related Methods
Exercises
Chapter 16
Regular Expressions
What are various methods of Regular Expressions?
6. re.compile()
What are the most commonly used operators?
Output:
Output:
Chapter 17
String, List & Dictionary
Creating a string (and special characters)
>>> u"\u0041"
Strings are immutable
Formatter
>>> print("%s %s" % ("a", "b"))
Operators
Methods
Methods for string manipulation
Methods that return a modified version of the string
Methods to find position of substrings
Methods to build or decompose a string
LISTS
Quick example
Difference between appendQ and extendQ
index
insert
remove
POP
count
sort
reverse
Slicing
List comprehension
... ifi%2==0:
Filtering Lists
Lists as Stacks
Lists as Queues
> > > import copy
Inserting items into a sorted list
MAPPINGS AND DICTIONARIES
Quick example
Methods to create new dictionary
Combining dictionaries
iterators
Chapter 18
Python Pandas
2. Open and run the installer
NumPy - Introduction
Operations using NumPy
Customizing NumPy
NumPy - Ndarray Object
Sr.No. Data Types & Description
Data Type Objects (dtype)
Example 2
NumPy - Ways of Array Creation
numpy.empty
Sr.No. Parameter & Description
numpy.zeros
Parameter & Description
numpy.ones
Shape
Dtype
Order
Introduction to Data Structures
Python Pandas - Series
Create a Series from ndarray
12 c
Create a Series from diet
Create a Series from Scalar
Accessing Data from Series with Position
Retrieve Data Using Label (Index)
Python Pandas DataFrame
pandas.DataFrame
S.No Parameter & Description
Create DataFrame
Create an Empty DataFrame
Create a DataFrame from Lists
Create a DataFrame from Diet of ndarrays I Lists
Create a DataFrame from List of Diets
Column Selection
Column Addition
Column Deletion
Row Selection, Addition, and Deletion
Selection by Label
Selection by integer location
Slice Rows
Addition of Rows
Deletion of Rows
Python Pandas - Panel
pandas.Panel()
Parameter Description
Create Panel
Create an Empty Panel
Selecting the Data from Panel
Using Items
Using major_axis
Using minor_axis
Data Transferring
Saving A pandas Dataframe As A CSV
Create dataframe
Data Transferring - SQLite Databases and DataFrame
Id Name Country Active
Reading results into a pandas DataFrame
Id Name Country Active
df.to_sql("daily_flights", conn, if_exists="replace")
index id Departure arrival number route_id
Chapter 19
GUI Programming using Tkinter
Label component
Options of Label
Changing label properties
grid
Spanning multiple rows or columns
Spacing
Entry component
Getting text
Deleting text
Inserting text
Button component
lambda trick
Frame component
Images
Canvas component
Naming things, changing them, moving them, and deleting them
Text component
Statement
Description
Scale component
GUI Events
Description
Key events
Note
Event examples
Title bar
Disabling things
Getting the state of a component
Destroying things
Stopping a window from being closed
Updating
Dialogs
Menu bars
New windows
pack
StringVar
The Python Imaging Library
Using images other than GIFs with Tkinter
Chapter 20
Database Connectivity
import MySQLdb
Description
INSERT Operation
READ Operation
Update Operation
DELETE Operation
Performing Transactions
COMMIT Operation
ROLLBACK Operation
Disconnecting Database
Procedure
Handling Errors
Warning
Error
InterfaceError
DatabaseError
DataError
OperationalError
IntegrityError
InternalError
ProgrammingError
Chapter 21
The Python Library
Overview of the Python Library
tarfile
xml.dom
urllib2
CGIHTPPServer
asyncore
Chapter 22
101 Python Programs
Recommend Papers

Python Programming: 4th Edition [4 ed.]
 9798321458457

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Ajit Singh

Python Programming

ISBN-13 :979-8321458457

4th Edition

Featured With...... Pandas GUI Programming Modules & Library Database Connectivity 101 Python Programs

Copyrighted Material

AKNOWLEDGEMENT This piece of study of python is an outcome of the encouragement, guidance, help and assistance provided to us by our colleagues, faculties, Tech-friends and my family members. As an acknowledgement, I would like to take the opportunity to express my deep sense of gratitude to all those who played a crucial role in the successful completion of this book, especially to my senior scholars; this book certainly has been benefited from discussions held with many IT professionals (Ex-students) over the years it took to write it.

Python reflects on a number of growing trends in software development, putting it at or near the leading

edge of good programming languages. It is a very simple language surrounded by a vast library of add­ on modules. It is an open source project, supported by many individuals. It is an object-oriented language, binding data and processing into class definitions. It is a platform-independent, scripted language, with

complete access to operating systems APIs. It supports integration of complex solutions from pre-built components. It is a dynamic language, which avoids many of the complexities and overheads of compiled

languages.

Three Faces of a Language. There are three facets to a programming language: how you write it, what it means, and the additional practical considerations that make a program useful. While many books cover the syntax and semantics of Python, in this book, the pragmatic considerations are also covered. My core

objective is to build enough language skills with a good object-oriented design so that the students can have

an efficient knowledge about python on windows platform. Special thanks goes to our respected professor Prof. Dr. Bal Gangadhar Prasad, who is not only my role model in learning and teaching but, also a source of encouragement who has always pushed me forward to utilize my skills in a timely manner and have ever obliged for bearing with me from time to time, insisting me on sticking to my determination all throughout. I would also like to thank those who provided the odd suggestions via email to me. All feedbacks were listened to, as you will no doubt see some content influenced by your suggestions.

I hope that the reader likes this book and finds it useful in learning the concepts of python.

Thank You!!

Ajit Singh!!

PREFACE Share the knowledge........ Strengthen the sur­ roundings........

Python Simply In Depth provides all essential programming concepts and information one shall need in order to start developing their own Python program. The book provides a comprehensive walk-through of

Python programming in a clear, straightforward manner that beginners will appreciate. Important con­ cepts are introduced through a step-by-step discussion and reinforced by relevant examples and illustra­

tions. This book can be used as a guide to help explore, harness, and gain appreciation of the capabilities and features of Python.

This book encapsulates rich practical hands-on experience in developing python based applications, combined with teaching the subject for graduate/post-graduate students. The book is therefore a culmi­ nation of putting together what has been both practiced as well as preached, which is the one of the most

compelling differentiators for this book. It can also be used for independent study by anyone interested in getting a broad introduction to a core useful set of the python language.

This book isfeatured with advanced topics, such as Python Pandas, Database Connectivity, Python GUI Programming and Python Libraries apartfrom the core concepts like Multithreading, Exception Handling

and Useful Modules. We explained these because they can solve some problems better than standard solutions. My approach in this book is to regard python as a language that readers will want to use as a primary tool in many different areas of their programming work - not just for creating programs with graphical content.

Nevertheless, I recognized that visual examples are much more fun to create and work with.

This book is a close-to-complete presentation of the Python language. Whether you are new to program­ ming or a professional developer, this book’s goal is to bring you quickly up to speed on the fundamentals of the core Python language. After reading this book, you will know enough about Python to apply it in what­

ever application domains you choose to explore in your career.

Compatible with CBSE and AICTE Syllabus........

About This Forth Edition

In the four years since the first edition of this book was published in late 2019, there have been sub­ stantial changes in Python itself. Although I have attempted to retain as much of the prior version of this text as possible, this new edition reflects many recent changes in the Python language, as well as a

handful of structural changes. This Book’s Prerequisites

There are no absolute prerequisites to speak of, really. Both true beginners and crusty programming veterans have used this book successfully. If you are motivated to learn Python, this text will probably

work for you. In general, though, we have found that any exposure to programming or scripting before this book can be helpful, even if not required for every reader.

Feedback

I have tried to wash out every error in our third edition of this book after being reviewed by lots of bachelor of Computer Science, but as happens with any programming language - A few difficult to understand bugs shall

remain and of course, typos - and therefore, suggestions from students that may lead to improvement in next

edition in shortcomingfuture are highly appreciated. Constructive suggestions and criticism always go a long way in enhancing any endeavour. I request all readers

to email me their valuable comments / views /feedback for the betterment of the book at [email protected], mentioning the title and author name in the subject line. Please report any piracy spotted by you as well. I would be glad to hear suggestionsfrom you.

Python Programming 4th Edition Copyrighted Material Copyright © 2024-25 by Ajit Singh, All Rights Reserved.

No part of this publication may be reproduced, stored in a retrieval system or transmitted, in any form or by any means — electronic, mechanical, photocopying, recording or otherwise— without prior written permission from the author, except for the inclusion of brief quotations in a review.

For information about this title or to order other books and/or electronic media, contact the

publisher:

Ajit Singh - 24+ years of experience in academics. M.Phil, UGC NET Qualified

?

PYTHON INSTITUTE MnM r.to*

PCAP: Programming Essentials in Python Statement of Achievement

Presented To: Ajit Singh Warne

During the Cisco Networking Academy® self-paced course.the student has studied the following Skills: • • • • • • •

the universal concepts of computer programing (i e variables, flow control, data structures, algorrthms, condtional execution loops, functions, etc,) developer teds and the runtime environment; lire syntax and semantics of the Python language; the fundamentals d cbicct oncnlod programing and the way they arc acoprtcd m Python . the means by which to rcsdvc typ Customize installation Choose location and features

python for

windows

Use admin privileges when installing py.exe

0 Add python.exe to PATH

Cancel

Note that depending on your needs, you may also check the box to add Python to the Path. It will inform the python file installer that the executable python path folder will be added to the environment variable with the name of ‘Path’.

Finally, if the installation process is a success, the following window will appear: £• Python 3.10.5 (64-bit) Setup

Setup was successful New to Python? Start with the online tutorial and documentation. At your terminal type "py" to launch Python,

or search for Python in your Start menu.

See what's new in this release, or find more info about using Python on Windows.

S Disable path length limit Changes your machine configuration to allow programs, including Python, to bypass the 260 character "MAX-PATH'' limitation.

python for

windows Windows Configuration (Optional)

Close

Windows users will need to be sure that python.exe is on their PATH. This is done with the System control panel. Click on the Advanced tab. Click on the Environment Variables... button. Click on the System vari­

ables Path line, and click the Edit... button. This will often have a long list of items, sometimes starting with %SystemRoot%. At the end of this list, add

and the direction location of Python.exe. On my ma­

chine, I put it in D:\Softwares\Python\. For Windows programmers, the windows command interpreter uses the last letters of the file name to

associate a file with an interpreter. You can have Windows run the python.exe program whenever you dou­

ble-click a .py file. This is done with the Folder Options control panel. The File Types tab allows you to pair a file type with a program that processes the file.

Follow the below steps to add Python Path to Environment Variables. Step 1: Click on Start Button and Open the Run Program.

Step 2: Now type sysdm.cpl and click OK. This opens the System Properties Dialog Box. Step 3: Go to Advanced Tab and and Click on Environment Variables. Step 4: In the System Variable section, Select the path variable. Step 5: Click on Edit button and add python path to variable value.

Step 6: Click OK. You can get your python path in the folder where you have installed python. Example - for us it D:\Soft-

wares\Python\

How to Test the Python in Microsoft Windows The last part, it will display on how to test the result of the python installation in the previous part. It is

actually very simple as it exist in the following steps: As usual, just execute the Command Prompt.

Following after, type the command below to test whether ‘python’ is available and it is exist in the environ­ ment variable of ‘Path’:

C:\Users\Personal>python Python 3.11.2 (tags/v3.11.2:f377153, Jun 6 2022,16:14:13) [MSC v.1929 64 bit (AMD64)] on Win32 Type "help", "copyright", "credits" or "license" for more information. >>>

Last but not least, just perform another test to make sure. The following test is executing a certain com­

mand to check the version of the installed python as follows : C:\Users\Personal>python -V Python 3.11.2 C:\Users\Personal>

As it appear in the output of the above command execution, the version is matched. It is python with the

version of‘3.11.2’.

Python Editors

> Vim

http://www.vim.org/

>

Emacs See http://www.gnu.org/software/emacs/

>

SciTE http://www.scintilla.org/SciTE.html.

> MS Windows only (1) TextPad http://www.textpad.com; (2) UltraEdit http://www.ultraedit.com/.

> Jed See http://www.jedsoft.org/jed/. > jEdit Requires a bit of customization for Python See http://jedit.org. >

Geany http://www.geany.org/

Interactive Interpreters: >

python

>

ipython

>

Idle IDEs

Integrated Development Environments for Python: >

PyWin MS Windows only. Available at: http://sourceforge.net/projects/pywin3 2/

>

Kdevelop Linux/KDE See http://www.kdevelop.org/

>

Eric Linux KDE?

See http://ericide.pythonprojects.org/index.html

> WingIDE See http://wingware.com/wingide/ >

Eclipse http://eclipse.org/ There is a plug in that supports Python.

>

Emacs and SciTE will evaluate a Python buffer within the editor.

Whats new in Python 3.11.x This new version of Python mainly focuses on improving the performance of the language. A few nice features around exception handling and error display should also improve the development experience. 1. Improved performance

On average, a 25% performance gain compared to Python 3.11 is announced. Some operations are even up to 60% faster than the latest version. The start-up time is reduced by 10% on average. The measurements were performed with performance using CPython compiled with GCC under Ubuntu. The performance gain comes mainly from the specialization of some instructions when the calls to a code are repet­ itive. The fact that the code repeats itself without necessarily changing allows the interpreter to analyze the running code and replace the generic code with type-specific pieces. Another improvement comes from reducing the number of calls to the system memory in favor of allocating more space. If these performance gains are always good to take, they do not turn Python into an efficient programming language.

An increase in memory consumption of around 20% is anticipated following the implementation of these changes on CPython. Further performance gains are already planned in versions 3.12 and later. 2. Better exception handling

Special work has been done on error management. It is now possible to add annotations on exceptions.

try: raise ExceptionGroup("Exception Group", ( TypeErrorf'Type error"), KeyError("Key error"), ValueError("Value error"),

except* (ValueError, TypeError) as exc: exc.add_note("Add more information about the error") raise exc except* KeyError as exc: raise exc Similarly, a new syntax is emerging. It is now possible to create groups of exceptions and break down the use of except to catch certain exceptions contained in a group.

3. A more accurate traceback

The error messages returned by Python are now more precise and include the position of the error on the faulty line of code. This function is very similar to what other modern languages can offer.

+-------------------------I Traceback (most recent call last): I File "test.py", line 5, in test I assertx < 0 |AAAAAAAAAAAA

I AssertionError: assert 0 < 0 +-------------------------4. Improved typing

As we know, Python is a dynamically typed language, which is not always appreciated by developers. The latest Python updates have already started the integration of an optional type system. This release continues that trend by adding more types like Self, Literalstring, Required, and NotRequired. from typing import Self class Foo: def _init_ (self, x: str): self.x = x

def bar(self, y: str) -> Self: return Foo(y) 5. An update to the standard library

The standard Python library is not left out and is experiencing some changes. TOML

First of all, the tomllib module is added to the standard library to parse TOML. This addition follows support for the pyproject.toml file, added in a previous version of Python. This library remains limited in terms of functionalities and has only one purpose: file parsing.

import tomllib with open("pyproject.toml", "rb") as f: data = tomllib.load(f) print(data["project"]["name"]) AsyncIO

The asyncio library is also entitled to an update with the addition of Task Group to replace the use of the .gather() method on asynchronous operations.

import asyncio async def task 1(): print("Foo") await asyncio.sleep(5) async def task2(): print("Bar") await asyncio.sleep(2) async def main(): try: async with asyncio.TaskGroupO as task_group: task_group.create_task(task 1 ()) task_group.create_task(task2()) except* ValueError as exc: print(exc.exceptions)

if_ name_ == "__ main_ asyncio.run(main()) This update makes the syntax more pleasant, but does not bring any other major changes to the library. StrEnum

The StrEnum makes an appearance. It allows to automatically convert a character string into an Enum. from enum import StrEnum, auto class Foo(StrEnum): BAR = auto() print(Foo.BAR.value) # "bar"

Path

The Path.glob() method of pathlib now allows you to specify whether you only want to retrieve folders. from pathlib import Path p = Path("/Users/foobar/") everything = p.glob("*") dirs = p.glob("7") 6. Miscellaneous deprecation

Finally, this version brings a background cleanup and removes support for many legacy modules: aifc, chunk, msilib, pipes, telnetlib, audioop, crypt, nis, sndhdr, uu, cgi, imghdr, nntplib, spwd, xdrlib, cgitb, mailcap, ossaudiodev, sunau... Some of these modules will not be removed from the standard library until Python 3.13. Others will simply be re­ placed by alternative solutions, more modern and better maintained, already present in the standard library.

In conclusion, if this update does not bring anything revolutionary, it continues the Python projects already launched with regard to the improvement of the performances of CPython, the improvement of the error management and the addition of solutions for typing requested by part of the community.

Chapter 3

Interacting with Python

Python is a flexible and dynamic language that you can use in different ways. You can use it interactively when you simply want to test a code or a statement on a line-by-line basis or when youre exploring its

features. You can use it in script mode when you want to interpret an entire file of statements or applica­ tion program.

To use Python interactively, you can use either the Command Line window or the IDLE Development Environment.

Command Line Interaction The command line is the most straightforward way to work with Python. You can easily visualize how

Python works as it responds to every completed command entered on the

> > > prompt. It may not be the most preferred interaction with Python, but it is the simplest way to explore how Python works.

Running Python There are three different ways to start Python -

1. Interactive Interpreter From Windows

Start-> All Programs -> Python 3.11 -> Python 3.11

2. Script from the Command-line A Python script can be executed at command line by invoking the interpreter on your application, as in the

following C: >python script.py

3. Integrated Development Environment You can run Python from a Graphical User Interface (GUI) environment as well, if you have a GUI applica­ tion on your system that supports Python.

Windows - PythonWin is the first Windows interface for Python and is an IDE with a GUL From Windows

Start-> All Programs -> Python 3.11 -> IDLE

Note: If youre using GNU/Linux, UNIX, and Mac OS systems, you have to run the Terminal Tool and enter the Python command to start your session.

We use commands to tell the computer what to do. When you want Python to do something for you, you

have to instruct it by entering commands that it is familiar with. Python will then translate these com­ mands to instructions that your computer or device can understand and execute.

To see how Python works, you can use the print command to print the universal program Hello, World! Open Pythons command line.

At the >>>prompt, type the following: print(Hello, World!) Press enter to tell Python that youre done with your command. Very quickly, the command line window

will display Hello, World! on the following line:

Type "help", "copyright", "credits" or "license" for more information. »> print(”hello”> hello »>

Python responded correctly because you gave it a command in a format that it requires. To see how it responds when you ask it to print the same string using a wrong syntax for the print command, type and

enter the following command on the Python command prompt: Print(Hello, World!)

This is how Python will respond: Syntax error: invalid syntax

Youll get syntax error messages whenever you enter invalid or incomplete statements. In this case, you

typed print with a capital letter which is a big no to a case-sensitive language like Python. If youre just using Python interactively, you can do away with the print command entirely by just typing

your statement within quotes such as Hello, World!

Getting Help Python has two closely-related help modes. One is the general help utility, the other is a help function that provides the documentation on a specific object, module, function or class.

The help() Utility Help is available through the help() function. If you enter just help() you will enter the online help utility. This help utility allows you to explore the Python documentation. The interaction looks like this:

> > > help

Type help() for interactive help, or help(object) for help about object.

>>> help()

Help on a specific topic If you enter help( object) for some object, you will be given help on that specific object. This help is dis­ played using a help viewer.

Youll enter something like this: »> help("EXPRESSIONS")

Exiting Python To exit from Python, you can type any of these commands:

quit()

exit() Control-Z then press enter

IDLE: Pythons Integrated Development Environment (IDE) The IDLE (Integrated Development and Learning Environment) tool is included in Pythons installation package but you can choose to download more sophisticated third party IDEs.

The IDLE tool offers a more efficient platform to write your code and work interactively with Python. You can access IDLE on the same folder where you found the command line icon or on the start menu. As soon

as you click on the IDLE icon, it will take you to the Python Shell window. You can run a code in Python via the Python IDLE.

A quick way to find your Python IDLE on Windows is by clicking on the Start menu. You should then see the IDLE under “Recently added”.

Recently added |L IDLE (Python 3.9 64-bit) |L Python 3.9 Module Docs (64-bit)

B?

Python 3.9 Manuals (64-bit)

Once you click on the Python IDLE, you’ll see the Shell screen.

The Python Shell Window The Python Shell Window has dropdown menus and a > > > prompt that you have seen earlier in the com­

mand line window. Here you can type and enter statements or expressions for evaluation in the same way that you used the command line earlier. This time however, IDLES editing menu allows you to scroll back to

your previous commands, cut, copy, and paste previous statements and make modifications. IDLE is quite a leap from the command line interaction.

The Python Shell window has the following menu items: File, Edit, Shell, Debug, Options, Windows, and

Help. Python 3.9.0 Shell File Edit Shell Debug Options Window Help Pychon 3.9.0 (cags/v3.9.O:9cf6752, Oct 64 bit (AMD64)] on Win32 Type "help", "copyright", "credits" or on . »> I



5 2020,

15:34:40)



X

[MSC v.1927

"license()" for more informati

Ln: 3

Col: 4

The Shell and Debug menus provide capabilities you would find useful when creating larger programs.

The Shell menu allows you to restart the shell or search the shells log to find the most recent reset. The Debug Menu has useful menu items for tracing the source file of an exception and highlighting the erring line. The Debugger option will usher in an interactive debugger window that will allow you to step

through the running program. The Stack Viewer option displays the current Python stack through a new

window. The Options menu allows you to configure IDLE to suit your Python working preferences. The Help menu opens Python Help and documentation. The File Menu The items on the File menu allows you to create a new file, open an old file, open a module, and/or save your session. When you click on the New File option, you will be taken to a new window, a simple and standard text editor where you can type or edit your code. Initially, this file window is named untitled but its name

will soon change as you save your code. The windows menu bar varies only slightly with the Shell Window. It doesnt have the Shell and Debug

menu found in the Shell Window but it introduces two new menus: the Run and the Format menu. When you choose to Run your code on the file window, you can see the output on the Shell Window.

The Script Mode

When working in script mode, you wont automatically see results the way you would in interactive mood. To see an output from a script, youll have to run the script and/or invoke the print() function within your code. Python 3.9.0 Shell

File | Edit

Shell



Debug

New File

Ctrl*N

Open...

Ctrl+O

_

....

Open Module...

Options

X

Help

:9cf6752, Oct

5 2020, 15:34:40)

[MSC v.1927

"credits" or "license ()" for more informati

... ..

Alt+M

Recent Files

Window





Module Browser Alt*C Path Browser

Save

Ctrl+S

Save As...

Ctrl*Shift*S

Save Copy As...

Alt*Shift*S

Print Window

Ctrl+P

Close

Alt*F4

Exit

Ctrl+Q

Click on File and then select New File (alternatively, you may use the keyboard shortcut of Ctrl+N): You would now see the following “untitled” box, where you can type your Python code:

j untitled

X

File Edit Format Run Options Window Help

For example, type/copy the command below. This command will print the famous expression of Hello World” print ("Hello World")

This is how the syntax would look like in the “untitled” box:

“untitled* File

Edit

print

Format



Run

Options

Window

OX

Help

("Hello World")

a

Ln: 16 Col: 0

Press F5 on your keyboard. You will then get the following message to save your code:

Save Before Run or Check

Source Must Be Saved OK to Save?

Cancel

Once you’re done, press Save, and you’ll then see the “Hello World” expression printed on your Python Shell:

4 File

Python 3.9.0 Shell Edit

Shell

Debug

Options Window

Help

Hello World

Chapter 4

Python Syntax Python Coding Style: •

Use spaces per indentation and no tabs.



Do not mix tabs and spaces. Tabs create confusion and it is recommended to use only spaces.



Maximum line length: 79 characters which help users with a small display.



Use blank lines to separate top-level function and class definitions and single blank line to separate methods definitions inside a class and larger blocks of code inside functions.



When possible, put inline comments (should be complete sentences).



Use spaces around expressions and statements.

Python syntax refers to the set of rules that defines how human users and the system should write and interpret a Python program. If you want to write and run your program in Python, you must familiarize yourself with its syntax. Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

Indentation While most programming languages such as Java, C, and C++ use braces to denote blocks of code, Python programs are structured through indentation. In Python, blocks of codes are defined by indentation not as a matter of style or preference but as a rigid language requirement. This principle makes Python codes more readable and understandable.

A block of code can be easily identified when you look at a Python program as they start on the same distance to the right. If it has to be more deeply nestled, you can simply indent another block further to the right. For example, here is a segment of a program defining car_rental_cost: def car_rental_cost(days): cost =35 *days if days >= 8:

cost-= 70

elif days > = 3: cost-= 20

return cost

You have to make sure that the indent space is consistent within a block. When you use IDLE and other IDEs to input your codes, Python intuitively provides indentation on the subsequent line when you enter a statement that requires indentation. Indentation, by convention, is equivalent to 4 spaces to the right.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example if True:

print "True" else:

print "False"

Keywords Python keywords are reserved words in Python that should not be used as variable, constant, function name, or identifier in your code. Take note of these keywords if you dont want to run into errors when you

execute your program:

and

assert

raise

break

class

continue

def

try with

del else exec for

elif except

True

finally from

return while

None

global import is not print

If

yield

In lambda

False

or

pass

as

Python Identifiers A Python Identifier is a name given to a function, class, variable, module, or other objects that youll be using in your Python program. Any entity youll be using in Python should be appropriately named or iden­

tified as they will form part of your program.

Here are Python naming conventions that you should be aware of: ■

An identifier can be a combination of uppercase letters, lowercase letters, underscores, and digits (0-9). Hence, the following are valid identifiers: myClass, my_variable, var_l, and print-

_hello_world.



Special characters such as %, @, and $ are not allowed within identifiers.



An identifier should not begin with a number. Hence, 2variable is not valid, but variable 2 is acceptable.



Python is a case-sensitive language and this behavior extends to identifiers. Thus, Labor and labor are two distinct identifiers in Python.



You cannot use Python keywords as identifiers.



Class identifiers begin with an uppercase letter, but the rest of the identifiers begin in lower­

case.



You can use underscores to separate multiple words in your identifier.



You should always choose identifiers that will make sense to you even after a long gap. Hence, while it is easy to set your variable to c = 2, you might find it more helpful for future reference

if you use a longer but more relevant variable name such as count = 2.

Using Quotations Python allows the use of quotation marks to indicate string literals. You can use single, double, or triple

quotes but you must start and end the string with the same type. You would use the triple quotes when your string runs across several lines.

Python Statements Statements are instructions that a Python interpreter can execute. When you assign a value to a variable,

say my_variable = dog, youre making an assignment statement. An assignment statement may also be as short as c = 3. There are other kinds of statements in Python, like if statements, while statements,/or state­ ments, etc.

Multiple Statements on a Single Line The semicolon (;) allows multiple statements on the single line given that neither statement starts a new

code block. Here is a sample snip using the semicolon -

import sys; x = 'foo'; sys.stdout.write(x + '\n')

Multi-line statements A statement may span over several lines. To break a long statement over multiple lines, you can wrap the expression inside parentheses, braces, and brackets. This is the preferred style for handling multi-line ex­

pressions. Another way to wrap multiple lines is by using a backslash (\) at the end of every line to indicate

line continuation.

Comments When writing a program, youll find it helpful to put some notes within your code to describe what it does. A comment is very handy when you have to review or revisit your program. It will also help another programmer who might need to go over the source code. You can write comments within your program by starting the line with a hash (#) symbol. A hash symbol tells the Python interpreter to ignore the comment

when running your code. For multi-line comments, you can use a hash symbol at the beginning of each line. Alternatively, you can

also wrap multi-line comment with triple quotes.

print statement vs print() function In order to see output from a Python script, well introduce the print statement and the print() function.

The print statement is the Python 2.6 legacy construct. The print() function is a new Python 3 construct that will replace the print statement.

The print Statement The print statement takes a list of values and prints their string representation on the standard output file. The standard output is typically directed to the Terminal window.

> >>print "PI = ", 355.0/113.0 »>print65, "F" »>print(65-32)*5/9,"C"

The print() function The print() functions takes a list of values and prints their string representation on the standard output

file. The standard output is typically directed to the Terminal window. Until Python 3, we have to request the print() function with a special introductory statement: from_ fu­ ture_ import print_function.

from_ future_ import print_function

print( 65, "F") print( ( 65 - 32 ) * 5 / 9, "C") Note: Python 3.0

Python 3.0 will replace the irregular print statement with a built-in print() function that is perfectly regu­

lar, making it simpler to explain and use.

Multi-Line Output. Ordinarily, each print statement produces one line of output. You can end the print statement with a trail­

ing , to combine the results of multiple print statements into a single line. Here are two examples.

print "335/113=", print 335.0/113.0 print "Hi, Mom", "Isn't it lovely?", print 'I said, "Hi".', 42, 91056 Since the first print statement ends with a, it does not produce a complete line of output. The second print statement finishes the line of output.

from_ future_ import print_function

print( "335/113=", end="") print( 3 3 5.0/113.0) print( "Hi, Guys", "Isn't it lovely?", end="" )print( 'I said, "Hi".', 42, 91056 )

Input Functions Python provides two simplistic built-in functions to accept input and set the value of variables. These are

not really suitable for a complete application, but will do for our initial explorations.

Typically, interactive programs which run on a desktop use a complete graphic user interface (GUI), often written with the Tkinter module or the pyGTK module. Interactive programs which run over the Internet

use HTML forms. The primitive interactions were showing with input() and raw_input() are only suitable for very simple

programs.

Important: Python 3.x In Python 3, the raw_input() function will be renamed to input(). The Python 2 input() function will be removed. Its that useless.

The raw_input() Function The first way to get interactive input is the raw_input() function. This function accepts a string parameter,

which is the users prompt, written to standard output. The next line available on standard input is re­ turned as the value of the function.

raw_input(/prompt/) If a prompt is present, it is written to sys.stdout.

Input is read from sys.stdin and returned as a string. The raw_input() function reads from a file often called sys. stdin. When running from the command-line,

this will be the keyboard, and what you type will be echoed in the command window or Terminal window.

Heres an example script that uses raw_input(). a= raw_input( "yes?")

print "you said", a shares = int( raw_input("shares:")) price = float( raw_input("dollars:"))

price += float( raw_input("eights:") )/8.0

print "value", shares * price The raw_input() mechanism is very limited. If the string returned by raw_input() is not suitable for use

by int(), an exception is raised and the program stops running. Well cover exception handling in detail in

Exceptions. The input() Function In addition to the raw_input() function, which returns the exact string of characters, there is the input() function. This applies the eval() function to the input, which will typically convert numeric input to the ap­

propriate objects.

Important: Python 3 This function will be removed. Its best not to make use of it. The value of the input() function is eval( raw_input( prompt)).

Chapter 5 Variables and Data Types Variables A variable is like a container that stores values that you can access or change. It is a way of pointing to a memory location used by a program. You can use variables to instruct the computer to save or retrieve data to and from this memory location.

Python differs significantly from languages such as Java, C, or C++ when it comes to dealing with variables.

Other languages declare and bind a variable to a specific data type. This means that it can only store a unique data type. Hence, if a variable is of integer type, you can only save integers in that variable when

running your program.

Python is a lot more flexible when it comes to handling variables. If you need a variable, youll just think of a name and declare it by assigning a value. If you need to, you can change the value and data type that the

variable stores during program execution.

To illustrate these features:

In Python, you declare a variable by giving it a value: my_variable =10

Take note that when you are declaring a variable, you are not stating that the variable my_variable is equal to 10. What the statement actually means is my_variable is set to 10.

To increase the value of the variable, you can enter this statement on the command line:

> >>my_variable = my_variable + 3 To see how Python responded to your statement, invoke the print command with this statement:

> > >print(my_variable) Youll see this result on the next line: 13

To use my_variable to store a literal string yellow, youll simply set the variable to yellow:

> >>my_variable = yellow To see whats currently store in my_variable, use the print command:

> > >print(my_variable) On the next line, youll see:

yellow

Data Types Python handles several data types to facilitate the needs of programmers and application developers for workable data. These include strings, numbers, Booleans, lists, date, and time, dictionary, tuple.

Strings A string is a sequence of Unicode characters that may be a combination of letters, numbers, and special

symbols. To define a string in Python, you can enclose the string in matching single or double quotes: > >>stringl = I am enclosed in single quotes. > >>string2 = I am enclosed in double quotes. If a literal string enclosed in single quotes contains a single quote, youll have to place a backslash (\) before

the single quote within the string to escape the character. For example:

strings = It doesn't look good at all.

To print strings: print(stringS)

It doesnt look good at all. Of course, you wouldnt have to do this if you used double quotes to enclose the string:

> >>string3 = It doesnt seem nice Similarly, youll have to place a backslash before a double quote if your string is enclosed in double quotes: >> >txt = He said: \You should get the same results no matter how you choose to enclose a stringA

> > > print(txt)

He said: You should get the same results no matter how you choose to enclose a string.

Numbers Numeric Data Types One of the many conveniences of using Python is that you dont really have to declare a numeric value to distinguish its type. Python can readily tell one data type from another when you write and run your

statement. It has four built-in numeric data types. Python 3 supports three types: integer, floating-point

numbers, and complex numbers. Long integers (long) no longer form a separate group of integers but are

included in the int or integer category.

1. Integer (int) Integers are whole numbers without decimal point. They can be positive or negative as long as they dont contain a decimal point that would make a number a floating number, a distinct numeric type. Integers

have unlimited size in Python 3. The following numbers and literals are recognized by Python:

Regular integers Examples:

793,-254,4

Octal literals (base 8) To indicate an octal number, you will use the prefix Oo or OO (zero followed by either a lowercase or upper­ case letter o).

Example:

>>>a = 007

>>>print(a) 7

Hexadecimal literals (base 16) To indicate hexadecimal literals, you will use the prefix OX or Ox (zero and uppercase or lowercase letter x). Example: >>>hex_lit = OxAOC >>>print(hex_lit) 2572

Binary literals (base 2) To signify binary literals, youll use the prefix OB or Ob (zero and uppercase or lowercase b). Example:

c = Obi 100 print(c) 12

Converting Integers to their String Representation Earlier, you have seen how the print command converted literals to their equivalent in integers. Python makes it possible for you to work the other way around by converting

integers to their literal representation. To convert an integer into its string representation, you can use the

functions hex(), bin(), and oct(). Examples:

To convert the integer 7 to its octal literal, type and enter oct(7) on the command prompt. Youll get the output Oo 7: >>>oct(7)

0o7 Here is what happens when you convert the integer 2 5 7 2 to a hexadecimal literal: >>>hex(2572)

OxaOc

Finally, see what happens when you use the bin() function to convert the integer 12 to its binary string: >>>bin(12) Obi100

You can store the result to a variable by defining a variable with the hex(), bin(), and oct() functions: For example:

>>>x = hex(2572)

>>>x

OxaOc

To see the object type created and stored in the variable x, you can use and enter the command type(): >>>type(x) You should get this result:

2. Floating-point numbers Also known as floats, floating-point numbers signify real numbers. Floats are written with a decimal point that segregates the integer from the fractional numbers. They may also be written in scientific notation

where the uppercase or lowercase letter e signifies the 10th power: >>>6.2e3

6200.0 >>>6.2e2

620

3. Complex numbers Complex numbers are pairs of real and imaginary numbers. They take the form a + bj where a is a float and the real part of the complex number. On the other side is bj where b is a float and J or its lowercase indicates the square root of an imaginary number, -1. This makes b the imaginary part of the complex number.

Here are examples of complex numbers at work:

> >>a = 2 + 5j > >>b = 4 2j > >>c = a + b

>>>print(c) (6 + 3j)

Note: Complex numbers are not extensively used in Python programming.

Conversion of Number Type You can expect Python to convert expressions with mixed types of numbers to a common type to facilitate

evaluation. In some situations, however, you may have to convert one number type to another explicitly, like when the conversion is required by a function parameter. You can type the

following expressions to convert a number to another type: To convert x to a float: > > > float(x) Example: >>>float(12)

12.0

To convert x to a plain integer: int(x) >>>int(12) 12

To convert x to a complex number: type complex(x) >>>complex(12)

(12+0j)

Numeric Conversion Functions We can convert a number from one type to another. A conversion may involve a loss of precision because

weve reduced the number of bits available. A conversion may also add a false sense of precision by adding

bits which dont have any real meaning.

Well call these factory functions because they are a factory for creating new objects from other objects. The idea of factory function is a very general one, and these are just the first of many examples of this pattern.

Numeric Function Definitions There are a number of conversions from one numeric type to another.

int(x) Generates an integer from the object x. If x is a floating point number, digits to the right of the decimal

point are truncated as part of creating an integer. If the floating point number is more than about 10 digits, a long integer object is created to retain the precision. If x is a long integer that is too large to be

represented as an integer, theres no conversion. Complex values cant be turned into integers directly. If x is a string, the string is parsed to create an integer value. It must be a string of digits with an

optional sign (+ or -). int("1243") 1243

int(3.14159) 3

float(x) Generates a float from object x. If x is an integer or long integer, a floating point number is created.

Note that long integers can have a large number of digits, but floating point numbers only have approximately 16 digits; there can be some loss of precision. Complex values cant be turned into floating point numbers directly. If x is a string, the string is parsed to create an float value. It must be a string of digits with an optional

sign (+ or -). The digits can have a single decimal point (.). Also, a string can be in scientific notation and include e or E followed by the exponent as a simple

signed integer value.

float(23) 23.0

float("6.02E24")

6.0200000000000004e+24

float(22)/7

3.14285714286

long(x) Generates a long integer from x. If x is a floating point number, digits to the right of the decimal point

are truncated as part of creating a long integer. long(2) 2L

long(6.02E23)

601999999999999995805696L

long(2)**64 18446744073709551616L

complex(reaZ, [imagf) Generates a complex number from real and imag. If the imaginary part is omitted, it is 0.0. Complex is not as simple as the others. A complex number has two parts, real and imaginary. Con­

version to complex typically involves two parameters.

complex(3,2) (3 + 2j) complex(4)

(4+0j) complex(" 3+4j")

(3+4j) Note that the second parameter, with the imaginary part of the number, is optional. This leads to a

number of different ways to call this function. In the example above, we used three variations: two numeric parameters, one numeric parameter and one string parameter.

Built-In Math Functions

The bulk of the math functions are in a separate module, called math, which we will cover in The math

Module. The formal definitions of mathematical built-in functions are provided below.

abs(number) Return the absolute value of the argument, /%/.

pow(x, y, [z]) Raise x to the y power, x>. If z is present, this is done modulo z, x> mod z.

round(number, [digits]) Round number to ndigits beyond the decimal point. If the ndigits parameter is given, this is the number of decimal places to round to. If ndigits is positive,

this is decimal places to the right of the decimal point. If ndigits is negative, this is the number of places to the left of the decimal point.

Examples:

print round(678.456,2) 678.46

print round(678.456,-1) 680.0

Collection Functions These are several built-in functions which operate on simple collections of data elements.

max(value,...) Return the largest value.

> » max(l,2,3) 3

min(yalue,...) Return the smallest value. »> min(l,2,3)

1

Date and Time Most applications require date and time information to make it work efficiently and effectively. In Python,

you can use the function datetime.now() to retrieve the current date and time. The command date-

time.now() calls on a built-in Python code which gives the current date and time. To get the date and time from Python, encode the following on the command prompt: from datetime import datetime

datetime.nowO

datetime.datetime(2016, 3,10, 2,16,19, 962429) The date and time in this format is almost unintelligible and you might want to get a result that is more

readable. One way to do this is by using strftime from Pythons standard library.

Try entering these commands and see if youll get the format you like.

> > >from time import strftime > » strftime(%Y-%m-°/od %H:%M:%S) ‘

2016-03-10 02:20:03

Boolean Data Type

Comparisons in Python can only generate one of two possible responses: True or False. These data types are called booleans.

To illustrate, you can create several variables to store Boolean values and print the result:

bool.l =4 ==2*3 bool_2 = 10 < 2 * 2**3 bool_3 = 8 > 2 * 4 + 1

print(bool_l)

print(bool_2) print(bool_3) The Python Shell will display these results: False

True False

Exercises 1. Print a box like the one below.

******************* ******************* ******************* 2. Print a box like the one below.

******************* * * *******************

3. Print a triangle like the one below. * **

*** ****

Chapter 6

Python Basic Operators Python operators allow programmers to manipulate data or operands. Here are the types of operators sup­ ported by Python:

Arithmetic Operators Assignment Operators

Relational or Comparison Operators Logical Operators

Identity Operators Bitwise Operators

Membership Operators Arithmetic Operators Python does a good job of processing mathematical expressions with its basic arithmetic

operators. You can easily make programs to automate tasks such as computing tax, tips, discounts, or rent. +

Addition

adds the value of the left and right operands



Subtraction

subtracts the value of the right operand from the value of the left operand

*

Multiplica­

multiplies the value of the left and right operand

tion

1

Division

divides the value of the left operand by the right operand

**

Exponent

performs exponential calculation

%

Modulus

returns the remainder after dividing the left operand

with the right operand

Addition, subtraction, multiplication, and division are the most basic operators and are invoked by enter­

ing the following expressions: Addition:

>»1 + 3

4 Subtraction: >>>104 6

Multiplication: >>>4 * 2 8

Division: >>>10/2

5.0 Exponent

Exponential calculation is invoked by raising the first number to the power defined by the number after the ** operator: >>>2**3 2 raised to the power of 3 8

Modulus

The modulus operator gives the remainder after performing division:

>>>17% 5 2

Floor Division Floor division, on the other hand, returns the quotient after removing fractional numbers: >»17//5 3

Using Basic Operators to Compute Sales Tax, Tip, and Total Bill To put your knowledge of variables, data types, and operators to good use, you can design a simple program that will compute the sales tax and tip on a restaurant meal.

Meal cost

$65.50

Sales tax rate

6.6%

Tip

20% of meal + tax

First, set up a variable meal to store the food cost: meal = 65.50

Next, set up the tax and tip variable. Assign both variables the decimal value of the percentages given. You can do this by using 100 as divisor

tax = 6.6 1100 tip = 20/100

Your tip is based on meal cost and the added sales tax so you need to get the total amount of the meal and the sales tax. One way to do this is by simply creating a new variable to store the total cost of the meal and

tax. Another way is by reassigning the variable meal so that it stores both values: meal = meal + meal * tax

Now that you have reassigned meal to take care of the meal cost and tax, youre ready to compute for the tip. This time, you can set a new variable to store the value of the tip, meal, and tax. You can use the variable

total to hold all values: total = meal * tip

Heres your code to compute for the total bill amount: meal = 65.50

tax = 6.6 1100 tip = 20/100 meal = meal + meal * tax

total = meal + meal * tip If youre using the file editor in IDLE, you can save the file in a filename of your choice and Python

automatically appends the .py extension. As you may have noticed, the file editor will always prompt you to save your file before it does anything about your code. Just like when naming other data files and types,

you should use a filename thats descriptive of the file. In this case, a filename like Billcalculator should do the trick.

To get the total amount, go to the Python Shell and type total: >>>total 83.78760000000001

Now you have the bill amount: 83.78 760000000001 If youre using the line command window, you can simply enter the above code on a per line basis. This simple program shows how straightforward Python programming is and how useful it could be in

automating tasks. Next time you eat out, you can reuse the program by simply changing the figures on your bill calculator. Think forward and visualize how convenient it could be if you could put your code in a

bigger program that will simply ask you to input the bill amount instead of accessing the original code. You can do that with Python.

Assignment Operators These operators are useful when assigning values to variables:

Operators Function = +=

Assigns the value of the right operand to the left operand Adds the value of the right and left operand and assigns the

total to the left operand

Subtract deducts the value of the right operand from the value of the and left operand and assigns the new value to the left operand

*=

Multiply multiplies the left and right operand and assigns the product

and to the left operand

I=

Divides the left operand with the value of the right operand and assigns the quotient to the left operand

** =

Performs exponential operation on the left operand and

Exponent assigns the result to the left operand // =

Performs floor division on the left operand and assigns the

result to the left operand = Operator

You have seen this operator at work in previous chapters when you have assigned different values to vari­

ables. Examples: a=c a=b+c a=8 a=8+6

s = I love Python. + = add and

The add and (+ =) operator is simply another way to express x = x + a so that youll end up with the statement

x + = a. -= subtract and The subtract and (-=) operator is equivalent to the expression x = x a and is expressed with the statement x-

=a

*= multiply and The multiply and (*=) operator is the equivalent of the statement x = x * a and is expressed with x*=a.

/ = divide and The divide and (/=) operator is like saying x = x/a and is expressed with the statement x/=a.

%= modulus and The modulus and (%=) operator is another way to say x = x % a where youll end up instead with the expres­ sion x%= a.

//= floor division and

The floor division and is equivalent to the expression x = x//a and takes the form x//=a.

Multiple Assignment Statement The basic assignment statement can do more than assign the result of a single expression to a single vari­

able. The assignment statement can also assign multiple variables at one time. The essential rule is that the left and right side must have the same number of elements. For example, the following script has several examples of multiple assignment.

xl,yl = 2,3 #point one x2,y2 = 6,8 # point two

The del Statement

An assignment statement creates or locates a variable and then assigns a new object to the variable. This change in state is how our program advances from beginning to termination.

Python also provides a mechanism for removing variables, the del statement. The del statement looks like this:

del object ,... Each object is any kind of Python object. Usually these are variables, but they can be functions, modules or classes.

Relational or Comparison Operators Relational operators evaluate values on the left and right side of the operator and return the relation as either True or False. Here are the relational operators in Python:

Operator Meaning ==

is equal to




is greater than




=

!=

is not equal to

Examples:

8 == 6+2

is greater than or equal to

True

6 != 6 False 1 > 0 False 7 >= 5

True

Logical Operators Python supports 3 logical operators:

or

and not x or y

If the first argument, x, is false, then it evaluates the second

argument, y. Else, it evaluates x.

x and y If x is false, then it evaluates x. Else, if x is true, it evaluates y. not x

If x is false, then it returns True. If x is true, it returns False.

Examples: (8>9)and(21)and (2>9)

False

(2 = = 2) or (920)

False not(8 > 2)

False

>>> not (2 > 10)

True

Bit Manipulation Operators Weve already seen the usual math operators: +,

*, /, %, **; as well as the abs() and pow() functions. There

are several other operators available to us. Principally, these are for manipulating the individual bits of an

integer value.

Well look at ~, &, A, I, >. The unary - operator flops all the bits in a plain or long integer. Is become Os and Os become Is. Since

most hardware uses a technique called 2s complement, this is mathematically equivalent to adding 1 and switching the numbers sign.

»> print -0x12345678 -305419897

There are binary bit manipulation operators, also. These perform simple Boolean operations on all

bits of the integer at once. The binary & operator returns a 1 -bit if the two input bits are both 1.

> >> print 0&0, 1&0, l&l, 0&1 0 0 10 Heres the same kind of example, combining sequences of bits. This takes a bit of conversion to base 2 to understand whats going on.

> >> print 3&5 1 The number 3, in base 2, is 0011. The number 5 is 0101. Lets match up the bits from left to right:

00 11 0 10 1

000 1 The binary

a

operator returns a 1 -bit if one of the two inputs are 1 but not both. This is sometimes called

the exclusive or.

> > > print 3 a 5 6 Lets look at the individual bits

00 11 0 10 1

0 110

Which is the binary representation of the number 6. The binary I operator returns a 1 -bit if either of the two inputs is 1. This is sometimes called the inclusive

or. Sometimes this is written and/or.

»> print 3|5 7 Lets look at the individual bits.

00 11 0 10 1 0 111

Which is the binary representation of the number 7.

There are also bit shifting operations. These are mathematically equivalent to multiplying and dividing by powers of two. Often, machine hardware can execute these operations faster than the equivalent multiply

or divide. The < < is the left-shift operator. The left argument is the bit pattern to be shifted, the right argument is the

number of bits.

> > > print OxA < < 2 40

OxA is hexadecimal; the bits are 1 -0-1 -0. This is 10 in decimal. When we shift this two bits to the left, its like multiplying by 4. We get bits of 1-0-1-0-0-0. This is 40 in decimal.

The > > is the right-shift operator. The left argument is the bit pattern to be shifted, the right argument is the number of bits. Python always behaves as though it is running on a 2s complement computer. The left­

most bit is always the sign bit, so sign bits are shifted in.

> >> print 80 >> 3 10 The number 80, with bits of 1-0-1-0-0-0-0, shifted right 3 bits, yields bits of 1-0-1-0, which is 10 in decimal.

Exercise 1. Write a program that asks the user for a weight in kilograms and converts it to pounds. There are 2.2 pounds in a kilogram.

2. Write a program that asks the user to enter three numbers (use three separate input state-ments).

Create variables called total and average that hold the sum and average of the three numbers and print out the values of total and average.

3. A lot of cell phones have tip calculators. Write one. Ask the user for the price of the meal and the percent

tip they want to leave. Then print both the tip amount and the total bill with the tip included. 4. Write a program that generates and prints 50 random integers, each between 3 and 6. 5. Write a program that generates a random number, x, between 1 and 50, a random number y between 2 and 5, and computes

.

6. Write a program that generates a random number between 1 and 10 and prints your name that many

times.

7. Write a program that generates a random decimal number between 1 and 10 with two decimal places of accuracy. Examples are 1.23, 3.45, 9.80, and 5.00.

8. Write a program that generates 50 random numbers such that the first number is between 1 and 2, the second is between 1 and 3, the third is between 1 and 4,..., and the last is between 1 and 51. 9. Write a program that asks the user to enter two numbers, x andy, and computes hx+ yj.

10. Write a program that asks the user to enter an angle between 180 and 180 . Using an expression with the modulo operator, convert the angle to its equivalent between 0 and 360.

Chapter 7 Pythons Built-in Functions Functions provide efficiency and structure to a programming language. Python has many useful built-in functions to make programming easier, faster, and more powerful.

The input() Function Programs usually require input that can come from different sources: keyboard, mouse clicks, database,

another computers storage, or the internet. Since the keyboard is the most common way to gather input, Python provided its users the input() function. This function has an optional parameter called the prompt string. Once the input function is called, the prompt string will be displayed on the screen and the program flow

stops until the user has entered an input. The input is then interpreted and the input() function returns the

users input as a string. To illustrate, here is a sample program that collects keyboard input for name and age:

name = input(May I know your name?) print(Its a pleasure to meet you + name + !) age = input(Your age, please?)

print(So, youre + age + years old, + name + !)

Before you save the code, take a close look at the string to be printed on the second line. Youll notice that there is a blank space after you and before the double quote. This space ensures that there will be a space between you and the name input when the print command is executed. The same convention can be seen

on the 4th line with the print command where youre is separated by a single space from the age input and old is separated by a space from the name input.

Save the code as info_input.py and run it. The Python Shell will display the string on the first line: May I know your name?

A response is needed at this point and the program stops executing until a keyword input is obtained. Lets

type and enter the name Jeff to see what happens: Its a pleasure to meet you Jeff!

Your age, please?

The program has now proceeded to the next input function and is waiting for the keyboard input. Lets

enter 22 as Jeffs age and see what the program does next: So, youre 22 years old, Jeff!

The program printed the last string on the program after a keyboard response was obtained. Here is the

entire output on the Python Shell: May I know your name? Jeff

Its a pleasure to meet you Jeff!

Your age, please? 22 So, youre 22 years old, Jeff! Python 3.5.1 Shell File

Edit

Shell

Debug

Options

Window

Help

Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [M5C v.1900 32 bit (In tel)] on Win32 Type "copyright", "credits" or "license ()" for more information. »> RESTART:

May I know your name? Jeff

It’s a pleasure to meet you Jeff! Your age, please? 22 So, you're 22 years old, Jeff1

»> I

The range() function Python has a more efficient way to handle a series of numbers and arithmetic progressions and this is by using one its built-in functions: range(). The range function is particularly useful in for loops.

Here is an example of the range() function:

> >> range(5) range(0, 5) The expression range(5) above generates an iterator that progresses integers from zero and ends with 4 (5-1). To show the list of numbers, you can use the command list(range(n)):

>>>list(range(5))

[0,1, 2, 3,4] You can exercise more control over the list output by calling the range() function with two arguments: range (begin, end)

Example:

> >> range(5, 9) range(5, 9)

To show the list:

> >> list (range(5, 9)) [5, 6, 7, 8]

The above examples of range() demonstrated an increment of 1. You can change the way Python incre­

ments the number by introducing a third argument, the step. It can be a negative or positive number, but

never zero. Here is the format: range(begin, end, step)

Example:

> >> range(10, 71, 5) range(10, 71, 5) Invoking list, well see this sequence of numbers:

>>> list (range(10, 71, 5)) [10,15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70]

The print() Function Python 3 turned print from a statement into a function. Hence, you must always enclose your print param­ eters within the round parentheses.

Examples:

print(This is Python 3 print function) print(s)

print(5) The print() function can print any number of values within the parentheses; they must be separated by commas. For example: a = 3.14

b = age c = 32 print(a = , a, b, c)

The result: a = 3.14 age 32

The Python shell displayed values with blank spaces between them.

abs() The abs() function returns the absolute value of a single number. It takes an integer or float number as

argument and always returns a positive value.

Examples:

abs(-lO) 10 abs(5)

10

When complex numbers are used as argument, the abs() function returns its magnitude: >>> abs(3 + 4j) 5.0

max() The max() function takes two or more arguments and returns the largest one.

Examples: max(9,12, 6,15) 15

max(-2,-7,-35,-4)

min()

The min() function takes two or more arguments and returns the smallest item.

Examples: min(23,-109, 5, 2)

-109 min(7, 26, 0, 4)

0

type() The type() function returns the data type of the given argument.

Examples: type(This is a string) type(12) < class int>

type(2 +3j) < class complex>

len() The len() function returns the length of an object or the number of items in a list given as argument. Examples:

len(pneumonoultramicroscopicsilicovolcanoconiosi) 44

s = (winter, spring, summer, fall) len(s)

4

Here is a list of Pythons built-in functions:

abs()

all()

any()

ascii()

bin()

bool()

bytearray()

bytes()

callable()

chr()

classmethodO

compile()

complex()

delattr()

dict()

dir()

divmod()

enumerate()

eval()

exec()

filter()

float()

format()

frozenset()

getattr()

globals()

hasattr()

hash()

help()

hex()

id()

_ import_ ()

input()

int()

isinstance()

issubclass()

iter()

len()

list()

locals()

map()

max()

memoryview()

min()

next()

object()

oct()

open()

ord()

pow()

print()

property()

range()

repr()

reversed()

round()

set()

setattr()

slice()

sorted()

staticmethod()

str()

sum()

super()

tuple()

type()

vars()

zip()

Chapter 8

Conditional Statements Conditional statements are common among programming languages and they are used to perform actions

or calculations based on whether a condition is evaluated as true or false. If-then-else statements or condi­ tional expressions are essential features of programming languages and they make programs more useful to users.

The if-then-else statement in Python has the following basic structure:

if condition 1:

block 1 .statement elif condition2:

block2_statement else:

block3_statement This structure will be evaluated as:

If conditionl is True, Python will execute block 1-Statement. If conditionl is False, condition2 will be executed. If condition2 is evaluated as True, block2_statement will be executed. If condition2 turns out to

be False, Python will execute block3_statement.

To illustrate, here is an if-then-else statement built within the function your_choice: def your_choice(answer):

if answer > 5: print(You are overaged.)

elif answer < = 5 and answer > 1:

print(Welcome to the Toddlers Club!) else: print(You are too young for Toddlers Club.)

print(your_choice(6))

print(your_choice( 3)) print(your_choice( 1)) print(your_choice(0))

You will get this output on the Python Shell: You are overaged.

None Welcome to the Toddlers Club!

None You are too young for Toddlers Club.

None You are too young for Toddlers Club.

None

Conditional constructs may branch out to multiple elif branches but can only have one else branch at the end. Using the same code block, another elif statement maybe inserted to provide for privileged member of the Toddlers club: 2 year-old kids.

def your_choice(answer):

if answer > 5:

print(You are overaged.) elif answer < = 5 and answer >2:

print(Welcome to the Toddlers Club!) elif answer = = 2: print(Welcome! You are a star member of the Toddlers Club!)

else: print(You are too young for Toddlers Club.)

print(your_choice(6))

print(your_choice( 3)) print(your_choice( 1)) print(your_choice(0)) print(your_choice(2))

You are overaged. None

Welcome to the Toddlers Club! None You are too young for Toddlers Club. None You are too young for Toddlers Club. None

Welcome! You are a star member of the Toddlers Club! None The pass Statement The pass statement does nothing. Sometimes we need a placeholder to fill the syntactic requirements of a

compound statement. We use the pass statement to fill in the required suite of statements. The syntax is trivial.

pass Heres an example of using the pass statement. ifn%2 ==0:

pass # Ignore even values else: count + = 1 # Count the odd values Yes, technically, we can invert the logic in the if-clause. However, sometimes it is more clear to provide the explicit do nothing than to determine the inverse of the condition in the if statement. As programs grow and evolve, having a pass statement can be a handy reminder of places where a program can be expanded. Also, when we come to class declarations in Data + Processing = Objects, well see one other use for the pass

statement.

The assert Statement An assertion is a condition that were claiming should be true at this point in the program. Typically, it summarizes the state of the programs variables. Assertions can help explain the relationships among vari­

ables, review what has happened so far in the program, and show that if statements and for or while loops

have the desired e ect.

When a program is correct, all of the assertions are true no matter what inputs are provided. When a pro­ gram has an error, at least one assertion winds up false for some combination of inputs.

Python directly supports assertions through an assert statement. There are two forms:

assert condition assert condition, expression

If the condition is False, the program is in error; this statement raises an AssertionError exception. If the condition is True, the program is correct, this statement does nothing more. If the second form of the statement is used, and an expression is given, an exception is raised using the value

of the expression. Well cover exceptions in detail in Exceptions. If the expression is a string, it becomes an the value associated with the AssertionError exception.

Note: Additional Features There is an even more advanced feature of the assert statement. If the expression evaluates to a class, that class is used instead of AssertionError. This is not widely used, and depends on elements of the language

we havent covered yet. Heres a typical example:

max= 0

if a < b: max= b if b < a: max= a

assert (max = = a or max = = b) and max > = a and max > = b If the assertion condition is true, the program continues. If the assertion condition is false, the program

raises an AssertionError exception and stops, showing the line where the problem was found. Run this program with a equal to b and not equal to zero; it will raise the AssertionError exception. Clearly, the if statements dont set max to the largest of a and b when a = b . There is a problem in the if statements, and the presence of the problem is revealed by the assertion.

The if-else There are situations where an expression involves a simple condition and a full-sized if statement is dis­

tracting syntatic overkill. Python has a handy logic operator that evaluates a condition, then returns either of two values depending on that condition.

Ternary Operator Most arithmetic and logic operators have either one or two values. An operation that applies to a single value is called unary. For example -a and abs(b) are examples of unary operations: unary

negation and unary absolute value. An operation that applies to two values is called binary. For example,

a*b shows the binary multiplication operator.

The if-else operator trinary (or ternary) It involves a conditional expression and two alternative expres­

sions. Consequently, it doesnt use a single special character, but uses two keywords: if and else. Some folks will mistakenly call it the ternary operator as if this is the only possible ternary operator.

The basic form of the operator is expression if condition else expression

Python evaluates the condition in the middle first. If the condition is True, then the left-hand expression is

evaluated, and thats the value of the operation. If the condition is False, then the right-hand expression is evaluated, and thats the value of the operation. Note that the condition is always evaluated. Only one of the other two expressions is evaluated, making

this a kind of short-cut operator like and and or. Here are a couple of examples. average = sum/count if count != 0 else None

oddSum = oddSum + (n if n % 2 = = 1 else 0)

The intent is to have an English-like reading of the statement. The average is the sum divided by the count if the count is non-zero; else the average is None.

The wordy alterative to the first example is the following,

if count != 0: average = sum/count

else:

average = None

This seems like three extra lines of code to prevent an error in the rare situation of there being no values to

average. Similarly, the wordy version of the second example is the following:

ifn% 2 = = O:pass

else: oddSum = oddSum + n For this second example, the original statement registered our intent very clearly: we were summing the

odd values. The long-winded if-statement tends to obscure our goal by making it just one branch of the ifstatement.

Exercise 1. Write a program that asks the user to enter a length in centimeters. If the user enters a negative length, the program should tell the user that the entry is invalid. Otherwise, the program should convert the length to inches and print out the result. There are 2.54 centimeters in an inch. 2. Write a program that asks the user how many credits they have taken. If they have taken 23 or less, print

that the student is a freshman. If they have taken between 24 and 53, print that they are a sophomore. The

range for juniors is 54 to 83, and for seniors it is 84 and over.

3. Generate a random number between 1 and 10. Ask the user to guess the number and print a message

based on whether they get it right or not. 4. Write a program that asks the user for two numbers and prints Close if the numbers are within .001 of

each other and Not close otherwise. 5. A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years unless they are also divisible by 400. Write a program that asks the user for a year and prints out whether it is a leap

year or not. 6. Write a program that asks the user to enter a number and prints out all the divisors of that number.

7. Write a program that lets the user play Rock-Paper-Scissors against the computer. There should be five rounds, and after those five rounds, your program should print out who won and lost or that there is a tie.

Chapter 9 Loops A loop is a programming construct that enables repetitive processing of a sequence of statements. Python provides two types of loops to its users: the for loop and the while loop. The for and while loops are iteration

statements that allow a block of code (the body of the loop) to be repeated a number of times.

The for Loop Python implements an iterator-based for loop. It is a type of for loop that iterates over a list of items through an explicit or implicit iterator. The loop is introduced by the keyword for which is followed by a random variable name which will contain the values supplied by the object.

This is the syntax of Pythons for loop: for variable in list:

statements

else: statements Here is an example of a for loop in Python: pizza = [Delhi Style Pizza, Pan Pizza, Crispy Pizza, Stuffed Pizza]

for choice in pizza:

if choice = = Pan Pizza: print(Please pay $ 16. Thank you!)

print(Delicious, cheesy + choice) else: print(Cheesy pan pizza is my all-time favorite!) print(Finally, Im full!)

Run this and youll get the following output on Python Shell:

Delicious, cheesy Delhi Style Pizza Please pay $ 16. Thank you!

Delicious, cheesy Pan Pizza Delicious, cheesy Crispy Pizza Delicious, cheesy Stuffed Pizza Cheesy pan pizza is my all-time favorite!

Finally, Im full!

Using the range() Function with the for Loop The range() function can be combined with the for loop to supply the numbers required by the loop. In the

following example, the range(l, x+1) provided the numbers 1 to 50 needed by the for loop to add the sum of 1 until 50:

x = 50

total= 0 for number in range(l, x+1):

total = total + number print(Sum of 1 until %d: %d % (x, total))

The Python Shell will display:

Sum of 1 until 50: 1275

The while Loop A Python while loop repeatedly carries out a target statement while the condition is true. The loop iterates as long as the defined condition is true. When it ceases to be true and becomes false,

control passes to the first line after the loop. The while loop has the following syntax: while condition statement

statement Here is a simple while loop: counter = 0 while (counter < 10):

print(The count is:, counter) counter = counter + 1

print(Donef)

If you run the code, you should see this output: The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 The count is: 9 Done!

Using pass Statement The pass statement tells the Python interpreter to do nothing. The interpreter simply continues with the programs execution whenever the pass statement is encountered. This attribute makes it a good place­

holder whenever Python syntactically requires a line but the program itself does not require action. It can be very useful when youre creating a program and you need to focus on specific areas of your code, but you

still want to reserve some loops or test run the incomplete code. Here is how you would use a pass statement to fill gaps within a code: def function_name(x):

pass

Infinite loops When working with while loops, sooner or later you will accidentally send Python into a never-ending loop. Here is an example:

i=0 while i< 20: print(i)

In this program, the value of i never changes and so the condition i< 20 is always true. Python will continu­ ously print zeroes. To stop a program caught in a never-ending loop, use Restart Shell under the Shell menu.

You can use this to stop a Python program before it is finished executing. Sometimes a never-ending loop is what you want. A simple way to create one is shown below:

while True:

# statements to be repeated go here The value True is called a boolean value and is discussed further in Section 10.2.

Using a break statement A Python break statement ends the present loop and instructs the interpreter to starts executing the next statement after the loop. It can be used in both for and while loops. Besides leading the program to the statement after the loop, a break statement also prevents the execution of the else statement.

To illustrate, a break statement may be placed right after the print function of the if statement: pizza = [Delhi Style Pizza, Pan Pizza, Crispy Pizza, Stuffed Pizza] for choice in pizza:

if choice = = Pan Pizza:

print(Please pay $ 16. Thank you!) break

print(Delicious, cheezy + choice) else:

Using continue Statement The continue statement brings back program control to the start of the loop. You can use it for both for and while loops.

To illustrate, the continue statement may be placed right after the print function of the for loop to replace the break statement:

pizza = [Delhi Style Pizza, Pan Pizza, Crispy Pizza, Stuffed Pizza] for choice in pizza:

if choice = = Pan Pizza: print(Please pay $ 10. Thank you!) continue

print(Delicious, cheesy + choice) else: print(Cheesy pan pizza is my all-time favorite!) print(Finally, Im full!)

The output will be:

Delicious, cheesy Delhi Style Pizza Please pay $ 10. Thank you!

Delicious, cheesy Crispy Pizza Delicious, cheesy Stuffed Pizza Cheesy pan pizza is my all-time favorite!

Finally, Im full!

Nested loops You can put loops inside of other loops. A loop inside of another loop is said to be nested, and you can, more

or less, nest loops as deeply as you want. Example 1 Print a 10 10 multiplication table.

for i in range(l,ll): for jin range(l,ll):

print('{:3d}'.format(i*j), end='')

print() A multiplication table is a two-dimensional object. To work with it, we use two for loops, one for the horizontal direction and one for the vertical direction. The print statement right justifies the products to make them look nice. The end=" allows us to print several things on each row. When we are done printing a

row, we use printQ to advance things to the next line.

Example 2 Write a program that does a brute force search for finding the solutions (x, y) to a system 2x + 2 y = 4, x-y = 6, where x and y are both between -40 and 40 ? for x in range(-40,41): for y in range(-40,41):

if 2*x+2*y==4 and x-y==6: print(x,y)

Exercise 1. Write a program that uses a for loop to print the numbers 8,11,14,17, 20,, 83,86,

2. Write a program that uses a for loop to print the numbers 100, 98, 96,..., 4, 2. 3. Write a program that uses exactly four for loops to print the sequence of letters

below. AAAAAAAAAABBBBBBBCDCDCDCDEFFFFFFG 4. Write a program that asks the user for their name and how many times to print it. The pro-gram

should print out the users name the specified number of times. 5. The Fibonacci numbers are the sequence below, where the first two numbers are 1, and each num­

ber thereafter is the sum of the two preceding numbers. Write a program that asks the user how many

Fibonacci numbers to print and then prints that many. 1, 1,2, 3,5, 8, 13,21,34,55,89...

Chapter 10

User-Defined Functions A function is a set of instructions/statements that perform a specific task, a common structuring element that allows us to use a piece of code repeatedly in different sections of a program. The use of functions im­ prove a programs clarity and comprehensibility and makes programming more efficient by reducing code

duplication and breaking down complex tasks into more manageable pieces.

Functions are also known as routines, subroutines, methods, procedures, or subprograms. They can be passed as arguments, assigned to variables, or stored in collections.

A user-defined Python function is created or defined by using the def statement and follows the syntax:

def function_name(parameter list):

function body/statements return [expression] The indented statements make up the body of the function and are executed when the function is called. Once the function is called, parameters inside round brackets become arguments.

Function bodies can have more than one return statement which maybe placed anywhere within the func­ tion block. Return statements end the function call and return the value of the expression after the return

keyword. A return statement with no expression returns the special value None. In the absence of a return statement within the function body, the end of the function is indicated by the return of the value None.

Functions are useful for breaking up a large program into smaller pieces to make it easier to read and

maintain. They are also useful for repeatedly use the same code at several different points in your program. You can put that code in a function and call the function whenever you want to use & execute that code. You can also use functions to create your own utilities, math functions, etc.

Function Varieties Some programming languages make a distinction between various types of functions or subprograms.

There can be functions or subroutines or procedure functions. Python (like Java and C++) doesnt enforce this kind of distinction. Instead, Python imposes some distinction based on whether the function uses parameters and returns a value or yields a collection of values.

Ordinary Functions - Functions which follow the classic mathematical definitions will map input argu­ ment values to a resulting value. These are, perhaps, a common kind of function. They include a return statement to express the resulting value.

Procedure Functions - One common kind of function is one that doesnt return a result, but instead carries out some procedure. This function would omit any return statement. Or, if a return statement is used to

exit from the function, the statement would have no value to return. Carrying out an action is sometimes termed a side-e ect of the function. The primary e ect is always the value returned.

Factory Functions Another common form is a function that doesnt take a parameter. This function is a factory which generates a value.

Generators. A generator function contains the yield statement. These functions look like conventional functions, but they have a di erent purpose in Python. We will examine this in detail in Iterators and.

Generators. These functions have a persistent internal processing state; ordinary functions cant keep data around from

any previous calls without resorting to global variables. Further, these functions interact with the for statement. Finally, these functions dont make a lot of sense until weve worked with sequences in Sequences:

Strings, Tuples and Lists.

Function Definition

Functions are defined with the def statement. The statement ends with a colon, and the code that is part of the function is indented below the def statement. Here we create a simple function that just prints something.

def print_hello(): print('Hello!!') print_hello()

print('14567')

printJhelloO

Hello!!

14567 Hello!! The first two lines define the function. In the last three lines we call the function twice. One use for functions is if you are are using the same code over and over again in various parts of your program, you can make your program shorter and easier to understand by putting the code in a function. For example, suppose for some reason you need to print a box of stars like the one below at several points

in your program. **************

**************

Put the code into a function, and then whenever you need a box, just call the function rather than typing

several lines of redundant code. Here is the function.

def draw_square(): print('*'* 15) printC*',"*!!, 1*1 print('*', ''*11, 1*1

print('*' *15) One benefit of this is that if you decide to change the size of the box, you just have to modify the code in the function, whereas if you had copied and pasted the box-drawing code everywhere you needed it, you would

have to change all of them.

Arguments We can pass values to functions. Here is an example: def print_hello(n):

print('Hello' * n)

print() print_hello(2) print_hello(4)

times = 2 print_hello(times)

Hello Hello Hello Hello Hello Hello Hello Hello

When we call the print_hello function with the value 2, that value gets stored in the variable n. We can then refer to that variable n in our functions code. You can pass more than one value to a function: def multiple_print( string, n)

print( string * n) print() multiple_print('Hello', 4)

multiple_print('A', 8) HelloHelloHelloHello

AAAAAAAA

Returning values We can write functions that perform calculations and return a value as a result.

Example 1 Here is a simple function that converts temperatures from Celsius to Fahrenheit.

def cnvrt(t):

return t*9/5 + 32 print(cnvrt(20))

68 The return statement is used to send the result of a functions calculations back to the caller.

Notice that the function itself does not do any printing. The printing is done outside of the function. That

way, we can do math with the result, like below. print(cnvrt(2O)+5) If we had just printed the result in the function instead of returning it, the result would have been printed to the screen and forgotten about, and we would never be able to do anything with it.

The return Statement The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

You can return a value from a function as follows -

# Function definition is here def sum( argl, arg2 ):

# Add both the parameters and return them." total = argl + arg2

print "Inside from the function:", total return total;

# Now you can call sum function total = sum( 30, 20);

print "Outside from the function:", total When the above code is executed, it produces the following result Inside from the function: 50 Outside from the function: 5 0

Example 2 As another example, the Python math module contains trig functions, but they only work in radians. Let us write our own sine function that works in degrees. from math import pi, sin def deg_sin(x):

return sin(pi*x/l 80)

Example 3 A function can return multiple values as a list. Say we want to write a function that solves the system of equations a x + b y = e and cx + dy = f.It turns out

that if there is a unique solution, then it is given by x = (d e b f )=(ad be) and y = (a f ce)=(ad be). We need our

function to return both the x and y solutions. def solve(a,b,c,d,e,f):

x = (d*e-b*f )/(a*d-b*c) y = (a*f-c*e)/(a*d-b*c) return [x,y]

xsol, ysol = solve(2,3,4,l,2,5)

print('The solution is x =', xsol, 'and y = ', ysol) The solution is x = 1.3 and y= -0.2

This method uses the shortcut for assigning to lists that was mentioned in Section 10.3.

Default arguments and keyword arguments You can specify a default value for an argument. This makes it optional, and if the caller decides not to use it, then it takes the default value. Here is an example:

def multiple_print(str, n = 1)

print(str * n) print() multiple_print('Hello', 3)

multiple_print('Hello') HelloHelloHello

Hello Default arguments need to come at the end of the function definition, after all of the non-default

arguments.

Keyword arguments A related concept to default arguments is keyword arguments. Say we have the fol­ lowing function definition:

def fancy_print(text, color, background, style, justify): Every time you call this function, you have to remember the correct order of the arguments. Python allows

you to name the arguments when calling the function, as shown below: fancy_print(text='Hi', color='yellow', background='black', style='bold', justify='left') fancy_print(text='Hi', style='bold', justify='left', background='black', color='yellow') As we can see, the order of the arguments does not matter when you use keyword arguments.

When defining the function, it would be a good idea to give defaults. For instance, most of the time, the caller would want left justification, a white background, etc. Using these values as defaults means the caller does not have to specify every single argument every time they call the function. Here is a example:

def fancy_print(text, color='black', background='white',style='normal', justify='left'):

# function code goes here fancy_print('Hi', style='bold') fancy_print('Hi', color='yellow', background='black') fancy_print('Hi')

Local variables Lets say we have two functions like the ones below that each use a variable i:

deffunclQ:

fori in range(lO):

print(i) def func2():

i=100 func 1 ()

print(i)

When a variable is defined inside a function, it is local to that function, which means it essentially does not exist outside that function. This way each function can define its own variables and not have to worry about if those variable names are used in other functions.

Global variables On the other hand, sometimes you actually do want the same variable to be available to multiple functions. Such a variable is called a global variable. You have to be careful using global variables, especially in larger

programs, but a few global variables used judiciously are fine in smaller programs. Here is a short example: def reset():

global time_left

time_left = 0

def print_time(): print(time_left) time_left=30

In this program we have a variable timejeft that we would like multiple functions to have access to. If a function wants to change the value of that variable, we need to tell the function that timejeft is a global variable. We use a global statement in the function to do this. On the other hand, if we just want to use the value of the global variable, we do not need a global statement.

Arguments We finish the chapter with a bit of a technical detail. You can skip this section for the time being

if you dont want to worry about details right now. Here are two simple functions:

deffuncl(x):x = x + 1 def func2(L):

L = L + [1] a=2 M=[l,2,3]

func 1 (a) func2(M)

When we call fund with a and func2 with L, a question arises: do the functions change the values of a and L? The answer may surprise you. The value of a is unchanged, but the value of L is changed. The reason

has to do with a difference in the way that Python handles numbers and lists. Lists are said to be mutable objects, means they can be changed, whereas numbers and strings are immutable, means they cannot be

changed. If we want to reverse the behavior of the above example so that a is modified and L is not, do the following:

deffuncl(x):

x=x+ 1 return x def func2(L):

cpy = L[:] cpy = cpy + [l] a=3

M=[l,2,3]

a=funcl(a) # note change on this line func2(M)

lambda Use a lambda, as a convenience, when you need a function that both:



is anonymous (does not need a name) and



contains only an expression and no statements. Example:

fn = lambda x, y, z: (x ** 2) + (y * 2) + z In [2]: fn(4, 5, 6) format = lambda obj, category: 'The "%s" is a "%s".' % (obj, category,)

format('pine tree', 'conifer') Out[4]: 'The "pine tree" is a "conifer".'

A lambda can take multiple arguments and can return (like a function) multiple values. Example:

a = lambda x, y: (x * 3, y * 4, (x, y))

a(3,4) Suggestion: In some cases, a lambda may be useful as an event handler. Example:

class Test:

def _ init__(self,first=",last-'):self.first = first self.last = last

def test(self, formatter): min

Test for lambdas. formatter is a function taking 2 arguments, first and last names. It should return the formatted name. iiiiii

msg = 'My name is %s' % (formatter(self.first, self.last),) print msg

def test(): t = Test('Dave', 'Kuhlman') t.test(lambda first, last: '%s %s' % (first, last,)) t.test(lambda first, last: '%s, %s' % (last, first,)) test()

The global Statement In Functions and Namespaces well describe some of the internal mechanisms Python uses for storing vari­ ables. Well introduce the global statement in The global Statement.

Well include a digression on the two common argument binding mechanisms: call by value and call by reference in Call By Value and Call By Reference. Note that this is a distinction that doesnt apply to Python,

but if you have experience in languages like C or C++, you may wander where and how this is implemented.

Finally, well cover some aspects of functions as first-class objects in Function Objects.

Functions and Namespaces This is an overview of how Python determines the meaning of a name. Well omit some details to hit the more important points. For more information, see section 4.1 of the Python Language Reference.

The important issue is that we want variables created in the body of a function to be private to that func­

tion. If all variables are global, then each function runs a risk of accidentally disturbing the value of a global variable. In the COBOL programming language (without using separate compilation or any of the modern extensions) all variables are globally declared in the data division, and great care is required to prevent ac­ cidental or unintended use of a variable.

To achieve privacy and separation, Python maintains several dictionaries of variables. These dictionaries define the context in which a variable name is understood. Because these dictionaries are used for reso­ lution of variables, which name objects, they are called namespaces. A global namespace is available to all modules that are part of the currently executing Python script. Each module, class, function, lambda, or

anonymous block of code given to the exec command has its own private namespace.

Names are resolved using the nested collection of namespaces that define an execution environment. Python always checks the most-local dictionary first, ending with the global dictionary. Consider the following script.

def deep( a, b): print "a=", a print "b=", b def shallow( hows, things): deep( hows, 1) deep( things, coffee)

hows= 1 coffee = 2

shallow( "word", 3.1415926) shallow( hows, coffee) The deep() function has a local namespace, where two variables are defined: a and b. When deep() is

called from shallow(), there are three nested scopes that define the environment: the local namespace for deepQ: the local namespace for shallowQ, and the global namespace for the main script.

The shallowQ function has a local namespace, where two variables are defined: hows and things.

When shallowQ is called from the main script, the local hows is resolved in the local namespace. It

hides the global variable with the same name. The reference to coffee is not resolved in the local namespace, but is resolved in the global namespace.

This is called afree variable, and is sometimes a symptom of poor software design.

The main script by definition executes in the global namespace, where two variables (hows and coffee) are defined, along with two functions, deep() and shallowQ.

Some Consequences. Since each imported module exists in its own namespace, all functions and classes within that module must have their names qualified by the module name. We saw this when we imported math and random. To use the sqrt() function, we must say math.sqrt, providing the module name that is

used to resolve the name sqrt(). This module namespace assures that everything in a module is kept separate from other modules. It makes our programs clear by qualifying the name with the module that defined the name.

The module namespace also allow a module to have relatively global variables. A module, for example, can

have variables that are created when the module is imported. In a sense these are global to all the functions and classes in the module. However, because they are only known within the modules namespace, they

wont conflict with variables in our program or other modules. Having to qualify names within a module can become annoying when we are making heavy use of a

module. Python has ways to put elements of a module into the global namespace. Well look at these in Com­

ponents, Modules and Packages. Warning: Global Warning

The global statement has a consequence of tightly coupling pieces of software. This can lead to diffi­

culty in maintenance and enhancement of the program. Classes and modules provide better ways to as­ semble complex programs. As a general policy, we discourage use of the global statement.

Exercises 1. Write a function called rectangle that takes two integers m and n as arguments and prints out an m n box

consisting of asterisks. Shown below is the output of rectangle(2,4)

2. Write a function called add_excitement that takes a list of strings and adds an excla-mation point (!) to

the end of each string in the list. The program should modify the original list and not return anything. 3. Write a function called sum_digits that is given an integer num and returns the sum of the digits of num.

4. The digital root of a number n is obtained as follows: Add up the digits n to get a new number. Add up the digits of that to get another new number. Keep doing this until you get a number that has only one digit.

That number is the digital root. For example, ifn = 45893,we add up the digits to get 4 + 5 + 8 + 9 + 3 = 29. We then add up the digits of 29 to

get 2 + 9 = 11. We then add up the digits of 11 to get 1 + 1 = 2. Since 2 has only one digit, 2 is our digital root. 5. Write a function that returns the digital root of an integer n. [Note: there is a shortcut, where the digital root is equal to n mod 9, but do not use that here.] 6. Write a function called first_diff that is given two strings and returns the first location in which the

strings differ. If the strings are identical, it should return -1. 7. Write a function that takes an integer n and returns a random integer with exactly n digits. For instance, if n is 3, then 125 and 593 would be valid return values, but 09 3 would not because that is really 93, which is a two-digit number. 8. Write a function called number_of_factors that takes an integer and returns how many factors the num­

ber has. 9. Write a function called factors that takes an integer and returns a list of its factors.

Chapter 11

Useful Modules A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the modules name (as a string) is available as the value of the

global variable_ name_ .

Python contains hundreds of modules that do all sorts of tasks. There are also so many third-party modules available for download from the internet. We disscuse a few modules that we have found useful.

Importing modules There are different ways to import modules in Python which are as follows;

from random import randint, choice from random import * import random

The first way imports just two functions from the module. The second way imports every function from the module. You should usually avoid doing this, as the module may contain some names that will interfere with your own variable names. For instance if your

program uses a variable called sum and you import a module that contains a function called sum, there can be problems. Some modules, however, like tkinter, are fairly safe to import this way.

The third way imports an entire module in a way that will not interfere with your variable names. To use a

function from the module, preface it with random followed by a dot. For example: random.randint(l,10).

Changing module names The as keyword can be used to change the name that your program uses to refer to a module or things from a module. Here are three examples:

import random as rnd from math import log as In

Location Usually, import statements go at the beginning of the program, but there is no restriction. They can go

anywhere as long as they come before the code that uses the module.

Getting help

To get help on a module (say the math module) at the Python shell, import it using the third way above. Then dir(math) gives a list of the functions and variables in the module, and help(math) will give you a

rather long description of what everything does. To get help on a specific function, like log, type help(math■log).

math module The math module contains some common math functions that are as follows;

Function

Description

sin, cos, tan

trig functions

asin, acos, atan

inverse trig functions

tan2(y,x)

gives arctan( y=x) with proper sign be­

havior sinh, cosh, tanh

hyperbolic functions

asinh, acosh, atanh

inverse hyperbolic functions

log, log 10

natural log, log base 10

loglp

log(l +x), more accurate near 1 than log

exp

exponential function ex

degrees, radians

convert from radians to degrees or vice-

versa

floor

floor(x) is the greatest integer x

ceil

ceil(x) is the least integer x

e, pi

the constants e and pi

factorial

Factorial

modf

returns a pair (fractional part, integer

part) erf

the Error function

Note that the floor and intfunctions behave the same for positive numbers, but differentlyfor negative numbers.

For instance, floor(4.5 7) and int(4.57) both return the integer 4. However, floor(-4.57) returns -5, the greatest

integer less than or equal to -4.57, while int(-4.57) returns -4, which is obtained by throwing away the decimal part of the number.

Time module The time module has some useful functions for dealing with time.

sleep The sleep function pauses your program for a specified amount of time (in seconds). For instance, to pause your program for 5 seconds or for 20 milliseconds, use the following:

from time import sleep

sleep(5) sleep(.O2)

Timing things The time function can be used to time things. Here is an example: from time import time start = time()

# do some stuff printflt took', round(time()-start, 3), 'seconds.') The resolution of the time() function is milliseconds on Windows and microseconds on Linux. The above example uses whole seconds. If you want millisecond resolution, use the following print

statement:

print('{:.3f} seconds'.format(time()-start)) You can use a little math on this to get minutes and hours. Here is an example:

t = time()-start

secs = t%60

mins = t//60 hours = mins//60

When you call time(), you get a rather strange value like 1306372108.045. It is the number of seconds elapsed since January 1,1970.

datetime modules

The module datetime allows us to work with dates and times together. The following line creates a date-

time object that contains the current date and time: from datetime import datetime

d = datetime(l,l,l).now() The datetime object has attributes year, month, day, hour, minute, second, and microsecond.

Here is a short example:

d = datetime(l,l,l).now() print('{}:{:02d] {}/{}/{}'.format(d.hour,d.minute,d.month,d.day,d.year))

9:38 22/6/2018 The hour is in 24-hour format. To get 12-hour format, you can do the following:

am_pm = 'am' if d.hour import re

The most common uses of regular expressions are:



Search a string (search and match)



Finding a string (findall)



Break string into a sub strings (split)



Replace part of a string (sub)

Lets look at the methods that library re provides to perform these tasks.

What are various methods of Regular Expressions? The re package provides multiple methods to perform queries on an input string. Here are the most com­ monly used methods, I will discuss:

1. re.match()

2. re.searchO

3. re.findall() 4. re.split()

5. re.subQ

6. re.compile()

Lets look at them one by one.

re.match(pattern, string): This method finds match if it occurs at start of the string. For example, calling match() on the string AM Analytics AM and looking for a pattern AM will match. However, if we look for only Analytics, the pattern

will not match. Lets perform it in python now. import re

result = re.match(r AM', AM Analytics Vidhya AM')

print result

Output:

Above, it shows that pattern match has been found. To print the matching string well use method group (It helps to return the matching string). Use r at the start of the pattern string, it designates a python raw string.

result = re.match(r'AM', AM Analytics Vidhya AM')

print result.group(O) Output:

AM

Lets now find Analytics in the given string. Here we see that string is not starting with AM so it should

return no match. Lets see what we get:

result = re.match(r Analytics', 'AM Analytics Vidhya AM')

print result Output: None There are methods like startQ and end() to know the start and end position of matching pattern in the string.

result = re.match(r'AM', AM Analytics Vidhya AM')

print result.startQ print result.end()

Output: 0 2

Above you can see that start and end position of matching pattern AM in the string and sometime it helps a lot while performing manipulation with the string.

re.seaich(pattern, string):

It is similar to match() but it doesnt restrict us to find matches at the beginning of the string only. Unlike previous method, here searching for pattern Analytics will return a match.

result = re.search(rAnalytics', 'AM Analytics Vidhya AM')

print result.group(O) Output: Analytics Here you can see that, search() method is able to find a pattern from any position of the string but it only

returns the first occurrence of the search pattern.

re.findall (pattern, string): It helps to get a list of all matching patterns. It has no constraints of searching from start or end. If we

will use method findall to search AM in given string it will return both occurrence of AM. While searching a string, I would recommend you to use re.findall() always, it can work like re.searchQ and re.matchQ both,

result = re.findall(r'AM', AM Analytics Vidhya AM')

print result Output: ['AM', AM']

re.split(pattern, string, [maxsplit=O]): This methods helps to split string by the occurrences of given pattern. result=re.split(r'y',Analytics') result Output:

['Anal', 'tics'] Above, we have split the string Analytics by y. Method split() has another argument maxsplit. It has default value of zero. In this case it does the maximum splits that can be done, but if we give value to maxsplit, it will split the string. Lets look at the example below: result=re.split(r'i',Analytics Vidhya') print result Output: ['Analyt', 'cs V', 'dhya'] #It has performed all the splits that can be done by pattern "i". result=re.split(r'i',Analytics Vidhya',maxsplit= 1) result Output: [Analyt1, 'cs Vidhya'] Here, you can notice that we have fixed the maxsplit to 1. And the result is, it has only two values whereas first example has three values.

re.sub(pattern, repl, string): It helps to search a pattern and replace with a new sub string. If the pattern is not found, string is returned unchanged. result=re.sub(r'India','the World', AM is largest Analytics community of India')

result

Output: AM is largest Analytics community of the World'

re.compile(pattern, repl, string):

We can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search a pattern again without rewriting it. import re

pattern=re.compile('AM')

result=pattern.findall('AM Analytics Vidhya AM') print result result2=pattern.findall('AM is largest analytics community of India')

print re suit 2 Output: ['AM', 'AM'] ['AM']

Till now, we looked at several methods of regular expression using a constant pattern (fixed characters).

But, what if we do not have a constant search pattern and we want to return specific set of characters (de­

fined by a rule) from a string? This can easily be solved by defining an expression with the help of pattern

operators (meta and literal characters).

What are the most commonly used operators? Regular expressions can specify patterns, not just fixed characters. Here are the most commonly used

operators that helps to generate an expression to represent required characters in a string or file. It is com­ monly used in web scrapping and text mining to extract required information. Operators

Description

Matches with any single character except newline \n. :>

Match 0 or 1 occurrence of the pattern to its left

+

1 or more occurrences of the pattern to its left

»

0 or more occurrences of the pattern to its left

\w

Matches with a alphanumeric character whereas \W (upper case W) matches non alphanumeric character.

\d

Matches with digits [0-9] and /D (upper case D) matches with non-digits.

\s

Matches with a single white space character (space, newline, return, tab, form) and \S (upper case S) matches any non-white space character.

\b

boundary between word and non-word and /B is opposite of /b

[••]

Matches any single character in a square bracket and [a..] matches any single character not in square bracket

\

It is used for special meaning characters like \. to match a period or \+ for plus sign.

a

and$

A and $ match the start or end of the string respectively

{n,m}

Matches at least n and at most m occurrences of preceding expression if we write it as {,m} then it will return at least any minimum occurrence to max m preceding expression.

a| b

Matches either a or b

0

Groups regular expressions and returns matched text

\t, \n, \r

Matches tab, newline, return

Now, lets understand the pattern operators by looking at the below examples.

Practical Examples of Regular Expressions

Problem 1: Return the first word of a given string Solution-1 Extract each character (using \w“) import re result=re.findall(r'.','AM is largest Analytics community of India')

print result

Output: [A', 'M','', 'i', 's','', '1', 'a', 'r', 'g', 'e', 's', 't','', A', 'n', 'a', '1', 'y', 't', 'i', 'c', 's','', 'c', 'o', 'm', 'm', 'u', 'n', 'i', 't', 'y','', 'o', 'f','', T, 'n', 'd', 'i', 'a']

Above, space is also extracted, now to avoid it use \w instead of .. result=re.fmdall(r'\w','AM is largest Analytics community of India')

print result

Output: [A', 'M', 'i', 's','!', 'a', 'r', 'g', 'e', 's', 't', A', 'n', 'a', '1', 'y', 't', 'i', 'c', 's', 'c', 'o', 'm', 'm', 'u', 'n', 'i', 't', 'y', 'o', 'f', T, 'n', 'd',

Solution-2 Extract each word (using * or +") result=re.findall(r'\w*','AM is largest Analytics community of India')

print result

Output: ['AM',", 'is',", 'largest',", 'Analytics',", 'community',", 'of',", 'India',"] Again, it is returning space as a word because * returns zero or more matches of pattern to its left. Now to

remove spaces we will go with +. result=re.findall(r'\w+','AM is largest Analytics community of India')

print result

Output: ['AM', 'is', 'largest', Analytics', 'community', 'of, 'India']

Solution-3 Extract each word (using a") result=re.findall(r'A\w+','AM is largest Analytics community of India')

print result

Output: [AM1]

If we will use $ instead of a, it will return the word from the end of the string. Lets look at it. result=re.findall(r'\w+$','AM is largest Analytics community of India')

print result

Output: [India]

Chapter 17

String, List & Dictionary Strings In short, strings are immutable sequence of characters. There are a lot of methods to ease manipulation and creation of strings as shown here below.

Creating a string (and special characters) Single and double quotes are special characters. There are used to defined strings. There are actually 3 ways to define a string using either single, double or triple quotes:

text = 'The surface of the circle is 2 pi R = ' text = "The surface of the circle is 2 pi R = "

text = '"The surface of the circle is 2 pi R =

In fact the latest is generally written using triple double quotes: text = """The surface of the circle is 2 pi R = """ Strings in double quotes work exactly the same as in single quotes but allow to insert single quote character

inside them. The interest of the triple quotes (or) is that you can specify multi-line strings. Moreover, sin­ gle quotes and double quotes can be used freely within the triple quotes: text = """ a string with special character" and' inside"""

The and characters are part of the Python language; they are special characters. To insert them in a string,

you have to escape them (i.e., with a \ character in front of them to indicate the special nature of the char­ acter). For instance: text = " a string with escaped special character \", V inside"

There are a few other special characters that must be escaped to be included in a string.

To include unicode, you must precede the string with the u character:

>>> u"\u0041" A

Note Unicode is a single character set used to represent 65536 different characters.

Similarly, you may see strings preceded by the r character to indicate that the string has to be interpreted

as it is without interpreting the special character \. This is useful for docstrings that contain latex code for

instance: r" \textbf{this is bold text in LaTeX}"

Strings are immutable You can access to any character using slicing:

text[0] text[-l ]

text[0:]

However, you cannot change any character:

text[0] = 'a' #this is incorrect.

Formatter In Python, the % sign lets you produce formatted output. A quick example will illustrate how to print a formatted string:

>>> print("%$" % "some text") "some text"

The syntax is simply:

string % values

If you have more than one value, they should be placed within brackets:

>>> print("%s %s" % ("a", "b")) The string contains characters and conversion specifiers (here %s)

To escape the sign %, just double it:

>>> print "This is a percent sign: %%" This is a percent sign: %

There are different ways of formatting a string with arguments. The one based on a string method called

formatQ is more and more common: >>> "{a}!={b}".format(a=2, b=l)

2!=1

Operators The mathematical operators + and * can be used to create new strings:

t = 'This is a test' t2 = t+t t3 = t*3

and comparison operators >,>=,==, > > "44".isdigit() # is the string made of digits only ? True

>>> "44".isalpha() # is the string made of alphabetic characters only 1

False

> > > "44".isalnum() # is the string made of alphabetic characters or digits only ? True

>>> "Aa".isupper() # is it made of upper cases only 1

False

> > > "aa".islower() # or lower cases only ? True

> > > "Aa".istitle() # does the string start with a capital letter ? True

>>> text = "There are spaces but not only" >>> text.isspaceQ # is the string made of spaces only ?

False

You can count the occurence of a character with countQ or get the length of a string with len():

>>> mystr = "This is a string"

> > > mystr.count('i') 3

> > > len(mystr) 16

Methods that return a modified version of the string The following methods return modified copy of the original string, which is immutable. First, you can modify the cases using titleQ, capitalizeQ, lowerQ, upperQ and swapcaseQ:

> > > mystr = "this is a dummy string" > > > mystr.titleQ

# return a titlecase version of the string

'This Is A Dummy String'

> > > mystr.capitalize() # return a string with first letter capitalised only. 'This is a dummy string'

> > > mystr.upper()

# return a capitalised version of the string

'THIS IS A DUMMY STRING'

> > > mystr.lower()

# return a copy of the string converted to lower case

'this is a dummy string'

> > > mystr.swapcaseQ # replace lower case by upper case and vice versa 'THIS IS A DUMMY STRING'

Second, you can add trailing characters with centerQ and just() methods:

> > > mystr = "this is a dummy string"

# center the string in a string of length 40

> > > mystr.center(40) '

this is a dummy string

> > > mystr.ljust(30) 'this is a dummy string

'

# justify the string to the left (width of 20) '

> > > mystr.rjust(3 0,# justify the string to the right (width of 20) 1-------- this is a dummy string'

There is also a zfillQ methods that adds zero to the left, which is equivalent to .rjust(width, '0'):

> >> mystr.zfill(30) 'OOOOOOOOthis is a dummy string'

or remove trailing spaces with the stripQ methods:

> > > mystr = " string with left and right spaces " > >> mystr.stripO 'string with left and right spaces'

> > > mystr.rstripQ ' string with left and right spaces' > >> mystr.lstripO 'string with left and right spaces '

or expand tabs with expandtabsQ:

> > > 'this is a \t tab'.expandtabsQ

'this is a

tab'

You can remove some specific characters with translated or replace words with replaced:

> > > mystr = "this is a dummy string" > > > mystr.replace('dummy', 'great', 1) # the 1 means replace only once 'this is a great string'

> > > mystr.translate(None, 'aeiou') ths s dmmy strng

Finally, you can separate a string with respect to a single separator with partitionQ:

> > > mystr = "this is a dummy string" > > > t.partition('is') ('th', 'is','is a line')

> > > t.rpartition('is') ('this', 'is',' a line')

Methods to find position of substrings The are methods such as endswithQ, startswithQ, findQ and indexQ that allow to search for substrings in a string.

> > > mystr = "This is a dummy string" > > > mystr.endswith('ing')

# may provide optional start and end indices

True

> > > mystr.startswith('This') # may provide start and end indices

True

> > > mystr.find('is')

# returns start index of'is'first occurence

2

> > > mystr.find('is', 4)

# starting at index 4, returns start index of'is'first occurence

5

> > > mystr.rfind('is')

# returns start index of'is' last occurence

5

> > > mystr.index('is')

# like find but raises error when substring is not found

2

>>> mystr.rindex('is')

# like rfind but raises error when substring is notfound

5

Methods to build or decompose a string A useful function is the splitQ methods that splits a string according to a character. The inverse function exist and is called joinQ. >>> message = ' '.join(['this' ,'is', 'a', 'useful', 'method']) >>> message

'this is a useful method' >>> message.split('')

['this', 'is', 'a', 'useful', 'method']

The splitQ function can be applied to a limited number of times if needed. However, it starts from the left. If you want to start from the right, use rsplitQ instead:

> >> message = ' '.join(['this' ,'is', 'a', 'useful', 'method']) > >> message.rsplit('', 2) ['this is a', 'useful', 'method'] If a string is multi-lines, you can split it with splitlinesQ:

> >> 'this is an example\n of\ndummy sentences'.splitlines() ['this is an example',' of', 'dummy sentences']

you can keep the endline character by giving True as an optional argument. Finally, note that splitQ removes the splitter:

> >> "this is an example".split(" is") ['this', 'an example'] If you want to keep the splitter as well, use partitionQ

> >> "this is an exemple".partition(" is") ('this',' is', 'an exemple')

LISTS A list is a container for variable length sequence of Python objects. A list is mutable, which means that items within the list can be changed. Also, items can be added to the list or removed from the list.

In python, lists are part of the standard language. You will find them everywhere. Like almost everything in Python, lists are objects. There are many methods associated to them. Some of which are presented here

below.

Quick example »>1 = [1,2,3] »> l[0]

1 >>> l.append(l)

>>>1 [1,2, 3,1]

Difference between appendQ and extendQ Lists have several methods amongst which the appendQ and extendQ methods. The former appends an

object to the end of the list (e.g., another list) while the later appends each element of the iterable object (e.g., another list) to the end of the list. For example, we can append an object (here the character c) to the end of a simple list as follows:

>>> stack = ['a','b'] >>> stack.append('c')

> > > stack ['a', 'b', 'c']

However, if we want to append several objects contained in a list, the result as expected (or not...) is: >>> stack.append(['d', 'e', 'f'])

> > > stack ['a', 'b', 'c', ['d', 'e', 'f ’]]

The object ['d', 'e', 'f'] has been appended to the existing list. However, it happens that sometimes what we

want is to append the elements one by one of a given list rather the list itself.

You can do that manually of course, but a better solution is to use the extendQ method as follows: >>> stack = ['a', 'b', 'c'] >>> stack.extend(['d', 'e','f'])

> > > stack ['a', 'b', 'c', 'd', 'e', 'f']

Other list methods index The index() methods searches for an element in a list. For instance:

> >> myjist = ['a','b','c','b', 'a']

> > > my_list.index('b') 1

It returns the index of the first and only occurence of b. If you want to specify a range of valid index, you can indicate the start and stop indices: >>> myjist = ['a','b','c','b', 'a'] >>> myjist.index('b', 2)

3

Warning if the element is not found, an error is raised

insert You can remove element but also insert element wherever you want in a list:

> >> my_list.insert(2, 'a')

> > > myjist ['b', 'c', 'a', 'b']

The insertQ methods insert an object before the index provided.

remove Similarly, you can remove the first occurence of an element as follows:

> > > my_list.remove('a') > > > myjist ['b', 'c', 'b', 'a']

POP Or remove the last element of a list by using: > > > myJist.popO 'a'

> > > myjist ['b', 'c', 'b']

which also returns the value that has been removed.

count You can count the number of element of a kind:

> > > myJist.count('b') 2

sort There is a sort() method that performs an in-place sorting:

> >> my_list.sort()

> > > myjist ['a', 'b', 'b', 'c'] Here, it is quite simple since the elements are all characters. For standard types, the sorting works well.

Imagine now that you have some non-standard types. You can overwrite the function used to perform the

comparison as the first argument of the sort() method. There is also the possibility to sort in the reverse order:

> >> my_list.sort(reverse=True)

> > > myjist ['c', 'b', 'b', 'a']

reverse Finally, you can reverse the element in-place:

> >> myjist = ['a', 'c' ,'b']

> > > myJist.reverse() > > > myjist ['b', 'c', 'a']

Operators

The + operator can be used to extend a list:

> >> myjist = [1] > >> myjist += [2]

> > > myjist [1,2]

> >> myjist += [3,4]

> > > myjist [1,2, 3,4] The * operator ease the creation of list with similar values

> >> myjist = [1,2]

> > > myjist = myjist * 2 > > > myjist [1,2,1,2]

Slicing Slicing uses the symbol: to access to part of a list:

>>> list[first indexdast index:step] >>> list[:]

»> a = [0,1, 2, 3, 4, 5]

[0,1, 2, 3,4, 5] >>>a[2:]

[2, 3, 4, 5] »> a[:2] [0,1]

»>a[2:-l] [2,3,4]

By default the first index is 0, the last index is the last one..., and the step is 1. The step is optional. So the following slicing are equivalent: »> a = [1,2, 3,4, 5, 6, 7,8] »>a[:]

[1,2, 3,4, 5, 6, 7,8] >>> a[::l]

[1,2, 3,4, 5, 6, 7,8] >>> a[0::l]

[1,2, 3,4, 5, 6, 7,8]

List comprehension Traditionally, a piece of code that loops over a sequence could be written as follows:

> > > evens = []

> > > for i in range( 10):

...

ifi%2==0: evens.append(i)

> > > evens [0,23,6,8]

This may work, but it actually makes things slower for Python because the interpreter works on each loop to determine what part of the sequence has to be changed.

A list comprehension is the correct answer:

> >> [ifor iinrange( 10) if i % 2 == 0] [0,2, 4, 6, 8] Besides the fact that it is more efficient, it is also shorter and involves fewer elements.

Filtering Lists »> li = [1, 2]

> >> [elem*2 forelem inli ifelem>l]

[4]

Lists as Stacks The Python documentation gives an example of how to use lists as stacks, that is a last-in, first-out data

structures (LIFO).

An item can be added to a list by using the appendQ method. The last item can be removed from the list by using the pop() method without passing any index to it.

>>> stack = ['a','b','c','d'] >>> stack.append('e')

>>> stack.append('f')

> > > stack ['a', 'b', 'c', 'd', 'e', ’f’] >>> stack.popO

■f'

> > > stack ['a, 'b', 'c', 'd', 'e']

Lists as Queues Another usage of list, again presented in Python documentation is to use lists as queues, that is a first in first out (FIFO). >>> queue = ['a', 'b', 'c', 'd'] >>> queue, append('e') >>> queue, append('f')

> > > queue ['a', 'b', 'c', 'd', 'e', 'f']

> >> queue.pop(O) 'a'

How to copy a list There are three ways to copy a list:

> >>12 = list® > >> 12 = 1[:]

> > > import copy > >> 12 = copy.copy(l) Warning Dont do 12 = 1, which is a reference, not a copy.

The preceding techniques for copying a list create shallow copies. IT means that nested objects will not be

copied. Consider this example:

»> a = [1,2, [3,4]]

> >> b = a[:] >>> a[2][0] = 10 >>> a

[1,2, [10, 4]] >>> b

[1,2, [10, 4]]

To get around this problem, you must perform a deep copy:

> > > import copy »> a = [1,2, [3,4]]

> > > b = copy.deepcopy(a) >>>a[2][0] = 10 >>> a

[1,2, [10, 4]] >>> b

[1,2, [3, 4]]

Inserting items into a sorted list The bisect module provides tools to manipulate sorted lists.

»>x = [4,1] >>> x.sort()

> > > import bisect > >> bisect.insort(x, 2) >>> x

[1,2,4]

To know where the index where the value would have been inserted, you could have use:

»>x = [4,1]

> >> x.sort()

> > > import bisect > >> bisect.bisect(x, 2) 2

MAPPINGS AND DICTIONARIES

Many algorithms need to map a key to a data value. This kind of mapping is supported by the Python dictionary, diet. Well look at dictionaries from a number of viewpoints: semantics, literal values, opera­

tions, comparison operators, statements, built-in functions and methods.

Quick example A dictionary is a sequence of items. Each item is a pair made of a key and a value. Dictionaries are not

sorted. You can access to the list of keys or values independently. >>> d = {'first':'string value', 'second':[l,2]} >>> d.keys()

['first', 'second'] >>> d.valuesO

['string value', [1,2]] You can access to the value of a given key as follows: >>> df'first']

'string value'

Warning You can not have duplicate keys in a dictionary

Warning

dictionary have no concept of order among elements

Methods to query information

In addition to keys and values methods, there is also the items method that returns a list of items of the form (key, value). The items are not returned in any particular order: >>> d = {'first':'string value', 'second':[l,2]} >>> d.items()

[('a', 'string value'), ('b', [1, 2])] The iteritems method works in much the same way, but returns an iterator instead of a list. You can check for the existence of a specific key with has_key: >>> d.has_key('first') True

The expression d.has_key(k) is equivalent to k in d. The choice of which to use is largely a matter of taste.

In order to get the value corresponding to a specific key, use get or pop: >>> d.get('first') # this method can set an optional value, if the key is not found

'string value'

It is useful for things like adding up numbers: sum[value] = sum.get(value, 0) + 1 The difference between get and pop is that pop also removes the corresponding item from the dictionary: >>> d.pop('first')

'string value' >>> d

{'second': [1, 2]}

Finally, popitem removes and returns a pair (key, value); you do not choose which one because a dictionary is not sorted >>> d.popitemO

('a', 'string value') >>> d {'second': [1, 2]}

Methods to create new dictionary Since dictionaries (like other sequences) are objects, you should be careful when using the affectation sign:

> >> dl = {'a': [1,2]} > >> d2 = dl

»>d2['a'] = [l,2,3,4] > >> dl['a] [1,2,3,4]

To create a new object, use the copy method (shallow copy): >>> d2 = dl.copyO

You can clear a dictionary (i.e., remove all its items) using the clear() method: >>> d2.clear()

{}

The clear() method deletes all items whereas del() deletes just one:

>>>d = {'a':l,'b':2,'c':3}

>>> del d['a'] >>> d.clear()

Create a new item with default value (if not provided, None is the default): >>> d2.setdefault('third',")

>>>d2 ['third']

Create a dictionary given a set of keys: >>> d2.fromkeys(['first', 'second'])

another syntax is to start from an empty dictionary: >>> {}.fromkeys(['first', 'second'])

{'first': None, 'second': None} Just keep in ,ind thqt the fromkeys() method creates a new dictionary with the given keys, each with a

default corresponding value of None.

Combining dictionaries Given 2 dictionaries dl and d2, you can add all pairs of key/value from d2 into dl by using the update method (instead of looping and assigning each pair yourself: >>> dl = {'a':l}

>>>d2 = {'a':2; ’b':2} >>> dl.update(d2) >>> dl['a'] 2

>>> d2['b'] 2

The items in the supplied dictionary are added to the old one, overwriting any items there with the same keys.

iterators Dictionary provides iterators over values, keys or items:

> > > [x for x in t.itervaluesQ] ['string value', [1,2]] >>>

> > > [x for x in t.iterkeysQ] ['first', 'csecond']

> > > [x for x in t.iteritems()] [('a', 'string value'), ('b', [1, 2])]

Chapter 18

Python Pandas What is Pandas? Pandas is an open-source Python Library pro­

viding high-performance data manipulation and analysis tool using its powerful data struc­

tures. The name Pandas is derived from the

Pandas

word Panel Data an Econometrics from Multi­

dimensional data.

In 2008, developer Wes McKinney started developing pandas when in need of high performance, flexible tool for analysis of data.

Prior to Pandas, Python was majorly used for data munging and preparation. It had very little contribu­ tion towards data analysis. Pandas solved this problem. Using Pandas, we can accomplish five typical steps in the processing and analysis of data, regardless of the origin of data load, prepare, manipulate, model, and analyze. Python with Pandas is used in a wide range of fields including academic and commercial domains includ­ ing finance, economics, Statistics, analytics, etc.

Key Features of Pandas > > > > > > > > >

Fast and efficient DataFrame object with default and customized indexing. Tools for loading data into in-memory data objects from different file formats. Data alignment and integrated handling of missing data. Reshaping and pivoting of date sets. Label-based slicing, indexing and subsetting of large data sets. Columns from a data structure can be deleted or inserted. Group by data for aggregation and transformations. High performance merging and joining of data. Time Series functionality.

Python Pandas - Customizing Environment Standard Python distribution doesn't

come bundled with Pandas module. If

you install Anaconda Python package for Windows, Pandas will be installed by

default with the following -

ANACONDA Powered by Continuum Analytics

1. Download Anaconda (from https://www.anaconda.com/download/ is a free Python distri­ bution) for SciPy stack. It is also available for Linux and Mac.

Anaconda 5.2 For Windows Installer

Python 3.6 version A Download 64-Brt Graphical Installer (631 MB) (

32-Bit Graphical Installer (506 MB)

Choose either the Python 2 or Python 3 Version depending on your

needs. It doesnt effect the installation process.

2. Open and run the installer Once the download completes, open and run the .exe installer

Open Always open files of this type

li

Show in folder Canceli r

1

Anaconda3-5.0.1 -....exe

2. When installing Anaconda, a pop-up menu may ask whether or not to Add Anaconda to my PATH environment variable, and Register Anaconda as my default Python 3.11. We suggest accepting both options for the purposes of this class.

J Anaconda3 4.1.1 (64-bit) Setup Advanced Installation Options

O

ANACONDA

Customize how Anaconda integrates with Windows

Advanced Options

0 Add Anaconda to the system PATH environment variable This ensures that PATH is set correctly when using Python, IPython, conda, and any other program in the Anaconda distribution. If unchecked, then you must use the Anaconda Command Prompt (located in the Start Menu under 'Anaconda (64-bit)9.

Q Register Anaconda as the system Python 3.5 This will allow other programs, such as Python Tools for Visual Studio PyCharm, Wing IDE, PyDev, and MSI binary packages, to automatically detect Anaconda as the primary Python 3.5 on the system.

Continuum Analytics, Inc.

< Back

Install

Cancel

NumPy - Introduction NumPy is a Python package. It stands for 'Numerical Python'. It is a library consisting of multidimen­ sional array objects and a collection of routines for processing of array.

Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed, having some additional functionalities. In 2005, Travis Oliphant created NumPy package by incorporating the features of Numarray into Numeric package. There are many contributors to this open source project.

Operations using NumPy Using NumPy, a developer can perform the following operations • Mathematical and logical operations on arrays. • Fourier transforms and routines for shape manipulation. • Operations related to linear algebra. NumPy has in-built functions for linear algebra and ran­

dom number generation.

Customizing NumPy Standard Python distribution doesn't come bundled with NumPy module. If you install Anaconda

Python package for Windows, Pandas will be installed by default with the following Download and install Anaconda (from https://www.anaconda.com/download/ is a free Python distribu­

tion) for SciPy stack, it is also available for Linux and Mac. If you're using Anaconda Python, your system

should already have numpy and matplotlib installed.

NumPy - Ndarray Object The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index.

Every item in an ndarray takes the same size of block in the memory. Each element in ndarray is an object of data-type object (called dtype).

Any item extracted from ndarray object (by slicing) is represented by a Python object of one of array scalar types. The following diagram shows a relationship between ndarray, data type object (dtype) and array

scalar type -

ndarray

An instance of ndarray class can be constructed by different array creation routines described later in the tutorial. The basic ndarray is created using an array function in NumPy as follows numpy. array

It creates an ndarray from any object exposing array interface, or from any method that returns an array. numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

The above constructor takes the following parameters -

Sr.No.

Parameter & Description

Object Any object exposing the array interface method returns an array, or any (nested) sequence.

2

Dtype Desired data type of array, optional

3

Copy Optional. By default (true), the object is copied

4

Order C (row major) or F (column major) or A (any) (default)

5

Subok By default, returned array forced to be a base class array. If true, sub-classes passed through

6

Ndmin Specifies minimum dimensions of resultant array

Take a look at the following examples to understand better. Example 1 import numpy as np

a = np.array([l,2,3]) print a

The output is as follows -

[1,2,3] Example 2

# more than one dimensions import numpy as np

a = np.array([[l, 2], [3,4]]) print a

The output is as follows [[1,2] [3,4]]

Example 3

# minimum dimensions import numpy as np

a = np.array([l, 2, 3,4,5], ndmin = 2) print a

The output is as follows -

[[1,2, 3,4, 5]]

The ndarray object consists of contiguous one-dimensional segment of computer memory, combined

with an indexing scheme that maps each item to a location in the memory block. The memory block holds the elements in a row-major order (C style) or a column-major order (FORTRAN or MatLab style).

Python Lists vs. Numpy Arrays - What is the difference? Numpy is the core library for scientific computing in Python. It provides a high-performance multidimen­ sional array object, and tools for working with these arrays. A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the

array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

The Python core library provided Lists. A list is the Python equivalent of an array, but is resizable and can contain elements of different types.

A common beginner question is what is the real difference here. The answer is performance. Numpy data structures perform better in:

Size - Numpy data structures take up less space

Performance - they have a need for speed and are faster than lists Functionality - SciPy and NumPy have optimized functions such as linear algebra operations built in.

NumPy - Data Types NumPy supports a much greater variety of numerical types than Python does. The following table shows different scalar data types defined in NumPy.

Data Types & Description

Sr.No. 1

bool_ Boolean (True or False) stored as a byte

2

int_ Default integer type (same as C long; normally either int64 or int32)

3

Intc Identical to C int (normally int32 or int64)

4

Intp Integer used for indexing (same as C ssize_t; normally either int3 2 or int64)

5

int8 Byte (-128 to 127)

6

int 16 Integer (-32768 to 32767)

7

int 3 2 Integer (-2147483648 to 2147483647)

8

int64 Integer (-9223372036854775808 to 9223372036854775807)

9

uint8 Unsigned integer (0 to 2 5 5)

10

uint 16 Unsigned integer (0 to 6 5 5 3 5)

11

uint32 Unsigned integer (0 to 4294967295)

12

uint64 Unsigned integer (0 to 18446744073709551615)

13

float_ Shorthand for float6 4

14

floatl6 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa

15

float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa

16

float64 Double precision float: sign bit, 11 bits exponent, 5 2 bits mantissa

17

complex. Shorthand for complex 128

18

complex64 Complex number, represented by two 3 2-bit floats (real and imaginary compo­ nents)

19

complexl28 Complex number, represented by two 64-bit floats (real and imaginary compo­ nents)

NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. The dtypes are available as np.bool_, np.float32, etc.

Data Type Objects (dtype) A data type object describes interpretation of fixed block of memory corresponding to an array, depending on the following aspects • Type of data (integer, float or Python object) • Size of data • Byte order (little-endian or big-endian) • In case of structured type, the names of fields, data type of each field and part of the memory block taken by each field.

• If data type is a subarray, its shape and data type The byte order is decided by prefixing '' to data type. '' means that encoding is big-endian (most significant byte is

stored in smallest address). A dtype object is constructed using the following syntax -

numpy.dtype(object, align, copy)

The parameters are -

• Object - To be converted to data type object • Align - If true, adds padding to the field to make it similar to C-struct

• Copy - Makes a new copy of dtype object. If false, the result is reference to builtin data type object Example 1 # using array-scalar type import numpy as np dt = np.dtype(np.int32)

print dt The output is as follows int32

Example 2 #int8, inti 6, int32, int64 can be replaced by equivalent string 'il', ,i2','i4', etc. import numpy as np

dt = np.dtype('i4')

print dt

The output is as follows int32

The following examples show the use of structured data type. Here, the field name and the corresponding

scalar data type is to be declared.

Example 3

# first create structured data type import numpy as np dt = np.dtype([('empage',np.int8)])

print dt

The output is as follows [('empage', 'il')]

Each built-in data type has a character code that uniquely identifies it.

• 'b' - boolean • 'i' - (signed) integer • 'u' - unsigned integer • 'f' - floating-point • 'c' - complex-floating point • 'm' - timedelta • 'M' - datetime • 'O' - (Python) objects • 'S', 'a' - (byte-)string • 'U' - Unicode • 'V' - raw data (void)

NumPy - Ways of Array Creation A new ndarray object can be constructed by any of the following array creation routines or using a lowlevel ndarray constructor.

numpy.empty It creates an uninitialized array of specified shape and dtype. It uses the following constructor numpy.empty(shape, dtype = float, order = 'C') The constructor takes the following parameters.

Parameter & Description

Sr.No. 1

Shape Shape of an empty array in int or tuple of int

2

Dtype Desired output data type. Optional

3

Order 'C for C-style row-major array, 'F' for FORTRAN style column-major array

Example The following code shows an example of an empty array.

import numpy as np

x = np.empty([3,2], dtype = int)

print x

The output is as follows -

[[22649312

1701344351]

[1818321759 1885959276] [16779776 156368896]]

Note - The elements in an array show random values as they are not initialized. numpy.zeros Returns a new array of specified size, filled with zeros.

numpy.zeros(shape, dtype = float, order = 'C')

The constructor takes the following parameters.

Sr.No.

Parameter & Description

1

Shape Shape of an empty array in int or sequence of int

2

Dtype Desired output data type. Optional

3

Order 'C for C-style row-major array 'F' for FORTRAN style column-major array

Example 1

# array of five zeros. Default dtype is float import numpy as np

x = np.zeros(5) print x

The output is as follows [0. 0. 0. 0. 0.] Example 2

import numpy as np

x = np.zeros((5,), dtype = np.int) print x

Now, the output would be as follows [0 0 0 0 0]

numpy.ones Returns a new array of specified size and type, filled with ones.

numpy.ones(shape, dtype = None, order = 'C') The constructor takes the following parameters.

Sr.No.

Parameter & Description

Shape Shape of an empty array in int or tuple of int 2

Dtype Desired output data type. Optional

Order 'C for C-style row-major array, 'F' for FORTRAN style column-major array

Example 1

# array of five ones. Default dtype is float import numpy as np

x = np.ones(5) print x

The output is as follows [1. 1. 1. 1. 1.]

Example 2 import numpy as np

x = np.ones([2,2], dtype = int)

print x

Now, the output would be as follows [[1 1]

[1 1]]

Introduction to Data Structures Pandas deals with the following three data structures -



Series



DataFrame



Panel

These data structures are built on top of Numpy array, which means they are fast.

Dimension & Description The best way to think of these data structures is that the higher dimensional data structure is a container of

its lower dimensional data structure. For example, DataFrame is a container of Series, Panel is a container of DataFrame.

Data Structure

Dimensions

Description

Series

1

ID labeled homogeneous array, sizeimmutable.

Data Frames

2

General 2D labeled, size-mutable tabular structure with

potentially heterogeneously typed columns. Panel

3

General 3D labeled, size-mutable array.

Python Pandas - Series Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python

objects, etc.)- The axis labels are collectively called index. pandas.Series

A pandas Series can be created using the following constructor pandas.Series( data, index, dtype, copy) The parameters of the constructor are as follows -

S.No

Parameter & Description

1

Data data takes various forms like ndarray, list, constants

2

Index Index values must be unique and hashable, same length as data. Default np.ar-

range(n) if no index is passed. 3

Dtype dtype is for data type. If None, data type will be inferred

4

copy Copy data. Default False

A series can be created using various inputs like •

Array



Diet

.

Scalar value or constant

Create an Empty Series A basic series, which can be created is an Empty Series. Example #import the pandas library and aliasing as pd

import pandas as pd

s = pd.Series()

print s Its output is as follows -

Series([], dtype: float64)

Create a Series from ndarray If data is an ndarray, then index passed must be of the same length. If no index is passed, then by default index will be range(n) where n is array length, i.e., [0,1,2,3.... range(len(array))-l].

Example 1 #import the pandas library and aliasing as pd

import pandas as pd import numpy as np

data = np.arrayd'a'/j'/i'/t']) s = pd.Series(data) print s Its output is as follows -

0 a

1 j

2 i 3 t

dtype: object We did not pass any index, so by default, it assigned the indexes ranging from 0 to len(data)-!, i.e., 0 to 3.

Example 2 #import the pandas library and aliasing as pd

import pandas as pd import numpy as np

data = np.array(['a','b','c','d']) s = pd.Series(data,index=[10,l 1,12,13]) print s Its output is as follows 10 a

11 b

12 c 13 d

dtype: object We passed the index values here. Now we can see the customized indexed values in the output.

Create a Series from diet A diet can be passed as input and if no index is specified, then the dictionary keys are taken in a sorted order to construct index. If index is passed, the values in data corresponding to the labels in the index will

be pulled out.

Example 1

#import the pandas library and aliasing as pd

import pandas as pd import numpy as np

data = {'a': 0., 'b':l., 'c':2.} s = pd.Series(data) print s

Its output is as follows a 0.0

b 1.0 c 2.0

dtype: float64

Observe - Dictionary keys are used to construct index. Create a Series from Scalar If data is a scalar value, an index must be provided. The value will be repeated to match the length of index

#import the pandas library and aliasing as pd

import pandas as pd import numpy as np

s = pd.Series(5, index=[0,1, 2, 3]) print s Its output is as follows -

0 5

1 5

2 5 3 5

dtype: int64

Accessing Data from Series with Position Data in the series can be accessed similar to that in an ndarray. Example 1 Retrieve the first element. As we already know, the counting starts from zero for the array, which means the first element is stored at zeroth position and so on.

import pandas as pd

s = pd.Series([l,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the first element print s[0] Its output is as follows -

1 Example 2

Retrieve the first three elements in the Series. If a : is inserted in front of it, all items from that index

onwards will be extracted. If two parameters (with : between them) is used, items between the two in­ dexes (not including the stop index)

import pandas as pd

s = pd.Series([l,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the first three element print s[:3] Its output is as follows a 1

b 2 c 3

dtype: int64

Retrieve Data Using Label (Index) A Series is like a fixed-size diet in that you can get and set values by index label. Example 1

Retrieve a single element using index label value.

import pandas as pd

s = pd.Series([l,2,3,4,5],index = ['a'/b'/c'/d'/e'])

#retrieve a single element print s['a'] Its output is as follows -

1

Example 2 Retrieve multiple elements using a list of index label values.

import pandas as pd

s = pd.Series([l,2,3,4,5],index = ['a'/b'/c'/d'/e'])

#retrieve multiple elements print s[['a','c','d']] Its output is as follows a 1

c 3

d 4 dtype: int64

Python Pandas DataFrame The

Pandas

library

Python Pandas

documentation defines a DataFrame as a two-dimensional, size-mutable, po­

tentially heterogeneous tabular data

DataFrame Basics

structure with labeled axes (rows and

columns).

In plain terms, think of a DataFrame as a table of data, i.e. a single set of formatted two-dimensional data, with the following characteristics:



There can be multiple rows and columns in the data.



Each row represents a sample of data,



Each column contains a different variable that describes the samples (rows).



The data in every column is usually the same type of data e.g. numbers, strings, dates.



Usually, unlike an excel data set, DataFrames avoid having missing values, and there are no gaps and empty values between rows or columns.

Structure Let us assume that we are creating a data frame with students data.

Columns

Regd. No

Name

MarksQ/o

1 ooo

Steve

86.29

1 OOl

Mathew

91.63

1002

Jose

72.90

1003

Patty

69.23

100-4

Vin

88.30

You can think of it as an SQL table or a spreadsheet data representation.

pandas.DataFrame A pandas DataFrame can be created using the following constructor pandas.DataFrame( data, index, columns, dtype, copy)

The parameters of the constructor are as follows -

S.No

Parameter & Description

1

Data data takes various forms like ndarray, series, map, lists, diet, constants and also another DataFrame.

Index For the row labels, the Index to be used for the resulting frame is Optional Default np.ar-

2

range(n) if no index is passed.

Columns For column labels, the optional default syntax is - np.arrange(n). This is only true if

3

no index is passed. 4

Dtype Data type of each column.

4

Copy This command (or whatever it is) is used for copying of data, if the default is False.

Create DataFrame A pandas DataFrame can be created using various inputs like Lists

diet Series

Numpy ndarrays Another DataFrame

Create an Empty DataFrame A basic DataFrame, which can be created is an Empty Dataframe.

Example #import the pandas library and aliasing as pd

import pandas as pd df = pd.DataFrame()

print df Its output is as follows -

Empty DataFrame Columns: [] Index: []

Create a DataFrame from Lists The DataFrame can be created using a single list or a list of lists.

Example 1 import pandas as pd

data = [1,2,3,4,5] df = pd.DataFrame(data)

print df Its output is as follows -

0

0 1

1

2

2 3 3 4

4 5

Example 2 import pandas as pd

data = [['Ajit', 10],['Boby', 1 2 ],['Clarke', 13]] df = pd.DataFrame(data,columns=['Name', 'Age'])

print df Its output is as follows -

Name 0

Ajit

10

1

Boby

12

2

Clarke 13

Age

Example 3 import pandas as pd

data = [['Ajit',10],['Boby',12],['Clarke',13]] df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)

print df

Its output is as follows -

Name Age 0

Ajit

10.0

1

Boby

12.0

2

Clarke 13.0

Note - Observe, the dtype parameter changes the type of Age column to floating point. Create a DataFrame from Diet of ndarrays I Lists All the ndarrays must be of same length. If index is passed, then the length of the index should equal to the length of the arrays. If no index is passed, then by default, index will be range(n), where n is the array length.

Example 1

import pandas as pd

data = {'Name':[Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]} df = pd.DataFrame(data)

print df Its output is as follows -

Age

Name

0

28

Tom

1

34

Jack

2

29

Steve

3

42

Ricky

Note - Observe the values 0,1,2,3. They are the default index assigned to each using the function range(n). Create a DataFrame from List of Diets List of Dictionaries can be passed as input data to create a DataFrame. The dictionary keys are by default

taken as column names.

Example 1 The following example shows how to create a DataFrame by passing a list of dictionaries.

import pandas as pd

data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] df = pd.DataFrame(data)

print df Its output is as follows a b

0 1 2

c NaN

1 5 10 20.0

Note - Observe, NaN (Not a Number) is appended in missing areas. Column Selection Let us now understand column selection, addition, and deletion through examples.

We will understand this by selecting a column from the DataFrame.

Example import pandas as pd

d = {'one': pd.Series([l, 2, 3], index=['a', 'b', 'c']), 'two': pd.Series([l, 2, 3,4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

print df ['one'] Its output is as follows a

1.0

b

2.0

c

3.0

d

NaN

Name: one, dtype: float64

Column Addition We will understand this by adding a new column to an existing data frame.

Example

import pandas as pd

d = {'one': pd.Series([l, 2, 3], index=['a', 'b', 'c']), 'two': pd.Series([l, 2, 3,4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

# Adding a new column to an existing DataFrame object with column label by passing new series

print ("Adding a new column by passing as Series:") df['three']=pd.Series([10,20,30],index=['a','b','c'])

print df

print ("Adding a new column using the existing columns in DataFrame:")

df['four']=df['one']+df{'three']

print df Its output is as follows -

Adding a new column by passing as Series: one two three

a 1.0 1

10.0

b 2.0 2 20.0 c 3.0 3 30.0

d NaN 4 NaN

Adding a new column using the existing columns in DataFrame: onetwothree four a

1.0 1

10.0

11.0

b

2.0 2

20.0

22.0

c

3.0 3

30.0 33.0

d

NaN 4

NaN

NaN

Column Deletion Columns can be deleted or popped; let us take an example to understand how.

Example

# Using the previous DataFrame, we will delete a column # using del function import pandas as pd

d = {'one': pd.Series([l, 2, 3], index=['a', 'b', 'c']), 'two': pd.Series([l, 2, 3,4], index=['a', 'b', 'c', 'd']),

'three': pd.Series([ 10,20,30], index=['a','b','c'])}

df = pd.DataFrame(d)

print ("Our dataframe is:") print df

# using del function

print ("Deleting the first column using DEL function:") del dfl'one']

print df

# using pop function

print ("Deleting another column using POP function:") df.pop('two')

print df Its output is as follows -

Our dataframe is:

one three two a

1.0 10.0 1

b

2.0 20.0 2

c

3.0 30.0 3

d

NaN

NaN 4

Deleting the first column using DEL function:

three two a

10.0

1

b

20.0

2

c

30.0

3

d

NaN

4

Deleting another column using POP function:

three a 10.0

b 20.0 c 30.0

d NaN

Row Selection, Addition, and Deletion We will now understand row selection, addition and deletion through examples. Let us begin with the concept of selection.

Selection by Label Rows can be selected by passing row label to a loc function. import pandas as pd

d = {'one': pd.Series([l, 2, 3], index=['a', 'b', 'c']), 'two': pd.Series([l, 2, 3,4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

print df.loc['b'] Its output is as follows -

one 2.0 two 2.0 Name: b, dtype: float64 The result is a series with labels as column names of the DataFrame. And, the Name of the series is the

label with which it is retrieved.

Selection by integer location Rows can be selected by passing integer location to an iloc function.

import pandas as pd

d = {'one': pd.Series([l, 2, 3], index=['a', 'b', 'c']), 'two': pd.Series([l, 2, 3,4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

print df.iloc[2] Its output is as follows -

one 3.0 two 3.0 Name: c, dtype: float64

Slice Rows Multiple rows can be selected using ‘’ operator.

import pandas as pd

d = {'one': pd.Series([l, 2, 3], index=['a', 'b', 'c']), 'two': pd.Series([l, 2, 3,4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

print df[2:4] Its output is as follows -

one two c

3.0

d

NaN

3

4

Addition of Rows Add new rows to a DataFrame using the append function. This function will append the rows at the end.

import pandas as pd df = pd.DataFrame([[l, 2], [3,4]], columns = ['a'/b'])

df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a'/b']) df = df.append(df2)

print df

Its output is as follows ab

0 1 2

1 3 4 0 5 6

1 7 8

Deletion of Rows Use index label to delete or drop rows from a DataFrame. If label is duplicated, then multiple rows will be

dropped. If you observe, in the above example, the labels are duplicate. Let us drop a label and will see how many rows will get dropped.

import pandas as pd df = pd.DataFrame([[l, 2], [3,4]], columns = ['a','b'])

df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b']) df = df.append(df2)

# Drop rows with label 0 df = df.drop(O)

print df

Its output is as follows -

ab

134 1 78

In the above example, two rows were dropped because those two contain the same label 0.

Python Pandas - Panel A panel is a 3D container of data. The term Panel data is derived from econometrics and is partially re­ sponsible for the name pandas - pan(el)-da(ta)-s.

The names for the 3 axes are intended to give some semantic meaning to describing operations involving

panel data. They are -

• items - axis 0, each item corresponds to a DataFrame contained inside. • major_axis - axis 1, it is the index (rows) of each of the DataFrames. • minor_axis - axis 2, it is the columns of each of the DataFrames. pandas.Panel() A Panel can be created using the following constructor pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)

The parameters of the constructor are as follows -

Parameter

Description

data

Data takes various forms like ndarray, series, map, lists, diet, constants and also another DataFrame

items

axis=O

major_axis

axis=l

minor_axis

axis=2

dtype

Data type of each column

copy

Copy data. Default, false

Create Panel A Panel can be created using multiple ways like •

From ndarrays



From diet of DataFrames

From 3D ndarray

# creating an empty panel import pandas as pd import numpy as np

data = np.random.rand(2,4,5) p = pd.Panel(data) print p Its output is as follows -

Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)

Items axis: 0 to 1 Major_axis axis: 0 to 3 Minor_axis axis: 0 to 4

Note - Observe the dimensions of the empty panel and the above panel, all the objects are different. From diet of DataFrame Objects #creating an empty panel import pandas as pd import numpy as np

data = {'Iteml': pd.DataFrame(np.random.randn(4, 3)), 'Item2': pd.DataFrame(np.random.randn(4, 2))} p = pd.Panel(data)

print p Its output is as follows -

Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)

Items axis: 0 to 1 Major_axis axis: 0 to 3 Minor_axis axis: 0 to 4

Create an Empty Panel An empty panel can be created using the Panel constructor as follows #creating an empty panel import pandas as pd

p = pd.Panel() print p Its output is as follows -

Dimensions: 0 (items) x 0 (major_axis) x 0 (minor_axis)

Items axis: None Major_axis axis: None Minor_axis axis: None

Selecting the Data from Panel Select the data from the panel using -



Items



Major_axis



Minor_axis

Using Items # creating an empty panel import pandas as pd import numpy as np

data = {'Iteml' :pd.DataFrame(np.random.randn(4, 3)), 'Item2': pd.DataFrame(np.random.randn(4, 2))} p = pd.Panel(data) print pf'Iteml'] Its output is as follows -

0

1

2

0 0.488224 -0.128637 0.930817

1 0.417497 0.896681 0.576657

2 -2.775266 0.571668 0.290082 3 -0.400538 -0.144234 1.110535 We have two items, and we retrieved iteml. The result is a DataFrame with 4 rows and 3 columns, which

are the Major_axis and Minor_axis dimensions.

Using major_axis Data can be accessed using the method panel.major_axis(index).

# creating an empty panel import pandas as pd import numpy as np

data = {'Iteml' :pd.DataFrame(np.random.randn(4, 3)), 'Item2': pd.DataFrame(np.random.randn(4, 2))} p = pd.Panel(data) print p.major_xs(l) Its output is as follows -

Iteml

Item2

0 0.417497 0.748412

1 0.896681 -0.557322

2 0.576657

NaN

Using minor_axis Data can be accessed using the method panel.minor_axis(index).

# creating an empty panel import pandas as pd import numpy as np

data = {'Iteml' :pd.DataFrame(np.random.randn(4, 3)), 'Item2': pd.DataFrame(np.random.randn(4, 2))}

p = pd.Panel(data) print p.minor_xs( 1) Its output is as follows -

Iteml

Item2

0 -0.128637 -1.047032

1 0.896681 -0.557322

2 0.571668 0.431953 3 -0.144234 1.302466

Data Transferring

Use pandas to Import a CSV File into Python This is how the info would look like in the CSV file (where the CSV file name is (Client_info):

A 1 Client Name 2 Jon Smith 3jBill Martin 4 Maria Blue 5 Rita Yu oljack Mo 7 Ron Green 8 Jeff Long 9 Carrie Lan 10 Marry Sig 11 Ben Baker

B Country Japan US Canada Brazil UK Spain China Italy Peru Russia

C D Product Purchase Price Computer $800 Tablet $450 Printer $150 Laptop $1,200 Monitor | $300 Laptop $1,200 Laptop $1,200 Computer $800 Computer $800 Printer $150

(1) First, capture the full path where your CSV file is stored. In my case, the CSV file is stored under the

following path: C:\Client_info.csv

(2) Make sure that the number of columns and names in the CSV file match with the number of columns and names assigned in the Python code.

Here is the code for our example (you can find additional comments within the code itself to clarify some of the components): import pandas as pd from pandas import DataFrame

#read the csv file (put 'r' before the path string to address any special characters, such s '\'). Don't forget to put the file name at the end of the path + ".csv"

read_clients = pd.read_csv (r'C:\client_info.csv')

# assign column names df = DataFrame(read_clients,columns=['Client Name','Country','Product','Purchase Price'])

print (df) (3) Finally, run the Python code. This is the result that youll get:

In [7]: runfile(’C:/Users/327006128/De5ktop/Import_panda5/Import_panda5_001.py’, wdir='C:/Users/327006128/Desktop/Import_pandas’) Client Name Country Product Purchase Price 0 Jon Smith Japan Computer S800 1 Bill Martin Tablet $450 US Printer 2 Maria Blue Canada $150 Brazil Laptop $1,200 3 Rita Yu Monitor UK 4 Jack Mo $300 5 Ron Green Spain Laptop $1,200 6 Jeff Long China Laptop $1,200 Italy Computer $800 7 Carrie Lan 8 Marry Sig Peru Computer $800 Printer 9 Ben Baker Russia $150

A dataframe can be manipulated using methods, the minimum and maximum can easily be extracted:

from pandas import DataFrame, read_csv import matplotlib.pyplot as pit import pandas as pd

file = r'c:\client_info' df = pd.read_csv(file)

print('Max', dff'Purchase Price'].max())

print('Min', df[' Purchase Price'].min())

Use pandas to write a Pandas DataFrame to a File When you have done your data munging and manipulation with Pandas, you might want to export the DataFrame to another format. This section will cover two ways of outputting your DataFrame: to a CSV or to an Excel file.

Saving A pandas Dataframe As A CSV To write a DataFrame as a CSV file, you can use to_csv():

imgor^anda^^d^J df.to_csvCexample.csv1) import modules import pandas as pd

Create dataframe raw_data = {'first_name': ['Jas', 'Moly', 'Tina', 'Jakson', 'Amit'], 'last_name': ['Millar', 'Jacson', 'Ali', 'Singh', 'Cozey'],

'age': [45, 52, 33, 24, 73],

'preTestScore': [4,26, 35,2, 3], 'postTestScore': [29, 94, 57, 52, 60]}

df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])

Save the dataframe called df as csv

# df.to_csv('example.csv')

That piece of code seems quite simple, but this is just where the difficulties begin for most people because

you will have specific requirements for the output of your data. Maybe you dont want a comma as a delim­ iter, or you want to specify a specific encoding, Dont worry! You can pass some additional arguments to to_csv() to make sure that your data is outputted the way you want it to be! To delimit by a tab, use the sep argument:

import pandas as pd

df.to_csv('example.csv', sep='\t') To use a specific character encoding, you can use the encoding argument:

import pandas as pd

df.to_csv('example.csv', sep='\t', encoding='utf-8')

Output a DataFrame to Excel

Similarly to what you did to output your DataFrame to CSV, you can use to_excel() to write your table to

Excel. However, it is a bit more complicated:

import pandas as pd

writer = pd.ExcelWriter('myDataFrame.xlsx') df.to_excel(writer, 'DataFrame')

writer.save()

Data Transferring - SQLite Databases and DataFrame SQLite is a database engine that makes it simple to store and work with relational data. Much like the csv format, SQLite stores data in a single file that can

be easily shared with others. Most programming lan­

guages and environments have good support for work­ ing with SQLite databases. Python is no exception, and a library to access SQLite databases, called sqlite3, has been included with Python since version 2.5.

In this section, we'll walk through how to use sqlite3 to read data into dataframe from sqlite table with the pandas package. We'll be using Python 3.5, but this same approach should work with Python 2. All of the data is in a SQLite database called flights.db, which contains table- airlines. Here are two rows from the airlines table:

Id

Name

Country

Active

11

4D Air

Thailand

Y

Canada

N

611897 Alberta 12

Limited

Reading results into a pandas DataFrame We can use the pandas read_sql query function to read the results of a SQL query directly into a pandas

DataFrame. The below code will execute the same query that we just did, but it will return a DataFrame. It

has several advantages over the query we did above: It automatically reads in the names of the headers from the table. It creates a DataFrame, so we can quickly explore the data.

import pandas as pd import sqlite3 conn = sqlite3.connect("flights.db")

df = pd.read_sql_query("select * from airlines", conn)

print(df)

Id

Name

Country

Active

11

4D Air

Thailand

Y

Canada

N

611897 Alberta 12

Limited

As you can see, we get a nicely formatted DataFrame as the result. We could easily manipulate the columns:

print(df["Country"]) 0

Thailand

1

Canada

Name: Country, dtype: object It's highly recommended to use the read_sql_query function when possible.

Creating tables with pandas The pandas package gives us a much faster way to create tables. We just have to create a DataFrame first, then export it to a SQL table. First, we'll create a DataFrame:

from datetime import datetime df = pd.DataFrame( [[1, datetime(2016, 9, 29,0, 0), datetime(2016, 9, 29,12,0), 'Tl', 1]],

columns=["id", "departure", "arrival", "number", "route_id"] )

Then, we'll be able to call the to sql method to convert df to a table in a database. We set the keep_exists parameter to replace to delete and replace any existing tables named dailyjflights:

df.to_sql("daily_flights", conn, if_exists="replace")

We can then verify that everything worked by querying the database:

pd.read_sql_query("select * from daily_flights;", conn)

0

index

id Departure

0

1

2016-09-29 00:00:00

arrival

number route_id

2016-09-29 12:00:00

T1

1

Chapter 19 GUI Programming using Tkinter Until now, the only way our programs interact with the user is through keyboard input via the input statement. But most real applications use windows, buttons, checkboxes, scrollbars, and other compo­

nents. These components are part of a Graphical User Interface (GUI).

Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkin-

Tkinter

ter provides a powerful object-oriented interface to the Tk GUI toolkit (tkinter module comes by de­ fault with python 3.x versions).

Almost every GUI program we will write will contain the following three lines:

from tkinter import. root = Tk()

mainloopQ

Line-1 imports all of the GUI stuff from the tkinter module.

Line-2 creates a window on the screen, which we call root. Line-3 puts the program into what is essentially a long-running while loop called the event loop. This loop

runs, waiting for keypresses, button clicks, etc., and it exits when the user closes the window. Here is a working GUI program that converts temperatures from Fahrenheit to Celsius from tkinter import *

def calculate(): tmp = int(entry.get()) tmp = 9/5*tmp+32 output_label.configure(text = 'Converted: {:.lf]'.format(temp)) entry.delete(O,END) root = Tk()

message_label = Label(text='Enter a temperature', font=(Arial1,14)) outputjabel = Label(font=('Arial', 14))

entry = Entry(font=('Arial', 14), width=4)

calc_button = Button(text='Ok', font=('Arial', 14), command^ calculate)

message_label.grid(row=0, column=0) entry.grid(row=0, column = 1)

calc_button.grid(row=0, column= 2) output_label.grid(row=l, column=0, columnspan=3) mainloopO

Here is the output of program:

=_LQl2£l

Enter a temperature 20

Ok

Converted: 68.0

Components of the program in details.

Label component A label is a container for your program to place some text on the screen. The following code creates a label and places it on the screen.

hllo_label=Label(text='hello')

hllo_label.grid(row=0, column=0)

We call Label to create a new label where labels name is hllojabel. Once created, use the grid method to place the label on the screen.

Options of Label There are a number of options we can change including font size and color. Here are some examples: hellojabel =

Label(text='hello', font=('Arial', 14, 'bold'), bg='blue', fg-white')

Note the use of keyword arguments.

Here are a few common options:

font

The basic structure is font= (font name, font size, style). You can leave out the font size or the style. The choices for style are

'bold', 'italic', 'underline', 'overstrike', 'roman', and 'normal' (which is the default). We can combine multiple styles like this:

'bold underline italic'.

fg and bg These stand for foreground and background. Many common color names can be used, like 'red', 'yellow', etc. width

This is how many characters long the label should be.

height

This is how many rows high the label should be.

You can use this for multiline labels. Use newline characters in the text to get it to span multiple lines. For example, text='hello \n ajit'.

Changing label properties

Later in your program, after youve created a label, you may want to change something about it. To do that,

use its configure method. Here are two examples that change the properties of a label called label: label.configure(text='Bye Bye') label.configure(bg='white', fg='red')

Setting text to something using the configure method is kind of like the GUI equivalent of a print state­

ment. However, in calls to configure we cannot use commas to separate multiple things to print. We instead need to use string formatting. Here is a print statement and its equivalent using the configure method.

print('a =', a, 'and b =', b) label.configure(text='a = {}, andb = {}'.format(a,b)) Note: The configure method works with most of the other components we will see.

grid The grid method is used to place things on the screen. It lays out the screen as a rectangular grid of rows and

columns. The first few rows and columns are show below.

Spanning multiple rows or columns There are optional arguments, rowspan and columnspan, that allow a component to take up more than one row or column. Here is an example of several grid statements followed by what the layout will look like:

label l.grid(row=0, column=0)

label2.grid(row=0, column= 1) labels.grid(row= 1, column=0, columnspan=2)

label4.grid(row= 1, column=2) label5.grid(row=2, column=2)

Spacing To add extra space between components, there are optional arguments padx and pady.

Important note: Any time you create a component, to place it on the screen you need to use grid (or one of its

cousins, like pack, which we will talk about later). Otherwise it will not be visible. Entry component Entry boxes are used to get text input. The following example creates a simple entry box and places it on the

screen. entry = EntryO

entry.grid(row=0, column=0) Most of the same options that work with labels are work with entry boxes. The width option is particularly

helpful because the entry box will often be wider than you need.

Getting text To get the text from an entry box, use its get method. This will return a string. If you need numerical

data, use eval (or int or float) on the string. Here is a simple example that gets text from an entry box named entry.

string_value = entry. get()

num_value = eval(entry.get())

Deleting text To clear an entry box, use the following: entry.delete(O,END)

Inserting text To insert text into an entry box, use the following:

entry.insert(O, 'hello')

Button component The following example creates a simple button: ok_button = Button(text='Ok')

To get the button to do something when clicked, use the command argument. It is set to the name of a function, called a callback function. When the button is clicked, the callback function is called. Here is an

example: from tkinter import *

def callback(): label.configure(text='Button clicked') root = Tk()

label = Label(text='Not clicked')

button = Button(text='Click me', command=callback) label.grid(row=0, column=0) button.grid(row= 1, column=0) mainloopQ

When the program starts, the label says Click me. When the button is clicked, the callback function callback is called, which changes the label to say Button clicked.

Button clicked Click me

lambda trick Sometimes we will want to pass information to the callback function, like if we have several buttons that

use the same callback function and we want to give the function infor-mation about which button is being

clicked. Here is an example where we create 26 buttons, one for each letter of the alphabet. Rather than use

2 6 separate ButtonQ statements and 2 6 different functions, we use a list and one function, from tkinter import * alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def callback(x): label.configure(text='Button {} clicked'.format(alphabet[x])) root = Tk()

label = Label() label.grid(row=l, column=0, columnspan=26) buttons = [0]*2 6 # create a list to hold 26 buttons for i in range(26):

buttons[i] = Button(text=alphabet[i],

command = lambda x=i: callback(x)) buttons[i].grid(row=0, column=i)

mainloopO We set buttons=[0].2 6. This creates a list with 26 things in it. An alternate way to create the list would be to

set buttons=[] and use the append method.

We use one callback function and it has one argument, which indicates which button was clicked. As far

as the lambda trick goes, without getting into the details, command=callback(i) does not work, and that is

why we resort to the lambda trick, s.

Frame component Lets say we want 2 6 small buttons across the top of the screen, and a big Ok button below them, like below:

The frames job is to contain other components and essentially combine them into one large component.

In this case, we will create a frame to group all of the letter buttons into one large component. The code is shown below:

from tkinter import * alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' root = Tk()

buttonjframe = Frame()

buttons = [0]*26

fori in range(26):

buttonsfi] = Button(button_frame, text=alphabet[i]) buttons[i].grid(row=0, column=i)

ok_button = Button(text='Ok', font=('Verdana', 24)) button_frame.grid(row=0, column=0) ok_button.grid(row= 1, column=0) mainloopQ

To create a frame, we use FrameQ and give it a name. Then, for any components we want include in the

frame, we include the name of the frame as the first argument in the components declaration. We still have to grid the components, but now the rows and columns will be relative to the frame. Finally, we have to grid

the frame itself.

Colors

Tkinter defines many common color names, like 'yellow' and 'red'. It also provides a way to get access to

millions of more colors. We first try to understand how colors are displayed on the screen. Each color is broken into three components; a red, a green, and a blue component.

Each component can have a value from 0 to 255, with 255 being the full amount of that color. Equal parts

of red and green create shades of yellow, equal parts of red and blue create shades of purple, and equal parts of blue and green create shades of turquoise. Equal parts of all three create shades of gray. Black is when all

three components have values of 0 and white is when all three components have values of 255. Varying the values of the components can produce up to 25 63 = 16 million colors. There are a number of resources on the web that allow you to vary the amounts of the components and see what color is produced.

To use colors in Tkinter is easy, but with one catchcomponent values are given in hexadecimal. Hexadeci­ mal is a base 16 number system, where the letters A-F are used to represent the digits 10 through 15. It was

widely used in the early days of computing, and it is still used here and there. Here is a table comparing the

two number bases: 0

0

8

8

16

10

80

50

1

1

9

9

17

11

100

64

2

2

10

A

18

12

128

80

3

3

11

B

31

IF

160

AO

4

4

12

C

32

20

200

C8

5

5

13

D

33

21

254

FE

6

6

14

E

48

30

255

FF

7

7

15

F

64

40

256

100

Because the color component values run from 0 to 2 5 5, they will run from 0 to FF in hexadeci-mal, and thus are described by two hex digits. A typical color in Tkinter is specified like this: '#A2O2FF'. The color name is prefaced with a pound sign. Then the first two digits are the red component (in this case A2, which is 162 in decimal). The next two digits specify the green compo-nent (here 02, which is 2 in decimal), and the last

two digits specify the blue component (here FF, which is 2 5 5 in decimal).

label = Label(text='Hi', bg='#A202FF') If you would rather not bother with hexadecimal, you can use the following function which will convert

percentages into the hex string that Tkinter uses.

def color_convert(r, g, b): return '#{:02x}{:02x}{:02x}'.format(int(r.2.55),int(g.2.55), int(b.2.55)) Example of it to create a background color that has 10% of the red component, 85% of green and 60% of blue. label = Label(text='Hi', bg=color_convert(10, 85, 60))

Images Labels and buttons can display images instead of text. To use an image requires a little set-up work. We first

have to create a PhotoImage object and give it a name. Example: cat_image = Photolmage(file='cat.gif')

Here are some examples of putting the image into components:

label = Label(image=cat_image)

button = Button(image=cat_image, command=cat_callback()) You can use the configure method to set or change an image: label.configure(image=cat_image)

File types One limitation of Tkinter is the only common image file type it can use is GIF. If you would like to use other types of files, one solution is to use the Python Imaging Library, which will be discussed further.

Canvas component A canvas is a component on which we can draw things like lines, circles, rectangles. We can also draw text, images, and other components on it. It is a very versatile component, though we will only describe the ba­ sics here.

Creating canvases The following line creates a canvas with a white background that is 200 x 300 pixels in size:

canvas = Canvas(width=200, height=300, bg='white')

Rectangles The following code draws a red rectangle to the canvas: canvas.create_rectangle(20,100,30,180, fill-blue')

See the image below on the left. The first four arguments specify the coordinates of where to place the rec­ tangle on the canvas. The upper left corner of the canvas is the origin, (0,0). The upper left of the rectangle is at (20,100), and the lower right is at (30,150). If were to leave off fill='red', the result would be a rectan­

gle with a black outline. JOjxJ

tk

Ovals and lines Drawing ovals and lines is similar. The image above on the right is created with the follow­ ing code:

canvas.create_rectangle(20,100,70,180)

canvas.create_oval(20,100,70,180,-

fill='red') canvas.create_line(20,100,70,180, fill='green')

The rectangle is here to show that lines and ovals work similarly to rectangles. The first two coor-dinates are the upper left and the second two are the lower right.

To get a circle with radius r and center (x,y), we can create the following function:

Def create_circle(x,y,r): canvas.create_oval(x-r,y-r,x+r,y+r)

Images We can add images to a canvas. Here is an example: cat_image=Photolmage(file='cat.gif')

canvas.create_image(5 0,50, image=catjmage) The two coordinates are where the center of the image should be.

Naming things, changing them, moving them, and deleting them We can give names to the things we draw on the canvas. We can then use the name to refer to the object in case we want to move it or remove it from the canvas. Here is an example where we create a rectangle,

change its color, move it, and then delete it:

rect=canvas.create_rectangle(O,0,20,20)

canvas. itemconfigure(rect,-

fill='red') canvas.coords(rect,40,40,60,60) canvas.delete(rect)

The coords method is used to move/resize an object and the delete method is used to delete it. If you want to delete everything from the canvas, use the following:

canvas.delete(ALL)

Check buttons and Radio buttons

tk v Show totals ' ’ Red

Green

''’ Blue

In the image below, the

top line shows a check button and the bottom

line shows a radio button.

Check buttons The code for the above check button is: show_totals = IntVarQ check = Checkbutton(text='Show totals', var=show_total) The one thing to note here is that we have to tie the check button to a variable, and it cant be just any

variable, it has to be a special kind of Tkinter variable, called an IntVar. This variable, show_total, will be 0 when the check button is unchecked and 1 when it is checked. To access the value of the variable, you need

to use its get method, like this:

show_total.get()

We can also set the value of the variable using its set method. This will automatically check or uncheck the check button on the screen. For instance, if you want the above check button checked at the start of the pro­ gram, do the following: show_total=IntVar()

show_total.set(l)

check = Checkbutton(text='Show totals', var=show_total)

Radio buttons Radio buttons work similarly. The code for the radio buttons shown at the start of the sec­ tion is:

color = IntVar() redbutton = Radiobutton(text='Red', var=color, value=l)

greenbutton = Radiobutton(text-Green', var=color, value=2)

bluebutton = Radiobutton(text='Blue', var=color, value=3)

The value of the IntVar object color will be 1, 2, or 3, depending on whether the left, middle, or right button is selected. These values are controlled by the value option, specified when we create the radio buttons.

Commands: Both check buttons and radio buttons have a command option, where you can set a callback function to run whenever the button is selected or unselected.

Text component The Text component is a bigger, extended version of the Entry component. Here is an example of creating

one: textbox = Text(font=('Arial', 16), height=6, width=50)

The component will be 5 0 characters wide and 6 rows tall. You can still type past the sixth row; the compo­

nent will just display only six rows at a time, and you can use the arrow keys to scroll. If you want a scrollbar associated with the text box you can use the ScrolledText component. Other than the scrollbar, ScrolledText works more or less the same as Text. An example is of what it looks like is shown below. To use the ScrolledText component, we will need the following import:

from tkinter.scrolledtext import ScrolledText =jgj2

The left mouse button is clicked.

< Double-Button-1 >

The left mouse button is double-clicked.

< Button-Release-1 >

The left mouse button is released.

A click-and-drag with the left mouse button.

The mouse wheel is moved.

< Motion >

The mouse is moved.

< Enter >

The mouse is now over the component.

< Leave >

The mouse has now left the component.

A key is pressed.

< key name >

The key name key is pressed.

For all of the mouse button examples, the number 1 can be replaced with other numbers. Button 2 is the

middle button and button 3 is the right button. The most useful attributes in the Event object are:

Attribute

Description

Keysym

The name of the key that was pressed

x,y

The coordinates of the mouse pointer

Delta

The value of the mouse wheel

Key events For key events, we can either have specific callbacks for different keys or catch all keypresses and deal with

them in the same callback. Here is an example of the latter:

from tkinter import * def callback(event): print(event.keysym)

root = Tk()

root.bind('', callback) mainloopO

The above program prints out the names of the keys that were pressed. You can use those names in if state­

ments to handle several different keypresses in the callback function, like below:

if event.keysym = 'percent': percent (shift+5) was pressed, do something about it...

elif event.keysym = 'a': lowercase a was pressed, do something about it...

Use the single callback method if you are catching a lot of keypresses and are doing something similar with

all of them. On the other hand, if you just want to catch a couple of specific keypresses or if certain keys have very long and specific callbacks, you can catch keypresses separately like below: from tkinter import *

def callback 1 (event): print('You pressed the enter key.')

def callback 1 (event): print('You pressed the up arrow.') root = Tk()

root.bind('< Return >', callback!) root.bind('', callback2)

mainloopQ

The key names are the same as the names stored in the keysym attribute. You can use the program from earlier in this section to find the names of all the keys.

Here are the names for a few common keys: Tkinter name

Common name

< Return >

Enter key

Tab key

Spacebar

,...,

F1,...,F12

,

Page up, Page down

, , ,

Arrow keys

,

Home, End

,

Insert, Delete

,

Caps lock, Number lock

,

Left and right Control keys

,

Left and right Alt keys

,

Left and right Shift keys

Most printable keys can be captured with their names, like below:

root.bind('a', callback) root.bind('A', callback) root.bind('-', callback) The exceptions are the spacebar () and the less than sign (). You can also catch key combina­

tions, such as , , , or .

Note These examples all bind keypresses to root, which is our name for the main window. You can also bind keypresses to specific components. For example, if you only want the left arrow key to work on a Canvas

called canvas, you could use the following:

canvas.bind(, callback) One trick here, is that the canvas wont recognize the keypress unless it has the GUIs focus. This can be done

as below: canvas.focus_set()

Event examples Example 1 An example where we can move a rectangle with the up or down arrow keys. from tkinter import *

def callback(event): global move if event.keysym=='Up': move + = 1

elif event.keysym=='Down': move - = 1 canvas.coords(rect, 5 0+move, 50,100+move, 100) root = Tk()

root.bind('', callback)

canvas = Canvas(width=200,height=200) canvas.grid(row=0,column=0) rect = canvas.create_rectangle(50,50,100,100,fill='blue')

move = 0

mainloopQ

Title bar The GUI window that Tkinter creates says Tk by default. Here is how to change it:

root.title('Your title')

Disabling things Sometimes you want to disable a button so it cant be clicked. Buttons have an attribute state that allows

you to disable the component. Use state=DISABLED to disable the button and state=NORMAL to enable it. Here is an example that creates a button that starts out disabled and then enables it: button=Button(text='Hi',state=DISABLED,command=function) button.configure(state=NOR­

MAL) You can use the state attribute to disable many other types of components, too.

Getting the state of a component Sometimes, you need to know things about a component, like exactly what text is in it or what its back­

ground color is. The cget method is used for this. For example, the following gets the text of a label called

label:

label.cget('text') This can be used with buttons, canvases, etc., and it can be used with any of their properties, like bg, fg, state, etc. As a shortcut, Tkinter overrides the [] operators, so that label['text'] accomplishes the same thing

as the example above.

Message boxes

Message boxes are windows that pop up to ask you a question or say something and then go away. To use them, we need an import statement:

from tkinter.messagebox import. There are a variety of different types of message boxes. For each of them you can specify the message the user will see as well as the title of the message box. Here are three types of message boxes, followed by the

code that generates them:

showinfo(title='Message for you', message='Hi There!')

askquestion(title='Quit?',message='Do you really want to quit?') showwarning(title='Warning', message='Unsupported format')

Below is a list of all the types of message boxes. Each displays a message in its own way. Message Box

Special properties

showinfo

OK button

askokcancel

OK and Cancel buttons

askquestion

Yes and No buttons

askretrycancel

Retry and a Cancel buttons

askyesnocancel

Yes, No, and Cancel buttons

showerror

An error icon and an OK button

showwarning

A warning icon an an OK button

Each of these functions returns a value indicating what the user clicked. See the next section for a simple

example of using the return value. Here is a table of the return values: showinfo

Always returns 'ok'

askokcancel

OKTrue Cancel or window closedFalse

askquestion

Yes'yes' No'no'

askretrycancel

RetryTrue Cancel or window closedFalse

askyesnocancel

YesTrue NoFalse anything elseNone

showerror

Always returns 'ok'

showwarning

Always returns 'ok'

Destroying things To get rid of a component, use its destroy method. For instance, to get rid of a button called button, do the following: button.destroyO

To get rid of the entire GUI window, use the following: root.destroyO

Stopping a window from being closed When your user tries to close the main window, you may want to do something, like ask them if they really want to quit. Here is a way to do that: from tkinter import * from tkinter.messagebox import askquestion def quitter_function():

answer = askquestion(title='Quit?', message='Really quit?') if answer=='yes': root.destroyO

root = Tk()

root.protocol('WM_DELETE_WINDOW', quitter_function) mainloopO

The key is the following line, which cause quitter_function to be called whenever the user tries to close the

window. root.protocol('WM_DELETE_WINDOW', quitter_function)

Updating Tkinter updates the screen every so often, but sometimes that is not often enough. For instance, in a func­ tion triggered by a button press, Tkinter will not update the screen until the function is done.

If, in that function, you want to change something on the screen, pause for a short while, and then change something else, you will need to tell Tkinter to update the screen before the pause. To do that, just use this:

root.update() If you only want to update a certain component, and nothing else, you can use the update method of that

component. For example, canvas.update()

A related thing that is occasionally useful is to have something happen after a scheduled time interval. For instance, you might have a timer in your program. For this, you can use the after method. Its first argument is the time in milliseconds to wait before updating and the second argument is the function to call when the time is right. Here is an example that implements a timer:

from time import time from tkinter import *

def update_timer(): time_left = int(90 - (time()-start))

minutes = timejeft // 60 seconds = timejeft % 60 timejabel.configure(text='{}:{:02d}'.format(minutes, seconds))

root. after( 100, update_timer) root = Tk() timeJabel = LabelQ

time_label.grid(row=0, column=0) start = time() update_timer()

mainloopQ

Dialogs Many programs have dialog boxes that allow the user to pick a file to open or to save a file. To use them in

Tkinter, we need the following import statement:

from tkinter.filedialog import. Tkinter dialogs usually look like the ones that are native to the operating system.

Here are the most useful dialogs:

Dialog

Description

Askopenfilename

Opens a typical file chooser dialog

Askopenfilenames

Like previous, but user can pick more than one file

Asksaveasfilename

Opens a typical file save dialog

Askdirectory

Opens a directory chooser dialog

The return value of askopenfilename and asksaveasfilename is the name of the file selected. There is no return value if the user does not pick a value. The return value of askopenfilenames is a list of files, which

is empty if no files are selected. The askdirectory function returns the name of the directory chosen.

There are some options you can pass to these functions. You can set initialdir to the directory you want the dialog to start in. You can also specify the file types. Here is an example:

filename=askopenfilename(initialdir='c:\\python31W, filetypes=[('Image files', '.jpg .png .gif'),('All files', '.')])

Here is an example that opens a file dialog that allows you to select a text file. The program then displays the contents of the file in a textbox.

from tkinter import * from tkinter.filedialog import * from tkinter. scrolledtext import ScrolledText root = Tk() textbox = ScrolledText()

textbox.gridQ

filename=askopenfilename(initialdir-c:\\python31W, filetypes=[('Text files', '.txt'),('All files', '*')])

s = open(filename).read() textbox.insert(1.0, s)

mainloopQ

Menu bars We can create a menu bar, like the one below, across the top of a window.

Here is an example that uses some of the dialogs from the previous section: from tkinter import * from tkinter.filedialog import *

def open_callback(): filename = askopenfilenameQ

# add code here to do something with filename def saveas_callback(): filename = asksaveasfilenameQ

# add code here to do something with filename root = Tk() menu = Menu()

root.config(menu=menu) file_menu = Menu(menu, tearoff=0)

file_menu.add_command(label='Open',command=open_callback)

file_menu.add_command(label='Save

as', command=saveas_callback)

file_menu.add_separator() file_menu.add_command(label='Exit', command=root.destroy)

menu.add_cascade(label='File', menu=file_menu)

mainloopQ

New windows Creating a new window is easy. Use the Toplevel function: window = Toplevel()

You can add components to the new window. The first argument when you create the component needs to

be the name of the window, like below

new_window = ToplevelQ

label = Label(new_window, text='Hi') label.grid(row=0, col­ umn =0)

pack There is an alternative to grid called pack. It is not as versatile as grid, but there are some places where it is useful. It uses an argument called side, which allows you to specify four locations for your components:

TOP, BOTTOM, LEFT, and RIGHT. There are two useful optional arguments, fill and expand. Here is an example.

button 1=Button(text='Hi')

button 1 .pack(side=TOP,

fill=X)

button2=Button(text='Hi')

button2 .pack(side=BOT­

TOM)

The fill option causes the component to fill up the available space given to it. It can be either X, Y or BOTH. The expand option is used to allow the component to expand when its window is resized. To enable it, use

expand=YES.

Note You can use pack for some frames, and grid for others; just dont mix pack and grid within the same frame, or Tkinter wont know quite what to do.

StringVar Tkinter has another type of variable called a StringVar that holds strings. This type of variable can be used to change the text in a label or a button or in some other components. We already know how to change text using the configure method, and a StringVar provides another way to do it.

To tie a component to a StringVar, use the textvariable option of the component. A StringVar has get and set methods, just like an IntVar, and whenever you set the variable, any components that are tied to it are au­ tomatically updated.

Here is a simple example that ties two labels to the same StringVar. There is also a button that when clicked

will alternate the value of the StringVar (and hence the text in the labels) from tkinter import *

def callback(): global count s.set('Goodbye' if count%2==0 else 'Hello')

count + = 1 root = Tk()

count = 0 s = StringVarO s.set('Hello') labell = Label(textvariable = s, width=10) label2 = Label(textvariable = s, width=10)

button = Button(text = 'Click me', command = callback) label l.grid(row=0, column=0)

label2.grid(row=0, column= 1) button.grid(row= 1, column=0) mainloopO

The Python Imaging Library The Python Imaging Library (PIL) contains useful tools for working with images. As of this writing, the PIL is only available for Python 2.7 or earlier. The PIL is not part of the standard Python distribution, so youll

have to download and install it separately. Its easy to install, though. PIL hasnt been maintained since 2009, but there is a project called Pillow that it nearly compatible with PIL and works in Python 3.0 and later.

Using images other than GIFs with Tkinter Tkinter, as weve seen, cant use JPEGs and PNGs. But it can if we use it in conjunction with the PIL. Here is a simple example:

from Tkinter import. from PIL import Image, ImageTk root = Tk()

cat_image = ImageTk.PhotoImage(Image.open('sun.jpg'))

button=Button(image=catjmage)

button.grid(row=0, column=0)

mainloopO The first line imports Tkinter. Remember that in Python 2 its an uppercase Tkinter. The next line imports a few things from the PIL. Next, where we would have used Tkinters PhotoImage to load an image, we in­

stead use a combination of two PIL functions.

Chapter 20

Database Connectivity The Python database API supply with database-neutral programming interface for different databases.

These are: .

MySQL



SQLite

.

MS SQL



PostgreSQL



Informix



Sybase



Inter-base



Oracle etc.

The DB-API is the standard for Python's database interface.

What is Database? The database is a collection of organized information that can easily be used, managed, update, and they are classified according to their organizational approach.

The Python Database interfaces are categorized into two. These are:

Generic Database Interface Most Python's database interface remains to Python's DB-API standard and most of the databases have

ODBC support. Other than that Java database usually support JDBC and programmers can work with that

from Jython.

Relational Database System Interface This employs a relational model with support of SQL. Lists of general-purpose database systems are:

1. Oracle 2. Informix 3. SAPDB

4. MS SQL server

5. Access 6. Ingres

7. MySQL etc.

Native Python databases are: •

SnakeSQL



Buzhug

What Database API Includes Using Python structure, DB-API provides standard and support for working with databases. The API con­ sists of:



Bring in the API module



Obtain database connection



Issue SQL statements and then store procedures



Close the connection

Benefits of Python Database Programming •

Programming in Python is considerably simple and efficient with compared to other lan­ guages, so as the database programming



Python database is portable, and the program is also portable so both can give an advantage in

case of portability •

Python supports SQL cursors



It also supports Relational Database systems



The API of Python for the database is compatible with other databases also



It is platform independent

Defining MySQL Database It is an interface for associating SQL database server from Python and uses the DB-API of Python to work.

How to Implement MySQL Database To use MySQL database using Python, you need first to install it on your machine; then just type the script given below to implement MySQL within your program.

import MySQLdb If any error occurs then it means that the MySQL module is not installed and programmers can download

it from - https://sourceforge.net/projects/mvsql-python/

Database Program Using Python # importing the module import MySQLdb

# opening a database connection db = MySQLdb.connect (host="127.0.0.1", user="username", passwd="password", db="database")

Note: On success connect() method returns a connection object otherwise OperationalError exception will be

thrown.

print db Expected Output:

# define a cursor object cursor = conn.cursor

# drop table if exists Cursor.execute("IF STUDENT TABLE EXISTS DROP IT")

# query

sql = "CREATE TABLE STUDENT (NAME CHAR(30) NOT NULL, CLASS CHAR(5), AGE INT, GENDER CHAR(8), MARKS INT"

# execute query cursor.execute(sql)

# close object cursor.closeO

# close connection

conn.close()

Database Operations There are various operations that programmers can perform within Python program. To deal with these

statements, you must have a good knowledge of Database programming and SQL.

Statement

Description

INSERT

It is an SQL statement used to create a record into a table.

READ

Fetches useful information from the database.

UPDATE

It is used update those available or already existing record(s).

DELETE

It is used to delete records from the database.

ROLLBACK

It works like "undo", which reverts all the changes that you have made.

INSERT Operation It is required when you want to create your records into a database table.

Example The following example, executes SQL INSERT statement to create a record into EMPLOYEE table -

import MySQLdb

# Open database connection

db = MySQLdb.connect("localhost","ajituser","passl23","WORKDB")

# prepare a cursor object using cursorQ method cursor = db.cursor()

# Prepare SQL query to INSERT a record into the database,

sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""

try: # Execute the SQL command cursor.execute(sql)

# Commit your changes in the database db.commit() except:

# Rollback in case there is any error db.rollbackO

# disconnect from server db.closeQ

Above example can be written as follows to create SQL queries dynamically -

import MySQLdb

# Open database connection db = MySQLdb.connect("localhost","ajituser","passl23","WORKDB")

# prepare a cursor object using cursorQ method cursor = db.cursor()

# Prepare SQL query to INSERT a record into the database,

sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ LAST_NAME, AGE, SEX, INCOME) \ VALUES ('%s', '%s', '%d', '%c', '%d')" % \

('Mr', AJIT', 30, 'M', 20000) try: # Execute the SQL command cursor.execute(sql)

# Commit your changes in the database db.commit() except:

# Rollback in case there is any error db.rollbackO

# disconnect from server db.closeQ

Example Following code segment is another form of execution where you can pass parameters directly -

user_id = "ajit"

password = "pass 123" con.executefinsert into Login values("%s", "%s")' % \

(userjd, password))

READ Operation READ Operation on any database means to fetch some useful information from the database.

Once our database connection is established, you are ready to make a query into this database. You can use

either fetchone() method to fetch single record or fetchall() method to fetech multiple values from a data­ base table.

fetchone() - It fetches the next row of a query result set. A result set is an object that is returned when a cursor object is used to query a table.

fetchall() - It fetches all the rows in a result set. If some rows have already been extracted from the result set, then it retrieves the remaining rows from the result set.

rowcount - This is a read-only attribute and returns the number of rows that were affected by an executeQ method. Example The following procedure queries all the records from EMPLOYEE table having salary more t han 4000 -

import MySQLdb

# Open database connection db = MySQLdb.connect("localhost","ajituser","passl23","WORKDB")

# prepare a cursor object using cursorQ method cursor = db.cursor()

sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > '%d'" % (4000)

try: # Execute the SQL command cursor.execute(sql)

# Fetch all the rows in a list of lists. results = cursor.fetchall() for row in results:

fname = row[0] Iname = row[l]

age = row[2]

sex = row[3] income = row[4]

# Now print fetched result print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \

(fname, Iname, age, sex, income) except:

print "Error: unable to fetch data" # disconnect from server db.closeO This will produce the following result fname=Mr, lname=AJIT, age=30, sex=M, Income=20000

Update Operation UPDATE Operation on any database means to update one or more records, which are already available in the

database. The following procedure updates all the records having SEX as 'M'. Here, we increase AGE of all the males

by one year. Example import MySQLdb

# Open database connection

db = MySQLdb.connect("localhost","ajituser","passl23","WORKDB")

# prepare a cursor object using cursorQ method cursor = db.cursor()

# Prepare SQL query to UPDATE required records

sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')

try: # Execute the SQL command cursor.execute(sql)

# Commit your changes in the database db.commit() except:

# Rollback in case there is any error db.rollbackO

# disconnect from server db.closeQ

DELETE Operation DELETE operation is required when you want to delete some records from your database. Following is the

procedure to delete all the records from EMPLOYEE where AGE is more than 20 -

Example import MySQLdb

# Open database connection db = MySQLdb.connect("localhost","ajituser","passl23","WORKDB")

# prepare a cursor object using cursorQ method cursor = db.cursor()

# Prepare SQL query to DELETE required records

sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)

try: # Execute the SQL command cursor.execute(sql)

# Commit your changes in the database db.commitQ

except:

# Rollback in case there is any error db.rollbackO

# disconnect from server db.closeQ

Performing Transactions Transactions are a mechanism that ensures data consistency. Transactions have the following four proper­

ties -

Atomicity - Either a transaction completes or nothing happens at all.

Consistency - A transaction must start in a consistent state and leave the system in

a consistent state.

Isolation - Intermediate results of a transaction are not visible outside the current transaction.

Durability - Once a transaction was committed, the effects are persistent, even after a system failure. The Python DB API 2.0 provides two methods to either commit or rollback a transaction. Example

You already know how to implement transactions. Here is again similar example -

# Prepare SQL query to DELETE required records

sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)

try: # Execute the SQL command cursor.execute(sql)

# Commit your changes in the database db.commitQ

except:

# Rollback in case there is any error db.rollbackO

COMMIT Operation Commit is the operation, which gives a green signal to database to finalize the changes, and after this oper­ ation, no change can be reverted back. Here is a simple example to call commit method.

db.commit()

ROLLBACK Operation If you are not satisfied with one or more of the changes and you want to revert back those changes com­

pletely, then use rollback() method. Here is a simple example to call rollback() method. db.rollbackQ

Disconnecting Database To disconnect Database connection, use close() method.

db.closeO If the connection to a database is closed by the user with the close() method, any outstanding transactions

are rolled back by the DB. However, instead of depending on any of DB lower level implementation details, your application would be better off calling commit or rollback explicitly.

Connect and run SQL queries to an Oracle database from Python

Connecting to Oracle databases from Python provides the ability to run essential SQL queries that can be utilized in geoprocessing tasks.

Oracle database schemas can be accessed from Python programs using the cxJDracle Python extension.

cx_Oracle is a Python extension module that enables access to Oracle Database. It conforms to the Python database API 2.0 specification with a considerable number of additions and a couple of exclusions. cx_Oracle 6 has been tested with Python version 2.7, and with versions 3.4 and higher. You can use cx_Oracle with Oracle 11.2,12.1 and 12.2 client libraries. Oracle's standard client-server version interoperability

allows connection to both older and newer databases. For example Oracle 12.2 client libraries can connect to Oracle Database 11.2.

Instructions provided describe how to connect to an Oracle database and run SQL queries from a Python script.

Prerequisites For this Hands On Session, the following has already been installed for you:

1. Oracle Database 1 lgR2, with a user "ajituser" and password (case sensitive) of ajitpassword". The exam­

ple table is student_marks in this schema are from Oracle's orcl instance. 2. Python 3.11 with the cx_Oracle 6 extension.

Note: Prefer to install the cx_Oracle version as of client version of installed database.E.g. if you have 10g Client then install 1 Og version of Oracle, otherwise you may get an error as

cx_Oracle.DatabaseError: ORA-24315: illegal attribute type

Procedure 1. Download and install the appropriate cx_Oracle module for the installed Python release (for example,

Python 2.6, 2.7,3.11 etc.):

http://cx-oracle.sourceforge.net/ cx_Oracle is a Python extension module that allows access to Oracle databases. 2. Import the module in the Python script:

import cx_Oracle 3. Make a connection to an Oracle database by passing in the appropriate user I password to the following

connection string:

connection = cx_Oracle.connect('ajituser/ajitpassword@orcr) Note: orcl is oracle database instance name which may be found in tnsnames.ora file

Review the code as follows, which is contained in the connect.py file in the c:\python\bin directory.

import cx_Oracle

con = cx_0racle.connect('ajituser/[email protected]/orcr)

print conversion con.close() The cx_Oracle module is imported to provide the API for accessing the Oracle database. Many inbuilt and

third party modules can be included in this way in Python scripts.

The connect() method is passed the username "ajituser", the password "ajitpassword" and the connection string. In this case, Oracle's Easy Connect connection string syntax is used. It consists of the IP of your ma­ chine and the DB service name "orcl".

The close() method closes the connection. Any connections not explicitly closed will be automatically re­ leased when the script ends.

In a command line terminal run: C:\python\bin> python connect.py

11.2.0.1.0 4. Define a parameter to access the cursor method:

cursor = connection. cursor()

5. Create a query string: querystring = "select *from Students" 6. Pass the query string to the cursor method:

cursor, execute(querystring)

Example

The following example program shows how to issue an SQL SELECT command to retrieve data from a data­ base, and how to collect and print the retrieved data.

import cxJDracle connstr='ajituser/passabc@l 27.0.0.1/orcl' conn = cx_Oracle.connect(connstr) curs = conn.cursorQ

curs.arraysize=50 curs.executefSELECT STUDENT_NO, MODULE_CODE, MARK from Student_marks where MARK >=70 order by MARK desc')

print "Student No\tModule\tMarks\n" for column_l, column_2, column_3 in curs.fetchall():

print column_l, "\t", column_2, "\t", column_3 curs.closeQ

conn.closeQ

When this program is run, it produces the following output:

Student No

Module Marks

20060101

CM0001 80

20060103

CM0002 75

20060102

CM0001 75

20060102

CM0004 70

Handling Errors There are many sources of errors. A few examples are a syntax error in an executed SQL statement, a con­

nection failure, or calling the fetch method for an already canceled or finished statement handle. The DB API defines a number of errors that must exist in each database module. The following table lists

these exceptions.

Exception & Description

Sr.No. Warning

Used for non-fatal issues. Must subclass StandardError. 2

3

4

Error Base class for errors. Must subclass StandardError.

InterfaceError Used for errors in the database module, not the database itself. Must subclass Error.

DatabaseError Used for errors in the database. Must subclass Error.

5

DataError Subclass of DatabaseError that refers to errors in the data.

OperationalError 6

Subclass of DatabaseError that refers to errors such as the loss of a connection to the data­ base. These errors are generally outside of the control of the Python scripter.

IntegrityError 7

Subclass of DatabaseError for situations that would damage the relational integrity, such as uniqueness constraints or foreign keys.

InternalError 8

Subclass of DatabaseError that refers to errors internal to the database module, such as a

cursor no longer being active.

ProgrammingError 9

Subclass of DatabaseError that refers to errors such as a bad table name and other things that can safely be blamed on you.

10

NotSupportedError

Subclass of DatabaseError that refers to trying to call unsupported functionality.

Your Python scripts should handle these errors, but before using any of the above exceptions, make sure your

MySQLdb has support for that exception. You can get more information about them by reading the DB API 2.0 specification.

Chapter 21

The Python Library Consistent with the Pythonic Batteries Included philopsophy of Python, there are hundreds of extension modules. It can be difficult to match a programming need with a specific module. The Python Library Ref­

erence document can be hard to pick through to locate an appropriate module. Well start at the top of the library organization and work our way down to a useful subset of the tremendous wealth that is Python.

Overview of the Python Library Introduction

Built-in Objects. This chapter provides complete documentation of the built-in functions, exceptions and constants.

Built-in Types. All of the data types weve looked at are documented completely in this chapter of the library reference. Of course, there are additional types in the Python reference that we havent looked at.

String Services. This chapter includes almost a dozen modules for various kinds of string and text han­ dling. This includes regular expression pattern matching, Unicode codecs and other string-process­

ing modules. Data Types. This chapter has almost 20 modules providing additional data types, including datetime. Numeric and Mathematical Modules. This chapter describes math, decimal and random modules. Internet Data Handling. One secret behind the internet is the use of standardized sophisticated data

objects, like email messages with attachments. This chapter covers over a dozen modules for han­ dling data passed through the internet.

Structured Markup Processing Tools. XML, HTML and SGML are all markup languages. This chapter cov­

ers tools for parsing these languages to separate the content from the markup. File Formats. This chapter covers modules for parsing files in format like Comma Separated Values (CSV).

Cryptographic Services. This chapter has modules which can be used to develop and compare secure

message hashes. File and Directory Access. This chapter of the Library Reference covers many of the modules well look at in File Handling Modules.

Data Compression and Archiving. This chapter describes modules for reading and writing zip files, tar files and BZ2 files. Well cover these modules in File Handling Modules, also.

Data Persistence. Objects can be written to files, sockets or databases so that they can persist beyond the processing of one specific program. This chapter covers a number of packages for pickling objects so they are preserved. The SQLite 3 relational database is also described in this module.

Generic Operating System Services. An Operating System provides a number of services to our ap­ plication programs, including access to devices and files, consistent notions of time, ways to handle

command-line options, logging, and handling operating system errors. Well look some of these mod­ ules in Programs: Standing Alone.

Optional Operating System Services. This section includes operating system services that are common to

most Linux variants, but not always available in Windows. Unix Specific Services. There are a number of Unix and Linux-specific features provided by these modules.

Interprocess Communication and Networking. Larger and more complex application programs often consist of multiple, cooperating components. The World Wide Web, specifically, is based on the in­ teraction between client and server programs. This chapter describes modules that provide a basis for communicating among the OS processes that execute our programs.

Internet Protocols and Support. This chapter describes over two dozen modules that process a wide vari­

ety of internet-related data structures. This varies from the relatively simple processing of URLs to the relatively complex processing of XML-based Remote Procedure Calls (XML-RPC).

Multimedia Services. Multimedia includes sounds and images; these modules can be used to manipulate sound or image files.

Graphical User Interfaces with Tk. The Tkinter module is one way to build a graphical desktop application. The GTK libraries are also widely used to build richly interactive desktop applications; to make use of

them, youll need to download the pyGTK package.

Internationalization. These packages help you separating your message strings from the rest of your

application program. You can then translate your messages and provide language-specific variants of your software.

Program Frameworks. These are modules to help build command-line applications.

Development Tools. These modules are essential to creating polished, high-quality software: they support the creation of usable documents and reliable tests for Python programs.

Most Useful Library Sections Python includes a large number of pre-built modules. The more you know about these, the less program­ ming you have to do. Python provides many modules for dealing with times, dates and the calendar.

String Services. The String Services modules contains string-related functions or classes. See Strings for more information on strings.

re The re module is the core of text pattern recognition and processing. A regular expression is a formula that specifies how to recognize and parse strings. The re module is described in de­ tail in Complex Strings: the re Module.

struct The avowed purpose of the struct module is to allow a Python program to access Clanguage APIs; it packs and unpacks C-language struct object. It turns out that this module can also help you deal with files in packed binary formats.

diffib The difflib module contains the essential algorithms for comparing two sequences, usually sequences of lines of text. This has algorithms similar to those used by the Unix diff com­

mand (the Window COMP command).

StringlO cStringlO There are two variations on StringlO which provide file-like objects that read from or write to a string bu er. The StringlO module defines the class StringlO , from which subclasses can be derived. The cStringlO module provides a high-speed C-language imple­

mentation that cant be subclassed.

Note that these modules have atypical mixed-case names.

textwrap This is a module to format plain text. While the word-wrapping task is sometimes handled by word processors, you may need this in other kinds of programs. Plain text files are still the most

portable, standard way to provide a document.

codecs This module has hundreds of text encodings. This includes the vast array of Windows code pages and the Macintosh code pages. The most commonly used are the various

Unicode schemes (utf-16 and utf-8). However, there are also a number of codecs for trans­ lating between strings of text and arrays of bytes. These schemes include base-64, zip compression, bz2 compression, various quoting rules, and even the simple rot_13 substi­ tution cipher.

Data Types. The Data Types modules implement a number of widely-used data structures. These arent as useful as sequences, dictionaries or strings which are built-in to the language. These data types include dates, general collections, arrays, and schedule events. This module includes modules for searching lists,

copying structures or producing a nicely formatted output for a complex structure.

datetime The datetime handles details of the calendar, including dates and times. Additionally, the time module provides some more basic functions for time and date processing. Well cover both modules in detail in Dates and. Times: the time and datetime Modules.

These modules mean that you never need to attempt your own calendrical calculations. One of the important lessons learned in the late 90s was that many programmers love to tackle

calendrical calculations, but their efforts had to be tested and reworked prior to January 1, 2000, because of innumerable small problems.

calendar This module contains routines for displaying and working with the calendar. This can help you determine the day of the week on which a month starts and ends; it can count

leap days in an interval of years, etc.

collections This package contains some handy data types, plus the Abstract Base Classes that we use for defining our own collections. Data types include the collections.deque a double-ended queue that can be used as stack (LIFO) or queue (FIFO). The collections.de-

faultdict class, which can return a default value instead of raising an exception for missing

keys. The collections.namedtuple function helps us to create a small, specialized class that is a tuple with named positions.

We made use of this library in Creating or Extending Data Types.

bisect The bisect module contains the bisectQ function to search a sorted list for a specific value. It also contains the insort() function to insert an item into a list maintaining the sorted order. This module performs faster than simply appending values to a list and calling

the sort() method of a list. This modules source is instructive as a lesson in well-crafted algorithms.

array The array module gives you a high-performance, highly compact collection of values. It isnt as flexible as a list or a tuple, but it is fast and takes up relatively little memory. This is help­

ful for processing media like image or sound files.

sched The sched module contains the definition for the scheduler class that builds a simple task scheduler. When a scheduler is constructed, it is given two user-supplied functions: one

returns the time and the other executes a delay waiting for the time to arrive. For real-time scheduling, the time module time() and sleep() functions can be used. The scheduler has

a main loop that calls the supplied time function and compares the current time with the

time for scheduled tasks; it then calls the supplied a delay function for the difference in time. It runs the scheduled task, and calls the delay function with a duration of zero to re­ lease any resources.

copy The copy module contains functions for making copies of complex objects. This module contains a function to make a shallow copy of an object, where any objects contained within

the parent are not copied, but references are inserted in the parent. It also contains a func­ tion to make a deep copy of an object, where all objects contained within the parent object

are duplicated.

Note that Pythons simple assignment only creates a variable which is a label (or reference) to an object, not a duplicate copy. This module is the easiest way to create an independent copy,

pprint The pprint module contains some useful functions like pprint.pprint() for printing easyto-read representations of nested lists and dictionaries. It also has a PrettyPrinter class from which you can make subclasses to customize the way in which lists or dictionaries or

other objects are printed.

Numeric and Mathematical Modules. These modules include more specialized mathemathical functions and some additional numeric data types.

decimal The decimal module provides decimal-based arithmetic which correctly handles signif­ icant digits, rounding and other features common to currency amounts.

math The math module was covered in The math Module. It contains the math functions like sine, cosine and square root.

random The random module was covered in The math Module. File and Directory Access. Well look at many of these modules in File Handling Modules. These are the modules which are essential for handling data files.

os.path The os.path module is critical for creating portable Python programs. The popular oper­ ating systems (Linux, Windows and MacOS) each have di erent approaches to file names. A Python program that depends on os.path will behave more consistently in all environ­ ments.

fileinput The fileinput module helps your program process a large number of files smoothly and simply.

glob

fnmatch The glob and fnmatch modules help a Windows program handle wild-card file names in a standard manner.

shutil The shutil module provides shell-like utilities for file copy, file rename, directory moves, etc. This module lets you write short, effective Python programs that do things that are

typically done by shell scripts.

Why use Python instead of the shell? Python is far easier to read, far more efficient, and far more capable of writing moderately sophisticated programs. Using Python saves you from having to write long, painful shell scripts.

Data Compression and Archiving. These modules handle the various file compression algorithms that are available. Well look at these modules in File Handling Modules.

tarfile zipfile These two modules create archive files, which contain a number of files that are bound together. The TAR format is not compressed, where the ZIP format is compressed. Often a TAR archive is compressed using GZIP to create a .tar.gz archive.

zlib gzip bz2 These modules employee different compression algorithms. They all have similar features to compress or uncompress files.

File Formats. These are modules for reading and writing files in a few of the amazing variety of file formats that are in common use. In addition to these common formats, modules in chapter 20, Structured Markup

Processing Tools are also important.

csv The csv module helps you parse and create Comma-Separated Value (CSV) data files. This helps you exchange data with many desktop tools that produce or consume CSV files. Well

look at this in Comma-Separated Values: The csv Module.

ConfigParser Configuration files can take a number of forms. The simplest approach is to use a Python module as the configuration for a large, complex program. Sometimes configura­

tions are encoded in XML. Many Windows legacy programs use .INI files. The ConfigParser can gracefully parse these files.

Cryptographic Services. These modules arent specifically encryption modules. Many popular en-cryption algorithms are protected by patents. Often, encryption requires compiled modules for performance rea­

sons. These modules compute secure digests of messages using a variety of algorithms.

hashlib Compute a secure hash or digest of a message to ensure that it was not tampered with. The hashlib.md5 class creates an MD5 hash, which is often used for validating that a downloaded file was received correctly and completely.

Generic Operating System Services. The following modules contain basic features that are common to all operating systems. Most of this commonality is achieved by using the C standard libraries. By using this

module, you can be assured that your Python application will be portable to almost any operating system.

os The os (and os.path) modules provide access to a number of operating system features. The os module provides control over Processes, Files and Directories. Well look at os and os.path in

The os Module and The os.path Module.

time The time module provides basic functions for time and date processing. Additionally datetime handles details of the calendar more gracefully than time does. Well cover both mod­ ules in detail in Dates and Times: the time and datetime Modules.

Having modules like datetime and time mean that you never need to attempt your own

calendrical calculations. One of the important lessons learned in the late 90s was that many programmers love to tackle calendrical calculations, but their efforts had to be tested and re­

worked because of innumerable small problems.

getopt

optparse A well-written program makes use of the command-line interface. It is configured through options and arguments, as well as properties files. Well cover optparse in Pro­

grams: Standing Alone. Command-line programs for Windows will also need to use the glob module to perform

standard file-name globbing.

logging Often, you want a simple, standardized log for errors as well as debugging information. Well look at logging in detail in Log Files: The logging Module.

Interprocess Communication and Networking. This section includes modules for creating pro-cesses and doing simple interprocess communication (IPC) using the standard socket abstraction.

subprocess The subprocess module provides the class required to create a separate process. The standard approach is called forking a subprocess. Under Windows, similar functionality is provided.

Using this, you can write a Python program which can run any other program on your com­ puter. This is very handy for automating complex tasks, and it allows you to replace clunky,

difficult shell scripts with Python scripts.

socket This is a Python implementation of the standard socket library that supports the TCP/IP protocol.

Internet Data Handling. The Internet Data Handling modules contain a number of handy algo-rithms. A great deal of data is defined by the Internet Request for Comments (RFC). Since these effectively standardize

data on the Internet, it helps to have modules already in place to process this standardized data. Most of

these modules are specialized, but a few have much wider application.

mimify

base64 binascii

binhex quopri These modules all provide various kinds of conversions, escapes or quoting so that binary data can be manipulated as safe, universal ASCII text. The number of these modules reflects the number of di erent clever solutions to the problem of packing binary data into ordinary

email messages.

Structured Markup Processing Tools. The following modules contain algorithms for working with struc­ tured markup: Standard General Markup Language (SGML), Hypertext Markup Language (HTML) and Ex­ tensible Markup Language (XML).

htmllib Ordinary HTML documents can be examined with the htmllib module. This module based on the sgmllib module. The basic HTMLParser class definition is a superclass; you will typically override the various functions to do the appropriate processing for your application.

One problem with parsing HTML is that browsers in order to conform with the ap­ plicable standards must accept incorrect HTML. This means that many web sites publish HTML which is tolerated by browsers, but cant easily be parsed by htmllib. When con­

fronted with serious horrors, consider downloading the Beautiful Soup module (http://

www.crummy.com/software/BeautifulSoup/). This handles erroneous HTML more grace­

fully than htmllib.

xml.sax xml.dom

xml.dom.minidom The xml.sax and xml.dom modules provide the classes necessary to con­ veniently read and process XML documents. A SAX parser separates the various types of content and passes a series of events the handler objects attached to the parser. A DOM

parser decomposes the document into the Document Object Model (DOM). The xml.dom module contains the classes which define an XML documents structure. The xml.dom.minidom module contains a parser which creates a DOM object.

Additionally, the formatter module, in chapter 24 (Miscellaneous Modules) goes along with these.

Internet Protocols and Support. The following modules contain algorithms for responding the several of the most common Internet protocols. These modules greatly simplify developing applications based on

these protocols.

cgi The cgi module can be used for web server applications invoked as Common Gateway Inter­ face (CGI) scripts. This allows you to put Python programming in the cgi-bin directory.

When the web server invokes the CGI script, the Python interpreter is started and the Python script is executed.

wsgiref The Web Services Gateway Interface (WSGI) standard provides a much simpler frame­ work for web applications and web services. See PEP 333 for more information.

Essentially, this subsumes all of CGI, plus adds several features and a systematic way to com­ pose larger applications from smaller components.

urllib

urllib2 urlparse These modules allow you to write relatively simple application programs which open a URL as if it were a standard Python file. The content can be read and perhaps parsed

with the HTML or XML parser modules, described below. The urllib module depends on

the httplib, ftplib and gopherlib modules. It will also open local files when the scheme of the URL is file:. The urlparse module includes the functions necessary to parse or assemble URLs. The urllib2 module handles more complex situations where there is authentication

or cookies involved.

httplib

ftplib

gopherlib The httplib, ftplib and gopherlib modules include relatively complete support for building client applications that use these protocols. Between the html module and httplib

module, a simple character-oriented web browser or web content crawler can be built.

poplib

imaplib The poplib and imaplib modules allow you to build mail reader client applications. The poplib module is for mail clients using the Post-0 ice Protocol, POP3 (RFC 1725), to extract mail from a mail server. The imaplib module is for mail servers using the Internet Message

Access Protocol, IMAP4 (RFC 2060) to manage mail on an IMAP server.

nntplib The nntplib module allows you to build a network news reader. The newsgroups, like comp.lang.python, are processed by NNTP servers. You can build special-purpose news

readers with this module.

Socketserver The Socketserver module provides the relatively advanced programming re-quired to create TCP/IP or UDP/IP server applications. This is typically the core of a stand-alone

application server.

SimpleHTTPServer CGIHTPPServer

BaseHTTPServer The SimpleHTTPServer and CGIHTTPServer modules rely on the ba-sic BaseHTTPServer and Socketserver modules to create a web server. The SimpleHTTPServer

module provides the programming to handle basic URL requests. The CGIHTTPServer module adds the capability for running CGI scripts; it does this with the fork() and exec()

functions of the os module, which are not necessarily supported on all platforms.

asyncore

asynchat The asyncore (and asynchat) modules help to build a time-sharing application server. When client requests can be handled quickly by the server, complex multi-threading and multi-processing arent really necessary. Instead, this module simply dispatches each client communication to an appropriate handler function.

Internationalization. A well-written application avoids including messages as literal strings within the program text. Instead, all messages, prompts, labels, etc., are kept as a separate resource. These separate

string resources can then be translated.

locale The locale module fetches the current locales date, time, number and currency format-ting rules. This provides functions which will format and parse dates, times, numbers and cur­

rency amounts. A user can change their locale with simple operating system settings, and your application can work consistently with all other programs.

Program Frameworks. Well talk about a number of program-related issues in Programs: Standing Alone and

Architecture: Clients, Servers, the Internet and the World Wide Web. Much of this goes beyond the standard Python library. Within the library are two modules that can help you create large, sophisticated command­

line application programs.

cmd The cmd module contains a superclass useful for building the main command-reading loop of an interactive program. The standard features include printing a prompt, reading com­

mands, providing help and providing a command history bu er. A subclass is expected to

provide functions with names of the form do_command(). When the user enters a line be­ ginning with command, the appropriate do_command() function is called.

shlex The shlex module can be used to tokenize input in a simple language similar to the Linux shell languages. This module defines a basic shlex class with parsing methods that can sep­

arate words, quotes strings and comments, and return them to the requesting program.

Development Tools. The testing tools are central to creating reliable, complete and correct software, doctest When a function or a class docstring includes a snippet of interactive Python, the doctest module can use this snippet to confirm that the function or class works as advertised.

For example:

def myFunction( a, b):

""">>> myFunction(2, 3)6 >» myFunction^ 5.0, 7.0 )35.0 nun return a * b The >>> myFunction( 2, 3 ) lines are parsed by doctest. They are evaluated, and the actual

result compared with the docstring comment.

unittest This is more sophisticated testing framework in which you create TestCases which de­ fine a fixture, an operation and expected results.

2to3 This module is used to convert Python 2 files to Python 3. Prior to using this, you should run your Python programs with the -3 option to identify any potential incompatibilities. Once youve fixed all of the incompatibilities, you can confi­

dently convert your program to Python 3.

Do not tweak the output from this conversion. If your converted program doesnt work under Python 3, its almost always a problem with your original program playing fast and

loose with Python rules.

In the unlikely event that this module cannot convert your program, you should probably rewrite your program to eliminate the features that are causing problems.

Python Runtime Services. The Python Runtime Services modules are considered to support the Python runtime environment.

sys The sys module contains execution context information. It has the command-line arguments (in sys.argv) used to start the Python interpreter. It has the standard input, output and error file definitions. It has functions for retrieving exception information. It defines the

platform, byte order, module search path and other basic facts. This is typically used by a

main program to get run-time environment information.

Chapter 22

101 Python Programs SET-1

1. Hello World Program: print("Hello, World!")

2. Simple Calculator:

a = int(input("Enter the first number:")) b = int(input("Enter the second number:")) print("Sum:", a + b)

print("Difference:", a - b) print("Product:", a * b) print("Quotient:", a / b)

3. Factorial of a Number: def factorial(n): if n = = 0: return 1 else: return n * factorial(n -1)

num = int(input("Enter a number:")) print("Factorial:", factorial(num)) 4. Fibonacci Sequence:

def fibonacci (n): if n y:

greater = x else: greater = y while True: if greater % x = = 0 and greater % y = = 0:1cm = greater break greater + = 1

return 1cm

numl = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) print("LCM:",

compute_lcm(numl, num2)) 10. Basic Class and Object:

class Rectangle: def_ init_ (self, length, width): self.length = length self.width = width

def area(self): return self.length * self.width

length = float(input("Enter length of the rectangle: ")) width = float(input("Enter width of the rectan­

gle: ")) rect = Rectangle(length, width) print("Area of the rectangle:", rect.areaQ)

SET-3 1. Check Anagram:

def is_anagram(sl, s2): return sorted(sl) == sorted(s2) string 1 = inputf'Enter the first string: ") string2 = input("Enter the second string: ") if is_ana-

gram(stringl, string2): print("Anagrams") else:

print("Not anagrams") 2. Generate a Random Number: import random

print("Random number:", random.randint(l, 100))

3. Binary Search Algorithm: def binary_search(arr, x): low = 0

high = len(arr) - 1 while low < = high: mid = (low + high) // 2 if arr[mid] < x: low = mid + 1 elif arrfmid] > x: high = mid -1 else:

return mid return -1

arr = [2, 3,4,10, 40] x = 10

result = binary_search(arr, x) if result != -1:

print(f"Element found at index {result}") else: print("Element not found") 4. Check Armstrong Number:

def is_armstrong(n): order = len(str(n)) temp = n

sum = 0 while temp > 0: digit = temp % 10

sum += digit ** order temp //= 10 return n = = sum

number = int(input("Enter a number: ")) if is_armstrong(number): print("Armstrong number") else:

print("Not an Armstrong number") 5. Generate a Simple Pattern: n = 5 for i in range(n): print(* * (i + 1))

6. Linear Search Algorithm: def

linear_search(arr, x): for i in range(len(arr)): if arr[i] = = x:

return i return -1

arr = [4, 2,1, 7, 5] x = 7 result = linear_search(arr, x) if result != -1:

print(f"Element found at index {result}") else: print("Element not found")

7. Calculate the Power of a Number:

base = int(input("Enter the base: ")) exponent = int(input("Enter the exponent: ")) result = base ** exponent

print("Result:", result) 8. Print the Fibonacci Series:

def fibonacci_series(n): a, b = 0,1 for _ in range(n): print(a, end=" ") a, b = b, a + b

terms = int(input("Enter the number of terms: ")) printf'Fibonacci series:") fibonacci_series(terms)

9. Merge Two Sorted Lists:

listl = [1, 3, 5, 7] list2 = [2,4, 6, 8] mergedjist = sorted(listl + list2) print("Merged and sorted list:", mergedjist)

10. Generate a Simple Pyramid Pattern: n = 5 for i in range(n):

print("" * (n - i -1) + "*" * (2 * i + 1))

SET-4 1. Check if a Number is Positive, Negative, or Zero:

num = float(input("Enter a number:")) if num > 0: print("Positive number") elif num < 0: print("Negative number") else:

print("Zero") 2. Generate a List of Prime Numbers within a Range:

def generate_primes(start, end): primes = []

for num in range(start, end + 1): if num > 1: for i in range(2, num): if num % i = = 0: break else: primes.append(num) return primes

start_range = int(input("Enter the starting range: ")) end_range = int(input("Enter the ending range: ")) print("Prime numbers:", generate_primes(start_range, end_range))

3. Calculate the Area and Perimeter of a Rectangle: length = float(input("Enter the length of the rectangle: ")) width = float(input("Enter the width of the rectangle: ")) area = length * width

perimeter = 2* (length + width) print(f "Area: {area}, Perimeter: {perimeter}")

4. Find the GCD of Two Numbers:

def compute_gcd(x, y): while y: x, y = y, x % y return x numl = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) print("GCD:",

compute_gcd(numl, num2)) 5. Check if a Year is a Leap Year or Not Using Functions:

def is_leap_year(year): if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: return True else:

return False else: return True else: return False year = int(input("Enter a year: ")) if is_leap_year(year): print("Leap year") else:

print("Not a leap year") 6. Print the Sum of Natural Numbers up to a Given Number: n = int(input("Enter a number: ")) sum = 0 for i in range(l, n + 1): sum += i

print("Sum of natural numbers:", sum)

7. Reverse a String: string = input("Enter a string:") reversed_string = string[::-l] print("Reversed string:", reversed_string)

8. Check if a Number is a Perfect Number:

def is_perfect_number(n): sum = 0 for i in range(l, n): if n % i == 0:

sum += i return sum = = n

number = int(input("Enter a number: ")) if is_perfect_number(number): printf'Perfect number") else:

print("Not a perfect number") 9. Count the Number of Words in a String: string = input("Enter a string: ") word_count = len(string.split()) printf'Number of words:", word_

count)

10. Concatenate Two Strings: string 1 = input("Enter the first string: ") string2 = inputf'Enter the second string: ") concatenat-

ed_string = string 1 + string2

print("Concatenated string:", concatenated_string)

SET-5 1. Check if a Number is a Perfect Square: import math

def is_perfect_square(n): root = math.isqrt(n) return root * root = = n number = int(input("Enter a number: ")) if is_perfect_square(number): print("Perfect square") else:

print("Not a perfect square") 2. Implement a Stack Data Structure:

class Stack: def_ init_ (self): self.items = [] def push(self, item): self.items.append(item) def pop(self): return self.items.popO

def is_empty(self): return self.items == [] stack = Stack() stack.push(l) stack.push(2) stack.push(3)

print("Popped item:", stack.popO) print("Stack is empty:", stack.is_empty())

3. Calculate the Area of a Triangle:

base = float(input("Enter the base of the triangle:")) height = float(input("Enter the height of the trian­ gle: ")) area = 0.5 * base * height print("Area of the triangle:", area)

4. Find the ASCII Value of a Character:

char = input("Enter a character: ") ascii_value = ord(char) print("ASCII value:", ascii_value) 5. Generate a Simple Diamond Pattern:

n=5 for i in range(n):

print("" * (n - i -1) + "* " * (i + 1)) for i in range(n -1,0, -1): print("" * (n - i) + "*" * i)

6. Check if a Number is a Perfect Cube: def is_perfect_cube(n): root = round(n ** (1/3))

return root ** 3 = = n

number = int(input("Enter a number: ")) if is_perfect_cube(number): printf'Perfect cube") else:

print("Not a perfect cube") 7. Implement a Queue Data Structure:

class Queue: def_ init_ (self): self.items = [] def enqueue(self, item): self.items.insert(O, item) def dequeue(self): return self.items.pop() def is_empty(self): return self.items == []

queue = Queue() queue.enqueue(l) queue. enqueue(2) queue. enqueue(3) print("Dequeued item:", queue.dequeue()) print("Queue is empty:", queue.is_empty()) 8. Calculate the Power Set of a Set: from itertools import chain, combinations

def power_set(s): return list(chain.from_iterable(combinations(s, r) for r in range(len(s) + 1)))

input_set = [1, 2, 3]

print("Power set:", power_set(input_set)) 9. Swap Two Variables:

a = inputf'Enter the value of a: ") b = input("Enter the value of b: ") a, b = b, a print("Value of a after swapping:", a) print("Value of b after swapping:", b)

10. Print the Factors of a Number:def print_factors(n): factors = [] for i in range(l, n + 1): if n % i = = 0: factors.append(i) return factors

number = int(input("Enter a number: ")) print("Factors:", print_factors(number))

SET-6 1. Check if a String is a Pangram: import string

def is_pangram(s): alphabet = set(string.ascii_lowercase) return set(s.lower()) >= alphabet

input_string = input("Enter a string:") if is_pangram(input_string): print("Pangram") else:

print("Not a pangram") 2. Calculate the Volume of a Cylinder: import math

radius = float(input("Enter the radius of the cylinder: ")) height = float(input("Enter the height of the cylinder: ")) volume = math.pi * radius * radius * height print("Volume of the cylinder:", volume)

3. Check if a String is a Palindrome: def is_palindrome(s): return s == s[::-l] input_string = input("Enter a string:") if is_palindrome(input_string): print("Palindrome") else:

print("Not a palindrome") 4. Sort a List of Strings:

strings = ['apple, banana, cherry, date,

elderberry'] sorted_strings = sorted(strings)

print("Sorted strings:", sorted_strings)

5. Generate a Simple Pascals Triangle:

def generate_pascals_triangle(n): triangle = [[1]] for i in range(l, n): prev_row = triangle[-l]

curr_row = [1] + [prev_row[j] + prev_row[j + 1] for j in range(i -1)] + [1] triangle.append(curr_row) return triangle

rows = 5

print("Pascals Triangle:")

for row in generate_pascals_triangle(rows): print(row) 6. Implement a Binary Search Tree:

class Node: def_ init_ (self, value): self.value = value self.left = None self.right = None

class BinaryTree: def_ init_ (self): self.root = None def insert(self, value): if self.root is None: self.root = Node(value) else: self._insert_recursive(self.root, value)

def _insert_recursive(self, node, value): if value < node.value: if node.left is None: node.left = Node(value) else:

self._insert_recursive(node.left, value) elif value > node.value: if node.right is None: node.right = Node(value) else:

self._insert_recursive(node.right, value)

# Example usage: tree = BinaryTree() tree.insert(5) tree.insert(3) tree.insert(7) 7. Implement a Linear Regression Model: from sklearn.linear_model import LinearRegression import numpy as np

X = np.array([[l, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([l, 2])) + 3 reg = LinearRegression().fit(X, y) print("Coef:", reg.coef_) print("Intercept:", reg.intercept_)

8. Count the Number of Digits in an Integer: number = int(input("Enter an integer:")) num_digits = len(str(abs(number))) print("Number of digits:", num_digits)

9. Generate a Random Password: import random import string

def generate_password(length):

characters = string.asciijetters + string.digits + string.punctuation password = .join(random. choice(characters) for _ in range(length)) return password passwordjength = 12

print("Generated password:", generate_password(password_length))

10. Calculate the Exponential Value:

base = float(input("Enter the base: ")) exponent = float(input("Enter the exponent: ")) result = base ** exponent

print("Result:", result) SET-7 1. Find the Sum of Natural Numbers Using Recursion:

def sum_of_natural_numbers(n): if n = low: mid = (high + low) // 2 if arr[mid] == x: return mid elif arr[mid] > x:

return binary_search_recursive(arr, low, mid -1, x) else: return binary_search_recursive(arr, mid + 1, high, x) else: return -1

arr = [2, 3,4,10, 40] x = 10 result = binary_search_recursive(arr, 0, len(arr) -1, x) if result != -1:

print(f"Element found at index {result}") else: print("Element not found")

9. Find the Sum of Digits in a Number:

def sum_of_digits(n): return sum(int(digit) for digit in str(n)) number = int(input("Enter a number:")) printf'Sum of digits:",

sum_of_digits(number)) 10. Convert Decimal to Binary, Octal, and Hexadecimal: dec = int(input("Enter a decimal number:")) print("Binary:", bin(dec))

print("Octal:", oct(dec)) print("Hexadecimal:", hex(dec)) SET-8 1. Implement Selection Sort:

def selection_sort(arr): n = len(arr) for i in range(n): min_idx = i

for j in range(i + 1, n): if arr[j] < arr[min_idx]: min_idx = j arr[i], arr[min_idx] = arr[min_idx], arr[i] arr = [64, 25,12, 22,11] selection_sort(arr) print("Sorted array:", arr)

2. Find the Greatest Among Three Numbers: a =

float(input("Enter the first number: "))

b = float(input("Enter the second number: ")) c = float(input("Enter the third number: "))

max.num = max(a, b, c) printf'Greatest number:", maxnum)

3. Implement Insertion Sort: def insertion_sort(arr): for i in range(l, len(arr)): key = arr[i] j = i-l while j > = 0 and key < arr[j]: arr[j + 1] = arr[j]

j-=l

arr[j + 1] = key arr = [12,11,13, 5, 6] insertion_sort(arr) print("Sorted array:", arr) 4. Convert Decimal to Binary:

dec = int(input("Enter a decimal number: ")) binary = bin(dec)

print("Binary:", binary[2:]) 5. Convert Decimal to Octal: dec = int(input("Enter a decimal number: ")) octal = oct(dec)

print("Octal:", octal[2:]) 6. Convert Decimal to Hexadecimal:

dec = int(input("Enter a decimal number: ")) hexadecimal = hex(dec) print("Hexadecimal:", hexadeci­

mal^:])

7. Implement a Bubble Sort Algorithm: def bubble_sort(arr): n = len(arr)

fori in range(n-1):

for j in range(O, n - i -1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j]

arr = [64, 34, 25,12, 22,11, 90] bubble_sort(arr) printf'Sorted array:", arr) 8. Find the LCM and GCD of Two Numbers:

def compute_lcm(x, y): 1cm = (x * y) // compute_gcd(x, y) return 1cm

def compute_gcd(x, y): while y: x, y = y, x % y return x numl = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) print("LCM:",

compute_lcm(numl, num2)) print("GCD:", compute_gcd(numl, num2)) 9. Find the Factorial of a Number:

def factorial(n): if n = = 0: return 1 else:

return n * factorial(n -1)

num = int(input("Enter a number:")) print("Factorial:", factorial(num)) 10. Implement Quick Sort:

def quick_sort(arr): if len(arr) pivot] return quick_sort(left) + middle + quick_sort(right) arr = [12,11,13, 5, 6, 7]

sorted_arr = quick_sort(arr) print("Sorted array:", sorted_arr) SET-9 1. Find the Sum of Elements in a List: myjist = [1,2,3,4,5] sum_of_elements = sum(my_list)

print("Sum of elements:", sum_of_elements) 2. Generate a Fibonacci Sequence Using a Loop:

def generate_fibonacci(n): fibonacci_sequence = [0,1] for i in range(2, n): next_num = fibonacci_sequence[-l] + fibonacci_sequence[-2] fibonacci_sequence.append(next_num)

return fibonacci_sequence terms = 10 print("Fibonacci sequence:", generate_fibonacci(terms))

3. Calculate the Exponential Value Using a Loop:

base = int(input("Enter the base: ")) exponent = int(input("Enter the exponent: ")) result = 1

for _ in range(exponent): result *= base print("Result:", result) 4. Implement Linear Search Algorithm Using a Loop:

def linear_search(arr, x): for i in range(len(arr)): if arr[i] == x: return i return -1

arr = [4, 2,1, 7, 5] x = 7 result = linear_search(arr, x) if result

I hope, my contents make you able to equip you with the essential skills andfundamental knowledge to explore

the powerful features of Python as a programming language. By the time you finished reading the book, I am confident that you will be prepared to put your basic programming knowledge to practical everyday uses.

Finally, if you enjoyed this book, please take the time to share your thoughts and post a positive review at

[email protected]. It will be greatly appreciated!

> > > Happy Coding..