Coding Exercise for Beginners in Python with solution | Exercise 25 | Python for Beginners #lec75
Summary
TLDRIn this Python programming tutorial, viewers learn how to create a function to determine the number of days in a month, considering leap years. The instructor guides through coding a 'days_in_month' function that accepts year and month as inputs. The function checks if the year is a leap year and returns 29 days for February in that case. Otherwise, it uses a list to return the number of days based on the month. The video also touches on the concept of return versus print in functions, promising a deeper dive in the next episode.
Takeaways
- 💻 The video is part of a series on learning Python programming language, specifically focusing on functions.
- 📚 The previous video discussed how to return multiple values from a function and use multiple return statements.
- 📝 The coding exercise is to create a function that determines the number of days in a given month, considering leap years.
- 🗓️ For non-leap years, February has 28 days, and for leap years, it has 29 days.
- 🔢 Users input a year and a month number (1 for January, 2 for February, etc.) to get the number of days in that month.
- 🔄 The function 'days_in_month' checks if the year is a leap year and returns the correct number of days based on the month.
- 📑 A separate function 'leap_year' is used to determine if a given year is a leap year based on divisibility rules.
- 🔄 The 'leap_year' function returns 'True' if the year is a leap year and 'False' otherwise.
- 💡 The script emphasizes the importance of understanding the difference between using 'print' and 'return' in functions.
- 👨🏫 The video encourages viewers to practice the logic on paper before coding to solidify their understanding.
Q & A
What is the main topic discussed in the video script?
-The main topic discussed in the video script is learning how to create a Python function to determine the number of days in a given month, taking into account whether the year is a leap year or not.
What is the purpose of the coding exercise mentioned in the script?
-The purpose of the coding exercise is to practice creating a Python function that returns the correct number of days in a month based on the input year and month, with special consideration for leap years.
What is the twist in the coding exercise that the script refers to?
-The twist in the coding exercise is the need to account for leap years, where February has 29 days instead of the usual 28.
How does the script suggest to represent months when coding?
-The script suggests representing months numerically, where 1 means January, 2 means February, and so on.
What is the condition to check if a year is a leap year according to the script?
-A year is considered a leap year if it is divisible by 4, but years divisible by 100 are leap years only if they are also divisible by 400.
What is the significance of the 'days_list' in the script?
-The 'days_list' is a list in the script that contains the number of days in each month, used to return the correct number of days based on the month input.
Why is 'month - 1' used in the script when accessing the 'days_list'?
-The 'month - 1' is used because list indices start at 0 in Python, so to align the month number with the correct index in the list, 1 must be subtracted from the month number.
What is the difference between 'print' and 'return' in the context of the script?
-In the context of the script, 'print' is used to output the result directly to the console, while 'return' is used to return a value from a function, which can then be used or displayed elsewhere in the program.
What is the role of the 'leap_year' function in the script?
-The 'leap_year' function in the script is used to determine if the given year is a leap year. It returns 'True' if it is a leap year and 'False' otherwise.
What is the next topic the script mentions will be discussed in the next video?
-The next topic to be discussed in the next video is the difference between 'print' and 'return' in Python functions.
Outlines
🐍 Introduction to Python Functions for Days in a Month
This paragraph introduces a coding exercise in a Python programming tutorial series. The focus is on creating a function to determine the number of days in a given month, taking into account leap years for February. The exercise involves prompting the user to input a year and a month, then using a function to return the correct number of days in that month. For non-leap years, February has 28 days, but in a leap year, it has 29. The video provides a link to a previous tutorial on how to determine if a year is a leap year. The exercise is meant to be a practical application of functions in Python, specifically focusing on return statements and handling multiple return values.
📝 Implementing the Days in Month Function
The paragraph delves into the implementation of the 'days in month' function in Python. It explains the process of defining the function, taking 'year' and 'month' as parameters. The function checks if the year is a leap year and if the month is February; if both conditions are true, it returns 29 days. Otherwise, it uses a list of days corresponding to each month to return the correct number of days. The list is accessed using the month index, which is adjusted by subtracting one from the user input to account for zero-based indexing in Python. The paragraph also discusses the logic for determining leap years, which is incorporated into the function. The tutorial suggests copying the leap year logic into a separate function called 'leap_year' that returns a boolean value. The function's return value is then used within the 'days_in_month' function to decide the output.
🔍 Testing the Function and Upcoming Topics
The final paragraph discusses testing the 'days in month' function with various inputs, including leap years and different months. It provides examples of running the function with the year 2023 for February, which returns 28 days, and with 2024, which is a leap year, resulting in 29 days for February. The paragraph emphasizes the importance of understanding the logic and encourages learners to write the logic on paper to better grasp the concepts. It also hints at future tutorial topics, specifically the difference between the 'print' and 'return' statements in functions, which will be covered in the next video. The tutorial concludes with a reminder to practice and a thank you note to the viewers.
Mindmap
Keywords
💡Python programming language
💡functions
💡multiple return values
💡leap year
💡conditional statements
💡input function
💡list
💡indexing
💡return statement
💡logic
Highlights
Introduction to a series on learning Python programming language, focusing on functions.
Explanation of returning multiple values from a function and using multiple return statements.
Coding exercise to find the number of days in a month, with a twist for leap years.
Details on how to handle February days in a leap year, with 29 days instead of 28.
Instructions on how to input the year and month for the function.
Creation of a function named 'days_in_month' to determine the number of days.
Use of a list to store the number of days in each month.
Conditional logic to check if the year is a leap year before determining February's days.
Defining a separate function to check for leap years.
Logic to determine if a year is divisible by 4, 100, or 400 to ascertain leap year status.
Return values from functions instead of printing, to be used in other parts of the program.
Explanation of how to use the 'leap_year' function within the 'days_in_month' function.
Demonstration of the function with an example year and month.
Running the program and getting the correct number of days for February in a leap year.
Providing a link to a video explaining how to check for leap years in more detail.
Encouragement for viewers to practice the exercise and understand the logic.
Teaser for the next video, which will discuss the difference between print and return in functions.
Transcripts
hey everyone I hope you are safe and
doing good so in the series of learning
Python programming language we are
discussing functions in Python in the
previous video we have seen like how to
return multiple values from a function
and how to use multiple return statement
within one function right so now we'll
be doing one coding exercise simple one
and that is you have to find out number
of days in a month
suppose I enter like January then it
will tell number of days are 31 February
28 like this like December 31. okay but
here is one twist if it is a leap year
in that case in February we are having
29 days
so that also you have to apply that
logic if the year is Leap Year then
suppose
to think usable letter first is enter a
year suppose I am entering here two zero
two three
enter a month
month we will be entering not to January
February like one two three four like
this one means January 2 means February
like this so I'm entering like two
so two means February in February in 23
number of days 28 like this
but if it's two zero two four that would
be a leap year so it will give 29.
right so you have to apply to logic one
is that first need to check the air is
Leap Year or not if the air is Leap Year
and the month is February the month is
two then
it will return 29 right we are basically
doing this exercise with the help of
functions
okay otherwise according to you know
January February and like this it will
give number of days 31 28 30 and like
this right so one thing we have
discussed how to check a year is Leap
Year or not
the link of that video I'll put in the
side button you can check out that video
first
in the that video we have clearly you
know defined clearly you know defined
you can say or discuss the flowchart as
well as
how to
have to find uh how to find out if a
year is a leap year or not right with
the elbow program also I have shown
so you can check out that video first
then come to this video
okay I hope you heard what is the
problem statement what you need to do
right now pause the video and try this
out okay so I hope you have done this
exercise now let's do it together let's
create a new file and like I'm naming it
days in month Dot py
see this is a one according to size in
which we are checking we were checking
like the hair is Leap Year or not so
this logic we are going to use in a this
program also so two input you need to
take from user first is year and second
is month using input function we will be
taking this input
now we're defining a function suppose
the name of function I'm taking like uh
days in
month because we are checking number of
days in a month so what you will pass to
argument obviously here and month
okay so this is what we are calling this
function so before calling obviously you
have to Define this function so how to
define in depth
same name you need to take
days in month and two parameters so two
variable you need to take either you can
take same like yeah same name year and
month or you can take different name
that is also fine
like y m or any other thing okay
now you need to check see what will put
there's one condition if here is Leap
Year and the month is February month
equal to equal to two in that case it
should print 29 otherwise
it will print according to like 31 28 30
like this right so we have we have a
list
like days list I'm taking a name days
list equal to
for January it's 31 days then at second
index like February 28 like this
so this list we are having
so like at zeroth index for month first
January you will access 31 then 28 31
like this from this we can access days
in a month according to whatever day you
provide
but here one condition you need to pick
like this if
like I'm taking year is
leap year
one is this thing
obviously it is not a proper condition
we will write down that condition but
this thing you need to put right
or let's write down its only leap year
and and month should be
month equal to equal to 2 means February
means two January 1 February two like
this we are mapping the months
then
we are not printing
we are returning then it this function
should return 29.
else
you can use else or
simply without using else also we can
write down that that condition
that thing else what it will return
from this list we will be accessing
number of days okay
so provide the list name days list
and what index we will provide
whatever month
you will enter
but that is not correct because month if
I am entering month is 1.
means it should fetch this value 31 but
this value at least is from at zeroth
index because index is done from 0 0 1 2
3 4 like this not one
so we should give here month minus one
then it will give you correct value if
month I am providing 1 then 1 minus 1 is
0. so days list and index is 0 at 0
index we are having 31 so it will return
31. I hope you got this right but how to
check
if it is a leap year or not
this logic we are having leap year logic
right so let's
copy paste this thing
and before this function suppose
this is a logic right for finding leap
year
so what we can do we can just Define a
function here
for checking leap year depth
and what we will pass in parameter only
here
right and this logic we can put
within this
so we have to put this thing in
indentation as an indented block within
this function right
so if the ear is Leap Year like if it is
Mode 4 it is divided by 4 completely
then we will check if it's divisible by
100 if true then we will check if it
divided by 400 then only it will be a
leap year right else not a leap year but
if a number is divisible by 4 but not
divisible by hundred
then it is a leap year and if a number
is not divisible by 4 then it is not a
leap year so rather than printing leap
year and not leap year we are just
returning true and false okay so
whatever whatever this function will
return
that value will
accept here so means in this in this
condition we are calling this function
leap year so if
Leap Year and have to call obviously
parameter we have to pass here
right
and month equal to equal to 2 so if
it will return to so rather than leap
year what you need to
do here just write down
true
and if it is not a leap year then it
should return
false
here same return true
and here return false
right
so whatever it will return we will face
we will accept here so if it is
returning true means it is a leap year
true and month equal to equal to 2 means
true and true then it will return 29 if
it will return false like it is not a
leap year then obviously you will not
check this condition because here it is
and
to be
you know to make this condition true
both should be true but if air is not a
leap year then it will return false
means false means we will not enter into
this if part and else it will return
days list month minus one Whatever month
you will enter right so just because it
is returning we are not printing here so
we have to accept that thing in a
variable like let's suppose days and
simply we will print
that variable days
okay let's run this and see what output
you will get here
definitely roads and invalid syntax
where we have
C
this should be here
now let's run this
entire year here is 2 0 2 3.
and month is supposed to means February
it's 28 let's run this again and let's
enter a leap year
so 2024 would be a leap year
okay
month is 2 it should be in 29. see it is
returning 29 right or any other we can
also enter like f 2 0 to 2.
and it's 5 means may may is 31 so it
will return 31.
okay I hope you got the logic it's not
so tough if you are not getting this
it's okay you can take a break you can
just write down this thing the complete
logic
a piece of paper and dry on this
manually rather than on your laptop one
by one like first this line then this
line then this will happen then calling
of the function like this and like this
then definitely we'll get it so see in
if
statement we can
write down any expression like any
condition like this or we can call any
function in like this right so here we
are not printing the values we are
returning values so what is the
difference between print and return
when we should print the values when we
should use print when we should you know
use return in a function right so this
thing will be discussing in next video
right so I hope you have got this for
exercise now in the next video we'll see
what is the difference between print and
return
these are two different concept right so
now it's in the next video till then
thank you
تصفح المزيد من مقاطع الفيديو ذات الصلة
5.0 / 5 (0 votes)