#46 Python Tutorial for Beginners | Special Variable __name__

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] burnt the back aliens my name is Evans birthday and let's continue this series on Python now in the last video we have talked about modules right and it is awesome now in this video we are going to talk about a very special variable name as name so it's a variable such as variable name it's because we use a double underscore here so in Python we have this special on separate underscore and then we talk about that later but at this point let's focus on name now what is name so let's say if I go back here and if I say print I want to print a name variable so we can actually use the underscore and sin name and the moment you run this code you can see we will be getting mean now what is mean now if you are coming from C C++ or Java background we know that you know main is a starting point of execution same goes for Python the moment you run this code if this is your first code ok because C in your project you might have multiple modules right maybe five module 10 module but there will be some modules which you will run first so this example demo dot pi is my first module maybe with this demo I will be having multiple modules right so but this is my first module so the first module name is always mean but that is the point of execution that so that's where your code starts so the value for name here is main but here's a twist what if if I go back to calc okay Sophie this is demo right this is the file which I'm working with now is demo if I go back to Cal so you can see calculon t at this point is to test I will print hello that's it my main purpose here is to print hello that's it so if I run this one you can see it says hello nothing else just says hello now after hello let me print the name variable here so I will say name underscore underscore now what do you think will it print main or something else of course since I'm coding calc it will print me because I'm running capped right but if I input a calc in demo let me just do that I will say import cow now what will happen is everything which is there in calc will come to this demo file right including the print statement so that's why finalist code of course this will print name of just building named Edna also print demo says so I'm saying demo say his name so demo will plant what of course demo will print mean what about calc now count will played the name of the module so you can see it says hello Cal which means that's the name of the module so if you are running Cal of course this name will be mean but if you are importing gal another module they could print the name of the module okay that's important so the moment you said name it's very special you know the value changes that's what's what's variable right so name value changes as per the place we're using it so if you are running the file as a main call and if you're using name that will print mean but if you print name which is imported as a module that will print the module name now why is helpful it's because we have talked about functions right and we say that everything so let me just remove this part as of now so why it is helpful why this name is I'll put is because when you work on a project of course everything should be in a function right example if you when you trying to print hello that's it my main job here is to print hello and then I want to welcome the user so if I want to say print hello and then I want to say welcome user so I want to do these two things okay I want to say hello I want to say welcome user now I want to do this only when this demo is the first fight okay so I want to print hello and welcome user only when the demo is the first file so if another score you can see we got hello and welcome user but the problem is if I go back to calc now and calc is doing something okay calc will have its own statement but here I'm also importing demo and as I mentioned calc will have its own feature let's say it's time to calculate that's it I just want to print in time to calculate now since calc is my first code here okay so I am not running demo I am running calc so demo becomes a module for me a separate module the moment I run this calc it is also printing hello and welcome user I don't know I do that so to avoid that what you can do is you can define this in a function you will set death and he will write the function name as anything maybe I will just time it I will say main and I will put these two statements inside me so you can see I have to turn those two statements inside a main function okay now of course you need to call it right so normally if you want to execute that from demo itself you have to call them so if I don't anymore now and if I don't this code you can see it will print it okay that's a issue I want to call mean only when this is my first code so in this case you can check if if the name vary but remember the name variable if the name variable is equal to equal to main right now see the magic if another score it will print hello and welcome user but the moment I go back to Cal here and if I say run Cal get this time I'm using demo as a module and you can see it only prints int it's time to calculate so this is the idea okay so if you want to start a code and if this is the first thing you want to show the user because this is the start of the code and you want to call it that's where you will use this thing and that's why if you see Python codes or some books this is what they write okay so it simply says if name is main then only execute mean otherwise if you are importing this as a library you don't want to print mean so that's it everyone that's how you use this special variable called s name I hope you enjoyed this video let me know the comments section and click on that thumbs up button there thanks for watching everyone bye bye
Info
Channel: Telusko
Views: 209,999
Rating: 4.9045138 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: pzNISmtmzcY
Channel Id: undefined
Length: 5min 43sec (343 seconds)
Published: Sun Aug 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.