0:01
Hey guys, welcome back to this channel.
0:03
If you're new, then welcome to this
0:04
channel. Well, I'm back with one more
0:06
interesting Python basics series. So, I
0:10
think you guys are following me in this
0:12
journey of Python series. I hope you you
0:14
have followed each and every single
0:16
video. If you haven't, then I'm going to
0:18
keep the link of the playlist in the
0:20
description. You can just go there and
0:22
check what's going on and then you can
0:24
come here and you can continue learning
0:29
Okay, very quick question. What's the
0:31
very first thing almost every programmer
0:34
does when learning a new language? So
0:37
yeah, you guessed it. We print hello
0:41
world on the screen. It's like a ride of
0:44
passage, right? And that's exactly what
0:47
we are talking about today, which is the
0:50
print function in Python.
0:53
So whether you want to show a message to
0:55
the user, check your variables or debug
0:58
your code, you'll use print all the
1:01
time. So let's break it down.
1:06
All right. So what is print exactly?
1:10
So think of print like your program's
1:13
voice. Like it doesn't affect or change
1:16
your variables or calculations. It just
1:20
takes what you give it and displays it
1:23
to the screen. So it's super useful when
1:26
you want your code to talk to you or the
1:29
user. So let's start with the simplest
1:31
example, the famous hello world program.
1:34
So you can just type print
1:37
open brackets in quotes hello because
1:40
it's a string and world.
1:46
Okay, that's it. Just type print add
1:49
your message in quotes and Python will
1:52
show it in your terminal.
1:55
So let's go through the important
1:58
details. Now let's talk about a couple
2:00
of important details that can trip
2:02
beginners up. But let's just run that
2:05
first and you can just see the hello
2:08
world message. So now let's talk about
2:11
important things that we have to
2:13
remember when we are working with print.
2:16
So in Python 3 like even Python have
2:20
multiple versions don't worry about that
2:22
but keep this in mind.
2:25
In Python 3 you must put parentheses
2:30
after print. So this is correct.
2:47
Always keep this in mind because if you
2:49
see don't forget the parenthesis because
2:52
it is already throwing the error on your
2:54
face saying that it is expecting
2:56
something else. So always keep that in
2:59
mind that these things are really really
3:04
So whenever you want to print text you
3:07
need to wrap it either with single
3:09
quotes or with double quotes. Both will
3:13
work fine. Always keep that in mind
3:16
because if you give that in quotes it
3:18
will understand that oh this is some
3:20
text that user want to show. So I don't
3:23
have to bother about that. I just have
3:25
to display it. So that's what Python
3:27
thinks when you give whatever you want
3:30
in quotes and it will assume it as a
3:32
text and then it will print accordingly.
3:38
So always keep this in mind
3:42
or you can try either this or you can
4:01
So this is what happens when you run
4:07
Wait, one more question. Do quotes
4:10
matter if I am printing a number.
4:15
Like for example, if I print
4:19
45 here, will that be any problem?
4:23
That's a great question. Let's see.
4:26
So if you print like this and if you
4:30
either print like this, let's see both
4:32
will let's check whether both will work
4:35
or not. See if you see both are working
4:39
but actually the case is different in
4:44
Like here Python will treat 45 as a
4:46
number and it will print directly. But
4:49
here Python is actually treating it as a
4:53
text because it's in quotes. So it will
4:56
take it as a text but it will print
4:58
anyway because that its job.
5:02
So, Python will treat this as a text
5:05
which is string and it will print 45 as
5:10
But here, Python will take it as a
5:12
number and it will print 45 anyway. But
5:14
it's a number. So numbers, no quotes at
5:18
all. And text actually needs quote.
5:23
So now let's print a variable.
5:27
If you have if you guys didn't know what
5:30
exactly is a variable and why we use
5:32
this in programming, please go through
5:35
the variable uh video as well. I've made
5:37
it already. You can just go there and
5:39
check why we use it and what exactly a
5:41
variable is. I suggest you to go there,
5:43
check and then come back here.
5:51
So let's give a variable called maybe
6:04
So to print this variable's value you
6:08
just have to do print
6:15
So actually previously if you check we
6:18
have kept it inside here but now what we
6:22
have done is we created a variable and
6:26
we stored this value inside the variable
6:28
and then we are printing it.
6:32
So this is what we are doing exactly.
6:39
So this will if you run this one.
6:51
But but if you do this accidentally like
6:57
because you you might assume that this
6:59
is a string and we need this in a
7:03
So you might think that it is a text and
7:06
it is a string and we have to keep this
7:07
in quotes. This will not work for sure
7:10
if you see because it will actually
7:13
print text. So, Python will literally
7:16
print the letter, you know, the
7:19
characters text, not your variable
7:22
because you've wrapped it in quotes and
7:25
Python thinks it's just text.
7:29
So, without quotes, it will print the
7:31
value of the variable, but with quotes,
7:33
it will print the exact text that you
7:46
multiple items with commas.
7:54
So how to print multiple items with
7:56
commas? So one of the coolest things
7:58
about print is that you can print
8:01
multiple items at once by separating
8:03
them with commas. Python will add spaces
8:06
automatically like you can just check
8:08
here like for example name equal to
8:15
and now age equal to 25
8:18
and now you just have to print
8:59
So hello my name is John and I am 25
9:03
years old. Let's just check.
9:07
See if you see hello my name is John and
9:11
So no need for complicated string
9:13
concatenation which is you know plus in
9:17
between. For example if you do plus here
9:20
let's see what happens just like that.
9:24
Anyway we we are doing string.
9:31
So plus only works with string string
9:35
not string with number string with any
9:38
other data type. It will not work. If
9:41
you see there's something wrong we have
9:45
because age is a number. If you see the
9:48
name is good but age will not work. So
9:52
here you have to convert it to string
9:54
and then you have to do it like if you
9:56
keep this in string then it might work.
10:03
See but you have to give the spaces
10:05
because you are concatenating it. So it
10:08
will not create the spaces in between.
10:11
So this is really complex. So instead
10:14
you can just keep comma in between and
10:17
then you can make it work right instead
10:18
of making it really complex by
10:21
concatenating the strings.
10:24
So this is how you can print multiple
10:26
items at once by just separating them
10:29
with commas. So Python will add spaces
10:35
Now let's see what are the common
10:37
mistakes that we do with print. So let's
10:42
go over a couple of mistakes I see
10:44
beginners make all the time which is
10:52
using quotes when printing variables. So
10:56
that's what I see which is like this. So
10:58
these are the most common mistakes that
11:00
I see most of the time when beginners
11:02
you know working on working on print for
11:05
the very first time. So always keep
11:08
these things in mind. variable is
11:10
completely different from a string. So
11:11
if you give a variable name in string
11:14
then it will directly take it as a
11:16
string. It will not think it as a
11:17
variable. So you you just have to give
11:20
directly variable without strings. Just
11:22
keep that in mind without quotes. Okay.
11:25
So if you have ever unsure what's
11:28
happening, add some clear text before
11:31
the variable to make debugging easier
11:34
like whenever you're printing.
11:39
debugging in the sense you want to check
11:42
what is name like what value is stored
11:45
inside the variable name. So you can
11:51
user's name and you can just check by
11:56
giving the name here.
11:58
I think you don't have to give this.
12:06
So here this is user's name. What's the
12:11
So this is how you will debug the code.
12:13
Like for example, you you want to check
12:15
whether the name is John or not. Whether
12:18
the name is changing or not according to
12:20
the user's input. So like even we have
12:24
worked with input right? So like for
12:32
it will already in string. So all you
12:34
have to do is just keep input and you
12:37
can just remove this and now you want
12:40
the name from the user. Okay, like for
12:45
if you run this one and if you give
12:48
maybe lucky here and when you enter the
12:52
this is user's name which is lucky. So
12:54
you are checking whether the username is
12:56
storing correctly or not. So this you
12:59
can also use print for debugging as
13:00
well. So that is how we will debug when
13:02
we are working on coding to check each
13:05
and every value is you know working
13:06
right or not the logic is going good or
13:09
not. Sometimes we might not have any u
13:12
errors syntax errors like maybe this
13:16
could be a syntax error and this could
13:18
be a syntax error but sometimes there
13:21
might be errors from the logical point
13:23
of view. So at the time when you want to
13:25
debug when you want to check what's
13:27
going on where we are doing wrong where
13:30
the things are going wrong then at that
13:32
time print will really really help us to
13:35
check whether everything is good or not.
13:40
So this way you can also see the output
13:43
is super readable. It will be easy for
13:46
us to even check up to what line the
13:51
And yeah that's it. You know how to use
13:54
print function in Python
13:57
and you have learned how to print simple
13:59
statements and you have printed numbers
14:03
and variables and you also have worked
14:05
with combining multiple items with
14:07
commas and you worked with uh avoiding
14:11
the common mistakes. So these are the
14:13
most common mistakes. So with print you
14:16
can see what your program is doing which
14:18
is a game changer when you're just
14:20
getting started. And in our next
14:22
episode, we'll combine what we have
14:24
learned about input and print. I've just
14:26
shown you, but you have to still go
14:28
through it more to understand it more to
14:30
work on it more. So, we'll check that as
14:36
so, that is how we will build your, you
14:38
know, first interactive mini application
14:41
and it's going to be really, really
14:42
awesome. Just trust me. And if you found
14:45
this video helpful, don't forget to
14:47
like, subscribe, and hit the bell icon
14:49
so you don't miss what's coming next.
14:52
And thanks for coding with me.