IDLE Simple python integrated IDE
- Tab completion : Type pr + tab will give the choices of function names having "pr"
- Recall code statements: Alt + p (previous) , Alt + n (next)
- dir(__builtins__) : to list the python built-in functions
- help(map) : To get description of 'map' built-in functions
- PyPI (Python Package Index): Centralized repository for thir-party python modules
- import sys; sys.path : To get the list of locations where python interpreter searches for modules.
- type(varname) : It tells whether the variable mentioned in argument is either list, dictionary or any other data structure
Lists
Creating list:
movies = ['Toy Story', "UP", "Rio", "Finding Nemo" ]
Accessing list:
print(movies[1]) --> UP
print(movies) --> ['Toy Story', "UP", "Rio", "Finding Nemo" ]
Printing first three elements of list
print(unique_james[0:3])
Length of list:
print (len(movies)) --> 4
List methods
movies.append("Lionking") --> ['Toy Story', 'UP', 'Rio', 'Finding Nemo', 'Lionking']
movies.pop() --> 'Lionking'
movies.extend(["Lionking", "Despicable me"])
--> ['Toy Story', 'UP', 'Rio', 'Finding Nemo', 'Lionking', 'Despicable me']
movies.remove('Lionking') --> ['Toy Story', 'UP', 'Rio', 'Finding Nemo', 'Despicable me']
movies.insert(0,'Lionking')
--> ['Lionking', 'Toy Story', 'UP', 'Rio', 'Finding Nemo', 'Despicable me']
isinstance : is a function to check if specific identifier holds the data of specific type
isinstance(movies,list) --> True
isinstance(a,int) --> True
isinstance(movies,int) --> False
Lists within lists
movies = [ "UP", 2009, "Pete Docter", 96, ["Ed Asner" , ["Christopher Plummer", 'Jordan Nagai', 'Bob Peterson' ] ] ]
print(movies[4][0]) --> Ed Asner
print(movies[4][1]) --> ['Christopher Plummer', 'Jordan Nagai', 'Bob Peterson']
print(movies[4][1][0]) --> Christopher Plummer
Tuple
It's an unchangeable (immutable) list.
tup = ( "abc" , "def")
print(tup[0])
Sets
Set is another data structure in python. The data items in set are unordered and duplicates are not allowed.
>>> distances = {12, 44, 53, 44, 32, 101}
>>> distances
{32, 12, 44, 101, 53}
>>> my_list = [ 12, 14, 16, 12, 18, 20 ]
>>> my_list
[12, 14, 16, 12, 18, 20]
>>> filtered_set = set(my_list)
>>> filtered_set
{16, 20, 18, 12, 14}
Set example with list comprehension
set([sanitize(each_ele) for each_ele in my_list])
In the above example sanitize is a user defined function.
Dictionaries
Creating empty dictionary
>>> dhoni={}
Assigning values to dictionary
>>> dhoni['Name']="Mahendra Singh Dhoni"
>>> dhoni['Occupation']=['Cricketer','Businessmen','actor']
Another way of creating empty dictionary, using dict() factory method
>>> kumble=dict()
Another way of assigning values to dictionary
>>> kumble={'Name':'Anil Kumble','Occupations':['Cricketer','photographer','Engineer']}
Accessing dictionary values
>>> dhoni['Name']
'Mahendra Singh Dhoni'
>>> kumble['Occupations'][-1]
'Engineer'
Example of returning dictionary from a function
def get_runs(runs_scored) :
return ( { 'Name' : runs_scored.pop(0),
'Team' : runs_scored.pop(0),
'Runs' : sorted(runs_scored, reverse=True)
} )
>>> kohli=['Virat Kohli', 'India', 42, 121, 87, 91, 67, 117, 143, 3]
>>> kohli_dict = get_runs(kohli)
>>> print ("Top 3 runs scored by " + kohli_dict['Name'] + str(kohli_dict['Runs'][0:3]))
Top 3 runs scored by Virat Kohli [143, 121, 117]
String operations
line.find(':')
line.split(':',1) --> Split using delimiter ':' and the 1 suggests to split only based on 1st occurrence of ':'
line.strip() --> Remove unwanted white-space
Sorting
1) In-place sorting using sort() method: It sorts and replaces the original data. The original ordering is lost
data = [6,3,1,2,4,5]
data.sort()
>>> data
[1, 2, 3, 4, 5, 6]
2) Copied sorting using sorted() method. It returns a sorted copy of original data. Original data is untouched.
data = [6,3,1,2,4,5]
data2 = sorted(data)
>>> data
[6, 3, 1, 2, 4, 5]
>>> data2
[1, 2, 3, 4, 5, 6]
3) Use 'reverse=True' argument to sort in descending order. Ex sorted(my_list,reverse=True)
if else
if isinstance(n,list) :
for o in n:
print ("xx",o)
else:
print ("x", n)
if with Boolean variables
indent=True
if indent:
print("Yes")
else :
print("No")
If while comparing values
var=101
if var == 102 :
print("Correct, value = ",var)
else:
print("Incorrect, value != 102")
if /else if and string comparision example
if role == 'Man' :
man.append(line_spoken)
elif role == 'Other Man':
other.append(line_spoken)
if with not condition
if not line.find(':') == -1:
(role, line_spoken) = line.split(':',1)
If condition to check the contents of list
if ele not in unique_james :
unique_james.append(ele)
For loop
for m in my_list:
print(m)
for i in range(5) :
print(i) --> 0,1,2,3,4
range() is a built-in function which iterates a fixed number of times (starting from 0).
While loop
count = 0
while count < len(movies) :
print(movie[count])
count = count + 1
Functions
def desc_movies(movies) :
for m in movies:
if ( isinstance(m,list)) :
desc_movies(m)
else :
print(m)
>> desc_movies(movies)
Functions with optional arguments
Provide a default value for an argument and that argument will become optional.
def describe_list(the_list,level=0) :
for each_item in the_list :
if (isinstance(each_item, list)) :
describe_list(each_item,level+1)
else :
for tab in range(level) :
print("\t", end='')
print(each_item)
Now this function can be invoked either with argument or without argument.
describe_list(the_list)
describe_list(the_list, 2)
Function with return statement
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string)
(mins, seccs) = time_string.split(splitter)
return(mins + '.' + secs)
Modules
Create a python file with the functions:
vi depict.py
""" This is the depict.py module, and it provides one function called
describe_list() which prints lists that may or may not include nested lists"""
def describe_list(the_list) :
""" This function takes a positional argument called "the_list", which is any python list (of, possibly nested lists). Each data item in the provided list is (recursively) printed to the screen on its own line """
for each_item in the_list :
if (isinstance(each_item, list)) :
describe_list(each_item)
else :
print(each_item)
To use this module:
vi test_depcit.py
import depict
cast = ['Palin','Cleese','Idle','Jones','Gilliam','Chapman']
depict.describe_list(cast)
Note: You need to use the package namespace to invoke the function inside the package i.e. depict.describe_list() . Python's main namespace is __main__ and built-in functions namespace is __buildins__ .
Another way of using this module:
vi test_depcit.py
from depict import describe_list
cast = ['Palin','Cleese','Idle','Jones','Gilliam','Chapman']
describe_list(cast)
Note: In this case, no need to prefix module namespace while invoking the function.
To create a distribution out of your module, installation and distribution to PyPI , refer my next blog http://sliceoffcodes.blogspot.in/2014/02/creating-python-distribution.html
Comments
""" This is a
multi-line comment """
# This is a single line comment
File/Directory operations
import os
Current directory: os.getcwd()
Changing directory: os.chdir('../dir2')
Reading from file:
if os.path.exists('HeadFirstPython/chapter3/sketch.txt') :
data = open('readme.txt')
print(data.readline(), end='') --> The end='' argument will avoid newline's getting printed.
data.seek(0) --> Return to start of the file
Reading file through for loop
for line in data :
print(line , end='')
Another example of reading file.
for line in data :
if not line.find(':') == -1:
(role, line_spoken) = line.split(':',1)
print(role, end='')
print(' said ', line_spoken , end='')
Close the file handler
data.close()
Writing to files
out = open("parser.log","w")
print("This is the parser output",file=out) #Notice 'file=out', it makes print write to the the file pointed by 'out'
out.close()
Handling exceptions
for line in data :
try:
(role, line_spoken) = line.split(':',1)
print(role, end='')
print(' said ', line_spoken , end='')
except:
pass
pass is a python statement, which is equivalent to empty or null statement
try:
data = open('HeadFirstPython/chapter3/sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':', 1)
print(role, end='')
print(' said: ', end='')
print(line_spoken, end='')
except ValueError:
pass
finally:
if 'data' in locals():
data.close()
except IOError as err:
print('The data file is missing!' + str(err))
Note: The code in 'finally' section will get execute always, no matter whether exception caught or not. Hence closing of file handles should be put there.
The 'locals' built-in function returns a collection of names defined in the current scope. So we use it to check if the file pointed by 'data' is opened successfully.
The err in 'IOError as err' statement above, points to error message. You need to convert it to string using str(err)
Using 'with' to deal with file operations
If we use 'with' statement, then no need to use 'finally' while handling exceptions. The 'with' statement will automatically close the opened handlers. Check out the example below
try:
with open('parser.log','w') as data:
print('This is the parser output',file=data)
except IOError as err:
print('File error' + str(err))
Data persistance with Pickle
Pickle is used to save and load any python data object. It's like serialization in java. The picked data can be stored in disk file or in database. The pickle.dump() function saves data to disk and the pickle.load() function restores data from disk.
import pickle
#Pickling
with open('mydata.pickle','wb') as pickle_handler:
pickle.dump(['Siddesh',32,25000,'Bangalore'], pickle_handler)
#Unpickling
with open('mydata.pickle','rb') as pickle_restore:
emp = pickle.load(pickle_restore)
Another lengthy example
import pickle
import depict #It's our own module available in PyPI
try:
with open('man_data.txt','wb') as man_handler, open('other_data.txt','wb') as other_handler:
pickle.dump(man, man_handler)
pickle.dump(other, other_handler)
except pickle.PickleError as perr:
print("File error: " +str(perr))
new_man = []
try:
with open('man_data.txt', 'rb') as man_file:
new_man = pickle.load(man_file)
except IOError as err:
print('File error: ' +str(err))
except pickle.PickleError as perr:
print('Pickling error: '+str(perr))
depict.describe_list(new_man)
List comprehensions
It is designed to reduce the amount of code we need to write when transforming one list to another.
Meter to feet conversion example:
>>> meters = [1, 10, 3]
>>> feet = [ m*3.281 for m in meters]
>>> feet
[3.281, 32.81, 9.843]
Lower case to uppercase conversion
>>> lower = ["I", "don't", "like", "spam"]
>>> upper = [s.upper() for s in lower]
>>> upper
['I', "DON'T", 'LIKE', 'SPAM']
Using our own function while comprehending lists
>>> dirty = ['2-22', '2:22', '2.22']
>>> clean = [AthletesCoach.sanitize(t) for t in dirty]
>>> clean
['2.22', '2.22', '2.22']
Overwriting original list
>>> clean = [ float(s) for s in clean]
>>> clean
[2.22, 2.22, 2.22]
Method Chaining
with open(fname) as fh:
data=fh.readline()
times=data.strip().split(',')
It reads from left to right.
Function chaining
>>> clean = [
float(AthletesCoach.sanitize(t)) for t in ['2-22', '3:33', '4.44']]
>>> clean
[2.22, 3.33, 4.44]
It reads from right to left.
Class
- The class keyword used to define a class
- Class methods are defined with 'def' keyword
- Class attributes are like variables that exist within object instances
- The __init__() method can be defined within a class to initialize object instances
- Every method defined in a class must provide self as its first argument
- Every attribute in class must be prefixed with self. in order to associate its data with its instance
- "self" is a method argument that always refers to the current object instance
class NameAnalysis:
def __init__(self, value=0):
self.thing = value
def how_big(self):
return(len(self.thing))
d=NameAnalysis("Siddesh BG")
Full fledged example of class
class Athlete:
def __init__(self, a_name, a_dob=None,a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return(sorted(set([sanitize(t) for t in self.times]))[0:3])
def add_time(self,time_value):
self.times.append(time_value)
def add_times(self,l_times):
self.times.extend(l_times)
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string)
(mins, secs) = time_string.split(splitter)
return(mins + '.' + secs)
def get_coach_data(filename) :
try:
with open(filename) as fh:
data=fh.readline()
my_list = data.strip().split(',')
return(Athlete(my_list.pop(0),my_list.pop(0),my_list))
except IOException as err:
print("FIle error", str(err))
return(None)
james = get_coach_data('james2.txt')
julie = get_coach_data('julie2.txt')
mikey= get_coach_data('mikey2.txt')
sarah = get_coach_data('sarah2.txt')
print(james.name + "'s fastest times are: " + str(james.top3()))
print(julie.name + "'s fastest times are: " + str(julie.top3()))
print(mikey.name + "'s fastest times are: " + str(mikey.top3()))
print(sarah.name + "'s fastest times are: " + str(sarah.top3()))
james.add_times(['2.0.2','2.34','3.43'])
print(james.name + "'s fastest times are: " + str(james.top3()))
vera = Athlete("Vera Vi")
vera.add_time('1.31')
print(vera.top3())
vera.add_times(['2.22','1-21',"2:22"])
print(vera.top3())
Inheritance : Inheriting the built in class 'list'
- Classes can be built from scratch or can inherit from Python's built-in classes or from other custom classes.
class AthleteList(list):
def __init__(self, a_name, a_dob=None,a_times=[]):
list.__init__([])
self.name = a_name
self.dob = a_dob
self.extend(a_times)
def top3(self):
return(sorted(set([sanitize(t) for t in self]))[0:3])
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string)
(mins, secs) = time_string.split(splitter)
return(mins + '.' + secs)
def get_coach_data(filename) :
try:
with open(filename) as fh:
data=fh.readline()
my_list = data.strip().split(',')
return(AthleteList(my_list.pop(0),my_list.pop(0),my_list))
except IOException as err:
print("FIle error", str(err))
return(None)
james = get_coach_data('james2.txt')
julie = get_coach_data('julie2.txt')
mikey= get_coach_data('mikey2.txt')
sarah = get_coach_data('sarah2.txt')
print(james.name + "'s fastest times are: " + str(james.top3()))
print(julie.name + "'s fastest times are: " + str(julie.top3()))
print(mikey.name + "'s fastest times are: " + str(mikey.top3()))
print(sarah.name + "'s fastest times are: " + str(sarah.top3()))
vera = AthleteList("Vera Vi")
vera.append('1.31')
print(vera.top3())
vera.extend(['2.22','1-21',"2:22"])
print(vera.top3())