Coding Exercise for Beginners in Python with solution | Exercise 25 | Python for Beginners #lec75

Jenny's Lectures CS IT
2 Jul 202311:14

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

00:00

🐍 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.

05:02

📝 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.

10:02

🔍 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

Python is a high-level, interpreted, and general-purpose programming language. It is known for its readability and ease of use, making it a popular choice for beginners and professionals alike. In the context of the video, Python is the central theme as the script discusses learning and applying Python functions, specifically focusing on how to determine the number of days in a month.

💡functions

In Python, a function is a reusable block of code that performs a specific task. Functions allow programmers to organize code into manageable and reusable pieces. The video script discusses creating a function named 'days_in_month' to calculate the number of days in a given month, demonstrating the practical application of functions in Python programming.

💡multiple return values

A function in Python can return multiple values by returning a tuple. This feature is mentioned in the script, where the previous video's content is referenced, discussing how to return multiple values from a single function. This is a useful technique for returning multiple pieces of data from a function call.

💡leap year

A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not by 400. Leap years are important for calculating the number of days in February, as they have 29 days instead of the usual 28. The script discusses a function to determine if a year is a leap year, which is crucial for the 'days_in_month' function to return the correct number of days for February.

💡conditional statements

Conditional statements in Python, such as 'if' and 'else', allow the program to make decisions based on certain conditions. The script uses conditional statements to check if a year is a leap year and if the month is February, which determines whether to return 28 or 29 days for February.

💡input function

The 'input()' function in Python is used to take user input from the console. In the script, the 'input()' function is used to gather the year and month from the user, which are then passed to the 'days_in_month' function to determine the number of days in that month.

💡list

A list in Python is a mutable, ordered collection of items. The script uses a list to store the number of days for each month, allowing the function to easily access the correct number of days based on the user's input. This demonstrates the use of lists for storing and accessing data in Python.

💡indexing

Indexing in Python refers to accessing elements in a list or other sequences by their position. The script uses indexing to retrieve the number of days for the specified month from the 'days_list'. It's important to note that indexing starts at 0 in Python, so the script correctly adjusts the month number by subtracting one to get the right index.

💡return statement

The 'return' statement in Python is used to return a value from a function. The script explains the difference between using 'print' and 'return' within functions. It uses 'return' to send the calculated number of days back to the caller, which can then be printed or used in further processing.

💡logic

Logic in programming refers to the set of rules and procedures used to determine the correct sequence of operations. The script discusses the logic behind determining leap years and calculating the number of days in a month. Understanding logic is essential for writing correct and efficient code, as demonstrated in the video's coding exercise.

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

play00:00

hey everyone I hope you are safe and

play00:02

doing good so in the series of learning

play00:03

Python programming language we are

play00:04

discussing functions in Python in the

play00:07

previous video we have seen like how to

play00:08

return multiple values from a function

play00:10

and how to use multiple return statement

play00:12

within one function right so now we'll

play00:15

be doing one coding exercise simple one

play00:17

and that is you have to find out number

play00:20

of days in a month

play00:22

suppose I enter like January then it

play00:24

will tell number of days are 31 February

play00:27

28 like this like December 31. okay but

play00:31

here is one twist if it is a leap year

play00:33

in that case in February we are having

play00:35

29 days

play00:37

so that also you have to apply that

play00:39

logic if the year is Leap Year then

play00:42

suppose

play00:43

to think usable letter first is enter a

play00:47

year suppose I am entering here two zero

play00:49

two three

play00:50

enter a month

play00:52

month we will be entering not to January

play00:54

February like one two three four like

play00:56

this one means January 2 means February

play00:58

like this so I'm entering like two

play01:01

so two means February in February in 23

play01:04

number of days 28 like this

play01:08

but if it's two zero two four that would

play01:11

be a leap year so it will give 29.

play01:15

right so you have to apply to logic one

play01:18

is that first need to check the air is

play01:20

Leap Year or not if the air is Leap Year

play01:23

and the month is February the month is

play01:26

two then

play01:28

it will return 29 right we are basically

play01:32

doing this exercise with the help of

play01:34

functions

play01:35

okay otherwise according to you know

play01:38

January February and like this it will

play01:41

give number of days 31 28 30 and like

play01:43

this right so one thing we have

play01:46

discussed how to check a year is Leap

play01:48

Year or not

play01:48

the link of that video I'll put in the

play01:50

side button you can check out that video

play01:52

first

play01:53

in the that video we have clearly you

play01:56

know defined clearly you know defined

play01:58

you can say or discuss the flowchart as

play02:00

well as

play02:01

how to

play02:02

have to find uh how to find out if a

play02:05

year is a leap year or not right with

play02:07

the elbow program also I have shown

play02:09

so you can check out that video first

play02:11

then come to this video

play02:12

okay I hope you heard what is the

play02:14

problem statement what you need to do

play02:17

right now pause the video and try this

play02:20

out okay so I hope you have done this

play02:23

exercise now let's do it together let's

play02:25

create a new file and like I'm naming it

play02:29

days in month Dot py

play02:32

see this is a one according to size in

play02:36

which we are checking we were checking

play02:37

like the hair is Leap Year or not so

play02:39

this logic we are going to use in a this

play02:42

program also so two input you need to

play02:44

take from user first is year and second

play02:46

is month using input function we will be

play02:49

taking this input

play02:51

now we're defining a function suppose

play02:53

the name of function I'm taking like uh

play02:56

days in

play02:58

month because we are checking number of

play03:00

days in a month so what you will pass to

play03:02

argument obviously here and month

play03:10

okay so this is what we are calling this

play03:13

function so before calling obviously you

play03:15

have to Define this function so how to

play03:16

define in depth

play03:18

same name you need to take

play03:21

days in month and two parameters so two

play03:24

variable you need to take either you can

play03:25

take same like yeah same name year and

play03:28

month or you can take different name

play03:29

that is also fine

play03:31

like y m or any other thing okay

play03:36

now you need to check see what will put

play03:40

there's one condition if here is Leap

play03:42

Year and the month is February month

play03:44

equal to equal to two in that case it

play03:47

should print 29 otherwise

play03:49

it will print according to like 31 28 30

play03:53

like this right so we have we have a

play03:56

list

play03:58

like days list I'm taking a name days

play04:01

list equal to

play04:03

for January it's 31 days then at second

play04:07

index like February 28 like this

play04:10

so this list we are having

play04:13

so like at zeroth index for month first

play04:16

January you will access 31 then 28 31

play04:19

like this from this we can access days

play04:21

in a month according to whatever day you

play04:23

provide

play04:24

but here one condition you need to pick

play04:26

like this if

play04:29

like I'm taking year is

play04:32

leap year

play04:34

one is this thing

play04:38

obviously it is not a proper condition

play04:40

we will write down that condition but

play04:42

this thing you need to put right

play04:45

or let's write down its only leap year

play04:49

and and month should be

play04:52

month equal to equal to 2 means February

play04:56

means two January 1 February two like

play04:58

this we are mapping the months

play05:01

then

play05:03

we are not printing

play05:04

we are returning then it this function

play05:07

should return 29.

play05:11

else

play05:14

you can use else or

play05:17

simply without using else also we can

play05:19

write down that that condition

play05:21

that thing else what it will return

play05:25

from this list we will be accessing

play05:28

number of days okay

play05:31

so provide the list name days list

play05:36

and what index we will provide

play05:39

whatever month

play05:40

you will enter

play05:42

but that is not correct because month if

play05:45

I am entering month is 1.

play05:47

means it should fetch this value 31 but

play05:50

this value at least is from at zeroth

play05:53

index because index is done from 0 0 1 2

play05:55

3 4 like this not one

play05:57

so we should give here month minus one

play06:01

then it will give you correct value if

play06:03

month I am providing 1 then 1 minus 1 is

play06:06

0. so days list and index is 0 at 0

play06:09

index we are having 31 so it will return

play06:10

31. I hope you got this right but how to

play06:14

check

play06:15

if it is a leap year or not

play06:18

this logic we are having leap year logic

play06:21

right so let's

play06:24

copy paste this thing

play06:27

and before this function suppose

play06:30

this is a logic right for finding leap

play06:34

year

play06:35

so what we can do we can just Define a

play06:39

function here

play06:41

for checking leap year depth

play06:45

and what we will pass in parameter only

play06:49

here

play06:51

right and this logic we can put

play06:54

within this

play06:56

so we have to put this thing in

play06:58

indentation as an indented block within

play07:01

this function right

play07:03

so if the ear is Leap Year like if it is

play07:07

Mode 4 it is divided by 4 completely

play07:10

then we will check if it's divisible by

play07:12

100 if true then we will check if it

play07:14

divided by 400 then only it will be a

play07:16

leap year right else not a leap year but

play07:19

if a number is divisible by 4 but not

play07:21

divisible by hundred

play07:23

then it is a leap year and if a number

play07:26

is not divisible by 4 then it is not a

play07:29

leap year so rather than printing leap

play07:31

year and not leap year we are just

play07:32

returning true and false okay so

play07:34

whatever whatever this function will

play07:36

return

play07:37

that value will

play07:39

accept here so means in this in this

play07:42

condition we are calling this function

play07:44

leap year so if

play07:48

Leap Year and have to call obviously

play07:51

parameter we have to pass here

play07:54

right

play07:55

and month equal to equal to 2 so if

play08:00

it will return to so rather than leap

play08:03

year what you need to

play08:05

do here just write down

play08:09

true

play08:12

and if it is not a leap year then it

play08:14

should return

play08:17

false

play08:19

here same return true

play08:25

and here return false

play08:31

right

play08:32

so whatever it will return we will face

play08:34

we will accept here so if it is

play08:36

returning true means it is a leap year

play08:37

true and month equal to equal to 2 means

play08:40

true and true then it will return 29 if

play08:43

it will return false like it is not a

play08:45

leap year then obviously you will not

play08:46

check this condition because here it is

play08:48

and

play08:49

to be

play08:51

you know to make this condition true

play08:53

both should be true but if air is not a

play08:55

leap year then it will return false

play08:57

means false means we will not enter into

play08:59

this if part and else it will return

play09:02

days list month minus one Whatever month

play09:04

you will enter right so just because it

play09:08

is returning we are not printing here so

play09:11

we have to accept that thing in a

play09:13

variable like let's suppose days and

play09:17

simply we will print

play09:20

that variable days

play09:22

okay let's run this and see what output

play09:24

you will get here

play09:27

definitely roads and invalid syntax

play09:30

where we have

play09:34

C

play09:35

this should be here

play09:38

now let's run this

play09:41

entire year here is 2 0 2 3.

play09:45

and month is supposed to means February

play09:48

it's 28 let's run this again and let's

play09:51

enter a leap year

play09:53

so 2024 would be a leap year

play09:55

okay

play09:57

month is 2 it should be in 29. see it is

play10:01

returning 29 right or any other we can

play10:05

also enter like f 2 0 to 2.

play10:08

and it's 5 means may may is 31 so it

play10:12

will return 31.

play10:14

okay I hope you got the logic it's not

play10:17

so tough if you are not getting this

play10:19

it's okay you can take a break you can

play10:21

just write down this thing the complete

play10:22

logic

play10:23

a piece of paper and dry on this

play10:25

manually rather than on your laptop one

play10:28

by one like first this line then this

play10:30

line then this will happen then calling

play10:32

of the function like this and like this

play10:34

then definitely we'll get it so see in

play10:36

if

play10:37

statement we can

play10:40

write down any expression like any

play10:42

condition like this or we can call any

play10:44

function in like this right so here we

play10:46

are not printing the values we are

play10:47

returning values so what is the

play10:49

difference between print and return

play10:51

when we should print the values when we

play10:53

should use print when we should you know

play10:56

use return in a function right so this

play10:59

thing will be discussing in next video

play11:01

right so I hope you have got this for

play11:04

exercise now in the next video we'll see

play11:05

what is the difference between print and

play11:07

return

play11:09

these are two different concept right so

play11:11

now it's in the next video till then

play11:13

thank you

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Python ProgrammingFunctionsLeap YearDays in MonthCoding ExerciseEducational ContentProgramming TutorialYear ValidationMonth CalculationPython Syntax
Benötigen Sie eine Zusammenfassung auf Englisch?