Python Tutorial: if __name__ == '__main__'

///Python Tutorial: if __name__ == '__main__'

Python Tutorial: if __name__ == '__main__'

FavoriteLoadingAdd to favorites

In this video, we will take a look at a common conditional statement in Python:
if __name__ == ‘__main__’:

This conditional is used to check whether a python module is being run directly or being imported.

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2021-08-01T15:24:25+00:00August 1st, 2021|Python Video Tutorials|48 Comments

48 Comments

  1. Nitish Bansal August 1, 2021 at 3:24 pm - Reply

    Finally understood this after 2 years of starting programming!!

  2. Bhaskar Trivedi August 1, 2021 at 3:24 pm - Reply

    2021: These are future-proof tutorials!

  3. Jack T August 1, 2021 at 3:24 pm - Reply

    lol does it really save all that much time to copy and paste "print """ and backspace the extra?? xD

  4. szymon wojdyla August 1, 2021 at 3:24 pm - Reply

    a great explanation!

  5. Zeerak Khan August 1, 2021 at 3:24 pm - Reply

    Short, concise and clear explanation of this ever confusing thing. Thanks for the informative video.

  6. shrey jain August 1, 2021 at 3:24 pm - Reply

    Jesus fuckin Christ I am a dumb mofo. I still don't understand why we use this?

  7. Anuj Puranik August 1, 2021 at 3:24 pm - Reply

    very nice

  8. richard g. August 1, 2021 at 3:24 pm - Reply

    Drinking game: whenever he says name or main, you have to guess whether the next one is going to be name or main. Guess wrongly take a sip

  9. Ankita Chauhan August 1, 2021 at 3:24 pm - Reply

    wow easy and concept clearing

  10. Δ π August 1, 2021 at 3:24 pm - Reply

    Unrelated question: Your console show time of execution but I don't see a timeit function in your code. Please advise.

  11. Kelly Riordan August 1, 2021 at 3:24 pm - Reply

    You called the script to run main(). It would have eliminated all confusion if it had been called something else since "__main__" refers to something else, NO?

  12. Developer D August 1, 2021 at 3:24 pm - Reply

    really very very interesting!!!

  13. djtjpain August 1, 2021 at 3:24 pm - Reply

    Awesome, thank you!

  14. שגיא מנור August 1, 2021 at 3:24 pm - Reply

    wow thnks

  15. Samuel Lino August 1, 2021 at 3:24 pm - Reply

    Thanks really helpfull

  16. Player Science August 1, 2021 at 3:24 pm - Reply

    I have a question…
    So you are saying, when we import a module, we need to put that thing, otherwise it will run the entire module which is imported, right?
    Then, my question is does the built-in modules have that piece of code???

  17. InViSiBLe ShArK777 August 1, 2021 at 3:24 pm - Reply

    is this the same explanation for python 3?

  18. M Humayun August 1, 2021 at 3:24 pm - Reply

    best thing ever

  19. Sharad Agarwal August 1, 2021 at 3:24 pm - Reply

    What a crisp explanation. Thank you

  20. Emeraude August 1, 2021 at 3:24 pm - Reply

    Thanks a lot, it cleared this up in a big way for me.

  21. AK KHAN 07 420 August 1, 2021 at 3:24 pm - Reply

    Really amazing explained . I learned everything

  22. Anurag Tambe August 1, 2021 at 3:24 pm - Reply

    Question: Why we write code in python main() method ?

    Answer: As when you call the code from other module it provides additional flexibility. You can call the code without the main method getting executed. Or you can specifically call the main method of imported module.

    Key things

    1) Specify the code inside of main() method.
    2) Specify if condition for variable dunder name to be equal to dunder main
    3) Call main method in this if condition.

  23. Тимофей Карлин August 1, 2021 at 3:24 pm - Reply

    Thank you

  24. 1 conscience 0 dimension August 1, 2021 at 3:24 pm - Reply

    your explanations are Art

  25. Velmurugan M August 1, 2021 at 3:24 pm - Reply

    Subscribed ???

  26. mani 05 August 1, 2021 at 3:24 pm - Reply

    Hey Corey … You are so beautiful … Thanks for this

  27. Brenden Song August 1, 2021 at 3:24 pm - Reply

    Another fantastic video! Thank you for your explanation!

  28. Prasad Naik August 1, 2021 at 3:24 pm - Reply

    I have taken the python course for 300 $ in Bangalore , listened to the class but I have not understood the topic well and here I learned it better…Thank you so much sir…Keep spreading the knowledge and continue making more videos………SUBSCRIBED

  29. Deepank Chauhan August 1, 2021 at 3:24 pm - Reply

    Very good video, the very first minute watching into it got me the right idea about it. Keep it up bruh

  30. Alireza August 1, 2021 at 3:24 pm - Reply

    good video , Thanks

  31. Shubhrajit August 1, 2021 at 3:24 pm - Reply

    At 0:23 How do you comment the code without typing # separately?

  32. jayed nahian August 1, 2021 at 3:24 pm - Reply

    after learning every feature in a particular programming language, The hardest and important term is tress the code ! what is happening and where I'm at now! and you are always there. you are a great teacher, sir . love from bangladesh

  33. Ananda Roy August 1, 2021 at 3:24 pm - Reply

    Awesome explanation, I have just made a few change(one change accidentally) to the first module below

    def main():

    print('First Module Name :{}'.format(__name__))

    print('Run always')

    if _name_ == '__main__':

    main()

    Below is the code of second module

    import first_module

    first_module.main()

    print('Second Module Name :{}'.format(__name__))

    When I run second module,it is printing below.

    Run always

    First Module Name :first_module

    Second Module Name :_main_

    Shouldn't we expect the output from first module as 'First Module Name :__main__' when we are running second module?

  34. stay Home August 1, 2021 at 3:24 pm - Reply

    So why I have: print (__name__) not print __name the same with you. Thanks you

  35. Abhinand J August 1, 2021 at 3:24 pm - Reply

    Great video!
    Suppose we import only a function instead of the whole module, does this concept apply?
    Eg. from test import test_func
    How will the _name_ variable behave in such a case?

  36. Amal August 1, 2021 at 3:24 pm - Reply

    Thank you so much.

  37. Krish Shah August 1, 2021 at 3:24 pm - Reply

    With the amount of views this got, I thought this was gonna be a meme 30seconds-1minute clip (Cause my recommendation is filled with memes of that much time lmao)

  38. Cookie August 1, 2021 at 3:24 pm - Reply

    I just started python last night and I found all the comments below claiming 'clever', now i just feel dumb.

  39. Aceman August 1, 2021 at 3:24 pm - Reply

    That's great. This is exactly what I need.

  40. 60PlusCrazy August 1, 2021 at 3:24 pm - Reply

    Cool

  41. Ritesh Mukhopadhyay August 1, 2021 at 3:24 pm - Reply

    Nicely explained man made my life easier ?

  42. Rortox August 1, 2021 at 3:24 pm - Reply

    But what if you call the file __main__.py 🙂

    Just kidding, great video, thanks Corey.

  43. Ahmed Ellban August 1, 2021 at 3:24 pm - Reply

    شكرا للشخص الى مترجم للعربى

  44. Carlos Murcia August 1, 2021 at 3:24 pm - Reply

    Are you GOD?

  45. Anand Rathod August 1, 2021 at 3:24 pm - Reply

    Hello Corey, God bless you. You explained it so nicely. Thank You.

  46. Afzal Hussain August 1, 2021 at 3:24 pm - Reply

    I had one question throughout the video and you answered it at the end of the video. So every built-in module in python library uses this condition so that we can only call the desired method instead of all of the methods. Right ?

  47. MilanRBX August 1, 2021 at 3:24 pm - Reply

    Thank you!

Leave A Comment

*