Module 1 - Day 3

Login to Lab using your credentials. There is a notebook with name 1-3.ipynb already created for you. Open that and use it for today’s training.

Shut down all previous notebooks.

Recap Functions

def functionname(arg1, arg2): # next line is start of new code block
    # code block is continious lines of code which are indented at same level
    s = arg1 + arg2
    return s 
functionname
<function __main__.functionname(arg1, arg2)>
functionname(3, 4)
7
result = functionname(5, 6)

A function that does not have return statement by default returns None

def foo(): # a function without any argument
    print("Hello")
    
r = foo()
Hello
print(r)
None
X = 10 # python is case sensitive

def foo(a):
    X = 20 # but this local variable
    return X+a

print(X)
10
foo(30)
50
print(X)
10

A global variables

def foobar(b):
    return b + X # ?
f
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[17], line 1
----> 1 f

NameError: name 'f' is not defined
foobar(20) # X will be taken from global namesapce if it is not defined locally
30

global and local mixed!

def addone():
    X = X + 1    # X is not defined as well we are trying to define it!
addone()
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[21], line 1
----> 1 addone()

Cell In[20], line 2, in addone()
      1 def addone():
----> 2     X = X + 1

UnboundLocalError: local variable 'X' referenced before assignment
"jhsdf" + 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[22], line 1
----> 1 "jhsdf" + 1

TypeError: can only concatenate str (not "int") to str
def addone_(a):
    X = a + 1 # this will not modify global, 

addone_(X)
def addone__():
    global X # this keywords makes sure X is always global
    X = X + 1
    
X = 10
addone__() # this will modify gloab variable
X # look at value of X
11
X = 10

def func1():
    def func2():
        global X 
        X = X + 2

    func2()


func1()
X
12
X = 10

def func1():
    X = 20 # first line of code block for func1 starts
    
    def func2(): # 
        global X # this one is another nested level of code block
        X = X + 2

    func2()

func1()
X
12
X = 10

def func1():
    X = 20 # first line of code block for func1 starts
    
    def func2(): # 
        global X # this one is another nested level of code block
        X = X + 2


func1()
X
10
X = 10

def func1():
    X = 20 # this is defining local variable
func1()
X
10
X = 10

def func1():
    X = X + 20 # this is defining local variable
func1()
X
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[53], line 5
      3 def func1():
      4     X = X + 20 # this is defining local variable
----> 5 func1()
      6 X

Cell In[53], line 4, in func1()
      3 def func1():
----> 4     X = X + 20

UnboundLocalError: local variable 'X' referenced before assignment
X = 10

def func1():
    # even if I don't define X as global variable
    # and it is not defined locally
    # then X is available for me from global context  for access
    print(X + 20) # this is defining local variable
func1()
X
30
10
def add(a, b):
    return a + b
add(X, 20)
30

Methods

Methods are similar to functions but they are part of an object. Method is nothing but a function defined inside the object and it can operate only on data from same object

sentence = "I have this sentence"
sentence.upper() # call a method from sentence
'I HAVE THIS SENTENCE'
sentence.title()
'I Have This Sentence'
sentence.isalpha() 
False
"A".isalpha()
True
"TRYTRTYRTYR".isalpha()
True
ones = [1, 2, 3, 4]
sentence.islower()
False
digits =  str(2**100)
digits
'1267650600228229401496703205376'
digits.isdigit()
True
digits.isnumeric()
True
"1435345".isnumeric()
True
fileanme = "hello.py"
fileanme.endswith(".py")
True
fileanme.startswith("hel")
True
sentence.upper()
'I HAVE THIS SENTENCE'
sentence.lower()
'i have this sentence'
sentence.count("I")
1
upsentence = sentence.upper()
upsentence.count("I")
2
sentence
'I have this sentence'
sentence.upper().count("I") # method cahining
2
sentence.lower().replace("i", "e").count("e")
6
sentence
'I have this sentence'
sentence.split() # this will split the text on whitespaces
['I', 'have', 'this', 'sentence']
multiline = """this is first line
this is second
this is last"""
words = multiline.split()
words
['this', 'is', 'first', 'line', 'this', 'is', 'second', 'this', 'is', 'last']

problem

  • Write a function with name wordcount to count number of words from given text
sentence = "I have this sentence"
wordcount(sentence)
4
def wordcount(sent):
    return len(sent.split())

    
def wordcount(sentence): # this argument name has nothing to with above variable defines
    words = sentence.split()
    count = len(words)
    return count
another_text = "This is just another text data"
wordcount(another_text)
6
wordcount("This is yet another")
4
def wordcount(sentence): # this argument name has nothing to with above variable defines
    words = sentence.split()
    count = len(words)
    return count
    
def interactive_wordcount():
    userdata = input("Input your statement")
    return wordcount(userdata)
interactive_wordcount()
Input your statement This is my ssentence, find word count for this
9
csvdata = "vikrant,mohit,mohini,anshul"
csvdata.split() 
['vikrant,mohit,mohini,anshul']
csvdata.split(",")
['vikrant', 'mohit', 'mohini', 'anshul']
path = "/home/vikrant/data/python/"
path.split("/")
['', 'home', 'vikrant', 'data', 'python', '']
path = "c:\\Program Files\\Python\\"
path.split("\\")
['c:', 'Program Files', 'Python', '']
help(path.split)
Help on built-in function split:

split(sep=None, maxsplit=-1) method of builtins.str instance
    Return a list of the substrings in the string, using sep as the separator string.
    
      sep
        The separator used to split the string.
    
        When set to None (the default value), will split on any whitespace
        character (including \\n \\r \\t \\f and spaces) and will discard
        empty strings from the result.
      maxsplit
        Maximum number of splits (starting from the left).
        -1 (the default value) means no limit.
    
    Note, str.split() is mainly useful for data that has been intentionally
    delimited.  With natural text that includes punctuation, consider using
    the regular expression module.
path.split("\\", maxsplit=1)
['c:', 'Program Files\\Python\\']
def find_drive(windows_path):
    tokens = windows_path.split("\\", maxsplit=1)
    return tokens[0]
find_drive(path)
'c:'
find_drive("D:\\backup\\python\\data")
'D:'
words = ["one", "two", "three", "four"]
" ".join(words)
'one two three four'
"-".join(words)
'one-two-three-four'
",".join(words)
'one,two,three,four'
csvdata
'vikrant,mohit,mohini,anshul'

Convert a text given as csv(comma separated values) data in to tsv (tab separated values) data

"\t"
'\t'
csvdata.replace(",", "\t")
'vikrant\tmohit\tmohini\tanshul'
print(csvdata.replace(",", "\t"))
vikrant mohit   mohini  anshul
"\t".join(csvdata.split(","))
'vikrant\tmohit\tmohini\tanshul'

list methods

nums = [0, 1, 2, 3, 4, 5, 6, 7,8]
range(10) # create numbers from 0 till 9
range(0, 10)
digits = list(range(10))
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def csvtotsv():
    word = csvtotsv.split(",")
    return "\t".join(word)
csvtotsv("some,data")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[123], line 1
----> 1 csvtotsv("some,data")

TypeError: csvtotsv() takes 0 positional arguments but 1 was given
def csvtotsv(csvtext):
    word = csvtext.split(",")
    return "\t".join(word)
csvtotsv("some,data")
'some\tdata'
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits.append(10) # this will modify the list
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
digits.remove(10)
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
empty = []
empty.append(1)
empty.append(1)
empty
[1, 1]
empty.insert(0, -1) # insert 1 at position 0
empty
[-1, 1, 1]
digits.index(4)
4
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits.extend(empty) # concatenate in place, it modifies digits list, but not empty
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, 1, 1]
digits = list(range(10))
digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits + empty # none of the original lists get modified
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, 1, 1]
empty.count(1)
2

All the variables, function names etc is case sensitive. C and c are two different variables

List slicing

digits = sorted(digits, reverse=True)
digits
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
digits[0]
9
digits[:5] # this gives first 5 items
[9, 8, 7, 6, 5]
digits[3:] # drop first three items
[6, 5, 4, 3, 2, 1, 0]
path
'c:\\Program Files\\Python\\'
path.split("\\")[1:] # only folders
['Program Files', 'Python', '']
digits[3:7] # this returns items starting from index 3 till index 6 (exclude 7)
[6, 5, 4, 3]
digits
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
#digits[start:end]
digits[2:] # whatever you don't give is taken as default
[7, 6, 5, 4, 3, 2, 1, 0]
digits[:5] # start is taken zero
[9, 8, 7, 6, 5]
digits[1:8:2] # digits[start:end:step]
[8, 6, 4, 2]
digits[::2]
[9, 7, 5, 3, 1]
digits[::-1] # reverse
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sentence[::-1]
'ecnetnes siht evah I'
sentence[:5]
'I hav'
sentence[5:]
'e this sentence'