Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

Python 4.3.1.8 LAB: Day of the year: writing and using your own functions

 Day of the year: writing and using your own functions

4.3.1.8 LAB
Day of the year: writing and using your own functions

Prerequisites

LAB 4.3.1.6

LAB 4.3.1.7

Objectives

Familiarize the student with:

  • projecting and writing parameterized functions;
  • utilizing the return statement;
  • building a set of utility functions;
  • utilizing the student's own functions.

Scenario

Your task is to write and test a function which takes three arguments (a year, a month, and a day of the month) and returns the corresponding day of the year, or returns None if any of the arguments is invalid.

Use the previously written and tested functions. Add some test cases to the code. This test is only a beginning.


Stoooooooop .. we are here to real the REAL PYTHON, not this SILLY COURSE

Dear learner  .. if you reached here, that means you'd have had enough of this boring course already😏. Let's learn python the right way, not the old school way.

Solution Code:

def is_leap_year(year): # LAB 4.3.1.6 A leap year
    if year % 4 != 0:
        return False
    elif year % 100 != 0:
        return True
    elif year % 400 != 0:
        return False
    else:
        return True


def days_in_month(year, month): #LAB 4.3.1.7 How many days
    if year < 1 or month < 1 or month > 12:
        return None
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    res  = days[month - 1]
    if month == 2 and is_leap_year(year):
        res = 29
    return res


def day_of_year(year, month, day): #LAB 4.3.1.8 Day of the year
    days = 0
    for m in range(1, month):
        md = days_in_month(year, m)
        if md == None:
            return None
        days += md
    md = days_in_month(year, month)
    if day >= 1 and day <= md:
        return days + day
    else:
        return None

print(is_leap_year(1900))
print(days_in_month(2021, 11))
print(day_of_year(2000,2,29))

Output:

  • False
  • 30
  • 60

=================================================================================

Follow the Python page on the Blog to be the first to know






Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst