Posts

datetime

Refer to https://docs.python.org/3/library/datetime.html Use the datetime module to write a program that gets the current date and prints the day of the week . datetime. now datetime. weekday ( ) : Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The same as   self.date().weekday() .   from datetime import datetime as dt ct = dt.now() print("Week day", ct.weekday()) Write a program that takes a birthday as input and prints the user’s age and the number of days, hours, minutes and seconds until their next birthday. use the timedelta object that is created with the difference. from datetime import date ct = dt.now() print("Birthday is on 9th May 2020") bday_this_year=dt(ct.year,5,9) if bday_this_year > ct:    tb=bday_this_year - ct print(tb, "to birthday"))
Write a python program to counting digits in number with floor division Sample Test Case: Enter a number: 12345 5 Test case 1 :  Enter a number: 58947856525 11 Test case 2: Enter a number: 50 2 Test case 3 :  Enter a number: -589 3 Write a python program to calculate the sum of digits of a number. Enter a number:12345 15

Summation of numbers in a sequence

 Write a python program to sum of N natural numbers [even,odd,divisible by n] So, the immediate way to solve this is by putting it in a loop, use flow control to weed out the unnecessary values. You can also use range to generate the progression. But using formula for summation of numbers in a sequence,summation for even large values of N can be done efficiently. What is a natural number? 0 is usually considered a whole number and excluded from inclusion in the natural numbers. So numbers 1,2,3,... are considered natural numbers. Program using a loop # summation of N natural numbers N = 25 sum=0 for num in range(N+1):     sum+=num print("Sum of first",N,"natural numbers is",sum) Output python tyl_naturalNumbers.py Sum of first 25 natural numbers is 325 Using Arithmetic progression https://www.math-only-math.com/arithmetic-progression.html Definition of Arithmetic Progression: A sequence of numbers is known as an arithmetic progression (A.P.) if the difference of th...

TYL - Food Corner Program

Food Corner Program. We will make this program more robust after you learn try-catch.  For now, we will calculate for only 1 order.  Again, later you can use loops to calculate for many orders.  As we have not learnt any data structures as lists, dictionaries etc, we will not use them now. FoodCorner home delivers vegetarian and non-vegetarian combos to its customer based on order. A vegetarian combo costs Rs.120 per plate and a non-vegetarian combo costs Rs.150 per plate. Their non-veg combo is really famous that they get more orders for their non-vegetarian combo than the vegetarian combo. Apart from the cost per plate of food, customers are also charged for home delivery based on the distance in kms from the restaurant to the delivery point. The delivery charges are as mentioned below: Distance in kms          Delivery charge in Rs. Per kms For first 3 kms                       ...

TYL - Salary Hike - Python Problem

Salary Hike Problem using Python We are not going to use any complex data structures in Python here. You will need to know the if else construct.  After we learn try... catch we will make this program more robust. if  condition:      statements      .... elif condition:      statements     ... ... else:     statement An organization has decided to provide salary hike to its employees based on their job level. Employees can be in job levels 3, 4 or 5. Hike percentage based on job levels are given below: Job level         Hike Percentage (applicable on current salary) 3                        15 4                         7 5                         5 In case of invalid job level, consider hike percentage to be ...

Classes and objects solution

Image
Question 1 Write a definition for Student class Define __init__(self, marks=[]) for the Student class Define add_marks(self,lst) that appends mark to the marks list Create two students, s1 and s2. Add [25,45,65] to s1 Print marks of s1 and s2 Observe what happens? Fix the code. Initial Implementation class Student:     def __init__(self, marks=[]):         self.marks = marks     def add_marks(self,lst):         self.marks.extend(lst) s1= Student() s2=Student() s1.add_marks([25,45,65]) print('Contents of s1',s1.marks) print('Contents of s2',s2.marks) Output: Contents of s1 [25, 45, 65] Contents of s2 [25, 45, 65] Observation: list is getting added to s1, but s2 also shows the same contents.  What is expected is an empty list for s2 but s1 consisting of [25,45,65] Why this happens? https://stackoverflow.com/questions/4841782/python-constructor-and-default-value Solution: def __ini...

TYL Arrays

Question 1 ►Create an array of 5 integers [2,3,3,3,4]. Access the first individual item through indexes array(data type, value list) ►Append a new item, 8 to the end of the array. ►Reverse the items in the array arrays are mutable so we can use either obj.reverse() or obj.sort(reverse=True) ►Output the length in bytes of one array item in the internal representation itemsize  ►Output the current memory address and the length in elements of the buffer used to hold an array. arrObject.buffer_info() ►Find number of occurrences of 3 in the array. arrObject.count(item) returns the number of occurrences in the array ►Append the iterable lst = [8,6,48] to the array ►Create another integer array with values [14,15,16] and append to array array.append(x) : adds a new item with value x to the end of the array array.extend(iterable) : to add more than 1 element to the end of the array.   -- If iterable is another array, it should have have the exact same type cod...