Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
BENCHMARK: Playing with Python
A free workshop series conducted voluntarily by Benchmark with much love to
build the foundation concepts of beginners’ Python for all keen learners worldwide
Stay connected with us for more educational initiatives of your interest
Facebook | Twitter | LinkedIn | Instagram
Reach us at GMaps
Call us at +91 9836919972
Dear All,
This Handout comes to you as a letter from Benchmark written with much love and
best wishes to guide and inspire you through learning Python programming. We
truly wish that all of you become experts soon with more practice but whenever in
doubt, just reach out to us and we will be with you, always…
BENCHMARK +91 9836919972 Page | 1
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
Compiler vs Interpreter
Compiler Interpreter
Scans the whole code at once Scans the code line by line
A faster process A slower process
Compiler in itself is a large program It is a smaller lightweight program itself
All errors detected are shown at once Shows the immediate next error
For large source codes, this is preferred For smaller scripts, this is preferred
Python: Compiled or Interpreted?
Though many resources state Python is an interpreted language but that is half-
correct. Python programs are first compiled, then interpreted, like Java programs. But
unlike Java programs, the intermediate files created due to compiling are destroyed
after the Python program completes execution, for better security.
Python 2 or Python 3?
Python is not as new as many of you might think it to be, it has been in development
for decades now since its inception by Guido van Rossum in the late 1980’s. Python 3
is the advanced version of Python and is replacing Python 2, to the extent that many
modern Python libraries are supported only by Python 3. The last final build version
of Python 2 was released in the beginning of 2020, which was declared as the last
release, following which all new codes are expected to be written in Python 3, and old
codes to be migrated, if possible. So, the focus of our course will be on the latest
Python, which you can put to use in industry at this present time.
Python - A loosely typed language: Why?
Python is a loosely typed or weakly typed or dynamically typed language as variable
datatypes need not be defined and Python assigns the datatype as per the stored value.
BENCHMARK +91 9836919972 Page | 2
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
Datatypes: Why needed?
When storing data in a variable, it is required to specify the nature of data. For
example, if your age is 20, and when a program asks for it, you enter ‘twenty’ instead
of number 20, you are right but the computer cannot do arithmetic calculations on the
alphabet string ‘twenty’ which it can on numerical 20. So, it is essential to declare the
datatype of variables as otherwise, internally within the program, the data might
conflict with the operations intended to perform on the data.
Datatypes in Python
Nature of data Datatypes
Text str
Numeric int, float, complex
Sequence list, tuple, range
Mapped dict
Set set, frozenset
Boolean bool
Binary bytes, bytearray, memoryview
type(variableName) returns the datatype of the variable and we can check the datatype
of any variable by this command.
Semicolons, Braces and Indentations
Though most programming languages use a semicolon (;) to terminate each line,
Python does not need any semicolon. Just a line break by pressing ‘Enter’ terminates
each line.
Braces { } are not required. Only correct indentations mark the scopes.
Wrong indentation like this line is strictly marked as an error though.
Block1 • Statements 1 and 2 are under Block1
Statement1 • Block2 is also under Block1,
Statement2 with Statement3 further under Bloock2
Block2 • Statement4 is also under Block1
Staement3 but not under Block2
Statement4 • Statement5 is at same level as Block1
Staement5 and not under it
BENCHMARK +91 9836919972 Page | 3
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
How to run a Python (.py) program?
Many roads often lead to the same destination. Listing the 3 most popular ways:
1. The traditional way: Using Command Line and a Text Editor:
▪ Type the source code in a text file and save as a Python (.py) file.
▪ Download Python from [Link]/ for your respective OS.
▪ From the source code destination, launch Command Prompt.
▪ Compile and run the program by the following command:
python [Link]
Text editor with Python code saved as ‘[Link]’ ‘[Link]’ compiled and run from Command Line
This is the one of the most robust methods and most developers’ preferred way.
2. The safe way: Using an IDE:
▪ Download any supported Integrated Development Environment (IDE),
like Anaconda from [Link]/products/individual.
▪ Install the software, and open Spyder from Anaconda Navigator.
▪ Write the source code in the editor, compile and run directly from there.
Anaconda Navigator with Spyder highlighted
BENCHMARK +91 9836919972 Page | 4
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
Spyder with a code in editor panel (on left), compiled and run with output (on right)
This is a great way as one single IDE handles all the tasks of editing, compiling
and running the code. Additionally, errors reported are more detailed.
3. The quick way: Using a Notebook:
▪ A Notebook software like Jupyter eases the whole process for a quick
code check.
▪ From the same Anaconda Navigator, as in method 2, launch Notebook.
▪ The Jupyter Notebook opens in the default web-browser, to use a GUI,
though it is not an online software and can work offline.
▪ From the New dropdown, select Python 3 option under Notebook.
▪ A new Notebook is created with the default title ‘Untitled’.
▪ Rename the Notebook as required.
▪ The Notebook has an extension ‘.ipynb’ and it is visible in the directory
tree, and can be opened through Jupyter.
▪ Write any code in a Code cell and normal text in a Markdown cell and to
execute a cell, press Shift+Enter in the cell.
▪ Shutdown the Notebook from the Kernel menu after using. Restart the
kernel from that menu, if the Notebook freezes during any operation.
BENCHMARK +91 9836919972 Page | 5
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
Anaconda Navigator with Jupyter highlighted
Creating a new Jupyter Notebook
BENCHMARK +91 9836919972 Page | 6
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
Code written, compiled and run in a Jupyter Notebook
Jupyter Notebook files with ‘.ipynb’ extension as shown in directory tree
Jupyter Notebook kernel restart and shutdown controls
This is an easy to go method as no extra software is needed as Jupyter Notebook is
available even online on web and all is needed is opening a Notebook, typing the code
and hitting Run. It shows the outputs and errors, if any, and is a quick way to go coding.
Keep exploring the ways! As the more you explore, the more you learn…
BENCHMARK +91 9836919972 Page | 7
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
[LIST]
Lists in Python are heterogeneous collection of data. It is ordered, indexed, mutable
and can have duplicates. Lists are represented by [ ].
Indexed: Each item can be accessed by its index
Heterogeneous: Has the capability to contain data of multiple datatypes.
Mutable: Particular value at an index can be changed
= vs copy( )
When = is used to copy a list, a reference is created and if one list is changed, the other
list changes and vice-versa. But copy( ) creates a separate unlinked copy and changing
one of the lists does not change the other.
BENCHMARK +91 9836919972 Page | 8
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
String and List – split( ) and join( ):
In Python, split( ) function splits a string into a list based on a specified delimiter, or a
single space by default.
On the other hand, join( ) joins a list to form a string with the specified delimiter put
in between the elements of the list.
Iterating through a list:
Finding the length of a list:
BENCHMARK +91 9836919972 Page | 9
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
(TUPLE)
A heterogeneous collection of ordered, indexed data which is unchangeable or
immutable. Tuples are represented by ( ).
count(data) returns the number of times a value is present in the tuple, 0 if not found
index(data) returns the index of the first-time occurrence of the data, error if not found
The value at a particular index cannot be changed however, as tuple is immutable and
gives the following error:
BENCHMARK +91 9836919972 Page | 10
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
{SET}
A heterogeneous collection of unindexed and unordered data and do not contain
duplicate data. Sets are represented by { }.
remove( ) vs discard( ):
Both deletes data from a set, but remove( ) gives error if that data is not present but
discard( ) does not give error, even if that data is not present.
BENCHMARK +91 9836919972 Page | 11
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
{DICTIONARY}
A collection of {key:value} pairs. It is indexed and mutable as a whole. The keys need
to be immutable datatypes, like string, tuple, etc. The values can be of any datatype.
An example Python program to count the number of digits, alphabets and spaces in a
string and store in a dictionary:
BENCHMARK +91 9836919972 Page | 12
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
Membership Operators in Python: in, not in:
In Python, a membership operator returns a bool value upon checking if a particular
sub-value is contained in a specified value.
----------------------------------------------------------------------------------------------------
An example Python program involving lists, dictionaries and
membership operators:
Given a dictionary of car companies as keys and their models in values as lists, find the
car company by the model name:
BENCHMARK +91 9836919972 Page | 13
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
Python Classes and Objects:
Python is an Object Oriented Programming (OOP) and we can define classes to
simulate real-life objects. And, a class is the blueprint for creating an object of that
type. A class has its own attributes and functions which work with its objects and are
accessible through it.
To create a class, the class keyword is used as follows:
In the above code snippet, Point is defined as a class, with 2 attribute values x, y and
p1 is an object of the Point class. Now, every object of the Point class will have 2
attributes x, y both initialised to 0 as per the written code.
Printing p1.x and p1.y gives the values of the attributes of p1, though printing only p1
gives the memory location the object is at.
def __init__(self, <optional> attributes):
To initialise an object, a special function is used defined by <double underscore>
followed by the keyword init and again followed by <double underscore> with a
parameter list in ( ).
The self in a way directs to the object it is initialising.
BENCHMARK +91 9836919972 Page | 14
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
In the following code snippets, the use of __init__() is as illustrated:
In case a value is not passed while creating an object, we get an error as shown:
BENCHMARK +91 9836919972 Page | 15
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
Having default values for an attribute of an object by attribute = default value in
__init( )__:
In case values not passed, the object is initialised with the default values, else
initialised with the supplied values.
Defining functions in class and accessing by any object of that class is done as:
The grad( ) function called in the last line of the can be been called by any object of the
Point class. The output depends on the parameters passed in ( ) though.
Note that self in the __init__( ) function is not passed explicitly while creating an
object.
BENCHMARK +91 9836919972 Page | 16
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
File Handling in Python:
In Python, we can work with any type of file.
The 3 steps common to handling any kind of file are as:
▪ open( ): To open the file
▪ read( ) / write( ): To do the operations
▪ close(): To close the file
There are different modes to opening a file:
▪ r: Read only mode
▪ w: Write mode, overwrites previous content
▪ a: Append mode, appends to previous content
▪ x: Creates a file, and returns error, if it already exists
▪ Suffix t: Specifies text files, by rt, wt
▪ Suffix b: Specifies binary files, like images, by rb, wb
Note that write, append creates a new file if the specified file does not exist, but read
gives an error if the file is not found.
If the file is in the same directory as the .py source code, only file name with extension
works. But if in any other directory, the full file path needs to be supplied and Python
supports forward slash / in the file path.
read( ) mode:
BENCHMARK +91 9836919972 Page | 17
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs
Benchmark: Playing with Python – Workshop Handout
A live online workshop series conducted voluntarily by Benchmark, with much love, on learning Python
append( ) mode:
Observe the old version of the file ‘[Link]’ above and the appended version below.
write( ) mode:
Observe the old version of the file ‘[Link]’ above and the new version below with
previous contents destroyed on write( ).
BENCHMARK +91 9836919972 Page | 18
Reach us at: Facebook | Twitter | LinkedIn | Instagram | GMaps
Share among whoever needs