- Podcasts
- Python To You
- _4 Python Tutorial for Beginners _ Variables in Python
_4 Python Tutorial for Beginners _ Variables in Python
Python To You
- October 10, 2026
- 10 min
About this episode
No description.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
_4 Python Tutorial for Beginners _ Variables in Python
Welcome back Aliens, my name is Dhavan Reddy and let's continue with this series on Python. In fact, in the earlier videos, we have talked about what is Python, we have installed two softwares and we have also worked with certain operations, right? We had fun in the last session. Now, in this video, we are going to talk about what is variable. But first of all, why do we need one? Let's try something here. So what I will do is I will go back to my previous example, you know, we were adding two and three. So we have done with that, right? We are saying two plus three. Now, if you remember in maths, what we do is we of course, we work with numbers. But then the moment you start with algebra, you have this concept of using X and Y. So what we do is we say let X is equal to five. Can we do that here? So we have X equal to two.
Value two in a variable called X. And that's a new word now. What is variable? So basically variable is a container where you can put your values. Example, we are storing two here in a container and the name of that container is X. Of course, you can give any name doesn't matter. You can say ABC, you can say a box, you can give any name, right? So we can say X equal to two. And the moment I say enter, now this is the first time when you said enter and you have not got any output, right? So in the earlier prompts, what we did is when we said any, any operation, it was giving you some output. Here we have not got any output. It's just an assignment operation. So in assignment what we do is we have a variable name equal to a value. So we are assigning 2 to x. Okay that works. So can I do this now? Can I say x plus 3? Yes right because in x we have a value which is 2.
Now let's do this thing. What if I say x and if I say enter, what it will do? So the moment you say x and enter it will print the value of x in this case which is 9 right. What if I use a variable abc now of course we have not defined this variable right and we are still trying to use it. Now when I say enter you got an error the error is it says name error abc name abc is not defined and that's why we have not defined abc yet. So if you are if you are using a variable which is not defined so it will of course give you an error. Now, I will do one more thing. I will say X plus 10. So, of course, it will give you 19. Now, what if I want to add 19 with Y? I mean something in this way. I would say 19 plus Y. But then if you think about this 19, this is the output of the previous operation, right? Now, if you want to use the output of the previous operation, we can use underscore. So, underscore represents the output of the previous operation. And if I say enter, you can see we got 22. So, it is 19 plus 3, which is 22. So even this works, right? So this is amazing. You know, there are so many things you can you can work with this. So we can use variables for numbers, right? So we can use it for float or integer. What about string? Can I use names with can I use variables with string or a string variable?
So I will get a variable as name and I will use a name as YouTube. And if I say enter, oh, it works. And if I print name, we got YouTube. So it is perfectly working. so we can use string as well so example i can say name plus youtube rocks so you can you can see we got youtube rock so it is name which is youtube plus rocks okay this even this work that's perfectly fine in fact let me just do an experiment can i say name and space rocks okay this is not working so we have to make sure that you put plus there because if you have plus then then only it will try to interpret that But there's this one thing which is very fancy here, you know. So we talk about YouTube.
Name and if I want to fetch one character, how would I do that? So let's say if I have a YouTube here. And YouTube has seven letters or seven characters. If you when you break it down, it will create an array or you can say collection of characters. The numbering starts with zero because normally in computers, numbering always starts with zero. So here, if you want to fetch it, we have to use a square bracket and you have to mention zero. So we are specifying, hey, I want to fetch the first letter of name.
Now I will fetch, let's say eight. See, we don't have eight. We don't have nine characters, right? We only have seven characters. So the first one is zero. The last one is six. How about eight? The moment you do that, it will give you an error. It says zero. Index error string index out of range. So of course you're going out of your limit, right? So you cannot do that Okay, can I use negative numbers? Let's try I will use minus one So I want to I want your guess here what you think it will what it.
It goes in reverse order now. Now, what if I want to print two characters, not one, two? So let's say I want to print Y and O. So in this case, I will say name and I will start with zero. Of course, we want to print zero, the first character and the second character, right? That means we will give two. Now, this two doesn't mean number of characters. It means the ending. So I want to start with zero and one. So first two characters. And you can see we got Y zero. the same thing if i do it for one colon four so come on guess so one means second character right so it will start with o and it will end at three because you cannot include four so it will it is exclusive of four uh so it is one which is o u and t oh it works we got out yes yes out it works Now, what if I specify only one and colon? If I don't specify the ending, what happens? In this case, it will start with one and it will go till the end. Okay, that how it works. So, if you don't specify the ending part, it will go in that way. What if I specify the ending part but not the starting point? Example, if I say, if I keep it blank and after colon, I will say four. Now, in this case, it will start with the first letter and it will end at three, which is before four. And that's why you got Y-O-U-T. So this is fun, right? Okay, what else we can do here? What if I specify name and I will give wrong values. I will say 3 colon 10. Of course, we don't have 10 characters, right? Now, in this case, it will end at the last one, which is available. So it will not give you any error at this case.
So I'm changing YOU to my. The moment I say enter, you've got an error. It says string object does not support item assignment. So the thing is, once you assign the value, you cannot change it. Not even for one character. If I say 0, I want to change the first character to rawtube. Even this will not work. You can see we got the error. So you cannot change the value. That means strings in Python is immutable. You cannot change the value of it.
You work with string you work with variables right you can have a number of variables you can have a float variables and you can have a string variable as well in fact with string we have one more thing let's say if you define a string which is, let's say, my name is a variable and my name here is Naveen Reddy. And if I want to know the length of my name, we can do that. So we have a length function and we can pass my name. As I mentioned, there are some inbuilt functions available in Python and one of them is length. So you can see we got 11 characters.
