├── Functions ├── Assignment43.py ├── Assignment38 │ ├── __init__.py │ ├── 1.py │ └── 2.py ├── Assignment42.py ├── Assignment41.py └── Assignment39.py ├── Fundamentals ├── Assignment1.py ├── Assignment8.py ├── Assignment6.py ├── Assignment3.py ├── Assignment5.py ├── Assignment7.py ├── Assignment 9 │ ├── 4.py │ ├── 1.py │ ├── 2.py │ └── 3.py ├── Assignment4.py └── Assignment2.py ├── Control Structures ├── Assignment 20 │ ├── while_loop.py │ └── for_loop.py ├── Assignment19.py ├── Assignment 17 │ ├── 1.py │ ├── 3.py │ ├── 4.py │ ├── 2.py │ └── 5.py ├── Assignment10.py ├── Assignment18.py ├── Assignment11.py ├── Assignment12.py ├── Assignment15.py ├── Assignment13.py ├── Assignment14.py └── Assignment16.py ├── Object Oriented Fundamentals ├── Assignment43.py ├── Assignment54.py ├── Assignment47.py ├── Assignment48.py ├── Assignment52.py ├── Assignment59.py ├── Assignment50.py ├── Assignment51.py ├── Assignment58.py ├── Assignment56.py └── Assignment60.py ├── Strings ├── Assignment 24 │ ├── 2.py │ ├── 1.py │ ├── 4.py │ └── 3.py ├── Assignment25.py ├── Assignment27.py ├── Assignment28.py ├── Assignment29.py ├── Assignment23.py ├── Assignment26.py ├── Assignment30.py └── Assignment21&22.py ├── Lists ├── Assignment31.py ├── Assignment32.py ├── Assignment37.py ├── Assignment30.py ├── Assignment36.py ├── Assignment35.py ├── Assignment33.py └── Assignment34.py └── README.md /Functions/Assignment43.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Functions/Assignment38/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Fundamentals/Assignment1.py: -------------------------------------------------------------------------------- 1 | print("Hello world") -------------------------------------------------------------------------------- /Control Structures/Assignment 20/while_loop.py: -------------------------------------------------------------------------------- 1 | # 2 | -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment43.py: -------------------------------------------------------------------------------- 1 | ## 2 | -------------------------------------------------------------------------------- /Fundamentals/Assignment8.py: -------------------------------------------------------------------------------- 1 | num=16 2 | num1=num/6 3 | num2=num//6 4 | num3=num//6.0 5 | print(num1) 6 | print(num2) 7 | print(num3) -------------------------------------------------------------------------------- /Control Structures/Assignment19.py: -------------------------------------------------------------------------------- 1 | count = 0 2 | for count in range(0,10): 3 | if 4 == count: 4 | continue 5 | print(count) -------------------------------------------------------------------------------- /Control Structures/Assignment 20/for_loop.py: -------------------------------------------------------------------------------- 1 | for i in range (50,80): 2 | if i % 2 ==0: 3 | print(i) 4 | else: 5 | break -------------------------------------------------------------------------------- /Control Structures/Assignment 17/1.py: -------------------------------------------------------------------------------- 1 | counter = 1 2 | 3 | while counter <= 3: 4 | print(counter) 5 | counter += 1 6 | print("End of proagram") 7 | 8 | -------------------------------------------------------------------------------- /Control Structures/Assignment 17/3.py: -------------------------------------------------------------------------------- 1 | number =1 2 | result =0 3 | while number < 5: 4 | result = result + number 5 | number = number + 1 6 | print(result) -------------------------------------------------------------------------------- /Fundamentals/Assignment6.py: -------------------------------------------------------------------------------- 1 | pay_rate=400 2 | no_weeks=4 3 | no_hours=40 4 | 5 | monthly_pay= no_hours*pay_rate*no_weeks 6 | print("Monthly pay:" ,monthly_pay) 7 | -------------------------------------------------------------------------------- /Control Structures/Assignment 17/4.py: -------------------------------------------------------------------------------- 1 | result = 0 2 | for index in range (40,10,-2): 3 | if(index % 5 == 0): 4 | result = result + index 5 | print(result) -------------------------------------------------------------------------------- /Control Structures/Assignment10.py: -------------------------------------------------------------------------------- 1 | itemNo=1005 2 | unitprice=250 3 | quantity=2 4 | amount=quantity*unitprice 5 | print("Item no:",itemNo) 6 | print("Bill amount:",amount) -------------------------------------------------------------------------------- /Control Structures/Assignment 17/2.py: -------------------------------------------------------------------------------- 1 | print("To find the sum of 10 integers") 2 | result =0 3 | for value in range(1,11): 4 | result = result + value 5 | print("sum:",result); -------------------------------------------------------------------------------- /Fundamentals/Assignment3.py: -------------------------------------------------------------------------------- 1 | bill_id = 1001 2 | customer_id = 101 3 | bill_amount = 199.99 4 | 5 | print(type(bill_id)) 6 | print(type(customer_id)) 7 | print(type(bill_amount)) 8 | -------------------------------------------------------------------------------- /Control Structures/Assignment18.py: -------------------------------------------------------------------------------- 1 | count = 0 2 | result = 0 3 | for count in range (1,10): 4 | result = result + count 5 | if result >= 7: 6 | break 7 | print("Result=",result) -------------------------------------------------------------------------------- /Fundamentals/Assignment5.py: -------------------------------------------------------------------------------- 1 | bill_id = 1001 2 | customer_id = 101 3 | bill_amount = 199.99 4 | 5 | print("bill_id: %d" %bill_id,"\ncusotmer_id: %d" %customer_id,"\nbill_amount: %f" %bill_amount) -------------------------------------------------------------------------------- /Fundamentals/Assignment7.py: -------------------------------------------------------------------------------- 1 | 2 | a=int(input("Enter a > ")) 3 | b=int(input("\nEnter b > ")) 4 | a=a+b 5 | b=a-b 6 | a=a-b 7 | print("\nAfter swaping the numbers are:\n","a:",a,"b:",b) 8 | -------------------------------------------------------------------------------- /Strings/Assignment 24/2.py: -------------------------------------------------------------------------------- 1 | string = input("Enter string > " ) 2 | 3 | if(string==string[::-1]): 4 | print("The string is a Palindrome") 5 | else: 6 | print("The string isn't a Palindrome") -------------------------------------------------------------------------------- /Fundamentals/Assignment 9/4.py: -------------------------------------------------------------------------------- 1 | int_a = 10 2 | raw_input=input("Enter a number > ") 3 | print("type of int_a:",type(int_a)) 4 | print("type of raw_input:",type(raw_input)) 5 | print(int_a + raw_input) -------------------------------------------------------------------------------- /Control Structures/Assignment 17/5.py: -------------------------------------------------------------------------------- 1 | amount = 100.0 2 | intrest = 0.0 3 | months = 1 4 | while months < 6: 5 | intrest = amount * 0.2 6 | amount = amount + intrest 7 | months += 1 8 | print(amount) -------------------------------------------------------------------------------- /Fundamentals/Assignment 9/1.py: -------------------------------------------------------------------------------- 1 | obj_x=10 2 | obj_y = obj_x 3 | 4 | if(obj_x is obj_y): 5 | print("obj_x and obj_y have same identity") 6 | else: 7 | print("obj_x and obj_y do not have same identity") -------------------------------------------------------------------------------- /Fundamentals/Assignment 9/2.py: -------------------------------------------------------------------------------- 1 | obj_x=10 2 | obj_y = obj_x 3 | 4 | if(id(obj_x)==id(obj_y)): 5 | print("Address of obj_x and obj_y is same") 6 | else: 7 | print("Address of obj_x and obj_y is not same") -------------------------------------------------------------------------------- /Strings/Assignment 24/1.py: -------------------------------------------------------------------------------- 1 | stri = input("Enter a String > ") 2 | count=0 3 | 4 | for i in stri: 5 | if(i.isupper()): 6 | count = count + 1 7 | print("There are",count,"Uppercase Letter in Entered String.") 8 | -------------------------------------------------------------------------------- /Lists/Assignment31.py: -------------------------------------------------------------------------------- 1 | a = 0 2 | b = 1 3 | n=int(input("Enter n > ")) 4 | list1 = list([a,b]) 5 | 6 | for i in range(0,n-2): 7 | c=a+b 8 | a=b 9 | b=c 10 | list1.insert(i+2, c) 11 | 12 | print(list1) 13 | -------------------------------------------------------------------------------- /Fundamentals/Assignment 9/3.py: -------------------------------------------------------------------------------- 1 | obj_x=10 2 | obj_y = obj_x 3 | obj_y = obj_x +1 4 | if(obj_x is not obj_y): 5 | print("obj_x and obj_y do not have same identity") 6 | else: 7 | print("obj_x and obj_y have same identity") -------------------------------------------------------------------------------- /Strings/Assignment25.py: -------------------------------------------------------------------------------- 1 | string = input("Enter a string > ") 2 | ignore = " " 3 | res = "" 4 | 5 | for char in string: 6 | if char not in ignore: 7 | res = res + char 8 | 9 | print("Reverse of the string is > ") 10 | print(res[::-2]) -------------------------------------------------------------------------------- /Strings/Assignment27.py: -------------------------------------------------------------------------------- 1 | string1 = "I Like C" 2 | string2 = "Mary Likes Python" 3 | string = string1 + string2 4 | output = "" 5 | 6 | for i in string: 7 | if i.isupper(): 8 | output += i[0] 9 | string.split() 10 | print("merged_string:",output) -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment54.py: -------------------------------------------------------------------------------- 1 | class PrintDetails: 2 | def printheader(self, c='*', no=1): 3 | print(c * no) 4 | 5 | obj = PrintDetails() 6 | obj.printheader('#', 10) 7 | obj.printheader("Report") 8 | obj.printheader('#' ,10) 9 | obj.printheader() -------------------------------------------------------------------------------- /Fundamentals/Assignment4.py: -------------------------------------------------------------------------------- 1 | print("Value of e is %0.1f" , 2.713) 2 | print("Programming in Python: Version %d" %3.5) 3 | print("Value of e is %0.1f", 2.713) 4 | print("%20s : %d" % ("Python 3.0 is also known as Python",3000.57)) 5 | x=2 6 | print("{0:2d} {1:3d}{2:4d}".format(x,x*x,x*x*x)) -------------------------------------------------------------------------------- /Strings/Assignment 24/4.py: -------------------------------------------------------------------------------- 1 | punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' 2 | 3 | string = input("Enter a string > ") 4 | 5 | nopunctuation = "" 6 | for char in string: 7 | if char not in punctuations: 8 | nopunctuation = nopunctuation + char 9 | 10 | print(nopunctuation) -------------------------------------------------------------------------------- /Strings/Assignment28.py: -------------------------------------------------------------------------------- 1 | head = ("CEO",) 2 | elements = ('Air', 'Water', 'Fire', 'Light', 'Land') 3 | 4 | print("1.",head) 5 | print("2.",elements) 6 | print("3.",elements[3]) 7 | 8 | print("4.",elements[-1]) 9 | print("5.",elements[4]) 10 | print("6.",elements[5]) 11 | 12 | elements[0]='Ice' -------------------------------------------------------------------------------- /Strings/Assignment29.py: -------------------------------------------------------------------------------- 1 | courses = ("Python Programming", "RDBMS", "Web Technology", "Software Engg") 2 | electives = ("Business Intelligence", "Big Data Analytics") 3 | 4 | print("No. of courses opted by John >",len(courses)) 5 | print(courses) 6 | print(courses + electives) 7 | courses[3] = "Computer Networks" -------------------------------------------------------------------------------- /Lists/Assignment32.py: -------------------------------------------------------------------------------- 1 | list1 = [] 2 | list1 = input("Input No. seprated by ',' > ") 3 | list1 = list1.split(',') 4 | 5 | for i in range(len(list1)-1): 6 | for j in range(i+1, len(list1)): 7 | if ( list1[j] > list1[i]): 8 | list1[j],list1[i] = list1[i],list1[j] 9 | 10 | print(list1) -------------------------------------------------------------------------------- /Strings/Assignment 24/3.py: -------------------------------------------------------------------------------- 1 | string = input("Enter string > ") 2 | vowels = 0 3 | 4 | for i in string: 5 | if(i =='a' or i =='e' or i =='i' or i =='o' or i =='u' or i =='A' or i =='E' or i =='I' or i =='O' or i =='U'): 6 | vowels = vowels+1 7 | print("There are",vowels,"vowels in the Entered String") -------------------------------------------------------------------------------- /Fundamentals/Assignment2.py: -------------------------------------------------------------------------------- 1 | bill_id = 1001 2 | customer_id = 101 3 | bill_amount = 199.99 4 | """ 5 | print("bill_id: ",bill_id) 6 | print("cusotmer_id:",customer_id) 7 | print("bill_amount:",bill_amount) 8 | """ 9 | 10 | print("bill_id:",bill_id,"\ncusotmer_id:",customer_id,"\nbill_amount:",bill_amount) 11 | -------------------------------------------------------------------------------- /Strings/Assignment23.py: -------------------------------------------------------------------------------- 1 | string1 = "Infosys Limited" 2 | string2 = "Mysore" 3 | 4 | print("1.",string1[:4]) 5 | print("2.",string1[-1]) 6 | print("3.",string1 * 2) 7 | print("4.",string1[:-1] + string2 + string1[:-1]) 8 | print("5.",string2[1]) 9 | print("6.",string2[4]) 10 | print("7.",string1 * 2 + string1[:-1] + string2) -------------------------------------------------------------------------------- /Strings/Assignment26.py: -------------------------------------------------------------------------------- 1 | string = "ABaBCbGc" 2 | string1 = string.upper(); 3 | 4 | dic = {} 5 | 6 | for char in string1: 7 | dic[char] = 0 8 | 9 | for char in string1: 10 | dic[char]+= 1 11 | 12 | for char, count in dic.items(): 13 | if count > 0 and char != ' ': 14 | print ("%d%c" % (count, char)) -------------------------------------------------------------------------------- /Lists/Assignment37.py: -------------------------------------------------------------------------------- 1 | marks = {"John":86.5,"Jack":91.2,"Jill":84.5,"Harry":72.1,"Joe":80.5} 2 | 3 | sorted_marks = sorted(marks.values()) 4 | sorted_marks.reverse() 5 | 6 | top_3=[] 7 | 8 | for i in range(0,3): 9 | top_3.append(sorted_marks[i]) 10 | 11 | print("Top 3 Courses are >",top_3) 12 | 13 | print("Average marks >",sum(top_3)/3) 14 | -------------------------------------------------------------------------------- /Control Structures/Assignment11.py: -------------------------------------------------------------------------------- 1 | 2 | unitprice=int(input("Enter price: ")) 3 | quantity=int(input("Enter quantity: ")) 4 | amount=quantity*unitprice 5 | a1=amount*(2/100) 6 | a2=amount*(1/100) 7 | b1=amount-a1 8 | b2=amount-a2 9 | print("Bill amount:",amount) 10 | if(amount >= 500): 11 | print("Your discounted price is:",b1) 12 | else: 13 | print("Your discounted price is:",b2) -------------------------------------------------------------------------------- /Functions/Assignment38/1.py: -------------------------------------------------------------------------------- 1 | def compare(phoenix_to_slc, phoenix_to_tampa): 2 | if phoenix_to_slc > phoenix_to_tampa: 3 | print("SLC is far from Phoenix compared to Tampa, Florida") 4 | elif(phoenix_to_slc < phoenix_to_tampa): 5 | print("Tampa, Florida is far from Phoenix compared to SLC") 6 | else: 7 | print("Both locations are equidistance from Phoneix") 8 | 9 | compare (1790, 506) -------------------------------------------------------------------------------- /Lists/Assignment30.py: -------------------------------------------------------------------------------- 1 | language = ['Python'] 2 | languages = ['Python', 'C', 'C++', 'Java', 'Perl'] 3 | 4 | print("1.",language) 5 | print("2.",languages) 6 | print("3.",languages[0:3]) 7 | print("4.",languages[1:4]) 8 | print("5.",languages[2]) 9 | print ("7.",languages[0] + " and " + languages[1] + " are quite different!") 10 | print ("8. Accessing the last element of the list: " + languages[-1]) 11 | 12 | 13 | print(languages[5]) 14 | -------------------------------------------------------------------------------- /Lists/Assignment36.py: -------------------------------------------------------------------------------- 1 | customer_details = { 1001 : "John", 1004 : "Jill", 1005: "Joe", 1003 : "Jack" } 2 | 3 | print("1.",customer_details) 4 | 5 | print("2.",len(customer_details)) 6 | 7 | print("3.",sorted(customer_details.values())) 8 | 9 | del(customer_details[1005]) 10 | print("4.",customer_details) 11 | 12 | customer_details[1003] = "Mary" 13 | print("5.",customer_details) 14 | 15 | print("6.",1002 in customer_details) 16 | -------------------------------------------------------------------------------- /Functions/Assignment38/2.py: -------------------------------------------------------------------------------- 1 | def compare(phoenix_to_slc, phoenix_to_tampa): 2 | if phoenix_to_slc > phoenix_to_tampa: 3 | print("SLC is far from Phoenix compared to Tampa, Florida") 4 | elif(phoenix_to_slc < phoenix_to_tampa): 5 | print("Tampa, Florida is far from Phoenix compared to SLC") 6 | else: 7 | print("Both locations are equidistance from Phoneix") 8 | 9 | compare (phoenix_to_tampa = 506, phoenix_to_slc = 1790) -------------------------------------------------------------------------------- /Strings/Assignment30.py: -------------------------------------------------------------------------------- 1 | language = ['Python'] 2 | languages = ['Python', 'C', 'C++', 'Java', 'Perl'] 3 | 4 | print("1.",language) 5 | print("2.",languages) 6 | print("3.",languages[0:3]) 7 | print("4.",languages[1:4]) 8 | print("5.",languages[2]) 9 | print ("7.",languages[0] + " and " + languages[1] + " are quite different!") 10 | print ("8. Accessing the last element of the list: " + languages[-1]) 11 | 12 | 13 | print(languages[5]) 14 | -------------------------------------------------------------------------------- /Functions/Assignment42.py: -------------------------------------------------------------------------------- 1 | custid = [1001,1002,1003,1004,1005] 2 | 3 | x = input("Enter Customer Id > ") 4 | 5 | if type(x) is str: 6 | try: 7 | print("The Entry is", x) 8 | custid[3] = int(x) 9 | #break 10 | finally: 11 | print("Sorry!, Entry Can not be String") 12 | 13 | try: 14 | print(custid[5]) 15 | except IndexError: 16 | print("\nTrying to display unavailable value") 17 | 18 | print("\nValues are > ", custid) -------------------------------------------------------------------------------- /Lists/Assignment35.py: -------------------------------------------------------------------------------- 1 | java_course = {"John", "Jack", "Jill", "Joe"} 2 | python_course = {"Jake", "John", "Eric", "Jill"} 3 | 4 | print("1.",len(python_course)) 5 | print("2.",java_course) 6 | print("3.",python_course - java_course) 7 | print("4.", len(java_course & python_course),java_course & python_course) 8 | print("5.", len(java_course ^ python_course),java_course ^ python_course) 9 | print("6.", len(java_course | python_course),java_course | python_course) 10 | -------------------------------------------------------------------------------- /Control Structures/Assignment12.py: -------------------------------------------------------------------------------- 1 | bill_id = 1001 2 | customer_id=101 3 | bill_amount=200.0 4 | discounted_bill_amount=0.0 5 | print("Bill Id:%d"%bill_id) 6 | print("Customer Id:%d"%customer_id) 7 | print("Bill Amount:Rs. %f"%bill_amount) 8 | if bill_amount>200: 9 | discounted_bill_amount=bill_amount - bill_amount*2/100 10 | else: 11 | discounted_bill_amount=bill_amount-bill_amount*1/100 12 | print("Discounted Bill Amount:Rs.%f"%discounted_bill_amount) -------------------------------------------------------------------------------- /Control Structures/Assignment15.py: -------------------------------------------------------------------------------- 1 | Employee_id = 1001 2 | Basic_salary = 15000.00 3 | Allowances = 6000.00 4 | gross_Salary=Basic_salary+Allowances; 5 | Income_tax = gross_Salary*(20/100) 6 | Net_Salary=gross_Salary-Income_tax 7 | print("Employee id: %d" %Employee_id) 8 | print("Basic salary: %f" %Basic_salary) 9 | print("Allowances : %f" %Allowances) 10 | print("Gross salary: %f" %gross_Salary) 11 | print("Income tax: %f" %Income_tax) 12 | print("Net salary: %f" %Net_Salary) 13 | 14 | -------------------------------------------------------------------------------- /Control Structures/Assignment13.py: -------------------------------------------------------------------------------- 1 | bill_id = 1001 2 | customer_id=101 3 | bill_amount=1200.0 4 | discounted_bill_amount=0.0 5 | discount = 0 6 | print("Bill Id:%d"%bill_id) 7 | print("Customer Id:%d"%customer_id) 8 | print("Bill Amount:Rs. %f"%bill_amount) 9 | if bill_amount >= 1000: 10 | discount=5 11 | elif bill_amount >=500: 12 | discount=2 13 | else: 14 | discount=1 15 | discounted_bill_amount = bill_amount - bill_amount * discount/100 16 | print("Discounted Bill Amount:Rs. %f"%discounted_bill_amount) -------------------------------------------------------------------------------- /Lists/Assignment33.py: -------------------------------------------------------------------------------- 1 | lst = [["Sofa set",20000],["Dining table",8500],["T.V. Stand",4599],["Cupboard",13920]] 2 | 3 | item = input("Enter ITEM to be purchased > ") 4 | ind=0 5 | 6 | for i,sublist in enumerate(lst): 7 | if item in sublist: 8 | ind = i 9 | 10 | if item in [j for i in lst for j in i]: 11 | quantity = int(input("Enter Quantity > ")) 12 | if quantity > 0: 13 | print(quantity*lst[ind][1]) 14 | else: 15 | print("Quantity must be grater then 0") 16 | else: 17 | print("Item Not Available") -------------------------------------------------------------------------------- /Lists/Assignment34.py: -------------------------------------------------------------------------------- 1 | fruits = {"apple", "orange", "banana", "apple", "pear", "papaya", "papaya"} 2 | fruit_basket = {"apple", "banana", "grapes", "mango", "kiwi"} 3 | 4 | print("1.",fruits) 5 | print("2.",fruits & fruit_basket) 6 | print("3.",fruits | fruit_basket) 7 | print("4.",fruits - fruit_basket) 8 | print("5.",fruits ^ fruit_basket) 9 | print("6.",len(fruit_basket)) 10 | print("7.","pear" in fruits) 11 | print("8.","pear" not in fruit_basket) 12 | print("9.",fruits.issubset(fruit_basket)) 13 | print("10.",fruits.issuperset(fruit_basket)) 14 | print("11.",fruit_basket.copy()) 15 | -------------------------------------------------------------------------------- /Strings/Assignment21&22.py: -------------------------------------------------------------------------------- 1 | bill_id = input("Please enter Bill id > ") 2 | customer_id = input("Please enter Customer id > ") 3 | bill_amount = input("Please enter bill Amount > ") 4 | customer_name = input("Please enter Customer Name > ") 5 | 6 | if ((len(customer_name) >= 3) and (len(customer_name) <= 20)) is True: 7 | print("Bill Id: ",bill_id) 8 | print("Customer Id: ",customer_id) 9 | print("Bill Amount:Rs. ",bill_amount) 10 | print("Customer Name: ",customer_name) 11 | else: 12 | print("Invalid customer name. Customer name must be between 3 and 20 characters"); 13 | -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment47.py: -------------------------------------------------------------------------------- 1 | class Customer: 2 | def setcustomerid(self, id1): 3 | self.__customerid = id1 4 | 5 | def settelephoneno(self, teleno): 6 | self.__telephoneno = teleno 7 | 8 | def getcustomerid(self): 9 | return self.__customerid 10 | 11 | def gettelephoneno(self): 12 | return self.__telephoneno 13 | 14 | custobj = Customer() 15 | custobj.setcustomerid(1001,) 16 | custobj.settelephoneno(9201861311) 17 | print("Customer Id : ", custobj.getcustomerid()) 18 | print("Telephone No : ", custobj.gettelephoneno()) -------------------------------------------------------------------------------- /Control Structures/Assignment14.py: -------------------------------------------------------------------------------- 1 | bill_id = 1001 2 | customer_id=101 3 | bill_amount=200.0 4 | discounted_bill_amount=0.0 5 | print("Bill Id:%d"%bill_id) 6 | print("Customer Id:%d"%customer_id) 7 | print("Bill Amount:Rs. %f"%bill_amount) 8 | if ((customer_id > 100) and (customer_id <=1000)) is True: 9 | if bill_amount >= 500: 10 | discounted_bill_amount = bill_amount-bill_amount *10/100 11 | print("Discounted Bill Amount:Rs. %f" %discounted_bill_amount) 12 | else: 13 | print("No Discount") 14 | else: 15 | print("Invalid Customer id,customer id must between 101 and 1000") -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment48.py: -------------------------------------------------------------------------------- 1 | class Customer: 2 | def setcustomerid(self, customerid): 3 | self.customerid = customerid 4 | 5 | def settelephoneno(self, teleno): 6 | self.telephoneno = teleno 7 | 8 | def getcustomerid(self): 9 | return self.customerid 10 | 11 | def gettelephoneno(self): 12 | return self.telephoneno 13 | 14 | 15 | custobj = Customer() 16 | custobj.setcustomerid(1001) 17 | custobj.settelephoneno(9201861311) 18 | print("Customer Id : ", custobj.getcustomerid()) 19 | print("Telephone No : ", custobj.gettelephoneno()) -------------------------------------------------------------------------------- /Functions/Assignment41.py: -------------------------------------------------------------------------------- 1 | item_price = [1050, 2200, 8575, 485, 234, 150, 399] 2 | def price_max(item_price): 3 | print("Price of Costliest item in the Retail Store: ",max(item_price)) 4 | 5 | def average_price(item_price): 6 | total_price = 0 7 | for i in item_price: 8 | total_price += i 9 | average_price = total_price/len(item_price) 10 | print("Average Price of items in the Retail Store: ",average_price) 11 | 12 | def display_sorted_price(item_price): 13 | item_price.sort() 14 | print("Item Price List in increasing order: ",item_price) 15 | 16 | price_max(item_price) 17 | average_price(item_price) 18 | display_sorted_price(item_price) -------------------------------------------------------------------------------- /Functions/Assignment39.py: -------------------------------------------------------------------------------- 1 | def baggage_check(amount): 2 | if amount in range(0,40): 3 | return True 4 | else: 5 | return False 6 | 7 | def immigration_check (expiry_year): 8 | if expiry_year in range(2001,2025): 9 | return True 10 | else: 11 | return False 12 | 13 | def security(status): 14 | return status 15 | def traveler(): 16 | traveler_id = 1001 17 | traveler_name = "Jim" 18 | baggageAmount = 35 19 | expiryDate = 2019 20 | nocStatus = True 21 | 22 | if baggage_check(baggageAmount) is True & immigration_check(expiryDate) is True & security(nocStatus) is True: 23 | print(traveler_id,traveler_name,"\nAllowed to fly!") 24 | else: 25 | print(traveler_id,traveler_name,"\nDetain Traveller for Re-checking!") 26 | 27 | traveler() 28 | -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment52.py: -------------------------------------------------------------------------------- 1 | class Customer: 2 | counter = 1000 3 | def __init__(self): 4 | Customer.counter = Customer.counter+1 5 | self.__customerid=Customer.counter 6 | 7 | def setcustomerid(self, cid): 8 | self.__customerid=cid 9 | 10 | def getcustomerid(self): 11 | return self.__customerid 12 | 13 | @staticmethod 14 | def totalcustomers(): 15 | return Customer.counter-1000 16 | 17 | objcust = Customer() 18 | print("Customer Id: ", objcust.getcustomerid()) 19 | 20 | objcust2 = Customer() 21 | print("Customer Id: ", objcust2.getcustomerid()) 22 | print("Total Customers: ", objcust.totalcustomers()) 23 | print("Total Customers: ", objcust2.totalcustomers()) 24 | print("Total Customers: ", Customer.totalcustomers()) -------------------------------------------------------------------------------- /Control Structures/Assignment16.py: -------------------------------------------------------------------------------- 1 | Employee_id = 1001 2 | Basic_salary = 15000 3 | Allowances = 6000 4 | Income_tax = 0 5 | 6 | print("Employee id: %d" %Employee_id) 7 | print("Basic salary: %f" %Basic_salary) 8 | print("Allowances : %f" %Allowances) 9 | 10 | Gross_Salary=Basic_salary+Allowances 11 | print("Gross salary: %f" %Gross_Salary) 12 | 13 | if(Gross_Salary < 5000): 14 | print("Income tax: %f" %Income_tax) 15 | elif(Gross_Salary > 5000 and Gross_Salary > 10000): 16 | Income_tax = Gross_Salary*0.1 17 | print("Income tax: %f" %Income_tax) 18 | elif(Gross_Salary > 10000 and Gross_Salary > 20000): 19 | Income_tax = Gross_Salary*0.2 20 | print("Income tax: %f" %Income_tax) 21 | elif(Gross_Salary > 20000): 22 | Income_tax = Gross_Salary*0.3 23 | print("Income tax: %f" %Income_tax) 24 | 25 | Net_Salary=Gross_Salary-Income_tax 26 | print("Net salary: %f" %Net_Salary) -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment59.py: -------------------------------------------------------------------------------- 1 | class PrintDetails: 2 | def printheader(self, c, no=1): 3 | print(c*no) 4 | 5 | class PurchaseBill: 6 | def __init__(self, bid, billamount): 7 | self.__billid = bid 8 | self.__billamount = billamount 9 | 10 | def getbillid(self): 11 | return self.__billid 12 | 13 | def getbillamount(self): 14 | return self.__billamount 15 | 16 | def calculatebill(self, mode, processcharge): 17 | if(mode=="Credit"): 18 | self.__billamount = self.__billamount + (self.__billamount * processcharge/100) 19 | 20 | def displaybill(self): 21 | objprint = PrintDetails() 22 | objprint.printheader("-", 80) 23 | objprint.printheader(" Easy Shop Retail Store Bill ") 24 | objprint.printheader("-", 80) 25 | print("Bill Id: ", self.__billid) 26 | print("Final amount to be paid: Rs.", self.__billamount) 27 | objprint.printheader("-", 80) 28 | objprint.printheader(" Thank You!!! ") 29 | objprint.printheader("-", 80) 30 | 31 | objpur = PurchaseBill(101, 1055.0) 32 | objpur.calculatebill("Credit", 10.5) 33 | objpur.displaybill() -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment50.py: -------------------------------------------------------------------------------- 1 | class Customer: 2 | def __init__(self, customerid, telephoneno, customername): 3 | self.__customerid=customerid 4 | self.__customername=customername 5 | self.__telephoneno=telephoneno 6 | 7 | def setcustomerid(self, id): 8 | self.__customerid = id 9 | 10 | def setcustomername(self, customername): 11 | self.__customername = customername 12 | 13 | def settelephoneno(self, teleno): 14 | self.__telephoneno = teleno 15 | 16 | def getcustomerid(self): 17 | return self.__customerid 18 | 19 | def gettelephoneno(self): 20 | return self.__telephoneno 21 | 22 | def getcustomername(self): 23 | return self.__customername 24 | 25 | def validatecustomername(self): 26 | if(len(self.__customername)>=3 and len(self.__customername) <=20): 27 | return True 28 | else: return False 29 | 30 | telephoneno=[9201861311, 9201861321, 9201661311] 31 | custobj = Customer(1001, telephoneno, "Kevin") 32 | if(custobj.validatecustomername()): 33 | print("Customer Id : ", custobj.getcustomerid()) 34 | temp = custobj.gettelephoneno() 35 | print("Telephone Nos : ", temp[0], ",", temp[1], ",", temp[2]) 36 | print("Customer Name : ", custobj.getcustomername()) 37 | else: 38 | print("Invalid customer name. Customer name must be between 3 and 20 characters") -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment51.py: -------------------------------------------------------------------------------- 1 | class Customer: 2 | def __init__(self, customerid, telephoneno, customername): 3 | self.__customerid=customerid 4 | self.__customername=customername 5 | self.__telephoneno=telephoneno 6 | 7 | def setcustomerid(self, id): 8 | self.__customerid = id 9 | 10 | def setcustomername(self, customername): 11 | self.__customername = customername 12 | 13 | def settelephoneno(self, teleno): 14 | self.__telephoneno = teleno 15 | 16 | def getcustomerid(self): 17 | return self.__customerid 18 | 19 | def gettelephoneno(self): 20 | return self.__telephoneno 21 | 22 | def getcustomername(self): 23 | return self.__customername 24 | 25 | def validatecustomername(self): 26 | if(len(self.__customername)>=3 and len(self.__customername) <=20): 27 | return True 28 | 29 | else: 30 | return False 31 | 32 | telephoneno=[9201861311, 9201861321, 9201661311] 33 | custobj = Customer(1001, telephoneno, "Kevin") 34 | if(custobj.validatecustomername()): 35 | print("Customer Id : ", custobj.getcustomerid()) 36 | temp = custobj.gettelephoneno() 37 | print("Telephone Nos : ", temp[0], ",", temp[1], ",", temp[2]) 38 | print("Customer Name : ", custobj.getcustomername()) 39 | else: 40 | print("Invalid customer name. Customer name must be between 3 and 20 characters") -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment58.py: -------------------------------------------------------------------------------- 1 | class Address: 2 | def __init__(self, addressline, city, state, zip1): 3 | self.__addressline = addressline 4 | self.__city=city 5 | self.__state=state 6 | self.__zip=zip1 7 | 8 | def setaddressline(self, address): 9 | self.__addressLine = address 10 | 11 | def getaddressline(self): 12 | return self.__addressline 13 | 14 | def setcity(self, city): 15 | self.__city = city 16 | 17 | def getcity(self): 18 | return self.__city 19 | 20 | def setstate(self, state): 21 | self.__state = state 22 | 23 | def getstate(self): 24 | return self.__state 25 | 26 | def setzip(self, zip1): 27 | self.__zip = zip1 28 | 29 | def getzip(self): 30 | return self.__zip 31 | 32 | class Customer: 33 | def __init__(self, cid, address): 34 | self.__customerid = cid 35 | self.__address = address 36 | 37 | def getcustomerid(self): 38 | return self.__customerid 39 | 40 | def getaddress(self): 41 | return self.__address 42 | 43 | objaddress = Address("No.333,Oak street", "Strathfield", "New South Wales", "570018") 44 | objcust = Customer(1001, objaddress) 45 | print("Customer Id:", objcust.getcustomerid()) 46 | address = objcust.getaddress() 47 | print("Customer Address: ", address.getaddressline(), ",", address.getcity(), ",", address.getstate(), ",", address.getzip()) -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment56.py: -------------------------------------------------------------------------------- 1 | class Customer: 2 | def __init__(self, id1=0, name=None): 3 | self.__customerid=id1 4 | self.__customername=name 5 | 6 | def setcustomerid(self, id1): 7 | self.__customerid = id1 8 | 9 | def setcustomername(self, name): 10 | self.__customername=name 11 | 12 | def getcustomerid(self): 13 | return self.__customerid 14 | 15 | def getcustomername(self): 16 | return self.__customername 17 | 18 | class RegularCustomer(Customer): 19 | def __init__(self, id1=0, name=None, dis=0): 20 | super().__init__(id1,name) 21 | self.__discount=dis 22 | 23 | def setdiscount(self, dis): 24 | self.__discount=dis 25 | 26 | def getdiscount(self): 27 | return self.__discount 28 | 29 | class PrivilegedCustomer(Customer): 30 | def __init__(self, id1=0, name=None, card=None): 31 | super().__init__(id1,name) 32 | self.__memcardtype=card 33 | 34 | def setmemcardtype(self, card): 35 | self.__memcardtype=card 36 | 37 | def getmemcardtype(self): 38 | return self.__memcardtype 39 | 40 | objr = RegularCustomer() 41 | print("Regular Customer Details") 42 | print("Customer Id: ", objr.getcustomerid()) 43 | print("Customer Name: ", objr.getcustomername()) 44 | print("Discount Eligible: ", objr.getdiscount()) 45 | print("*********") 46 | objp = PrivilegedCustomer() 47 | print("Regular Customer Details") 48 | print("Customer Id: ", objp.getcustomerid()) 49 | print("Customer Name: ", objp.getcustomername()) 50 | print("Discount Eligible: ", objp.getmemcardtype()) 51 | print("*********") 52 | objr = RegularCustomer() 53 | objr.setcustomerid(1001) 54 | objr.setcustomername('Ram') 55 | objr.setdiscount(10.0) 56 | print("Regular Customer Details") 57 | print("Customer Id: ", objr.getcustomerid()) 58 | print("Customer Name: ", objr.getcustomername()) 59 | print("Discount Eligible: ", 60 | objr.getdiscount()) 61 | print("*********") 62 | objp = PrivilegedCustomer() 63 | objp.setcustomerid(1002) 64 | objp.setcustomername("Seetha") 65 | objp.setmemcardtype("Gold") 66 | print("Regular Customer Details") 67 | print("Customer Id: ", objp.getcustomerid()) 68 | print("Customer Name: ", objp.getcustomername()) 69 | print("Discount Eligible: ", objp.getmemcardtype()) -------------------------------------------------------------------------------- /Object Oriented Fundamentals/Assignment60.py: -------------------------------------------------------------------------------- 1 | class customer: 2 | counter = 1000 #class variable 3 | 4 | def __init__(self, telephoneno, customername, add): 5 | customer.counter += 1 6 | self.__customerid=customer.counter 7 | self.__customername=customername 8 | self.__telephoneno=telephoneno 9 | self.__address = add 10 | 11 | def setcustomerid(self, cid): 12 | self.__customerid = cid 13 | 14 | def settelephoneno(self, teleno): 15 | self.__telephoneno = teleno 16 | 17 | def getcustomerid(self): 18 | return self.__customerid 19 | 20 | def gettelephoneno(self): 21 | return self.__telephoneno 22 | 23 | def getcustomername(self): 24 | return self.__customername 25 | 26 | def getaddress(self): 27 | return self.__address 28 | 29 | @staticmethod 30 | def gettotalcustomer(): 31 | return customer.counter-1000 32 | 33 | class regularcustomer(customer): 34 | def __init__(self, telephoneno, customername, discount, add): 35 | # super to invoke baseclass init 36 | super().__init__(telephoneno, customername, add) 37 | self.__discount = discount 38 | 39 | def setdiscount(self, dis): 40 | self.__discount = dis 41 | 42 | def getdiscount(self): 43 | return self.__discount 44 | 45 | class address: 46 | def __init__(self, add): 47 | self.__addressline = add 48 | 49 | def setaddress(self, add): 50 | self.__addressline = add 51 | 52 | def getaddress(self): 53 | return self.__addressline 54 | 55 | regcustadd1 = address("No.22,Vijay Nagar Mysore Karnataka 570018") 56 | teleno=[9201861311, 9201861321, 9201661311] 57 | regcustobj1 = regularcustomer(teleno, "John", 12.5, regcustadd1) 58 | #custobj1.setcustomerid(1001) 59 | #custobj1.settelephoneno(1234567890) 60 | print("Customer id:", regcustobj1.getcustomerid()) 61 | print("Telephone no:", regcustobj1.gettelephoneno()) 62 | print("Customer name:", regcustobj1.getcustomername()) 63 | print("Discount:", regcustobj1.getdiscount()) 64 | #temp1 = regcustobj1.getaddress().getaddress() 65 | print("Customer's address:", regcustobj1.getaddress().getaddress()) 66 | print("\n") 67 | regcustadd2 = address("No.33,J.P. Nagar Bangalore Karnataka 570011") 68 | teleno1 = [1122334455, 1199887766, 2244668897] 69 | regcustobj2 = regularcustomer(teleno1, "Mary", 15.5,regcustadd2) 70 | print("Customer id:", regcustobj2.getcustomerid()) 71 | print("Telephone no:", regcustobj2.gettelephoneno()) 72 | print("Customer name:", regcustobj2.getcustomername()) 73 | print("Discount:", regcustobj2.getdiscount()) 74 | print("Customer's address:", regcustobj2.getaddress().getaddress()) 75 | print("\n") 76 | print("Total customers registered:", customer.gettotalcustomer()) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python 2 | Basic Programming in Python 3 | You'll get to know the basic fundamentals of Python, Programming is done on Eclipes based on Infosys Module 4 | 5 | 6 | ## Fundamentals 7 | 8 | ### Assignment 1: Simple Print Statement 9 | 10 | Objective: 11 | ``` 12 | print Hello World! 13 | ``` 14 | 15 | ### Assignment 2: Observations from a real-world problem - Guided Activity 16 | 17 | Objective: Given a real-world problem, be able to understand the need for programming fundamentals such as identifiers, variables, data types etc. 18 | 19 | Problem Description: A retail store management wants to automate the process of generating the bill amount for its customers. As an initial step, they want to initialize the bill details of a customer as given below: 20 | Bill id should be 1001, customer id should be 101 and bill amount should be 199.99. 21 | After initializing, all the values must be displayed in the format given below: 22 | ``` 23 | bill_id: 1001 24 | customer_id: 101 25 | bill_amount: Rs.199.99 26 | ``` 27 | Analyze the above problem statement and answer the following questions: 28 | 1. What do you think is needed to write a program to implement the solution for the above problem statement? 29 | 30 | Summary of this assignment: In this assignment, you have understood the need of a high-level programming language and programming fundamentals such as identifiers, variables, data types, operators etc. 31 | 32 | 33 | ### Assignment 3: Programming constructs in Python - Guided Activity 34 | 35 | Objective: Given a real-world problem, be able to identify the data types of the variables required to solve it 36 | 37 | Problem Description: For the previous assignment, identify the data types that may be used to represent bill id, customer id and bill amount. 38 | 39 | Summary of this assignment: In this assignment, you have learnt to identify the data type for the variables based on the real-world problem 40 | 41 | 42 | ### Assignment 4: Programming constructs in Python - Quiz 43 | 44 | Objective: Given a real-world problem, be able to identify the variables and operators required to solve the problem and implement it using a high-level programming language like Python 45 | Problem Description: Predict the output of following Python Statements. 46 | ``` 47 | print ("Value of e is %0.1f" , 2.713 ) 48 | print ("Value of e is %0.1f" %2.713 ) 49 | print ("Programming in Python: Version %d" %3.5 ) 50 | print ("%20s : %d" % ("Python 3.0 is also known as Python",3000.57 )) 51 | x=2 52 | print ("{0:2d} {1:3d} {2:4d}".format(x, x*x, x*x*x)) 53 | ``` 54 | Summary of this assignment: In this assignment, you have learnt to make use of format specifiers for different type of data variables based on the real-world problem 55 | 56 | 57 | ### Assignment 5: Programming constructs in Python - Demo 58 | 59 | Objective: Given a real-world problem, be able to identify the variables and operators required to solve the problem and implement it using a high-level programming language like Python 60 | 61 | Problem Description: Write a Pseudocode and Python program to implement the real-world problem discussed in Programming constructs in Python Assignment 2. Compile and execute the program using Eclipse IDE. 62 | You may want to refer to Programming constructs in Python Assignment 1 to understand how Eclipse IDE may be used for writing and executing a Python program. 63 | 64 | INITIALIZE_VARIABLES_DISPLAY 65 | 1. bill_id = 1001 66 | 2. customer_id = 101 67 | 3. bill_amount = 199.99 68 | 4. display "Bill Id:", bill_id 69 | 5. display "Customer Id:", customer_id 70 | 6. display "Bill Amount:Rs.", bill_amount 71 | 72 | Summary of this assignment: In this assignment, you have understood how to implement the solution for a simple real-world problem using variables and operators 73 | 74 | 75 | ### Assignment 6: Programming constructs in Python – Hands on practice 76 | 77 | Objective: Given a real-world problem, be able to identify the variables and operators 78 | required to solve the problem and implement it using a high-level programming language like Python 79 | 80 | Problem Description: The finance department of a company wants to calculate the monthly pay of one of its employee. Monthly pay should be calculated as mentioned in the below formula and display all the employee details. 81 | ``` 82 | Monthly Pay = Number of hours worked in a week * Pay rate per hour * No. of weeks in a month 83 | ``` 84 | Note: 85 | The number of hours worked by the employee in a week should be considered as 40, 86 | Pay rate per hour should be considered as Rs.400 and 87 | Number of weeks in a month should be considered as 4 88 | Write Pseudo code and Python program in Eclipse to implement the above real-world problem. 89 | 90 | Summary of this assignment: In this assignment, you have learnt to implement the solution for a simple real-world problem using variables and operators 91 | 92 | 93 | ### Assignment 7: Programming constructs in Python - Hands - on - Practice 94 | 95 | Objective: Given a real-world problem, be able to identify the variables and operators required to solve the problem and implement it using a high-level programming language like Python 96 | 97 | Problem Description: Write the assignment statement to swap 2 numbers x and y? (Without using temp variable) 98 | 99 | Summary of this assignment: In this assignment, you have learnt to implement the solution for a simple real-world problem using variables and operators 100 | 101 | 102 | ### Assignment 8: Programming constructs in Python - Guided Activity 103 | 104 | Objective: Given a real-world problem, be able to identify the variables and operators required to solve the problem and implement it using a high-level programming language like Python 105 | 106 | Problem Description: Predict the output of following code snippet 107 | ``` 108 | num = 16 109 | num1 = num/6 110 | num2 = num//6 111 | num3 = num//6.0 112 | print(num1) 113 | print(num2); 114 | print(num3) 115 | ``` 116 | Summary of this assignment: In this assignment, you have learnt to implement the solution for a simple real-world problem using variables and operators 117 | 118 | 119 | ### Assignment 9: id() and type() functions - Quiz 120 | 121 | Objective: Revisit the concept and usage of id() and type() functions 122 | 123 | Problem Description: 124 | Dry run the below code snippets and predict the output. You may want to confirm the output by executing the code using Eclipse IDE. 125 | 126 | #### Code – 1: 127 | ``` 128 | obj_x = 10 129 | obj_y = obj_x 130 | if ( id(obj_x) == id(obj_y) ): 131 | print("Address of obj_x and obj_y is same") 132 | else: 133 | print("Address of obj_x and obj_y is not same") 134 | ``` 135 | #### Code – 2: 136 | ``` 137 | obj_x = 10 138 | obj_y = obj_x 139 | if ( obj_x is obj_y ): 140 | print("obj_x and obj_y have same identity") 141 | else: 142 | print("obj_x and obj_y do not have same identity") 143 | ``` 144 | #### Code – 3: 145 | ``` 146 | obj_x = 10 147 | obj_y = obj_x 148 | obj_y = obj_x + 1 149 | if ( obj_x is not obj_y ): 150 | print("obj_x and obj_y do not have same identity") 151 | else: 152 | print("obj_x and obj_y have same identity") 153 | ``` 154 | #### Code – 4: 155 | ``` 156 | int_a = 10 157 | raw_input = input("Enter a number") 158 | print("Type of int_a:", type(int_a)) 159 | print("Type of raw_input:", type(raw_input)) 160 | print(int_a + raw_input) 161 | ``` 162 | Summary of this assignment: In this assignment, you have understood the concept and usage of id() and type() built-in functions. 163 | 164 | 165 | #### In Code 4 there's meant to be an error (type error), to show that int & str can't be concatenated straight away 166 | 167 | 168 | 169 | ## Control Structures 170 | 171 | ### Assignment 10: Coding Standards - Guided Activity 172 | 173 | Objective: Given a set of source code and an identified set of best practices, be able to implement the techniques during coding 174 | Problem Description: Coding standards are very important for maintenance of code and for understanding of the code. Identify the sections of the given program where the coding standards are not followed and correct them. 175 | ``` 176 | itemNo=1005 unitprice = 250 177 | quantity = 2 178 | amount=quantity*unitprice 179 | print("Item No:",itemNo) 180 | print("Bill Amount:",amount) 181 | ``` 182 | Summary of this assignment: In this assignment, you have learnt Python coding standards 183 | 184 | 185 | ### Assignment 11: Control Structures – Observations from a real-world problem - Guided Activity 186 | 187 | Objective: Given a real-world problem be able to understand the need for control structures and operators to implement the logic and solve the problem 188 | 189 | Problem Description: The scenario discussed in Programming Fundamentals Assignment 5 is revisited here. Suppose the retail store management now wants to provide 2% discount for all bill amounts above Rs.500 and for all other bill amount, a discount of 1%. 190 | What do you think is needed to implement this scenario? 191 | 192 | Summary of this assignment: In this assignment, you have understood the need of operators and control structures using a real-world problem 193 | 194 | 195 | ### Assignment 12: Control Structures - Demo 196 | 197 | Objective: Given a real world problem be able to understand the need for control structures and operators to implement the logic and solve the problem 198 | 199 | Problem Description: Suppose the retail store management now wants to provide 2% discount for all bill amounts above Rs.500 and for all other bill amont, a discount of 1%. 200 | 201 | Write a Python program to implement the same? 202 | ``` 203 | bill_id = 1001 204 | customer_id = 101 205 | bill_amount = 200.0 206 | discounted_bill_amount = 0.0 207 | print("Bill Id:%d" %bill_id) 208 | print("Customer Id:%d" %customer_id) 209 | Note the use if else statement 210 | print("Bill Amount:Rs.%f" %bill_amount) 211 | if bill_amount > 500: 212 | discounted_bill_amount = bill_amount - bill_amount * 2 / 100 213 | else: 214 | discounted_bill_amount = bill_amount - bill_amount * 1 / 100 215 | print("Discounted Bill Amount:Rs.%f" %discounted_bill_amount) 216 | ``` 217 | 218 | Summary of this assignment: In this assignment, you have understood the implementation of operators and control structures using a real world problem 219 | 220 | 221 | ### Assignment 13: Control Structures - Guided Activity 222 | 223 | Objective: Given a real world problem be able to understand the need for control structures and operators to implement the logic and solve the problem 224 | 225 | Problem Description: Suppose the retail store management now wants to provide discount for all bill amounts as mentioned below. 226 | Assume bill amount will be always greater than 0. 227 | Note Note Note Note the use if else statement the use if else statement the use if else statement the use if else statement the use if else statement the use if else statement the use if else statement the use if else statement the use if else statement 228 | ``` 229 | Bill Amount 230 | Discount % 231 | >=1000 5 232 | >=500 and <1000 2 233 | >0 and <500 1 234 | ``` 235 | Write a Python program to implement the same? 236 | ``` 237 | bill_id = 1001 238 | customer_id = 101 239 | bill_amount = 1200.0 240 | discounted_bill_amount = 0.0 241 | discount = 0 242 | print("Bill Id: %d" %bill_id) 243 | print("Customer Id: %d" %customer_id) 244 | print("Bill Amount:Rs. %f" %bill_amount) 245 | if bill_amount >= 1000: 246 | discount = 5 247 | elif bill_amount >= 500: 248 | discount = 2 249 | else: 250 | discount = 1 251 | discounted_bill_amount = bill_amount - bill_amount * discount / 100 252 | print("Discounted Bill Amount:Rs. %f" %discounted_bill_amount) 253 | ``` 254 | 255 | Summary of this assignment: In this assignment, you have understood the implementation of operators and control structures using a real world problem 256 | 257 | 258 | ### Assignment 14: Control Structures - Guided Activity 259 | 260 | Objective: Given a real-world problem be able to understand the need for control structures and operators to implement the logic and solve the problem 261 | 262 | Problem Description: Suppose the retail store management now wants to provide discount for all bill amounts as mentioned below. 263 | 264 | Customers can be considered to be valid, if their customer id is between 101 and 1000(both inclusive). 265 | For valid customers, discount must be provided as per the table given below: 266 | ``` 267 | Bill Amount 268 | Discount % 269 | >=500 10 270 | ``` 271 | 272 | 273 | ### Assignment 15: Control Structures – Hands – on – Practice 274 | 275 | Objective: Given a real-world problem be able to understand the need for control structures and operators to implement the logic and solve the problem 276 | 277 | Problem Description: Objective: Given a real-world problem be able to understand the need for control structures and operators to implement the logic and solve the problem 278 | 279 | Problem Description: The finance department of a company wants to calculate the monthly net pay of one of its employee by finding the income tax to be paid (in Indian Rupees) and the net salary after the income tax deduction. The employee should pay income tax if his monthly gross salary is more than Rs. 10,000 (Indian Rupees) and the percentage of income tax should be considered as 20% of the gross salary. 280 | Display the employee id, basic salary, allowances, gross pay, income tax and net pay. 281 | Note: 282 | ``` 283 | Employee Id must be considered as 1001, 284 | Basic salary of the employee must be considered as Rs.15000.00 and 285 | Allowances must be considered as Rs.6000.00 286 | ``` 287 | Write a Pseudo code and Python program in Eclipse to solve the above real-world problem. 288 | Refer below for the formulae to be used. 289 | ``` 290 | Monthly Gross Salary = Basic Salary + Allowances 291 | Net Salary = Gross Salary – Income Tax 292 | ``` 293 | Summary of this assignment: In this assignment, you have understood the implementation of operators and control structures using a real-world problem 294 | 295 | 296 | ### Assignment 16: Control Structures – Hands - on - Practice 297 | 298 | Objective: Given a real-world problem be able to understand the need for control structures and operators to implement the logic and solve the problem 299 | 300 | Problem Description: Extend the program written for Assignment 15 to find the income tax to be paid (In Indian Rupees) and the total salary after the income tax deduction as per the details given in below table. 301 | 302 | | Gross Salary (In Indian Rupees) | Income Tax percentage | 303 | |--- | --- | 304 | | Below 5,000 | Nil | 305 | | 5,001 to 10,000 | 10 % | 306 | | 10,001 to 20,000 | 20% | 307 | | More than 20,000 | 30% | 308 | 309 | Display the employee id, basic salary, allowances, gross pay, income tax and net pay. 310 | 311 | Note: 312 | Employee Id must be considered as 1001, 313 | Basic salary of the employee must be considered as Rs.15000.00 and 314 | Allowances must be considered as Rs.6000.00 315 | Write a Pseudo code and Python program in Eclipse to solve the above real-world problem. 316 | 317 | Summary of this assignment: In this assignment, you have understood the implementation of operators and control structures using a real-world problem 318 | 319 | 320 | ### Assignment 17: Iteration Control Structures - Guided Activity 321 | 322 | Objective: Given a real-world problem, implement the logic and solve the problem using appropriate constructs (sequential, selection, iteration) using an object-oriented programming language (Python) 323 | 324 | Problem Description: 325 | Dry run the below code snippets and predict the output. You may want to confirm the output by executing the code using Eclipse IDE. 326 | 327 | #### Code-1: 328 | ``` 329 | counter = 1 330 | while counter <= 3: 331 | print(counter) 332 | counter += 1 333 | print("End of Program") 334 | ``` 335 | #### Code-2: 336 | ``` 337 | print("To find the sum of first 10 integers:") 338 | result = 0 339 | for value in range(1,11): 340 | result = result + value 341 | print("Sum:",result); 342 | ``` 343 | #### Code-3: 344 | ``` 345 | number = 1 346 | result = 0 347 | while number < 5: 348 | result = result + number 349 | number = number + 1 350 | print(result) 351 | ``` 352 | #### Code-4: 353 | ``` 354 | result = 0 355 | for index in range(40, 10, -2): 356 | if(index % 5 == 0): 357 | result = result + index 358 | print(result) 359 | ``` 360 | #### Code-5: 361 | ``` 362 | amount = 100.0 363 | interest = 0.0 364 | months = 1 365 | while months < 6: 366 | interest = amount * 0.2 367 | amount = amount + interest 368 | months += 1 369 | print(amount) 370 | ``` 371 | Summary of this assignment: In this assignment, you have understood the implementation of iteration control structures. 372 | 373 | 374 | ### Assignment 18: break statement - Demo 375 | 376 | Objective: Given a real-world problem, implement the logic and solve the problem using appropriate constructs (sequential, selection, iteration) using an object-oriented programming language (Python) 377 | 378 | Problem Description: 379 | Dry run the below code snippet and predict the output. You may want to confirm the output by executing the code using Eclipse IDE. 380 | 381 | #### Code 382 | ``` 383 | count = 0 384 | result = 0 385 | for count in range (1, 10): 386 | result = result + count 387 | ``` 388 | On countering the break statement, control will move out of the loop as shown here 389 | Note the use of break statement 390 | ``` 391 | if result > 6: 392 | break 393 | print("Result =", result) 394 | ``` 395 | Summary of this assignment: In this assignment, you have understood the implementation of break statement. 396 | 397 | 398 | ### Assignment 19: continue statement - Demo 399 | 400 | Objective: Given a real-world problem, implement the logic and solve the problem using appropriate constructs (sequential, selection, iteration) using an object-oriented programming language (Python) 401 | 402 | Problem Description: 403 | Dry run the below code snippet and predict the output. You may want to confirm the output by executing the code using Eclipse IDE. 404 | 405 | #### Code 406 | ``` 407 | count = 0 408 | for count in range(0,10): 409 | if 4 == count: 410 | continue 411 | print(count) 412 | ``` 413 | Summary of this assignment: In this assignment, you have understood the implementation of continue statement. 414 | 415 | 416 | ### Assignment 20: Iteration Control Structure – Debugging - Guided Activity 417 | 418 | Objective: Given a real-world problem, implement the logic and solve the problem using appropriate constructs (sequential, selection, iteration) using an object- 419 | oriented programming language (Python) 420 | 421 | Problem Description: The code given below is written to display all the even numbers between 50 and 80 (both inclusive). Debug the program to get the correct output. 422 | 423 | Step 1: Type the below program in Eclipse, save the file as for_loop.py, compile and execute. 424 | ``` 425 | for i in range (50, 80): 426 | if i % 2 == 0: 427 | print(i) 428 | else: 429 | break 430 | Step 2: Correct the logical error in the code, save, compile and execute the code 431 | Step 3: Implement the same logic using while loop 432 | ``` 433 | Summary of this assignment: In this assignment, you have learnt the implementation of iteration control structure and break statement. 434 | 435 | 436 | 437 | ## Strings - Assignments 438 | 439 | ### Assignment 21: Strings – Observations from Retail Application - Guided Activity 440 | 441 | Objective: Observe the need for features in the retail application scenario to correlate the application of Strings 442 | 443 | Problem Description: In the retail application, along with the earlier details included for Customer, the retail shop wants to keep track of customer name. Customer name should be between 3 and 20 characters. 444 | 445 | Answer the following question: 446 | What do you think is needed to implement the solution for the above problem? 447 | 448 | Summary of this assignment: In this assignment, you have understood the need of strings using a retail application scenario 449 | 450 | 451 | ### Assignment 22: Strings – Demo 452 | 453 | Objective: Observe the need for features in the retail application scenario to correlate the application of Strings 454 | 455 | Problem Description: In the retail application, along with the earlier details included for Customer, the retail shop wants to keep track of customer name. Customer name should be between 3 and 20 characters. 456 | ``` 457 | bill_id = 1001 458 | customer_id = 1001 459 | bill_amount = 2000 460 | customer_name = Kevin 461 | ``` 462 | #### Code 463 | ``` 464 | bill_id = input("Please enter Bill id") 465 | customer_id = input("Please enter Customer id") 466 | bill_amount = input("Please enter bill Amount") 467 | customer_name = input("Please enter Customer Name") 468 | if ((len(customer_name) >= 3) and (len(customer_name) <= 20)) is True: 469 | print("Bill Id: ",bill_id) 470 | print("Customer Id: ",customer_id) 471 | print("Bill Amount:Rs. ",bill_amount) 472 | print("Customer Name: ",customer_name) 473 | else: 474 | print("Invalid customer name. Customer name must be between 3 and 20 characters"); 475 | ``` 476 | Summary of this assignment: In this assignment, you have understood the implementation of Strings and String methods using a retail application scenario 477 | 478 | 479 | ### Assignment 23: Strings – Quiz 480 | 481 | Objective: Revisit String concepts through a quiz 482 | 483 | Problem Description: 484 | Assume, 485 | string1 = “Infosys Limited” 486 | string2 = “Mysore” 487 | Predict the output of following statements: 488 | ``` 489 | Q1: print(string1[:4]) 490 | Q2: print(string1[-1]) 491 | Q3: print(string1 * 2) 492 | Q4: print(string1[:-1] + string2 + string1[:-1]) 493 | Q5: print (string2[1]) 494 | Q6: print (string2[4]) 495 | Q7: print(string1 * 2 + string1[:-1] + string2) 496 | ``` 497 | Summary of this assignment: In this assignment, you have revisited concepts of strings through code snippets 498 | 499 | 500 | ### Assignment 24: Strings – Hands - on - Practice 501 | 502 | Objective: Given a computational problem, be able to use the right data structures (lists and strings) and implement the solution to the problem and test using a set of values in an IDE 503 | 504 | Problem Description: 505 | ``` 506 | a. Write a program to count and display the number of capital letters in a given string. 507 | b. Write a program to check if the given string is Palindrome or not? 508 | c. Write a program to count the number of each vowel in a string 509 | d. Write a program to remove all punctuation from the string provided by the user 510 | punctuations = '''!()-[]{};:'"\,<>./?@###$%^&*_~''' 511 | ``` 512 | Hint: Use membership operators IN, Not IN 513 | 514 | Summary: In this assignment, you have understood the application and implementation of Strings concept for the given computational problem. 515 | 516 | 517 | ### Assignment 25: Strings – Hands – on - Practice 518 | 519 | Objective: Given a computational problem, be able to use the right data structures (lists and strings) and implement the solution to the problem and test using a set of values in an IDE 520 | 521 | Problem Description: Write a Python program to accept a string and display the resultant string in reverse order. The resultant string should contain all characters at the even position of accepted string ignoring blank spaces. 522 | ``` 523 | accepted_string: An apple a day keeps the doctor away 524 | resultant_string: Aapedyepteotrwy 525 | expected_output: ywrtoetpeydepaA 526 | ``` 527 | Summary: In this assignment, you have understood the application and implementation of Strings concept for the given computational problem. 528 | 529 | 530 | ### Assignment 26: Strings – Hands – on - Practice 531 | 532 | Objective: Given a computational problem, be able to use the right data structures (lists and strings) and implement the solution to the problem and test using a set of values in an IDE 533 | 534 | Problem Description: Given a string containing both upper and lower case letters. Write a Python program to count the number of repeated characters and display the maximum count of a character along with the character. 535 | ``` 536 | Sample Input: ABaBCbGc 537 | ``` 538 | Summary: In this assignment, you have understood the application and implementation of Strings concept for the given computational problem. 539 | 540 | 541 | ### Assignment 27: Strings – Hands - on - Practice 542 | 543 | Objective: Given a computational problem, be able to use the right data structures (lists and strings) and implement the solution to the problem and test using a set of values in an IDE 544 | 545 | Problem Description: Consider 2 strings string1 and string2 and display the merged_string as the output. The merged_string should be capital letters from both the strings in the order they appear. 546 | 547 | Note: Each character should be checked if it is a capital letter and then it should be merged. 548 | #### Sample Input: 549 | ``` 550 | string1: I Like C 551 | string2: Mary Likes Python 552 | merged_string: ILCMLP 553 | ``` 554 | Summary: In this assignment, you have understood the application and implementation of Strings concept for the given computational problem. 555 | 556 | 557 | ### Assignment 28: Operations on Tuples - Demo 558 | 559 | Objective: To understand the operations that can be performed on Tuples through a quiz 560 | 561 | Problem Description: 562 | Assume, 563 | ``` 564 | head = ("CEO",) 565 | elements = ('Air', 'Water', 'Fire', 'Light', 'Land') 566 | ``` 567 | Predict the output of following statements: 568 | ``` 569 | Q1: print(head) 570 | Q2: print(elements) 571 | Q2: print(elements[3]) 572 | Q3: elements[0]='Ice' 573 | Q5: print(elements[-1]) 574 | Q6: print(elements[4]) 575 | Q7: print(elements[5]) 576 | ``` 577 | Summary of this assignment: In this assignment, you have revisited concepts of tuples through code snippets 578 | 579 | #### There's meant to be 2 errors in this code in line 10 & 12 , however you won't be able to see the error of 12th line for that you need to remove line 10 to see it. 580 | ``` 581 | Line 10 - IndexError: tuple index out of range 582 | Line 12 - TypeError: 'tuple' object does not support item assignment 583 | ``` 584 | 585 | 586 | ### Assignment 29: Tuples – Hands – on - Practice 587 | 588 | Objective: Given a List of elements representing a computational problem, be able to provide solution by performing required operations using data structures like tuple from an object oriented language (Python) using Eclipse IDE 589 | 590 | Problem Description: Consider the list of courses opted by a Student “John” and available electives as a part of Student Management System. 591 | ``` 592 | courses = (“Python Programming”, “RDBMS”, “Web Technology”, “Software Engg”). 593 | electives = (“Business Intelligence”, “Big Data Analytics”) 594 | ``` 595 | Write a Python Program to satisy following business requirements: 596 | a. List the number of courses opted by Student by “John” 597 | b. List all the courses opted by Student “John”. 598 | c. Students “John” is also interested in elective course mentioned above. Print the updated tuple including electives 599 | d. Check whether Student “John” is allowed to change his course from “Software Engg” to “Computer Networks”. If yes, print the updated course list else mention the reason for the same. 600 | 601 | Summary of this assignment: In this assignment, you have learnt the implementation of Tuple operations for given business scenario. 602 | 603 | 604 | 605 | ## Lists - Assignments 606 | 607 | ### Assignment 30: Accessing Elements from Lists - Demo 608 | 609 | Objective: Given a List of elements representing a computational problem, be able to access elements in different ways using an object oriented language (Python) using an IDE 610 | 611 | Problem Description: 612 | Assume, 613 | ``` 614 | language = ['Python'] 615 | languages = ['Python', 'C', 'C++', 'Java', 'Perl'] 616 | ``` 617 | Predict the output of following statements: 618 | ``` 619 | Q1: print(language) 620 | Q2: print(languages) 621 | Q3: print(languages[0:3]) 622 | Q4: print(languages[1:4]) 623 | Q5: print(languages[2]) 624 | Q6: print(languages[5]) 625 | Q7: print (languages[0] + " and " + languages[1] + " are quite different!") 626 | Q8: print ("Accessing the last element of the list: " + languages[-1]) 627 | ``` 628 | Summary of this assignment: In this assignment, you have revisited concepts of lists through code snippets 629 | 630 | 631 | ### Assignment 31: Lists – Hands - on - Practice 632 | 633 | Objective: Given a computational problem, be able to use the right data structures (lists and strings) and implement the solution to the problem and test using a set of values in an IDE 634 | 635 | Problem Description: Write a Python program to generate first ‘n’ Fibonacci numbers. Store the generated Fibonacci numbers in a list and display it. 636 | #### Sample input: Enter n 5 637 | #### Sample Output: List: [0, 1, 1, 2, 3] 638 | 639 | Summary: In this assignment, you have understood the application and implementation of Lists concept for the given computational problem. 640 | 641 | 642 | ### Assignment 32: Lists – Hands - on - Practice 643 | 644 | Objective: Given a List of elements representing a computational problem, be able to sort the elements in ascending/descending order using an object oriented language (Python) using an IDE 645 | 646 | Problem Description: You have a bundle of currency of varied denominations. You want to arrange them in descending order. 647 | Given below is one approach to perform the above operation. 648 | ``` 649 | Algorithm: Bubble Sort (List of N element) 650 | Input: A List of N elements to be sorted 651 | ``` 652 | Summary: In this assignment, you have understood the application and implementation of Lists concept for the given computational problem 653 | 654 | 655 | ### Assignment 33: Lists - Hands - on - Practice 656 | 657 | Objective: Given a computational problem, Implement the solution for the problem using suitable data structures from an object oriented language (Python) using an IDE 658 | 659 | Problem Description: ABC Retail Store sells different varieties of Furniture to the customers. The list of furnitures available and its cost list are given below: 660 | 661 | | Furniture | Cost in Rs. | 662 | |---|---| 663 | | Sofa set | 20,000 | 664 | | Dining table | 8,500 | 665 | | T.V. Stand | 4,599 | 666 | | Cupboard | 13,920 | 667 | 668 | ### Assignment 34: Sets - Demo 669 | 670 | Objective: Given a Set of elements representing a computational problem, be able to perform different operations using an object oriented language (Python) using an IDE 671 | 672 | Problem Description: 673 | Consider the sets, 674 | ``` 675 | fruits = {"apple", "orange", "banana", "apple", "pear", "papaya", "papaya"} 676 | fruit_basket = {"apple", "banana", "grapes", "mango", "kiwi"} 677 | ``` 678 | Predict the output of following statements: 679 | ``` 680 | Q1: print(fruits) 681 | Q2: print(fruits & fruit_basket) 682 | Q3: print(fruits | fruit_basket) 683 | Q4: print(fruits - fruit_basket) 684 | Q5: print(fruits ^ fruit_basket) 685 | Q6: print(len(fruit_basket)) 686 | Q7: print("pear" in fruits) 687 | Q8: print("pear" not in fruit_basket) 688 | Q9: print(fruits.issubset(fruit_basket)) 689 | Q10: print(fruits.issuperset(fruit_basket)) 690 | Q11: print(fruit_basket.copy()) 691 | ``` 692 | Summary of this assignment: In this assignment, you have revisited concepts of Sets through code snippets 693 | 694 | 695 | ### Assignment 35: Sets – Hands - on - Practice 696 | 697 | Objective: Given a Set of elements representing a computational problem, be able to provide solution by performing required operations on sets from an object oriented language (Python) using an IDE 698 | 699 | Problem Description: Consider the scenario from course in student management system. Given below are 2 Sets representing the names of students enrolled for a particular course. 700 | ``` 701 | java_course = {“John”, “Jack”, “Jill”, “Joe”} 702 | python_course = {“Jake”, “John”, “Eric”, “Jill”} 703 | ``` 704 | Write a Python program to satisy below mentioned business requirements: 705 | a. List the number of Students enrolled for Python course 706 | b. List the names of Students enrolled for Java course only 707 | c. List the names of Students enrolled for Python course only 708 | d. List the number and names of Students enrolled for both Java and Python courses 709 | e. List the number and names of Students enrolled for either Java or Python courses but not both 710 | f. List names and number of Students enrolled for either Java or Python courses 711 | 712 | Summary of this assignment: In this assignment, you have learnt the implementation of Set operations for given business scenario. 713 | 714 | 715 | ### Assignment 36: Dictionary - Demo 716 | 717 | Objective: Given a Dictionary of elements representing a computational problem, be able to perform different operations using an object oriented language (Python) on Eclipse IDE 718 | 719 | Problem Description: Given below is a Dictionary customer_details representing customer Details from Retail Application - Customer Id is key and Customer Name is value. 720 | ``` 721 | customer_details = { 1001 : "John", 1004 : "Jill", 1005: "Joe", 1003 : "Jack" } 722 | ``` 723 | Write Python code to perform below mentioned operations: 724 | a. Print details of Customers 725 | b. Print number of Customers 726 | c. Print Customer names in ascending order 727 | d. Delete the details of customer with customer id = 1005 and print updated dictionary 728 | e. Update the name of customer with customer id = 1003 to “Mary” and print updated dictionary 729 | f. Check whether details of customer with customer id 1002 exists in the dictionary. 730 | 731 | Summary of this assignment: In this assignment, you have learnt the implementation of Dictionary operations for given business scenario. 732 | 733 | 734 | ### Assignment 37: Dictionary – Hands - on - Practice 735 | 736 | Objective: Given a computational problem, select the right set of data structures (lists and dictionary) and implement the solution to problem and test using a set of values in an IDE 737 | 738 | Problem Description: Consider the scenario of processing marks of students for a course in student management system. Given below is the list of marks scored by students. Find top three scorers for the course and also display average marks. 739 | Implement the solution for given business scenario. 740 | 741 | |Student Name | Marks Scored | 742 | |---|---| 743 | |John | 86.5 | 744 | |Jack | 91.2 | 745 | |Jill | 84.5 | 746 | |Harry | 72.1 | 747 | |Joe | 80.5 | 748 | 749 | Summary: In this assignment, you have understood the application and implementation of Dictionary concept for the given computational problem. 750 | 751 | 752 | 753 | ## Functions - Assignment 754 | 755 | ### Assignment 38 756 | 757 | Objective: Given a computational problem, implement functions and solve it using parameter passing technique followed in python 758 | 759 | Problem Description: Consider the distance in miles between two locations: 760 | distance between Phoenix, Arizona and Salt Lake City, Utah in USA 761 | distance between Phoenix, Arizona and Tampa, Florida in the US. 762 | 763 | We want to compare the distances and check which one is far off from Phoenix, Arizona. 764 | 765 | Provided below are codes written to solve the above problem using pass by reference technique – using Required arguments and Keyword Arguments. 766 | 767 | Code: 768 | 769 | Method 1: Pass by Reference – Required Arguments 770 | ``` 771 | def compare(phoenix_to_slc, phoenix_to_tampa): 772 | if phoenix_to_slc > phoenix_to_tampa: 773 | print("SLC is far from Phoenix compared to Tampa, Florida") 774 | elif(phoenix_to_slc < phoenix_to_tampa): 775 | print("Tampa, Florida is far from Phoenix compared to SLC") 776 | else: 777 | print("Both locations are equidistance from Phoneix") 778 | compare(1790,506) 779 | ``` 780 | 781 | Method 2: Pass by Reference – Keyword Arguments 782 | ``` 783 | def compare(phoenix_to_slc, phoenix_to_tampa): 784 | if phoenix_to_slc > phoenix_to_tampa: 785 | print("SLC is far from Phoenix compared to Tampa, Florida") 786 | elif(phoenix_to_slc < phoenix_to_tampa): 787 | print("Tampa, Florida is far from Phoenix compared to SLC") 788 | else: 789 | print("Both locations are equidistance from Phoneix") 790 | compare (phoenix_to_tampa = 506, phoenix_to_slc = 1790) 791 | ``` 792 | 793 | Output: 794 | ``` 795 | SLC is far from Phoenix compared to Tampa, Florida 796 | ``` 797 | 798 | Summary of this assignment: In this assignment, you have learnt the implementation of functions and parameter passing technique - pass by reference through keyword arguments and reference arguments 799 | 800 | 801 | ### Assignment 39 802 | 803 | Objective: Given a computational problem, implement functions and solve it using appropriate parameter passing techniques (pass by reference) 804 | 805 | Problem Description: At an airport, a traveler is allowed entry into the flight only if he clears the following checks 806 | i. Baggage Check 807 | ii. Immigration Check 808 | iii. Security Check 809 | 810 | The logic for the check methods are given below: 811 | 812 | Implementation details 813 | #### check_baggage (baggage_amount) 814 | Check if baggage_amount is greater than or equal to 0 and less than or equal to 40. 815 | ``` 816 | if baggage_amount is VALID 817 | return TRUE 818 | else 819 | return FALSE 820 | ``` 821 | #### check_immigration (expiry_year) 822 | Check if expiry_year is greater than or equal to 2001 and less than or equal to 2025. 823 | ``` 824 | if expiry_year is VALID 825 | return TRUE 826 | else 827 | return FALSE 828 | ``` 829 | #### check_security(noc_status):boolean 830 | ``` 831 | if noc_status is TRUE 832 | return TRUE 833 | else 834 | return FALSE 835 | ``` 836 | #### traveler() 837 | In traveler() function, initialize the traveler Id and traveler name and invoke the functions check_baggage(), check_immigration() and check_security() by passing required arguments. Refer the table below for values of arguments. 838 | 839 | |Variable | Value | 840 | |---|---| 841 | |traveler_id | 1001| 842 | |traveler_name | Jim| 843 | |baggageAmount | 35| 844 | |expiryDate | 2019| 845 | |nocStatus | true| 846 | 847 | ``` 848 | if all values of check_baggage(), check_immigration() and check_security() are true, diplay traveler_id and traveler_name 849 | display “Allow Traveller to fly!” 850 | else 851 | display traveler_id and traveler_name 852 | display “Detain Traveller for Re-checking!” 853 | ``` 854 | Summary of this assignment: In this assignment, you have learnt pass by reference technique 855 | 856 | 857 | #### Assignment 40 858 | 859 | Objective: Revisit String built-in functions through match the following. 860 | Problem Description: There are many built-in String methods available in Python. Match each method to what it does. 861 | 862 | |Method | What the method does | 863 | |---|---| 864 | |text.endswith(“.jpg”) |Return a copy of the string with all occurences of one substring replaced by another | 865 | |text.upper() | Return a copy of the string converted to lowercase | 866 | |text.lower() | Return the value True if the string has the given substring at the beginning | 867 | |text.replace(“tomorrow”, “Saturday”) | Return the value True if the string has the given substring at the end| 868 | |text.strip() | Returns the first index value when the given substring is found| 869 | |text.find(“python”) | Return a copy of the string with the leading and trailing whitespace removed| 870 | |Text.startswith(“”) | Return a copy of the string converted to uppercase| 871 | 872 | ANSWERS: 873 | 874 | |Method | What the method does | 875 | |---|---| 876 | |text.endswith(“.jpg”) | Return the value True if the string has the given substring at the end| 877 | |text.upper() | Return a copy of the string converted to uppercase | 878 | |text.lower() |Return a copy of the string converted to lowercase | 879 | |text.replace(“tomorrow”, “Saturday”) |Return a copy of the string with all occurences of one substring replaced by another | 880 | |text.strip() | Return a copy of the string with the leading and trailing whitespace removed| 881 | |text.find(“python”) | Returns the first index value when the given substring is found| 882 | |Text.startswith(“”) | Return the value True if the string has the given substring at the beginning| 883 | 884 | Summary of this assignment: In this assignment, you have revisited String built-in functions through match the following. 885 | 886 | 887 | ### Assignment 41 888 | 889 | Objective: Given a computational problem, select the right set of data structures (lists and dictionary) and implement the solution to problem and test using a set of values in an IDE 890 | 891 | Problem Description: Consider the price list of various items in the Retail Store. Customer John wants to know the 892 | i) Price of Costliest item sold in Retail store 893 | ii) Average price of items in the Retail store 894 | iii) Display of item price list in increasing order 895 | 896 | Implement the solution using user-defined and built-in functions to accomplish above mentioned business requirement. 897 | 898 | Code: 899 | ``` 900 | item_price = [1050, 2200, 8575, 485, 234, 150, 399] 901 | def price_max(item_price): 902 | print("Price of Costliest item in the Retail Store: ",max(item_price)) 903 | 904 | def average_price(item_price): 905 | total_price = 0 906 | for i in item_price: 907 | total_price += i 908 | average_price = total_price/len(item_price) 909 | print("Average Price of items in the Retail Store: ",average_price) 910 | 911 | def display_sorted_price(item_price): 912 | item_price.sort() 913 | print("Item Price List in increasing order: ",item_price) 914 | 915 | price_max(item_price) 916 | average_price(item_price) 917 | display_sorted_price(item_price) 918 | ``` 919 | 920 | Output: 921 | ``` 922 | Price of Costliest item in the Retail Store: 8575 923 | Average Price of items in the Retail Store: 1870.4285714285713 924 | Item Price List in increasing order: [150, 234, 399, 485, 1050, 2200, 8575] 925 | ``` 926 | 927 | Summary: In this assignment, you have understood the application and implementation of Lists concept for the given computational problem. 928 | 929 | 930 | ### Assignment 42 931 | 932 | Objective: Given a computational problem, implement the solution to the given problem using exception handling. 933 | 934 | Problem Description: Consider the customer ids of a customer in the Retail Store. Customer id can be vary between 1001 to 1005. Store the customer id in a list and handle appropriate exceptions for the following: 935 | i) Store the customer id “1002” as string 936 | ii) Print customerid[5] 937 | 938 | 939 | 940 | ## Object Oriented Fundamentals- Assignments 941 | 942 | ### Assignment 43 943 | 944 | Objective: Given a business scenario, able to identify the classes and objects 945 | 946 | Problem Description: A supermarket wants to automate the system of purchase of items by customers and the billing process. The automation involves the maintenance of items, employees, customers, purchase of items by customer and billing of items. Customers can be regular visitors to the store in which case they are eligible for discounts based on the bill amount. The customers can also be privileged ones, wherein they are given membership cards (Platinum, Gold and Silver). Such customers are eligible for gifts based on the type of membership card. The billing staff does the billing and delivery of items to the customer. The bill calculation involves the logic of computation of the bill depending on customer type. The customer can pay the bill through credit card or cash. In the former case, two percent processing charge is applicable. Sales tax is also applicable on the final bill amount. Employees in that supermarket can be permanent and temporary. Permanent employees will get additional benefits in salary. 947 | 948 | Questions 949 | `` 950 | Identify the classes. 951 | Identify the attributes / behaviors associated with each class. 952 | `` 953 | 954 | Summary of this assignment: In this assignment, you have revisited object oriented concepts using scenarios 955 | 956 | 957 | ### Assignment 44 958 | 959 | Objective: Revisit object oriented concepts using a quiz 960 | 961 | Answer the following questions: 962 | ``` 963 | 1. In the ATM machine, the customer chooses the operations using a touch screen. The customer need not know the internal working of the ATM machine. Which OO concept(s) can be used in this scenario? 964 | 2. Consider the following statement: “Vehicles can be of two types viz. Water vehicles and Land vehicles “. Which OO concept may be used to represent this scenario? 965 | 3. As part of our family trip plan we went to Zoo. My son asked me lot of questions I tried my level best to answer all of his questions. I showed him different types of monkeys which were locked in different rooms. He asked me, “Dad, you said all are monkeys then why they are kept in different rooms?” Now, which OO concept may be used to represent this scenario? 966 | ``` 967 | 968 | Summary of this assignment: In this assignment, you have revisited object oriented concepts using scenarios 969 | 970 | 971 | ### Assignment 45 972 | 973 | Objective: Revisit object oriented concepts using a simple True/False questions 974 | 975 | Answer the following questions: 976 | ``` 977 | 1. Wrapping up of data of different types into a single unit is known as encapsulation. 978 | 2. Inheritance means the ability to reuse the data values of one object by other objects. 979 | 3. Polymorphism is extensively used in implementing inheritance. 980 | 4. Object-oriented systems can scale up better from small to large 981 | 5. Object-based languages do not support inheritance and dynamic binding. 982 | ``` 983 | 984 | Summary of this assignment: In this assignment, you have revisited object oriented concepts using simple true/false questions 985 | 986 | ### Assignment 46 987 | 988 | Objective: Given a business scenario, be able to identify the components of a use case diagram 989 | 990 | Problem Description: Refer to the course registration system case study and answer the following questions. 991 | 992 | Situation: A Course Registration System needs to be developed for an engineering college. The college wants an automated system to replace its manual system for the purpose of registration of students to branches and calculation of fees for each year. The engineering college provides graduation courses in various branches of engineering. 993 | The system will be used by the admin staff to register students admitted to the college to the branches at the time of joining the college and also to calculate the yearly fees for the students. The student has to register every year for the next academic year. The Admin takes care of the yearly registration of the students and the calculation of yearly fees. The system needs to be authenticated with a login id and password. 994 | Registration of a student to a branch is based on the qualifying exam marks and the entrance counseling. For every branch, a yearly branch fee is applicable. Discounts are given to the branch fees of the first year based on the qualifying exam marks. There is a registration fees also applicable to the first year students. Students can opt to be a day scholar or hostelite. Yearly bus fees are applicable for all the day scholars based on the distance of travel. Yearly hostel fees are applicable for all the hostelites. Yearly infrastructure fees and library fees are also applicable to all the students. Admin calculates the yearly college fees for each student and the college fees include all the fees specified earlier based on the type of student. Admin will provide a printed receipt of the fees to the students once the annual college fees have been paid. 995 | At the time of registration, student has to provide the permanent address and in case the student is opting to be a day scholar, he/she has to provide the residential address also. 996 | Assumption: 997 | 1. Decision of the branch of study a student is allocated, is not within the scope of this case study 998 | 999 | Questions: 1000 | ``` 1001 | 1. Identify all the classes of the course registration system 1002 | 2. Identify the attributes and behaviors of those classes as well. 1003 | 3. Draw the complete class diagram using the information collected. 1004 | ``` 1005 | 1006 | Summary of this assignment: In this assignment, you have learnt how to actors and activities of a use case diagram using a course registration system scenario 1007 | 1008 | 1009 | ### Assignment 47 1010 | 1011 | Objective: Given a class diagram for a use case representing a computational problem, be able to recognize the three compartments of the class diagram and implement it using an object oriented language (Python) using an IDE 1012 | 1013 | Problem Description: In the retail application, there are many customers who visit the retail outlet to purchase various items. The manager of the retail outlet now wants to keep track of all its customers’ data. Let us assume that customer details include Customer Id and Telephone Number. 1014 | 1015 | For the class diagram identified in OO Fundamentals, implement the class using Eclipse IDE and execute it by writing a starter class. 1016 | 1017 | Code: Execute the code using Eclipse IDE with the given inputs and understand the following: 1018 | ``` 1019 | Access Specifiers 1020 | Variables – Local and Instance variables 1021 | Methods 1022 | Starter class 1023 | Creation of objects 1024 | Reference variables 1025 | Compilation and Execution of a python program 1026 | ``` 1027 | 1028 | ``` 1029 | class Customer: 1030 | def setcustomerid(self, id1): 1031 | self.__customerid = id1 1032 | 1033 | def settelephoneno(self, teleno): 1034 | self.__telephoneno = teleno 1035 | 1036 | def getcustomerid(self): 1037 | return self.__customerid 1038 | 1039 | def gettelephoneno(self): 1040 | return self.__telephoneno 1041 | 1042 | custobj = Customer() 1043 | custobj.setcustomerid(1001,) 1044 | custobj.settelephoneno(9201861311) 1045 | print("Customer Id : ", custobj.getcustomerid()) 1046 | print("Telephone No : ", custobj.gettelephoneno()) 1047 | ``` 1048 | 1049 | Summary of this assignment: In this assignment, you have understood object oriented fundamentals - Access Specifiers, Variables – Local and Instance variables. 1050 | 1051 | 1052 | ### Assignment 48 1053 | 1054 | Objective: Given a class diagram for an use case representing a computational problem, use the ‘self’ reference to create and initialize instance variables of a class and test using a set of values in an IDE 1055 | 1056 | Problem Description: Let us revisit the class diagram drawn and implemented for Customer class as part of Object Oriented Fundamentals. 1057 | 1058 | Class diagram: 1059 | 1060 | |Customer| 1061 | |---| 1062 | |-customerid : int| 1063 | |-telephoneno : long| 1064 | |---| 1065 | |+setcustomerid(int) : void| 1066 | |+getcustomerid() : int| 1067 | |+settelephoneno(long) : void| 1068 | |+gettelephoneno() : long| 1069 | 1070 | Code: 1071 | Execute the code using Eclipse IDE with the given inputs and observe the results. 1072 | ``` 1073 | class Customer: 1074 | def setcustomerid(self, customerid): 1075 | customerid = customerid 1076 | 1077 | def settelephoneno(self, teleno): 1078 | telephoneno = teleno 1079 | 1080 | def getcustomerid(self): 1081 | return customerid 1082 | 1083 | def gettelephoneno(self): 1084 | return telephoneno 1085 | 1086 | 1087 | custobj = Customer() 1088 | custobj.setcustomerid(1001) 1089 | custobj.settelephoneno(9201861311) 1090 | print("Customer Id : ", custobj.getcustomerid()) 1091 | print("Telephone No : ", custobj.gettelephoneno()) 1092 | ``` 1093 | Note: Without ‘self’, it will be considered as normal local variable and that will be removed at the end of the scope of method. Hence, it is not possible to access it from outside of the method where it is defined. 1094 | 1095 | Revised Code 1096 | ``` 1097 | class Customer: 1098 | def setcustomerid(self, customerid): 1099 | self.customerid = customerid 1100 | 1101 | def settelephoneno(self, teleno): 1102 | self.telephoneno = teleno 1103 | 1104 | def getcustomerid(self): 1105 | return self.customerid 1106 | 1107 | def gettelephoneno(self): 1108 | return self.telephoneno 1109 | 1110 | 1111 | custobj = Customer() 1112 | custobj.setcustomerid(1001) 1113 | custobj.settelephoneno(9201861311) 1114 | print("Customer Id : ", custobj.getcustomerid()) 1115 | print("Telephone No : ", custobj.gettelephoneno()) 1116 | ``` 1117 | Note: “self” is not a keyword and has no special meaning in Python. We can use any name in that place. However, it is recommended not to use any name other than “self” (merely a convention and for readability) 1118 | 1119 | Summary of this assignment: In this assignment, you have learnt the usage of self reference for accessing the instance variables using a retail application scenario 1120 | 1121 | 1122 | ### Assignment 49 1123 | 1124 | Objective: Observe the need for features in the retail application scenario to correlate the application of initializing the members and ‘static’ members 1125 | 1126 | Problem Description: In the retail application, each time a customer is registered, customer id must be automatically generated starting from 1001 and the details of the customer must be initialized. Also, retail shop management wants to know how many customers have registered at a point of time. 1127 | 1128 | Answer the following question: 1129 | ``` 1130 | What do you think is needed to implement the solution for the above problem? 1131 | ``` 1132 | 1133 | Summary of this assignment: In this assignment, you have learnt 1134 | ``` 1135 | The need of __init__() method and static keyword in the retail application scenario 1136 | ``` 1137 | 1138 | 1139 | ### Assignment 50 1140 | 1141 | Objective: Given a class diagram for a use case representing a computational problem, initialize the data members using __init__() method and test using a set of values in an IDE 1142 | 1143 | Problem Description: In the retail application, as a customer is registered to the retail shop, the details of the customer must also be initialized. 1144 | 1145 | Class diagram: 1146 | 1147 | |Customer| 1148 | |---| 1149 | |-customerid : int| 1150 | |-telephoneno : long[]| 1151 | |-customername : String| 1152 | |---| 1153 | |+ setcustomerid(int) : void| 1154 | |+getcustomerid() : int| 1155 | |+settelephoneno(long[]) : void| 1156 | |+gettelephoneno() : long[]| 1157 | |+setcustomername(String) : void| 1158 | |+getcustomername() : String| 1159 | |+validatecustomername() : boolean| 1160 | 1161 | Code: 1162 | Execute the code using Eclipse IDE with the given inputs and observe the results. 1163 | ``` 1164 | class Customer: 1165 | def __init__(self, customerid, telephoneno, customername): 1166 | self.__customerid=customerid 1167 | self.__customername=customername 1168 | self.__telephoneno=telephoneno 1169 | 1170 | def setcustomerid(self, id): 1171 | self.__customerid = id 1172 | 1173 | def setcustomername(self, customername): 1174 | self.__customername = customername 1175 | 1176 | def settelephoneno(self, teleno): 1177 | self.__telephoneno = teleno 1178 | 1179 | def getcustomerid(self): 1180 | return self.__customerid 1181 | 1182 | def gettelephoneno(self): 1183 | return self.__telephoneno 1184 | 1185 | def getcustomername(self): 1186 | return self.__customername 1187 | 1188 | def validatecustomername(self): 1189 | if(len(self.__customername)>=3 and len(self.__customername) <=20): 1190 | return True 1191 | else: return False 1192 | 1193 | telephoneno=[9201861311, 9201861321, 9201661311] 1194 | custobj = Customer(1001, telephoneno, "Kevin") 1195 | if(custobj.validatecustomername()): 1196 | print("Customer Id : ", custobj.getcustomerid()) 1197 | temp = custobj.gettelephoneno() 1198 | print("Telephone Nos : ", temp[0], ",", temp[1], ",", temp[2]) 1199 | print("Customer Name : ", custobj.getcustomername()) 1200 | else: 1201 | print("Invalid customer name. Customer name must be between 3 and 20 characters") 1202 | ``` 1203 | 1204 | Points to keep in mind 1205 | ``` 1206 | 1. There is no default __init()__ method in Python as like we have default constructor in Java. Hence, we have to initialize some value to the variable if we want to use it in the program later. 1207 | 2. No…Values initialized for each customer must be different. It depends on each customer. This can be achieved using parameterized __init__() 1208 | ``` 1209 | 1210 | Summary of this assignment: In this assignment, you have understood the implementation of __init__() method and the need of parameterized __init__() using a retail application scenario. 1211 | 1212 | 1213 | ### Assignment 51 1214 | 1215 | Objective: Given a class diagram for a use case representing a computational problem, initialize the data members using default constructors, parameterized __init__() and test using a set of values in an IDE 1216 | 1217 | Problem Description: In the retail application, as a customer is registered to the retail shop, the details of the customer must also be initialized. 1218 | 1219 | The class diagram discussed in Object Oriented Fundamentals Assignment 51 has been modified as shown below. 1220 | 1221 | Class diagram: 1222 | 1223 | |Customer| 1224 | |---| 1225 | |-customerid : int| 1226 | |-telephoneno : long[]| 1227 | |-customername : String| 1228 | |---| 1229 | |+ Customer(int, long[], String)| 1230 | |+ setcustomerid(int) : void| 1231 | |+getcustomerid() : int| 1232 | |+settelephoneno(long[]) : void| 1233 | |+gettelephoneno() : long[]| 1234 | |+setcustomername(String) : void| 1235 | |+getcustomername() : String| 1236 | |+validatecustomername() : boolean| 1237 | 1238 | Code: 1239 | Execute the code using Eclipse IDE with the given inputs in the starter class, Retail and observe the results. 1240 | ``` 1241 | class Customer: 1242 | def __init__(self, customerid, telephoneno, customername): 1243 | self.__customerid=customerid 1244 | self.__customername=customername 1245 | self.__telephoneno=telephoneno 1246 | 1247 | def setcustomerid(self, id): 1248 | self.__customerid = id 1249 | 1250 | def setcustomername(self, customername): 1251 | self.__customername = customername 1252 | 1253 | def settelephoneno(self, teleno): 1254 | self.__telephoneno = teleno 1255 | 1256 | def getcustomerid(self): 1257 | return self.__customerid 1258 | 1259 | def gettelephoneno(self): 1260 | return self.__telephoneno 1261 | 1262 | def getcustomername(self): 1263 | return self.__customername 1264 | 1265 | def validatecustomername(self): 1266 | if(len(self.__customername)>=3 and len(self.__customername) <=20): 1267 | return True 1268 | 1269 | else: 1270 | return False 1271 | 1272 | telephoneno=[9201861311, 9201861321, 9201661311] 1273 | custobj = Customer(1001, telephoneno, "Kevin") 1274 | if(custobj.validatecustomername()): 1275 | print("Customer Id : ", custobj.getcustomerid()) 1276 | temp = custobj.gettelephoneno() 1277 | print("Telephone Nos : ", temp[0], ",", temp[1], ",", temp[2]) 1278 | print("Customer Name : ", custobj.getcustomername()) 1279 | else: 1280 | print("Invalid customer name. Customer name must be between 3 and 20 characters") 1281 | ``` 1282 | 1283 | Summary of this assignment: In this assignment, you have understood the implementation of parameterized __init__() method and the need of parameterized __init__() using a retail application scenario. 1284 | 1285 | 1286 | ### Assignment 52 1287 | 1288 | Objective: Given a class diagram for a use case representing a computational problem, use static/class variable and test using a set of values in an IDE 1289 | 1290 | Problem Description: In the retail application, each time a customer is registered, customer id must be automatically generated starting from 1001. Also, retail shop management wants to know how many customers have registered at a point of time. 1291 | 1292 | Note: Few instance variables are not shown in the class diagram so as to keep the code simple to understand. 1293 | 1294 | Class diagram: 1295 | 1296 | |Customer| 1297 | |---| 1298 | |-customerid : int| 1299 | |---| 1300 | |+Customer()| 1301 | |+setcustomerid(int) : void| 1302 | |+getcustomerid() : int| 1303 | |+totalnoofcustomers() : int| 1304 | 1305 | Code: 1306 | Execute the code using Eclipse IDE with the given inputs in the starter class, Retail and observe the results. 1307 | ``` 1308 | class Customer: 1309 | counter = 1000 1310 | def __init__(self): 1311 | Customer.counter = Customer.counter+1 1312 | self.__customerid=Customer.counter 1313 | 1314 | def setcustomerid(self, cid): 1315 | self.__customerid=cid 1316 | 1317 | def getcustomerid(self): 1318 | return self.__customerid 1319 | 1320 | @staticmethod 1321 | def totalcustomers(): 1322 | return Customer.counter-1000 1323 | 1324 | objcust = Customer() 1325 | print("Customer Id: ", objcust.getcustomerid()) 1326 | 1327 | objcust2 = Customer() 1328 | print("Customer Id: ", objcust2.getcustomerid()) 1329 | print("Total Customers: ", objcust.totalcustomers()) 1330 | print("Total Customers: ", objcust2.totalcustomers()) 1331 | print("Total Customers: ", Customer.totalcustomers()) 1332 | ``` 1333 | 1334 | Note: customerId is an instance variable of Customer class and it will be created separately for each and every object of Customer class. Hence each time an object of Customer class is created, constructor is called and customerId will be initialized to 1000 and will be incremented by 1 in the constructor as a result for all the objects customerId will remain as 1001. Hence we need to have a variable common to all the objects of Customer class 1335 | 1336 | Summary of this assignment: In this assignment, you have understood the implementation of instatnce methods and static methods. 1337 | 1338 | 1339 | ### Assignment 53 1340 | 1341 | Objective: Observe the need for features in the retail application scenario to correlate the application of method overloading 1342 | 1343 | Problem Description: The Retail Store has the requirement for printing many reports including the bill. All reports contain header. The header may be 1344 | ``` 1345 | a line containing a character printed 70 times or 1346 | a title of a report or 1347 | a line containing a character specified number of times 1348 | ``` 1349 | 1350 | Answer the following questions: 1351 | ``` 1352 | 1. Which object oriented concept do you think is needed to implement this scenario? 1353 | ``` 1354 | 1355 | Summary of this assignment: In this assignment, you have understood the need of method overloading using a retail application scenario 1356 | 1357 | 1358 | ### Assignment 54 1359 | 1360 | Objective: Given a class diagram for a use case representing a computational problem, use method and constructor overloading techniques to solve the problem and test using a set of values in an IDE 1361 | 1362 | Problem Description: The scenario discussed in OO Concepts Part I Assignment 1 is revisited here. The Retail Store has the requirement for printing many reports including the bill. All reports contain header. The header may be 1363 | ``` 1364 | a line containing a character printed 70 times or 1365 | a title of a report or 1366 | a line containing a character specified number of times 1367 | ``` 1368 | 1369 | A new class called PrintDetails with overloaded methods have to be created as shown below: 1370 | 1371 | |PrintDetails| 1372 | |---| 1373 | |+printHeader (char c): void| 1374 | |+printHeader (char c,int no): void| 1375 | |+printHeader (String c): void| 1376 | 1377 | Code: 1378 | Execute the code using Eclipse IDE with the given inputs in the starter class, Retail and observe the results. 1379 | ``` 1380 | class PrintDetails: 1381 | def printheader(self, c='*', no=1): 1382 | print(c * no) 1383 | 1384 | obj = PrintDetails() 1385 | obj.printheader('#', 10) 1386 | obj.printheader("Report") 1387 | obj.printheader('#' ,10) 1388 | obj.printheader() 1389 | ``` 1390 | 1391 | Summary of this assignment: In this assignment, you have learnt the need and implementation of method with default parameters using a retail application scenario. 1392 | 1393 | ### Assignment 55 1394 | 1395 | Objective: Observe the need for features in the retail application scenario to correlate the application of relationships concept 1396 | Problem Description: 1397 | 1. The retail shop has two types of customers, Regular and Privileged. 1398 | a. What are the properties common to all Customers? 1399 | b. What are the properties which are different for the two kinds of customers? 1400 | c. What is the relationship between 1401 | i. Customer and Regular customer 1402 | ii. Customer and Privileged customer? 1403 | d. How can this relationship be represented using class diagram? 1404 | 1405 | Note: Regular customers are entitled to get some discount on each purchase apart from having all the properties of a customer. On the other hand, privileged customers hold membership card – Platinum, Gold or Silver based on which they receive gifts on each purchase. This is in addition to having all the properties of a customer. Thus in this case, Customer is the generalized case and Regular and Privileged customer are the specialized cases of Customer 1406 | 1407 | 2. The retail store management wants to keep track of the address of every customer so as to allow for home delivery at a later point of time. 1408 | a. How many fields represent the address? 1409 | b. Do you think address qualifies to be a class? 1410 | c. What do you think is the relationship between Customer and Address? 1411 | d. How can this relationship be represented using class diagram? 1412 | 1413 | Note: Address can be considered as a separate class since the same class if required can be reused at a later point of time. For example, if retail store needs to keep track of its employees address, the same address class can be reused. The relationship between Customer class and Address class will be “Has 1414 | –A” relationship since every customer has an address. 1415 | 1416 | 3. Consider PurchaseBill and PrintDetails classes, the bill needs to be created in a particular format. PrintDetails class contains methods which can be used to display the bill in the required format. 1417 | a. What do you think is the relationship between PurchaseBill and PrintDetails classes? 1418 | b. How can this relations 1419 | 1420 | Note: As stated, PrintDetails class contains methods which can be used to display the bill in the required format. Hence the relationship between PurchaseBill and PrintDetails class is “uses-a” relationship 1421 | 1422 | Summary of this assignment: In this assignment, you have understood the need of relationships and its representation using a retail application scenario 1423 | 1424 | 1425 | ### Assignment 56 1426 | 1427 | Objective: Given a class diagram for a use case representing a computational problem, implement inheritance (single level, multilevel and hierarchical) to solve the problem, trace the flow of the program between the base and derived classes and test using a set of values in an IDE and recognize the benefits of inheritance 1428 | 1429 | Problem Description: In the retail application, we have already seen that customers are of two types – Regular and Privileged. Regular customers are entitled for discount on each purchase and Privileged customers hold membership cards – Platinum, Gold or Silver based on which they are entitled to get gifts on each purchase. 1430 | 1431 | The class diagram presented to you as part of OO Concepts Part I Assignment 5 has been modified to represent the inheritance relationship. 1432 | 1433 | Note: Few instance variables and methods are not shown in the class diagram so as to keep the code simple to understand. 1434 | 1435 | Code: 1436 | Execute the code using Eclipse IDE with the given inputs in the starter class, Retail and observe the results. 1437 | ``` 1438 | class Customer: 1439 | def __init__(self, id1=0, name=None): 1440 | self.__customerid=id1 1441 | self.__customername=name 1442 | 1443 | def setcustomerid(self, id1): 1444 | self.__customerid = id1 1445 | 1446 | def setcustomername(self, name): 1447 | self.__customername=name 1448 | 1449 | def getcustomerid(self): 1450 | return self.__customerid 1451 | 1452 | def getcustomername(self): 1453 | return self.__customername 1454 | 1455 | class RegularCustomer(Customer): 1456 | def __init__(self, id1=0, name=None, dis=0): 1457 | super().__init__(id1,name) 1458 | self.__discount=dis 1459 | 1460 | def setdiscount(self, dis): 1461 | self.__discount=dis 1462 | 1463 | def getdiscount(self): 1464 | return self.__discount 1465 | 1466 | class PrivilegedCustomer(Customer): 1467 | def __init__(self, id1=0, name=None, card=None): 1468 | super().__init__(id1,name) 1469 | self.__memcardtype=card 1470 | 1471 | def setmemcardtype(self, card): 1472 | self.__memcardtype=card 1473 | 1474 | def getmemcardtype(self): 1475 | return self.__memcardtype 1476 | 1477 | objr = RegularCustomer() 1478 | print("Regular Customer Details") 1479 | print("Customer Id: ", objr.getcustomerid()) 1480 | print("Customer Name: ", objr.getcustomername()) 1481 | print("Discount Eligible: ", objr.getdiscount()) 1482 | print("*********") 1483 | objp = PrivilegedCustomer() 1484 | print("Regular Customer Details") 1485 | print("Customer Id: ", objp.getcustomerid()) 1486 | print("Customer Name: ", objp.getcustomername()) 1487 | print("Discount Eligible: ", objp.getmemcardtype()) 1488 | print("*********") 1489 | objr = RegularCustomer() 1490 | objr.setcustomerid(1001) 1491 | objr.setcustomername('Ram') 1492 | objr.setdiscount(10.0) 1493 | print("Regular Customer Details") 1494 | print("Customer Id: ", objr.getcustomerid()) 1495 | print("Customer Name: ", objr.getcustomername()) 1496 | print("Discount Eligible: ", 1497 | objr.getdiscount()) 1498 | print("*********") 1499 | objp = PrivilegedCustomer() 1500 | objp.setcustomerid(1002) 1501 | objp.setcustomername("Seetha") 1502 | objp.setmemcardtype("Gold") 1503 | print("Regular Customer Details") 1504 | print("Customer Id: ", objp.getcustomerid()) 1505 | print("Customer Name: ", objp.getcustomername()) 1506 | print("Discount Eligible: ", objp.getmemcardtype()) 1507 | ``` 1508 | 1509 | Note: Instance variables can be initialized at object creation using __init__() 1510 | 1511 | Summary of this assignment: In this assignment, you have understood implementation of inheritance relationship and use of super() invocation to invoke parent class __init__() using a retail application scenario. 1512 | 1513 | 1514 | ### Assignment 57 1515 | 1516 | Objective: Revisit inheritance concepts through a quiz 1517 | Q1. What is the output of the following code snippet? 1518 | ``` 1519 | class Base: 1520 | def __init__(self): 1521 | print("Parent init invoked") 1522 | 1523 | class Derived(Base): 1524 | def __init__(self): 1525 | print("Derived init invoked") 1526 | 1527 | obj = Derived() 1528 | ``` 1529 | 1530 | Q2. What is the output of the following code snippet? 1531 | ``` 1532 | class Base: 1533 | def __init__(self, v): 1534 | self.baseVar = v 1535 | self.var=0 1536 | print("Base Class init invoked") 1537 | 1538 | class Der(Base): 1539 | def __init__(self, v): 1540 | super().__init__(v) 1541 | self.derVar=v 1542 | self.var=0 1543 | print("Derived class init invoked") 1544 | 1545 | def display(self): 1546 | print("Base variable value: ", self.baseVar) 1547 | print("Derived variable value: ", self.derVar) 1548 | 1549 | def useOfSuper(self): 1550 | self.var=15 1551 | print("Base Variable Value -> ", Base.var) 1552 | print("Derived Variable Value -> ", self.var) 1553 | 1554 | d = Der(10) 1555 | d.display() 1556 | d.useOfSuper() 1557 | ``` 1558 | 1559 | Q3. What is the output of the following code snippet? 1560 | ``` 1561 | class Base: 1562 | def __init__(self, v): 1563 | self.baseVar = v 1564 | print("Base Class init invoked") 1565 | 1566 | class Der(Base): 1567 | def __init__(self, v): 1568 | self.derVar=v 1569 | print("Derived class init invoked") 1570 | 1571 | def display(self): 1572 | print("Base variable value: ", self.baseVar) 1573 | print("Derived variable value: ", self.derVar) 1574 | 1575 | d = Der(10) 1576 | d.display() 1577 | ``` 1578 | 1579 | Q4. What is the output of the following code snippet? 1580 | ``` 1581 | class Base: 1582 | def __init__(self, v): 1583 | self.baseVar = v 1584 | print("Base Class init invoked") 1585 | 1586 | class Der(Base): 1587 | def __init__(self, v): 1588 | self.derVar=v 1589 | print("Derived class init invoked") 1590 | 1591 | def display(self): 1592 | print("Base variable value: ", self.baseVar) 1593 | print("Derived variable value: ", self.derVar) 1594 | 1595 | d = Der(10) 1596 | d.display() 1597 | ``` 1598 | 1599 | Q5. Say true or false 1600 | a) Multilevel inheritance is not supported in Python 1601 | b) Inheritance can be done with the help of passing the base class to derived class in Python 1602 | c) In derived class __init__() method, ‘super’ should be the first statement 1603 | d) All the private data members of the base class are directly accessible in the derived class 1604 | 1605 | Summary of this assignment: In this assignment, you have revisited concepts of inheritance through code snippets 1606 | 1607 | 1608 | ### Assignment 58 1609 | 1610 | Objective: Given a class diagram for a use case representing a computational problem, implement has-a and uses-a relationships to solve the problem and test using a set of values in an IDE and recognize the benefits of the mentioned relationships 1611 | 1612 | Problem Description: In the retail application, we have already seen that every customer has an address. Address consists of address line, city, state and zipcode. 1613 | 1614 | The class diagram presented to you as part of aggregation (has-a) example in OO Concepts Part I Assignment 7 has been modified. 1615 | 1616 | Note: Few instance variables and methods are not shown in the class diagram so as to keep the code simple to understand. Similarly, regular and privileged customer classes are also not shown here. 1617 | 1618 | Code: 1619 | Execute the code using Eclipse IDE with the given inputs in the starter class, Retail and observe the results. 1620 | ``` 1621 | class Address: 1622 | def __init__(self, addressline, city, state, zip1): 1623 | self.__addressline = addressline 1624 | self.__city=city 1625 | self.__state=state 1626 | self.__zip=zip1 1627 | 1628 | def setaddressline(self, address): 1629 | self.__addressLine = address 1630 | 1631 | def getaddressline(self): 1632 | return self.__addressline 1633 | 1634 | def setcity(self, city): 1635 | self.__city = city 1636 | 1637 | def getcity(self): 1638 | return self.__city 1639 | 1640 | def setstate(self, state): 1641 | self.__state = state 1642 | 1643 | def getstate(self): 1644 | return self.__state 1645 | 1646 | def setzip(self, zip1): 1647 | self.__zip = zip1 1648 | 1649 | def getzip(self): 1650 | return self.__zip 1651 | 1652 | class Customer: 1653 | def __init__(self, cid, address): 1654 | self.__customerid = cid 1655 | self.__address = address 1656 | 1657 | def getcustomerid(self): 1658 | return self.__customerid 1659 | 1660 | def getaddress(self): 1661 | return self.__address 1662 | 1663 | objaddress = Address("No.333,Oak street", "Strathfield", "New South Wales", "570018") 1664 | objcust = Customer(1001, objaddress) 1665 | print("Customer Id:", objcust.getcustomerid()) 1666 | address = objcust.getaddress() 1667 | print("Customer Address: ", address.getaddressline(), ",", address.getcity(), ",", address.getstate(), ",", address.getzip()) 1668 | ``` 1669 | 1670 | Note: In this example, Address reference is passed to the ```__init()``` of Customer class. Since it is a reference of an object that is passed, the parameter passing technique used is pass by referene. 1671 | 1672 | Summary of this assignment: In this assignment, you have understood the implementation of aggregation relationship using a retail application scenario. 1673 | 1674 | 1675 | ### Assignment 59 1676 | 1677 | Objective: Given a class diagram for a use case representing a computational problem, implement has-a and uses-a relationships to solve the problem and test using a set of values in an IDE and recognize the benefits of the mentioned relationships 1678 | 1679 | Problem Description: In the retail application, we have already seen that purchase bill has to be created in a particular format. 1680 | 1681 | The class diagram presented to you as part of association (uses-a) example in OO Concepts Part I Assignment 7 has been modified to include instance variables and methods 1682 | 1683 | Note: Few instance variables and methods are not shown in the class diagram so as to keep the code simple to understand. 1684 | 1685 | Code: 1686 | Execute the code using Eclipse IDE with the given inputs in the starter class, Retail and observe the results. Few changes are made to PurchaseBill class, the changes are highlighted in the code below: 1687 | ``` 1688 | class PrintDetails: 1689 | def printheader(self, c, no=1): 1690 | print(c*no) 1691 | 1692 | class PurchaseBill: 1693 | def __init__(self, bid, billamount): 1694 | self.__billid = bid 1695 | self.__billamount = billamount 1696 | 1697 | def getbillid(self): 1698 | return self.__billid 1699 | 1700 | def getbillamount(self): 1701 | return self.__billamount 1702 | 1703 | def calculatebill(self, mode, processcharge): 1704 | if(mode=="Credit"): 1705 | self.__billamount = self.__billamount + (self.__billamount * processcharge/100) 1706 | 1707 | def displaybill(self): 1708 | objprint = PrintDetails() 1709 | objprint.printheader("-", 80) 1710 | objprint.printheader(" Easy Shop Retail Store Bill ") 1711 | objprint.printheader("-", 80) 1712 | print("Bill Id: ", self.__billid) 1713 | print("Final amount to be paid: Rs.", self.__billamount) 1714 | objprint.printheader("-", 80) 1715 | objprint.printheader(" Thank You!!! ") 1716 | objprint.printheader("-", 80) 1717 | 1718 | objpur = PurchaseBill(101, 1055.0) 1719 | objpur.calculatebill("Credit", 10.5) 1720 | objpur.displaybill() 1721 | ``` 1722 | 1723 | Summary of this assignment: In this assignment, you have understood the implementation of association relationship using a retail application scenario. 1724 | 1725 | 1726 | ### Assignment 60 1727 | 1728 | #### Sample code using OO concept 1729 | 1730 | ``` 1731 | class customer: 1732 | counter = 1000 #class variable 1733 | 1734 | def __init__(self, telephoneno, customername, add): 1735 | customer.counter += 1 1736 | self.__customerid=customer.counter 1737 | self.__customername=customername 1738 | self.__telephoneno=telephoneno 1739 | self.__address = add 1740 | 1741 | def setcustomerid(self, cid): 1742 | self.__customerid = cid 1743 | 1744 | def settelephoneno(self, teleno): 1745 | self.__telephoneno = teleno 1746 | 1747 | def getcustomerid(self): 1748 | return self.__customerid 1749 | 1750 | def gettelephoneno(self): 1751 | return self.__telephoneno 1752 | 1753 | def getcustomername(self): 1754 | return self.__customername 1755 | 1756 | def getaddress(self): 1757 | return self.__address 1758 | 1759 | @staticmethod 1760 | def gettotalcustomer(): 1761 | return customer.counter-1000 1762 | 1763 | class regularcustomer(customer): 1764 | def __init__(self, telephoneno, customername, discount, add): 1765 | # super to invoke baseclass init 1766 | super().__init__(telephoneno, customername, add) 1767 | self.__discount = discount 1768 | 1769 | def setdiscount(self, dis): 1770 | self.__discount = dis 1771 | 1772 | def getdiscount(self): 1773 | return self.__discount 1774 | 1775 | class address: 1776 | def __init__(self, add): 1777 | self.__addressline = add 1778 | 1779 | def setaddress(self, add): 1780 | self.__addressline = add 1781 | 1782 | def getaddress(self): 1783 | return self.__addressline 1784 | 1785 | regcustadd1 = address("No.22,Vijay Nagar Mysore Karnataka 570018") 1786 | teleno=[9201861311, 9201861321, 9201661311] 1787 | regcustobj1 = regularcustomer(teleno, "John", 12.5, regcustadd1) 1788 | #custobj1.setcustomerid(1001) 1789 | #custobj1.settelephoneno(1234567890) 1790 | print("Customer id:", regcustobj1.getcustomerid()) 1791 | print("Telephone no:", regcustobj1.gettelephoneno()) 1792 | print("Customer name:", regcustobj1.getcustomername()) 1793 | print("Discount:", regcustobj1.getdiscount()) 1794 | #temp1 = regcustobj1.getaddress().getaddress() 1795 | print("Customer's address:", regcustobj1.getaddress().getaddress()) 1796 | print("\n") 1797 | regcustadd2 = address("No.33,J.P. Nagar Bangalore Karnataka 570011") 1798 | teleno1 = [1122334455, 1199887766, 2244668897] 1799 | regcustobj2 = regularcustomer(teleno1, "Mary", 15.5,regcustadd2) 1800 | print("Customer id:", regcustobj2.getcustomerid()) 1801 | print("Telephone no:", regcustobj2.gettelephoneno()) 1802 | print("Customer name:", regcustobj2.getcustomername()) 1803 | print("Discount:", regcustobj2.getdiscount()) 1804 | print("Customer's address:", regcustobj2.getaddress().getaddress()) 1805 | print("\n") 1806 | print("Total customers registered:", customer.gettotalcustomer()) 1807 | ``` 1808 | --------------------------------------------------------------------------------