Welcome to PyBeginners ā your go-to place to learn Python programming the easy way! š
Today, we tackle a common coding challenge: finding the largest number and smallest number in a list without using Python's built-in functions. This python programming exercise reinforces your python basics and problem solving skills. š»š¬
Whether you're starting from scratch or revisiting the basics, this channel is built just for YOU. We break down Python concepts into bite-sized, beginner-friendly videos using real-life examples, everyday language, and fun visuals.
Think of PyBeginners as your coding buddy ā here to help you:
ā
Understand the why behind every concept
ā
Build confidence, one small step at a time
ā
Learn coding without feeling overwhelmed
š Check out more learning resources:
š Blogs: https://www.pybeginners.com/
š¼ LinkedIn: https://www.linkedin.com/newsletters/pybeginners-7267881715929415681/
š Free eBooks & PDFs: https://www.usandopy.com/livros/
š New videos every week ā covering Python basics, hands-on exercises, projects, and career tips!
š Subscribe, join the PyBeginners family, and let's make Python simple together š»š¬
Show More Show Less View Video Transcript
0:00
Hey everyone, welcome back to Python for
0:03
beginners series. Well, this side Rajal
0:05
Lakshmi Singum and in today's program we
0:07
are going to do something really
0:09
interesting where we'll find the largest
0:11
and smallest number in a list. But there
0:14
is a catch. We are not allowed to use
0:17
Python's readymade max or min functions.
0:22
That means we are going to solve it the
0:24
old school way with just if else logic.
0:30
So here's our game plan. So we will ask
0:33
the user for three numbers and compare
0:36
them one by one and decide which one is
0:39
the largest and which one is the
0:41
smallest and then we'll print the
0:43
results. And this is a great exercise to
0:46
practice your decision making skills in
0:48
Python.
0:50
Okay, let's write this code step by
0:53
step. So step one
1:00
take input
1:04
from the user
1:09
and numbers
1:11
equal to input.
1:18
Enter three
1:22
numbers
1:23
separated
1:26
with comma
1:35
dotsplit and you'll split them with
1:38
comma.
1:42
Okay. And then
1:48
Now
1:51
check if exactly we have three numbers
2:12
and how you'll check if L Then we have
2:16
alien function in list. So alien of
2:19
numbers
2:21
equal to three.
2:24
Then then we will do right. So then we
2:27
will convert
2:29
all the numbers
2:33
to integers.
2:38
So what you'll do you'll convert num one
2:41
num two the first number second number
2:44
and the third number you'll create three
2:46
variables and then you will map those
2:50
variables at the same time you will
2:52
change them to integers.
2:55
So what's happening here? So for example
2:58
user gave three numbers
3:01
as inputs.
3:03
Okay, like for example number one,
3:05
number two and number three and these
3:07
will be in strings, right?
3:12
These will be in strings. So here you
3:16
are creating three variables now itself
3:18
num 1, num two, num three and you are
3:22
mapping these three values
3:24
like
3:27
num 1 = 1,
3:30
num 2 = 2
3:34
and dum 3 = 3. So and you are changing
3:39
now this is string right? Now this is an
3:42
integer. see the difference even when
3:43
I'm presenting it. So here we have
3:46
strings and here we have integers. So
3:48
what this map will do it will assign the
3:51
numbers like we have three values in the
3:54
list like you're telling the Python that
3:56
we have three values in the list and
3:58
just create three variables and attach
4:02
and assign the values with respect to
4:05
the order like
4:08
1 2 and three. Num 1 equal to 1,2 equal
4:12
to 2 and 3 equal to 3. And at the same
4:15
time when you are assigning please
4:17
change the data type from string to an
4:20
integer. It could be anything just
4:21
change it to integers. So this is what
4:24
the map is doing.
4:28
Okay.
4:30
And step three is the most important
4:32
step. This is where we find the largest
4:37
number.
4:40
So if first we'll check the first two
4:43
numbers. So num one greater than or
4:47
equal to num two
4:51
and then we'll check num one is greater
4:56
than or equal to num three. So we are
4:59
checking here whether the first number
5:02
is greater than the second and third
5:04
number or not. whether one is greater
5:06
than two and three or not. So if one is
5:11
greater than two and one is greater than
5:13
three. So num one is greater than num
5:16
two and num 1 is greater than or equal
5:18
to num three. So then you will just
5:21
create a variable called largest and you
5:24
will just assign num one in it.
5:27
Okay, this is good. So you're checking
5:30
number one is greater than or equal to
5:33
like are these two numbers same or are
5:37
these two numbers same?
5:41
It could be same or maybe greater than.
5:43
So that's what you're checking. So
5:45
largest equal to number one. So else
5:49
l if now you'll check second number. So
5:53
num two
5:55
greater than or equal to num 1 and num 2
6:01
greater than or equal to num 3.
6:05
Then you will change the largest to num
6:08
two. So here you check the first number.
6:12
Right? Now you are checking the second
6:14
number whether it is greater than or
6:16
equal to with the first number and with
6:18
the third number. So num two is greater
6:21
than or equal to num one.
6:23
And at the same time n 2 is greater than
6:26
or equal to n3. So that is what you're
6:29
doing. So what is this and
6:32
I think if you guys know uh if you guys
6:35
have studied this and gate or gate so
6:38
then you will understand. So and is
6:40
nothing but
6:42
it is used to check the conditions. It
6:45
is like if these so you are telling the
6:48
program that hey Python if these two
6:53
conditions are true then perform this
6:56
operation.
6:58
If any if these two conditions are true
7:02
then only perform this operation. So
7:04
what if you keep or here? So or in the
7:08
sense it should like any one of the
7:11
condition must be true. anyone. It could
7:14
be either this. If this is true, then it
7:17
will not even check this condition. Then
7:19
directly it will jump here. If this
7:22
condition is false, then it will jump
7:27
here and it will check this condition.
7:30
And then if this is true,
7:33
though this is false, though this entire
7:36
thing is false, it will do this. It will
7:39
perform this line. So that is the
7:41
difference between and and or. Or means
7:45
either any one of the condition and
7:47
means every condition everything.
7:52
So I want every condition to be true. So
7:54
that's why I kept and here.
7:58
Okay. So here we check the first number,
8:01
the second number and we okay if these
8:05
two conditions are false then we have
8:08
the third option that will be the
8:09
largest anyway. So you will write else
8:15
largest
8:17
equal to numpy. That's it.
8:23
And then you will just print.
8:38
Largest number.
8:46
Largest
8:54
Okay, this is good. So you're printing
8:57
the largest number,
9:01
but we also need the smallest number,
9:03
right? So what you'll do? So I want you
9:06
to pause at this moment. The hint is
9:08
already in the slide, but I want you to
9:10
check
9:12
yourself that you are writing the code
9:14
with me. I want you to do it yourself
9:17
saying that how can you find the
9:19
smallest number? We printed the largest
9:21
number. We got the largest number. But
9:23
we want the smallest number. What you
9:25
will do to find the smallest number? You
9:28
just have to do less than or equal to in
9:32
place of greater than or equal to. And
9:34
you change the largest to smallest.
9:36
That's the only difference.
9:50
Find the smallest number.
9:56
Just give if num one is less than or
9:59
equal to num two. And if num one is less
10:02
than or equal to num three, then just
10:04
create a variable called smallest and
10:06
add it.
10:09
If num2 is less than or equal to
10:18
if num2 is less than or equal to num 1
10:22
and num2 is less than or equal to num
10:24
three then the smallest will be num2.
10:27
Else the smallest will be num3
10:31
and you just have to print
10:39
smallest number
10:44
smallest.
10:46
So this is inside the entire if
10:49
condition right but we have to write the
10:50
else condition as well. Else part as
10:53
well. Every if should have else. So
10:56
else, so this is when the user gives
10:59
exactly the three numbers. What if the
11:02
user didn't give exactly the three
11:04
numbers? Then you have to let the user
11:06
know that
11:10
print
11:13
please enter exactly
11:18
three numbers.
11:24
That's it.
11:26
Now when you run it
11:32
enter three numbers separated with comma
11:35
12
11:37
13
11:39
14.
11:41
So the largest number is 14 and the
11:43
smallest number is 12. Good job. What if
11:46
you give everything or maybe any two
11:49
numbers same? So 10 10 11. So the
11:55
largest is 11 and the smallest is 10.
11:58
Okay. What if you give
12:02
all same numbers?
12:09
The largest number seven and the
12:10
smallest number seven. Yeah, it even
12:13
handles when you give same values.
12:17
And there you go. We just built a
12:19
program that finds the largest and
12:21
smallest numbers without cheating with
12:24
max and min.
12:26
So this may seem simple but it's
12:29
actually training your brain to think
12:31
like a programmer step by step.
12:35
So I always insist you to do use pen and
12:39
paper when you're dealing with tracing.
12:41
So when you don't know how the logic is
12:43
going on, please take pen and paper and
12:45
do it yourself. And before we wrap up, I
12:48
have a little challenge for you. I want
12:51
you to try changing this code to work
12:54
with five numbers instead of three. And
12:57
trust me, it's fun and it will make you
12:59
even more confident with if else. And if
13:03
this video helped you even a tiny bit,
13:05
I'd love for you to stick around and hit
13:07
that subscribe button like it's your
13:09
enter key. And don't forget the bell
13:11
icon so Python lessons come to you, not
13:14
the other way around. Keep coding, keep
13:17
practicing, and I'll see you in the next
13:19
one. Happy learning.

