Python Exercise 2

Name of Innovation

Python Exercise 2

October 19, 2017 Uncategorized 1
------> file name hello.py
def foo():
    print "Hello Amar Sharma"

if __name__ == "__main__":
    foo()



Exercise-
Using List Comprehension get the list of vowels in word "INDIAN".

-------------------sample----------------------------
# Python Module example


def add(a, b):
   """This program adds two
   numbers and return the result"""
   result = a + b
   return result

Exercise - Make a module named mymath and add remaining function 
( multiply, division, subtraction in the module and call them 
in your program using following ways -
1. import mymath
2. from mymath import add,subtraction,division, multiplicaiton
3. from mymath import *


-------------------- sample class --------------------

class ComplexNumber:
 def __init__(self,r = 0,i = 0):
     self.real = r
     self.imag = i
 def getData(self):
     print("{0}+{1}j".format(self.real,self.imag))

# Create a new ComplexNumber object
c1 = ComplexNumber(2,3)

# Call getData() function
# Output: 2+3j
c1.getData()

Exercise - Create a class Employee with data members name, address and salary. 
Provide a method to display the salary and name of the employee. In the main 
program instantiate an object of the class and demonstrate the use of the class.


--------------------sample---------------------------------

Let's say you have a class ClassA which contains a method methodA defined as:

def methodA(self, arg1, arg2):
 # do something
and ObjectA is an instance of this class.
Now when ObjectA.methodA(arg1, arg2) is called, python internally converts it for you as:
ClassA.methodA(ObjectA, arg1, arg2)
The self variable refers to the object itself.
Exercise
Please read through a text file, split each line into its constituent
words, add each word to a dictionary, then add one to the number of times the
word occurs in the dictionary. Finally, we sort the dictionary and print it
out listing each word (key) and its count (value). 
import sys
filename = sys.argv[1]

def count_words(filename):
    """Reads through a text file and counts the number of appearances of each word."""
    # print("\n",filename,"\n") # You can check that the filename is correct
    text_file = open(filename) # open the file for reading
    # Set up an empty dictionary to start a standard design pattern loop
    words_dic = {}
    # This loop adds each word to the dictionary and updates its count. Change 
    # all words to lower case so Horse and horse are seen as the same word.
    for line in text_file: # step through each line in the text file
        for word in line.lower().split(): # split into a list of words
            word = word.strip("'?,.;!-/\"") # strip out the stuff we ignore
            if word not in words_dic:
                words_dic[word] = 0 # add word to words with 0 count
            words_dic[word] = words_dic[word] + 1 # add 1 to the count
    text_file.close()

    # Sorts the dictionary words into a list and then print them out
    print("List of words in the file with number of times each appears.")
    word_list = sorted(words_dic)
    for word in word_list:
         print(words_dic[word], word)


if __name__ == "__main__":
    count_words(filename)
#%% 


One Response

  1. […] at – Python Exercise 1 Python Exercise 2 Python Exercise […]

Comments are closed.