Anti Textbook Py
Anti Textbook Py
textbook
Suraj
Build applications in Python the anti
textbook
Suraj
This book is for sale at http://leanpub.com/antitextbookpy
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
2017 Suraj
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Python 2 vs Python3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Taking input from the user. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Variable types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Numeric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Operator priority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Multiply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Exercise: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Exercise: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Shortcut operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Membership test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Boolean operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Exercise: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
File handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Writing files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Reading files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Examples: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Appending files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Other methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Using modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Make Code Great Again! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Some examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Merge two files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Student Marks Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
CSV to SQL generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Introduction
Watch on YouTube
Python is an open source, cross platform, interpreted language. Cross platform means that a
program written on any platform is guaranteed to run across any platform which Python supports. It
is a powerful language because there are many third party packages available. The official repository
of those packages is http://pypi.python.org. Python comes with a tool called pip which allows
downloading libraries from the official repository.
Python is widely used across major domains. YouTube, Quora, Hulu, Dropbox are just a few
platforms written in Python. We can write command line applications, bootloaders, Robots, Machine
Learning algorithms, automate test cases, system administration automation etc using Python.
There are few things which make Python an awesome language:
1. English like syntax: It takes very little time to learn the language and write code. This makes
it an excellent prototyping language.
2. Less number of lines: Programs implemented in Python have significantly less number of lines
as compared to other languages.
3. High level data structures: Hashmaps, sets, lists make it really easy to write complex programs
with comparatively less effort..
Drawbacks:
Python 2 vs Python3
Python3 is the successor of Python2. In 2020 Python2 will be history. This tutorial is based on Python3
as it is the present and future of the language. Python3 is a backwards incompatible with Python2,
which means, code written for Python2 is not guaranteed to run on Python3. There is a way of
writing code which runs both on Python 2 and 3, but it is beyond the scope of this guide.
https://www.youtube.com/watch?v=7wuKDDMb3R4
https://github.com/python/cpython
Introduction 2
Installation
1. Windows: Download the latest .exe file of Python3 from https://python.org and click on Next
Next.
2. Android: Install Termux (https://termux.com/help.html), and then apt-get install python3..
3. Linux: sudo apt-get install python3 / sudo yum install python3 / use other package manager
4. Mac: brew install python3.
5. iOS: python 3 for ios (not free).
Links
Interactive mode
Code is typed inside an interpreter session which gets evaluated immediately. This mode is suitable
for small edits.
To start the interpreter, type python3 or python on your machine, when the interpreter starts, it
should look like this:
This shows that the default Python shell has started. >>> signifies the input location.
1 >>> print("hi")
2 hi
Batch Mode
Save the code in a file. if the file name is file.py, then we execute it like by typing python3 file.py
in the command line not the interpeter.
Executing 3 vs 2
We are going to use python3 to run the code, depending on your machine, you might need to use
python instead of python3 to execute code.
https://www.youtube.com/watch?v=wSqRUTS7uAg
Understanding the program 4
1 If it says Python 2, then you'll need to install python3 and execute code using \
2 `python3 file.py`.
3 If it says Python 3, then you'll need to run all the code in this book as `pytho\
4 n file.py`
Installing packages
pip or easy_install is used for installing third party packages in Python. pip stands for Python
installer package. The package that you are installing, needs to be hosted on the official packages
repository.
pip install ipython Installs the ipython package. This might work differently if you are on
Windows, read the docs for more details.
Comments
Watch on YouTube | Read the docs
Comments in Python start with the # character. It can be present in any position of a line, the text
after the # till the end of the line is the comment.
Comments are ignored by the interpreter. They are used for code readability & maintainability.
Non obvious code should have comments associated with it.
Consider a+=1, there is no need to add a comment saying increments the variable a by 1
because it is obvious.
Now, consider a = 4*c+4*d/6*c. This is not obvious, thus, would warrant a comment.
Getting Help
We need to understand how to get help in Python.
Example:
https://docs.python.org/3/installing/index.html
https://www.youtube.com/watch?v=oU1rHEnfgcM
https://docs.python.org/3/reference/lexical_analysis.html?highlight=comments#comments
Understanding the program 5
Output of help
1 >>> help('')
2 Help on class str in module builtins:
3
4 class str(object)
5 | str(object='') -> str
6 | str(bytes_or_buffer[, encoding[, errors]]) -> str
7 |
8 | Create a new string object from the given object. If encoding or
9 | errors is specified, then the object must expose a data buffer
10 | that will be decoded using the given encoding and error handler.
11 | Otherwise, returns the result of object.__str__() (if defined)
12 | or repr(object).
Output of dir
1 >>> dir('')
2 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '_\
3 _eq__', '__format__', '__ge__', '__getattribute__',
4 '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subcl\
5 ass__', '__iter__', '__le__', '__len__', '__lt__',
6 '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__rep\
7 r__', '__rmod__', '__rmul__', '__setattr__',
8 '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center',\
9 'count', 'encode', 'endswith', 'expandtabs', 'find',
10 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', '\
11 isidentifier', 'islower', 'isnumeric', 'isprintable',
12 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans'\
13 , 'partition', 'replace', 'rfind', 'rindex', 'rjust',
14 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', \
15 'swapcase', 'title', 'translate', 'upper', 'zfill']
Links
1 >>> i = 1
2 >>> # creates an integer variable of value 1.
3 >>> print(id(i))
4 405911019
5 # address of the variable i
6 >>> type(1) # returns the data type of variable i.
7 <class 'int'>
8 >>> i = 'Python'
9 >>> # Creates a string variable with value Python
10 >>> print(id(i))
11 505911019
12 >>> type(i) # variable i is of type string now.
13 <class 'string'>
id() is a function which returns the address of an object. You can read more in the docs here.
type is a builtin function which returns the data type of the argument passed to it. Read the docs
https://www.youtube.com/watch?v=3_-W0S1VdLo
https://docs.python.org/3/reference/expressions.html#atom-identifiers
https://docs.python.org/3/library/functions.html?highlight=id#id
https://docs.python.org/3/library/functions.html?highlight=id#type
Variables 7
Lets have a brief overview of the data types, we will be looking at them in detail in the next chapter.
By default, input returns a string, if we want to take input an integer, we use this.
The int(a) converts the variable a to integer and throws an error if it cant convert it into integer.
There are other functions like float(), str() which do data type conversions.
In the above case, we took only one input from the user, we can take as many as we want, we just
need to have that many input statements.
Exercise
1. Take the users name, age and height and print it to the terminal.
2. Take a number from the user and print it.
Variables 8
Variable types
Numeric
Read the docs.
Integer
1 >>> i = 0
2 >>> i = 10000
3 >>> i = 123333
4 >>> print(type(1))
5 <type 'int'>
Float
1 >>> i = 1.1
2 >>> i = 3.3333344445
3 >>> print(type(1.0))
4 <type 'float'>
Complex
1 i = complex(1,2) # 1+2j
2 i = complex(12,23) # 12 + 23j
String
1 i =
"this string\n\t" # \n and \t are special values if used with "
2 i =
'this string\n\t' # \n and \t are special values if used with '
3 i =
r'this string\n\t' # \n and \t do not hold special value if prepended with r\
4 , r
stands for raw'
5 i =
''' multi line string
6 which has
7 multi lines'''
8 i = """
9 a ridiculously multi line string
10 """
https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
Variables 9
String validity
The location of the ending quotes doesnt matter, the only thing which matters is that it should
exist. The value of a string is in between the starting and ending quotes, either single, double or
triple.
'this is a string', "this is a string", "'this is a string", '"this is a string', """
this is 'a string '""", '"""hi'
All are valid strings, they start and end with the same quote. If a string starts and ends with a single
quote, then any number of double quotes can be part of the string, the same is true vice versa.
'"python" and "python'' are invalid strings. Both of them do not start and end with the same quote.
Boolean
True and False are special values in Python3. In previous version of the language, we were allowed
to create a variable of name True and False, but now they are reserved.
Read the docs
1 >>> i = True
2 >>> j = False
3 >>> print(i)
4 True
Sequence type
Read the docs
List
1 i = [1,"Linux",3,"bash",[1,2,"sh"]]]
Set
Immutability
When a data type is said to be immutable, the data type doesnt allow modifications.
id() returns the address of the variable, if two id function call return the same address, then it is
the same variable, thus, mutable. Strings are immutable, as we can see in the below example.
Read the docs
1 >>> a = '1'
2 >>> id(a)
3 4469201376
4 >>> a = a + '1'
5 >>> id(a)
6 4469201408
### Hashing
Hashing is the process of converting some large amount of data into a much smaller amount
(typically a single integer) in a repeatable way so that it can be looked up in a table in constant-
time (O(1)), which is important for high-performance algorithms and data structures. You can read
more here
Dictionary and List are not allowed, but Tuples are.
1 i = set([1,2,3,4,5])
Tuples
1 >>> a = (1,2)
2 >>> a[0]=2
3 Traceback (most recent call last):
4 File "<stdin>", line 1, in <module>
5 TypeError: 'tuple' object does not support item assignment
https://docs.python.org/3/library/stdtypes.html#immutable-sequence-types
http://stackoverflow.com/questions/2671376/ddg#2671398
https://docs.python.org/3/library/stdtypes.html#tuple
Variables 11
1 a = (1,2,3)
2 print(a)
Dictionary
Operator priority
When expressions containing more than one operators are evaluated, the operator priority is
followed, it is just like the BODMAS rule of maths. Usage of () can override priority.
a = 3 * 2 + 20 - 45 returns -19
a = 3 * (2 + 20 - 45) returns -69. This will multiply 3 after doing other calculations.
Assignment
= is the assignment operator.
Multiply
1 >>> a = 12
2 >>> a * 12
3 144
4 >>> a = "ab_"
5 >>> a * 2 # when * is used with strings,
6 # it returns a new string twice.
7 ab_ab_
Operators 13
Exercise:
1. Take two numbers from the user and print their multiplication.
2. Take three numbers from the user and print their multiplication.
Add
1 >>> a = 1
2 >>> a + 1
3 2
4 >>> a = 'py'
5 >>> a + 'thon' # concatenates 'thon' to 'py' and creates a new string.
6 python
7 >>> a # we did not reassign a, so it's value is unchanged.
8 py
Exercise:
1. Take two strings from a variable and print their concatenation.
2. Take two numbers from the user and add them (you need to use the int() to convert the input
to integer)
3. Take three numbers from the user and print their addition.
Equality
== is the equality operator, it returns true if both operands have the same value.
Note: It is a classic mistake to use == when you really want to use = or vice versa.
Division
Operators 14
1 >>> i = 10
2 >>> i/2 # / returns the quotient (floating point number)
3 5.0
4 >>> i//2 # // returns the quotient (integer number)
5 5
6 >>> i%2 # % returns the remainder (integer number)
7 0
Power
** is the operator for power.
1 >>> a = 2
2 >>> a**3
3 8
Shortcut operators
+=, -=, /=, *=
There is no operator for ++ or --, you can use a+=1 and a-=1 instead. No spaces are allowed between
-=, +=.
a = a + 1 increments the value of variable a and stores it back in a.
Along with this, you can use this syntax for other operations like -=, /=. In each of such operation
you perform that operation and store its value in the variable itself.
Membership test
The in operator tests if the element on the left hand side is present in the right hand side sequence
(list, tuple, set).
Boolean operators
Read the docs
http://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
Operators 15
not
Read the docs not converts True to False and vice versa.
Variables of any data type when they are null or have no value, they are False like values. The
negation of a False like value is True
True like
Variables of any data type when have some value, any value, they are True like values. The negation
of a True like value is False
or
OR is true when either of the operand is true.
http://docs.python.org/3/library/stdtypes.html#truth-value-testing
Operators 16
and
AND is true when both the operands are true.
Comparision Operators
Read the docs
There are eight comparison operations in Python. They all have the same priority (which is higher
than that of the Boolean operations). Comparisons can be chained arbitrarily; for example, x < y <=
z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not
evaluated at all when x < y is found to be false).
This table summarizes the comparison operations:
Operation Meaning
< strictly less than
<= less than or equal
> strictly greater than
>= greater than or equal
== equal
!= not equal
is object identity
is not negated object identity
https://docs.python.org/3/library/stdtypes.html#comparisons
Operators 17
Links
List
Watch on YouTube | Read the docs
An ordered collection of n values (n >= 0)
Creating a list is simple:
https://www.youtube.com/watch?v=30S9LnvanwY
https://docs.python.org/3/library/stdtypes.html#list
High level data structures 19
1 l = [11,22,33,44,55]
2 print(l[0]) # first value.
3 print(l[1]) # second value.
4 print(l[-1]) # last value.
5 print(l[-2]) # second last value.
6 print(l[-100]) # index out of range error.
List Methods.
append
The append function takes one argument and adds it to the end of the list.
1 >>> a = []
2 >>> a.append(1)
3 >>> a
4 [1]
5 >>> a.append("2")
6 >>> a
7 [1, '2']
8 # here '2' was added at the end of the existing list.
9 >>> a.append(1.11111)
10 >>> a
11 [1, '2', 1.11111]
12 >>> a.append([1,2,3])
13 >>> a
14 [1, '2', 1.11111, [1, 2, 3]]
15 # here, the entire list was inserted at the end of
16 # the existing list.
extend
High level data structures 20
1 >>> a
2 [1, '2', 1.11111, [1, 2, 3]]
3 >>> b = [1,2,3]
4 >>> a.extend(b)
5 >>> a
6 [1, '2', 1.11111, [1, 2, 3], 1, 2, 3]
7 # All elements of the list b were added individually
8 # to the list a.
pop
pop deletes and returns one element. It takes one optional argument.
1 >>> a
2 [1, '2', 1.11111, [1, 2, 3], 1, 2, 3]
3 >>> a.pop()
4 3
5 >>> a
6 [1, '2', 1.11111, [1, 2, 3], 1, 2]
Valid index is passed: Deletes and returns the value at that index.
1 >>> a
2 [1, '2', 1.11111, [1, 2, 3], 1]
3 >>> a.pop(0)
4 1
5 >>> a
6 ['2', 1.11111, [1, 2, 3], 1]
### copy a.copy() creates a new list with the values of list a.
High level data structures 21
1 >>> a = [1, 2, 3, 4, 5, 6]
2 >>> b = a # a and b are pointing to same object.
3 >>> a[1]=-111 # when we change a, b also changes.
4 >>> a
5 [1, -111, 3, 4, 5, 6]
6 >>> b # b changed when a changed.
7 [1, -111, 3, 4, 5, 6]
8 >>> c = a.copy() # creates a new list object.
9 # having the values of list a.
10 >>> a
11 [1, -111, 3, 4, 5, 6]
12 >>> a[1]=999
13 >>> a
14 [1, 999, 3, 4, 5, 6]
15 >>> c # c didn't change when a changed.
16 [1, -111, 3, 4, 5, 6]
17 >>> b # b changed when a changed.
18 [1, 999, 3, 4, 5, 6]
#### Other functions Learning a language requires self practice! If we explain each and every
function, we will hinder your path of exploring the language, we encourage you to use the help()
function to find out more about other functions which are allowed on Lists.
Slicing
Slicing is an easy way to fetch sub parts of them. Lists, strings and tuples allow Slicing.
The syntax of slicing is l[start_index : end_index].
1 l: the list
2 start_index: Starting point with the element at this
3 index included; defaults to 0 if left blank.
4 end_index: Ending point, but this index is excluded;
5 defaults to -1 if left blank.
Slicing returns a new object (list or string or tuple) from the start_index to the end_index without
including end_index.
High level data structures 22
1 >>> l = [0,1,2,3,4]
2 >>> l[0:3] # from 0 till 2nd element
3 # the element of index 3 is not returned
4 [0, 1, 2]
5 >>> l[1:3] # new list starting from 1 till 2nd element
6 # 3rd element is not returned
7 [1, 2]
8 >>> l[-1:] # new list containing the last element
9 [4]
10 >>> l[0:-1] # new list from 0 excluding last element
11 [0, 1, 2, 3]
12 >>> l[0:-2]
13 [0, 1, 2]
14 >>> l[::-1] # returns a new list with values reversed
15 [4, 3, 2, 1, 0]
16 >>> for i in l:
17 ... print(i)
18 ...
19 0
20 1
21 2
22 3
23 4
Slicing also works with strings and tuples. We invite you to try them out. Dont just read this book,
execute code!
Tuples
Read the docs | Watch the video
Tuples are read only lists. A tuple object, once created, doesnt allow us to add, delete or update an
element. When we use the dir on a tuple object, we find that there are only two methods, count
and index.
https://docs.python.org/3/library/stdtypes.html#tuple
High level data structures 23
1 >>> a = (1,2,3,4)
2 >>> type(a)
3 <class 'tuple'>
4 >>> 1 in a
5 True
6 >>> a[0]
7 1
8 >>> a[::-1]
9 (4,3,2,1)
10 >>> a[0:2]
11 (1,2)
List vs Tuple
Lists are used when we are not sure how many values well be having.
Tuples are used when there is a fixed number of values to deal with.
Set
Watch on YouTube | Read the docs
Sets are same as lists with the following limitations:
1 >>> a = [1,2,3,4]
2 >>> b = set(a)
3 >>> b
4 {1, 2, 3, 4}
5 >>> type(b)
6 <class 'set'>
7 >>> b[0] # sets do not allow this.
8 TypeError: 'set' object does not support indexing
When we try to create a set from a list which has a list element, it is an error (only basic elements
int, float, string are allowed) :
https://www.youtube.com/watch?v=QmfDyjp0Z8E
https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset
High level data structures 24
1 a = [1,2,3,[2,3]]
2 set(a)
3 ## Throws an error.
Sets allow various methods like add, copy, deepcopy, update, pop, remove.
Dictionary
Read the docs | Watch on YouTube
Dictionaries are key value pairs. Keys can only be hashable data types i.e. basic data types. There is
no such restriction on the values.
Lists are indexed starting from 0, it is done by the interpreter itself. Dictionaries allow custom indexes
i.e. keys.
Note: the representation of both set and dictionary is by using curly braces, {}.
https://docs.python.org/3/library/stdtypes.html#dict
https://www.youtube.com/watch?v=pQV3wbSMBRI
High level data structures 26
1 >>> a.keys()
2 dict_keys(['IN', 'US', 'ES'])
3 >>> a.values()
4 dict_values(['India', 'United States of America', 'Espanol'])
We also encourage you to try out everything we did in this chapter, again! (on strings too)
Exercises:
Links
1 >>> a
2 ['2', 1.11111, [1, 2, 3], 1, 2, 3]
3 >>> del a
4 >>> a
5 Traceback (most recent call last):
6 File "<pyshell#0>", line 1, in <module>
7 a
8 NameError: name 'a' is not defined
Exercise:
1. Given that a = [1,2,3,4,[5,6,7]]
1. Delete the first value.
2. Delete the second value of the list present at index 4.
3. Delete the variable a.
4. Take an input value from the user and delete it (if it is present), print value not present
if the value is not present in the list.
2. Given that a = {IN:India, ES:Espanol}
1. Delete the key IN.
2. Delete the key ES
3. Delete the variable a.
if-else statment
Read the docs | Watch video 1| Watch video 2
Lets say we work for a school and we are asked to write a program to print out if a student has
passed or failed for a particular exam. a is the score that the student scored in a subject, b is the
maximum marks for the subject. Passing percentage is 35.
https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
https://youtu.be/fbCsCFuj6zE
https://youtu.be/YjUo6TQ2EzE
Constructs 28
1 a = 67
2 b = 100
3 percent = (a/b)*100 # calculating percentage marks.
4
5 if percent < 35:
6 # block executes if percent is less than 35.
7 print("Failed.")
8 else:
9 # block executes if percent is greater than 35.
10 print("Passed! Keep it up!")
Output:
Indentation
Python uses spaces for indentation. Either spaces or tabs can be used for indentation, but not both.
Usage of four spaces is recommended.
1 a = 67
2 b = 100
3 percent = (a/b)*100 # calculating percentage marks.
4
5 if percent < 35:
6 # block belongs to the if statement.
7 print("Failed.")
8 print("Please appear for exams again.")
9 else:
10 # block belongs to the else statement.
11 print("Passed!")
12 print("Study for the next exams now.")
13 print("END")
Scoping
Each statement belongs to one or more scopes, one direct scope and multiple indirect scopes.
For visualization, lets draw [] around the spaces, so the code now looks like this:
Note: This is not valid Python code, [] is used just to show the indentation.
Constructs 29
1. After a construct which ends with a colon (if/for/while/try/except), calculate the number of
spaces in the immediate next line. In this example, it is four spaces. The first [] block.
2. Every line below this line which has four spaces at the start until we get a line which doesnt
have four spaces is in the if block.
1 if a > 1:
2 print("hi")
3 print("bye")
4 print("hi")
The error is because the interpreter isnt able to find out what block the statement belongs to.
elif
The elif block is special, it gets evaluated if the parent if block has been evaluated to False.
In our school, we now want to print the grades of students.
1 F: percentage < 35
2 C: 35 < percentage <= 50
3 B: 50 < percentage <= 70
4 A: 70 < percentage <= 90
5 A+ 90 < percentage
Note: elif and else cant exist without a corresponding if block. Only the if block
can exist independently.
Constructs 30
1 a = 67
2 b = 100
3 percent = (a/b)*100 # calculating percentage marks.
4
5 if percent < 35:
6 # block belongs to the if statement.
7 print("F")
8 elif percent <= 50:
9 print("C")
10 elif percent <= 70:
11 print("B")
12 elif percent <= 90:
13 print("A")
14 else:
15 print("A+")
Output: B
Nesting
We can have multiple if blocks inside an if block (same is true with for/while statements). This is
called nesting. When we write nested if statements, then the same logic applies to indentation.
This is an example of two level nesting:
1 if a > 1:
2 if b < 1:
3 print(" b is less than 1")
4 print("a is greater than 1")
5 print("This is not in either of the above blocks")
Note: This is not valid Python code, [] is used just to show the indentation.
Constructs 31
1 if a > 1:
2 [ ]if b < 1:
3 [ ][ ]print(" b is less than 1")
4 [ ]print("a is greater than 1")
5 print("This is not in either of the above blocks")
1 if True:
2 # statements here are in the
3 # scope of the if statement.
4 print("Statement is true")
5 if True:
6 # statements here are in the inner if
7 # but overall, lie in the scope of
8 # the outer if.
9 print("Another statement")
10 else:
11 # statements here are in the
12 # scope of the else statement.
13 print("Statement is false")
14 if True:
15 # statements here are in the inner if
16 # but overall, lie in the scope of
17 # the outer else.
18 print("Another statement")
Exercise
1. Take the name, age and score of the user as input. Print their percentage. If the percentage is
less than 60, print F, if percentage is between 60 and 70 print B and if percentage is greater
than 70 print A. Marks are out of 100.
2. Take a number from the user and print if it is even or odd.
3. Take two numbers from the user and print which number is largest.
4. Take two numbers from the user and print which number is smallest.
5. Take three numbers from the user and print largest and smallest number.
6. Take four numbers from the user and create a list.
7. Take four numbers from the user and create a set.
Constructs 32
for loop.
Read the docs
for statement is used to cycle over a list/set/tuple.
1 l = [1,2,3,4,5]
2 for i in l:
3 print(i)
Output: 1 2 3 4 5
Explanation:
The program goes through five iterations
Iteration 1: Value of i is 1; loop prints 1;
Iteration 2: Value of i is 2; loop prints 2;
Iteration 3: Value of i is 3; loop prints 3;
Iteration 4: Value of i is 4; loop prints 4;
Iteration 5: Value of i is 5; loop prints 5;
In a for loop, we can perform other operations too, for the sake of simplicity, we just printed the
value of the variable i.
Another approach.
1 for i in range(len(l)):
2 print(l[i])
Output: 1 2 3 4 5
Explanation: range(5) returns a list [0,1,2,3,4].
for has an else block. It is strange at first glance, but it is quite helpful in certain cases, like finding
if a number if prime or not.
We will take a simple example where we want to find out if an element is present in a list or not.
https://docs.python.org/3/reference/compound_stmts.html#the-for-statement
Constructs 33
1 l = [1,2,3,4,5]
2 val = 99
3
4 for i in l:
5 if i == val:
6 # for understanding break
7 # check the below section.
8 break
9 else:
10 print("Not present")
Exercise:
1. Print all numbers till 100.
2. Print all numbers from 20 to 100.
3. Print all even numbers less than 100.
4. Print all odd numbers less than 100.
break
1 for i in range(10):
2 if i == 4:
3 break
4 print(i)
The above block will print all the numbers until it hits 4. The moment it hits 4, the for loop will stop
executing.
continue
1 for i in range(100):
2 if i == 4:
3 continue
4 print(i)
The above block will print all numbers except 4. It continues to the next iteration.
Note: break and continue work with loops only, either for or while.
Exercise:
1. Print all numbers from 0 till 10 except 4 and 5.
2. Print all numbers from 0 till 20 except those divisible by 3.
3. a = [1,2,3,4,5], take an integer as input from the user. Print all numbers which are less than the
input number using break.
pass
Pass can be used as an empty placeholder in places where you dont have anything to add.
1 if a > 1:
2 pass
3 else:
4 print("TODO")
For instance, in the above if-else block, you really dont know what logic you are going to put, so
you can either use print("TODO"), or use the pass statement. pass doesnt print anything. You can
use pass in any loop.
while loop.
Read the docs
while is used when you have to loop for a specific condition. If you dont have a condition, you can
use True and that would result in an infinite loop.
https://docs.python.org/3/reference/compound_stmts.html#the-while-statement
Constructs 35
1 i = 10
2 while i >= 0:
3 print(i)
4 i = i - 1
Output: 10 9 7 6 5 4 3 2 1 0
Explanation: The while statement will loop on the condition, until the condition evaluates to False.
The loop exits when the condition becomes False.
Note: The while statement also has an else block, we encourage you to play with it to
understand it better.
Exercise:
1. Print all numbers till 100 using while.
2. Print all numbers from 100 to 1 using while.
3. Print all even numbers less than 100 using while.
4. Print all odd numbers less than 100 using while.
5. For a list [1,2,3,4,5], write a program which checks if 6 is present in the list, using while.
6. Print all numbers from 0 to 10 except 4 and 5.
with
Note: requires the knowledge of file handling, you can come back to this chapter after
reading the file handling chapter.
Output: 10 1 21 19 30 20
Because of scoping, the input_file and output_file variables are only available within the with
clause. This can result in some clean code, but it is upto you totally. I like to use try-except-finally.
We encourage you to read more about the range function. Please do not use the Internet, use the
help function. The faster you get familiar to using the documentation, the better programmer you
become. In a world full of people Googling how to create a string in Python, we really need to be
self sufficient as to using the documentation that a language provides to differentiate from others.
The mark of a great programmer isnt in how much things she can store in her memory, it is in the
mastery of the tools she uses, documentation and the help command are among the tools.
Links
Writing files
Write all even numbers till 100 to a file named even.txt and write all odd numbers to odd.txt.
https://docs.python.org/3/library/io.html#io.TextIOWrapper
File handling 38
Reading files
Read all the content from the file even.txt and print it to the terminal
You will see that each number is printed on a different line. This is because of the new line (\n)
character which we wrote.
1 python
2 is a good language
on any text editor, because the \n gets converted into an actual new line. But, the content of the
text file is still python\nis a good language.
File handling 39
Output: [01234]
We can see that the file contains only one line. This is because, the write method doesnt add a new
line character at the end of each operation. To understand it better, open this file in a text editor.
Now, consider this block.
Output:
[0\n, 1\n, 2\n, 3\n, 4\n]
By adding a \n in each write call, we ensure that the new number gets written to a new line, thats
why, when we run the above code, we will get four lines in the text file. Verify this by opening the
file in a text editor.
While reading a file, we need to deal with the \n present at the end of each line. There is a shortcut
to remove the \n.
1 f = open("even.txt")
2 lines = f.readlines()
3 print(lines)
4 lines = [line.strip() for line in lines] # list comprehension
5 print(lines)
List comprehension
It is a shortcut of working with lists, to perform filter or some other operation on the entire list.
File handling 40
1 for i in range(len(lines)):
2 lines[i] = lines[i].strip()
##### Syntax
List comprehension returns a new list based on the current list, the first argument list.strip()
would be the elements of the list. The second argument is the for loop, the third argument is optional,
you can have an if block there.
Examples:
1. Convert each element to upper case (works only when each element is a string). python a =
["a.txt", "b.txt", "c.tct", "j.txt"] b = [i.upper() for i in a] print(b)
2. Convert each element to lower case (works only when each element is a string). python a =
['A.TXT', 'B.TXT', 'C.TCT', 'J.TXT'] b = [i.lower() for i in a] print(b)
3. Give all the values of list a which are greater than 3.
1 a = [1, 3, 4, 5, 5]
2 b = [i for i in a if i > 3]
3 print(b)
4. Give all the values of list a which are greater than 3. python a = ["Haskell", "Ruby",
"Python"] b = [i for i in a if len(i) > 3] print(b)
5. Give all values in a which are even.
1 a = [1, 3, 4, 5, 5]
2 b = [i for i in a if i % 2 == 0]
3 print(b)
Appending files
Write even numbers from 100 to 200 in a file even.txt which already contains even numbers from
0 to 100.
File handling 41
Binary Mode
1 f = open("file.txt", "rb")
Python allows us to manipulate binary files, we have to club the modes, b stands for binary, along
with b, we can use any of r/w/a.
Note: One essential part of working with files is removing the \n or adding it when it
is required.
Other methods
1. read() Takes an argument as number of bytes to be read.
When you open a file for reading, the pointer is at the 0th position. if you do f.read(1), the
pointer moves to 1. If you do f.read(1) again, the pointer would return the 2nd character of
the file and not the first one.
All the read functions returns characters based from the current pointer, you can know where
the pointer is located currently by using f.tell().
1 >>> f = open("lines.txt")
2 >>> f.tell() # pointer is at 0th position
3 0
4 >>> f.read(1) # returns the first character
5 'f'
6 >>> f.tell() # pointer shifted to 1st character
7 1
8 >>> f.read(20) # reads 20 characters after 1 ('\n' is considered as single char\
9 acter)
10 'irst line\n second li'
11 >>> f.tell()
12 21
13 >>> f.read(10000) # reads 17 char after 21
14 'ne \n third line \n'
15 >>> f.tell() # pointer on 38
16 38
17 >>> f.read(10) # read 10 more char
18 ''
19 >>> f.tell() # this is the last position!
20 38
21 >>> f.seek(0) # pointer is now at start of file
22 0
23 >>> f.read(1)
24 'f'
In this example, you hit the end of the file, there are only 38 characters in it, \n also is a
character. If you read beyond the max characters of a line, it will return you blank strings. but
there might be cases in which you would want to start reading the file again from any other
position.
You can use seek function to reset the pointer at any position of your choice. Lets say that
you want to re-read the file again
1 >>> f.seek(0)
This will reset the pointer and you should be able to read it from the first character of the file!
2. readline() Returns the current line that the pointer is located.
File handling 43
1 >>> f.seek(0)
2 0
3 >>> f.readline()
4 'first line\n'
5 >>> f.readline()
6 ' second line \n'
7 >>> f.readline()
8 ' third line \n'
You can try doing f.seek(0) and reading the lines again, youll get the same output as above.
3. readlines() Returns all the lines of the file as a list.
If the pointer is present at the end of file, then it returns an empty list. In that case, do a
f.seek(0) to reset the pointer.
1 >>> f.seek(13)
2 13
3 >>> f.readlines()
4 ['econd line \n', ' third line \n']
5 >>> f.seek(0)
6 0
7 >>> f.readlines()
8 ['first line\n', ' second line \n', ' third line \n']
4. write() Writes the string argument which you pass on the pointer. We need to be careful of
the pointer, otherwise writing to the file can be dangerous. You are responsible for adding the
\n character if you want to a new line, by default itll write at the position the pointer is
currentl f.tell().
5. writelines() If you want to write a list to a file, youll use writelines. Even here you are
responsible for adding \n wherever you feel necessary.
We encourage you to try out file IO on your own, please refer to help() for any details regarding
file, do not search how to open a file in Python on the internet.
Exercise
1. Take the users name, age and height as input and write them to a file using this format.
name,age,height.
File handling 44
2. Read a file input.txt and print all the lines which are present multiple times. Please create
the input.txt file in such a way that it has many lines and few of them are present multiple
times.
3. Read a file input.txt and print how many times each line is present.
4. There are two files, file1.txt and file2.txt
1. swap the content of both files.
2. append the content of file1.txt to file2.txt.
3. append content of file2.txt to file1.txt.
4. take unique content of both files and write them to file3.txt
5. Use any file created above. Take a positive number as input from the user and read those many
characters from the text file.
6. Prepare a csv file like this: first field = name, second onwards marks/100.
1 tom,10,10,10
2 tim,20,20,20
1 tom,10,10,10,30
2 tim,20,20,20,60
7. Read the result.csv file and create a new file named result.txt like the following sample output.
1 tom-10-10-10-30
2 tim-20-20-20-60
8. read the result.txt file. Print every other character from the file.
Links
file: exception.py
1 i = 0
2 j = 100/i
3 print("This is the print statement")
file: exception2.py
1 i = 0
2 try:
3 j = 100/i
4 except:
5 print("Can't divide by 0!!")
When you run this program, youll see that it doesnt kill the program.
A try catch block is to be used for lines which we suspect that something might go wrong, for
instance if you are trying to open a file, there are many things which could go wrong, for instance,
the disk space would go full, the file might not exist or a million other things, thus, we wrap the
open function call in a try catch block.
The try block contains all the lines where we suspect that something might go wrong, the except
block is where the damage control lines are present. What we do for handling exceptions is upto
us, we can just print on the terminal or we can send emails to the respective teams, or just log that
there was an exception.
https://docs.python.org/3/tutorial/errors.html#errors-and-exceptions
Exception handling 46
1 i = 0
2 try:
3 j = 100/i
4 except ZeroDivisionError as e:
5 print(e)
6 except Exception as e:
7 print(e)
8 finally:
9 print("this block always gets executed")
The try-catch block allows you to handle multiple exceptions, they must be in the reverse order of
generality. For instance, if you want to handle every kind of exception, just use except Exception,
the smallest exception statement should be at the top.
Finally
The finally block gets executed every time a try block gets executed. You can put the statements in
this block which you want to be executed every time.
Youd be doing this when you do handle errors for file handling programs.
1 try:
2 f = open("lines.txt", "r")
3 except Exception as e:
4 print(e)
Of course, you can pick up something other than the Exception class. If you want to handle some
specific scenario like FileNotFoundError, use it instead of the Exception as e class. When you use
Exception as e, you get the exception thrown by the code into an object e, you can print the object
or write it to a file. If you dont care for that, just use except Exception:, skip the as e.
Links
file: func.py
The following is the typical function definition on Python, def is the keyword. function_name is
the name to be given to the function.
Functions support zero or more arguments.
Lets write a simple program to print the square of a number passed to it as an argument.
Invoking functions.
1 def square(number1):
2 print(number1**2)
Default arguments
Now that we wrote the square function, we want to generalize it to calculate power. This function
will take two arguments, number and the power, and return number ** exponential.
Functions 48
file: defaultargs.py
Some or all arguments in Python can be optional. When an argument is optional, the default value
of the argument is used.
The rule with default arguments is that they need to be at the right most side of the argument listing,
after all the mandatory arguments.
The following block of code will result in a SyntaxError. python def do(var1=2,var2): print(hi)
do(1)
Return
In the above examples, we saw function which prints the output. A function can also return the its
output to the caller. This is helpful when processing data which we do not want to print.
There is no restriction on the number of values that can be returned. Values are returned using the
return statement and it is not a part of the function signature, a return statement should be directly
added in the function definition.
Here, we have a function which returns two values. The two values are then printed after the
function call.
Links
The software design phase is the most critical phase of software development, writing software is a
cakewalk when the design is done properly.
Before we can write the code, we have to:
1. Identify the input source. Our input source will be command line arguments.
2. Define the functionality of the program. Program will support adding/removing/listing todo
items.
3. Identify where to store data. Program will store content in a flat file.
4. Identify what will be the output. Program will print data on the terminal.
Input
The user is going to give input in the command line arguments.
The sys package has the command line arguments stored into a variable called argv.
file: tasks1.py
Building a todo list manager 50
1 import sys
2 print(sys.argv)
Try running the code, you will see something like this
For the interpreter, tasks.py, title and content are all command line arguments, this is what you see
as the output of the above script.
Output
The output of the program is going to be on the command line.
Formatting output
file: tasks2.py
1 import sys
2 for i, item in enumerate(sys.argv):
3 print("{0} {1}".format(i, item))
Formatting.
Starting from Python3.6, we can do this:
1 print(f"{i} {sys.argv[i]}")
We didnt give any parameter, since we know that the 0th argument is going to be the file name
itself, there are no suprises here.
We passed title and content, and the index 1 is title, index 2 is content.
We passed add title content as the command line argument, and we get the expected output.
Adding commands
file: tasks3.py
1 import sys
2 args = sys.argv
3
4 command = args[1]
5
6 if command not in ("add","remove","list"):
7 print("Invalid command, Use add/remove/list")
8
9 if command == "add":
10 print("adding")
11 elif command == "remove":
12 print("removing")
13 elif command == "list":
14 print("listing")
15 else:
16 print("invalid command!")
Youll get this output when you run the code, this is an exception.
Building a todo list manager 52
Handling errors
In the earlier file, we tried to access an list index which didnt exist. This raised an exception, we
saw in the chapter about exceptions that a try-catch block is used to handle exceptions.
Update the command = args[1] line in the file to this block: python try: command = args[1]
except IndexError: print("Invalid arguments!") sys.exit(1)
Exit
exit kills the program execution with the ID of what we pass in as an argument, 0 is successful exit,
anything greater than 0 is unsuccessful exit
We catch IndexError exception. If there is an IndexError exception that means that the user has not
given the appropriate arguments.
Save and run the file. The output should be this:
We have handled the scenario where the user gives less input than what is required. Now lets move
ahead and type an invalid command.
We can see that the invalid command message is being repeated twice, we have to do something
about that.
We add another exit call after printing Invalid command. The reason being there is no need to go
any further when we have established that the user has given us the invalid command.
Building a todo list manager 53
1 tasks = []
This will create a variable by the name tasks which is visible in this file to all functions.
In the list block, we want to now print the values stored inside tasks variable. If the values arent
present, we should print No tasks present, if there are tasks, then we should print the elements
inside tasks.
We have to use the len() function to check if there is nothing in the variable.
Update this block.
Listing tasks
Building a todo list manager 54
1 # This is a snippet
2 # can't have elif without parent if
3 elif command == "list":
4 if len(tasks) == 0:
5 print("there are no tasks!")
6 else:
7 for task in tasks:
8 print(task)
We now simulate data, before we let the user have the ability to add a task, we will populate the
task variable by ourselves.
For simplicity, we choose this format, the title and content would be concatenated by a | character.
update the tasks = [] to this line, tasks = ["title|content"].
And the else block of len(tasks) to this
Adding a task
We will now work on adding a new task. The input would be taken from the command line argument.
1 if command == "add":
2 print("adding")
1 if command == "add":
2 title = args[2]
3 content = args[3]
4 task = title + content
5 tasks.append(task)
But changing this does nothing, this is because the tasks variable is stored during the runtime. It
gets reset to the default variable when the program quits. We need to add file handling feature to
store the task list.
Replace the tasks line to this to store an empty variable.
Building a todo list manager 55
1 tasks = []
1 if command == "add":
2 title = args[2]
3 content = args[3]
4 task = title + content
5 file = open("tasks.txt", "a")
6 file.write(task+"\n")
7 file.close()
8 elif command == "remove":
9 print("removing")
10 elif command == "list":
11 file = open("tasks.txt", "r")
12 tasks = file.readlines()
13 if len(tasks) == 0:
14 print("there are no tasks!")
15 else:
16 for task in tasks:
17 title, content = task.split('|')
18 print("{0} {1}".format(title, content))
19 file.close()
1 try:
2 file = open("tasks.txt", "r")
3 except IOError as e:
4 print(str(e))
5 sys.exit(1)
6 tasks = file.readlines()
This is a graceful handling of the scenario where we arent able to access the file due to an I/O
(Input/Output) operation error.
Now, we run the add command and try to list the values. We get an error, we cant add a try-
except block to everything, so it is necessary to figure out what the issue is. Here, when we do cat
tasks.txt, we come to know that the content of the file is this.
We dont have a | character between the title and content! We did a mistake when we concatenated
title and content. Remove the file by doing rm tasks.txt, or, delete the file manually if you are on
windows.
Instead of task = title + content, we need this, task = title + "|" + content.
This is the output now
Building a todo list manager 57
This is great! We now are able to add and list the tasks.
Note:
When giving input over the command line, if you want to give multi word input, please enclose
them in either single or double quote. For instance, we gave the input new title, because our title
contained a space. If we had given tasks2.py add new title, new would be considered the title
because space is the delimiting character for any command line input, hence the new title enclosed
in quotes.
1 ch10 $ python tasks3.py add "Finish Python book" "Working on 10'th chapter"
2 ch10 $ python tasks3.py list
3 new title new content
4
5 Finish Python book Working on 10'th chapter
You can see that the output of the list command isnt particularly good, so lets use the advanced
features of the print function for this.
Replace the else block of if len(tasks)==0 by this.
1 print("|-----{0}----{1}----|".format("title", "content"))
2 tasks = [task.strip() for task in tasks]
3 for task in tasks:
4 title, content = task.split('|')
5 print("|-{0}----{1}-|".format(title, content))
Format specifiers enable us to control the layout of the print, we encourage you to try various things
out.
The final code should look like this.
Building a todo list manager 58
1 import sys
2
3 args = sys.argv
4
5 tasks = []
6
7 try:
8 command = args[1]
9 except IndexError:
10 print("Invalid arguments!")
11 sys.exit(1)
12
13 if command not in ("add","remove","list"):
14 print("Invalid command\n Use add/remove/list")
15 sys.exit(1)
16
17
18 if command == "add":
19 title = args[2]
20 content = args[3]
21 task = title + "|" + content
22 file = open("tasks.txt", "a")
23 file.write(task+"\n")
24 file.close()
25 elif command == "remove":
26 print("removing")
27 elif command == "list":
28 try:
29 file = open("tasks.txt", "r")
30 except IOError as e:
31 print(str(e))
32 sys.exit(1)
33 tasks = file.readlines()
34 if len(tasks) == 0:
35 print("there are no tasks!")
36 else:
37 print("|-----{0}----{1}----|".format("title", "content"))
38 tasks = [task.strip() for task in tasks]
39 for task in tasks:
40 title, content = task.split('|')
41 print("|-{0}----{1}-|".format(title, content))
42 file.close()
Building a todo list manager 59
43 else:
44 print("invalid command!")
Removing tasks
To remove tasks, we have to change the way we structure our data. We either can accept deletion
on the basis of the title of the task, or we can render index for each task, since deletion from the
title is not exactly scalable (two tasks can have the same title but different content), we choose to
modify our program to show index for each task, that way, the user can just give the index of the
task which they want to delete.
file: tasks4.py
We first need to modify the way we represent our tasks to the user, instead of showing just the title
and content, we will show the index too. For this, we need to make the following changes.
We cant loop like for task in tasks, we need to loop using range, for i in range(len(tasks))
is the way to go. The only difference is that we have to then fetch the task as tasks[i] rather than
just task, because now, there is no such variable as task.
In the actual delete block, we will use the del keyword which will simplify our task greatly.
1 # Snippet
2 elif command == "remove":
3 task_id = args[2]
4 del tasks[task_id]
Building a todo list manager 60
Note:
We are not validating if the user has given appropriate input, lets say the user gives python
tasks4.py remove instead of python tasks4.py remove 0, then our program should complain
about an error, the same is the case with add, if the user doesnt give both title and content, thats
an error and it should be handled appropriately.
We can see that list indices must be integers is the error we got for the del statement, the reason
for that is that as we said, all shell operations are string based, so when the user gave us the input 0,
it was 0, thus a string. We will typecast the task_id variable to an integer. Change it to this below
statement.
We also need to read the file, for each instance, we read the file or appended it as required.
1 del tasks[int(task_id)]
We also need to write the updated tasks variable to our file, we add a \n to each element using
the list comprehension mechanism.
Now try running the code. python ## snippet elif command == remove: try: file = open(tasks.txt,
r) except IOError as e: print(str(e)) sys.exit(1)
1 file.close()
2 tasks = file.readlines()
3 tasks = [task.strip() for task in tasks]
4 task_id = args[2]
5 del tasks[int(task_id)]
6
7 file = open("tasks.txt", "w")
8 tasks = [task + "\n" for task in tasks]
9 file.writelines(tasks)
Building a todo list manager 61
1 Output:
2
3 ch10 $ python3 tasks4.py remove 0
4 ch10 $ python3 tasks4.py remove 1
5
6 ###### Note:
7 We do not print confirmation like "task deleted", "task added", but you can add \
8 them if you want.
9
10 We have a fully working todo list manager as of now, what we need to do, is to r\
11 educe the redundancy. That'll be undertaken in the next chapter.
12
13 ## Homework
14 Translate this todo list app to use sqlite3 database which comes inbuilt with Py\
15 thon. Please do not use the Internet, use the sqlite3 documentation.
16
17 ##### Links
18
19 |[Next](11-function-tasks.md) | [Previous](09-functions.md) | [Index](SUMMARY.m\
20 d)
21 | ----| ----| ----|
22
23 {bump-link-number}
24
25 {leanpub-filename="11-function-tasks.md"}
26
27 # Adding functions
28
29 > Note: Please see the accompanying code. https://github.com/thewhitetulip/code-\
30 build-app-with-python-antitextbook
31
32 In the earlier chapter we saw how to take input, delete a task, but if you see t\
33 he code, `task4.py`, there is a lot of redundant code, what we need to do is wri\
34 te functions to save repeated code.
35
36 We first start by defining the `main` function. There is no special significance\
37 to the main function, just that we chose to call it as `main`, we can very well\
38 choose to call it 'somerandomfunctionasfasdf'.
39
40 We push everything _except_ the import statement into the main function.
41
42 If you try to run the file at this point, there would not be any output, the rea\
Building a todo list manager 62
43 son for that is that you have declared a function but not **called** it. The int\
44 erpreter runs the script and it creates a function named "main" and it does noth\
45 ing. If you want to **run** the main, you have to call it. At the bottom, add `m\
46 ain()` and then try running the file, this time, it'll give _some_ output.
47
48 Now, we create three functions, `add_task`, `remove_task`, and `list_task`. Thes\
49 e three would do the respective functions.
50
51 The logic of our program should be split between modules. There should be one "c\
52 ontroller" module which handles the IO and a supporting library which does somet\
53 hing with the data, in that way, we can enable different input sources for the s\
54 ame app. Currently, the input source, which is the command line arguments is dee\
55 ply coupled with our todo list manager, so if tomorrow, we want to take input fr\
56 om some other source, we have to rewrite the complete application.
57
58 We manage the functions in this way:
59
60 main: handles the IO
61
62 add_task(title,content): adds a new task with title and content
63
64 list_task(): lists the tasks
65
66 remove_task(index): deletes the task of index.
67
68 By doing this, the main if-else ladder looks like this
69
70 ```python
71 if command == "add":
72 title = args[2]
73 content = args[3]
74 add_task(title, content)
75 elif command == "remove":
76 task_id = args[2]
77 remove_task(task_id)
78 elif command == "list":
79 list_task()
80 else:
81 print("invalid command!")
Now, if we change the input from command line to say FTP, all we have to do is change the main
function, write the FTP input function and call add_task, with the newly fetched title and content.
Modularity is really important in programming.
Building a todo list manager 63
We have no need of declaring the tasks variable in the main function, so we remove it.
We also do not want to write add every time we refer to the add command, so we define three
variables.
1 ADD,REMOVE,LIST = "add","remove","list"
So, the next time we want to refer to add, we will refer to ADD.
This block also changes.
The code looks cleaner than tasks4.py, but there is work to do! We can create a module for these
three functions and reuse the file object instead of redefining the object in each function.
A parting glance at the design of the functions. Each function should do one thing and do it well.
That way, our program is split into multiple functions which do one thing well, just like the UNIX
philosophy.
Links
In the last chapter, we left off by creating three functions for our core functionality. In this chapter,
wed split the functions into two packages,
The todo package is going to contain everything related to our todo manager and the main package
is just going to be the executable program.
The first thing we do, is create a main.py file, and cut paste our main function inside it and remove
the invocation at the bottom of the tasks.py file.
So now our code is split into two files, in Python3 a file can be imported as a package directly, it
should just be present in the same directory.
1 import tasks as t
By doing this, we can access all the members of the tasks.py file by appending t., like t.list_-
task().
Replace the three functions according to this rule. If we had not given the alias t to the library, as is
the keyword used to give alias, then wed have to write tasks.list_task(), but since we are lazy,
we prefer to give it an alias to call it like t.list_task().
Modules are one of the most important aspects of Python. When we import a module, the interpreter
executes the file which points to module.py (if we import a module named module).
This is the way any module is imported:
1. The interpreter first goes to the stdlib folder and tries to find the .py file
2. If found, it executes it
3. If not found, it looks in the current directory
4. If found, it executes it
5. If not found, it complains as ModuleNotFoundError: No module named
Using modules 65
Special care needs to be taken so that we dont create a file with the same file name as a stdlib
package, the humans reading the code would surely get confused, even if Python wont!
As an example, try creating a file sys.py in the current folder. Add one line to that file,
print("something"). Add an import statement import sys and later run python3 tasks.py, youll
see that it doesnt print something, it refers to the stdlib package called sys.
But since this is not a perfect world, wed not be doing all that, in this tutorial we hoped to teach
you the way to develop an app, so you can build everything any feature you want, make sure that
you ping me if you upload it to Github!
Now, we will make a few changes to our tasks library. We do not want the open() call three times,
so we will wrap it in an function of our own, thatll save us multiple open calls.
This will be our function which wraps the file open statement for now.
Why would we need such a thing? Why would we bother to wrap the open in an abstract function?
The reason behind this is that by doing so, we have migrated the entire try-catch block inside one
function. Now, if we were to change something there, we just have to change one block. Hence, the
UNIX philosophy, one program does one thing well.
Using modules 66
You can see that the print statement got executed, as we said, a module when imported is executed
like just another python program. The solution to this problem is to add an if block.
1 if __name__ == "__main__":
2 print("this is some print statement")
3
4 ch12 $ python3 main.py
5 Invalid arguments!
Now we can see that the output is the way we want it. The __name__ is the variable which stores
the way the file was executed, if we run it like python3 file.py then __name__ stores the value
main, otherwise, it stores how it was invoked, in this case, it was invoked as import tasks, thus
__name__ stores the value tasks.
This example of course was basic, but when we write test cases for the functions of that module,
they need to be inside this if block, so that we dont accidentally run the entire suite of test cases
whenever we import the package.
To check the value of __name__, add
1 print(__name__)
2
3 if __name__ == "__main__":
4 print("this is some print statement")
At the bottom of tasks.py NOT in the if block, if you add it to the if block then it wont be executed
unless the file was executed.
Using modules 67
Links
Some examples
1. Merge two files
2. Student Marks manager
3. CSV to SQL generator
1 first_file = open("file.txt")
2 second_file = open("file2.txt")
3
4 first_lines = first_file.readlines()
5 second_lines = second_file.readlines()
6
7 first_lines = [line.strip() for line in first_lines]
8 second_lines = [line.strip() for line in second_lines]
9
10 final_lines = {}
11
12 for line in first_lines:
13 if line not in final_lines.keys():
14 final_lines[line] = 0
15 else:
16 final_lines[line]+=1
13-examples.md
11-function-tasks.md
SUMMARY.md
Using modules 68
17
18 for line in second_lines:
19 if line not in final_lines.keys():
20 final_lines[line] = 0
21 else:
22 final_lines[line]+=1
23
24 lines = "\n".join(list(final_lines.keys()))
25
26 file = open("output.txt", "w")
27 file.write(lines+"\n")
28 file.close()
1 name,science,math,history
2 tony,12,12,12
3 antony,13,13,13
4 bantony,14,14,14
17
18 #TODO print the name of the student who got the maximum aggregare marks
19 # This part is intentionally kept as homework for the readers.
Links
| Previous | Index | -| -|
12-modules-tasks.md
SUMMARY.md
Using modules 70