Python Tutorial - 15. if __name__ == "__main__"
Key Takeaways
Explains Python if __name__ == '__main__'
Full Transcript
hello and welcome to code Basics coding tutorial today we are going to talk about very interesting topic if you have looked at python programs uh you might have noticed this line and this is very common in any Python program let's see what this is it is saying if undor unor name equal toore main let's first check out what is underscore uncore name if you have idle open let me open idle first we will see what is the value of underscore uncore name when I do this it is telling me the value of this particular variable is mean so this is a predefined variable in Python which is which gets set to mean if you run here for example if if you run area. py directly then this will be set as mean so let's just run it you see it is now calculating the area Okay so this thing is used as kind of like an entry point for any Python program now if you have done any coding in C or Java uh the entry point in those programming languages is main function as shown here so if you recall call if you're in C Java this is the function from where your program execution begins that's why it is called an entry point similarly in Python this thing is often used as an entry point inside the program okay now when does the value of underscore uncore name is something other than main well if you have another file called caller. py where you are importing area module so you are importing area meaning you are importing all the code in area. py file which is here and I'm saying I'm in color. py and you're calculating the area okay now if you're in here in area I'm just going to print uh the value of the name okay so underscore underscore let's see what is the value of underscore name when we call it from caller. py okay so I'm saying I am in caller. py let me add one more line here saying that I am in area. py Okay and first I will execute area py and call. py and we will see the difference uh of this print statement okay now this time I'm going to execute it in a little different way usually I run it from py Cham uh editor itself but today I'm going to show you how you can run any python file using Windows command prompt okay I have Windows command prompt open here and I am in the directory where I have area. py and color. piy okay now if I if I want to execute uh area. py directly then this is how I will execute it I will say python area. py so python is my interpreter and I'm executing area. py when I say this I get I am in area. py and the value of underscore name isore main okay let's see what happens if you execute caller. p Y when you do that it is saying that I'm in caller. py but the underscore name value is now area so if you compare that with here it begin execution of this program and that's why it printed I'm in caller. py here and from here it went into another file which is area so it went in here and when it started executing calcul Cal area the value of underscore uncore name was area so the summary here is when you execute python area. py directly the value of underscore uncore name is Main and when you execute it from some other file the value of that variable is the name of that module here we are treating area. py as a module okay so that was all all about uh uncore uncore name thanks for watching
Original Description
Video without background music: https://youtu.be/2AKJzc-cSj8
The python tutorial guides us about if “ __name__”==”__main__”. It will teach us what is “__name__” in python, how it is executed and how to execute python file from a command prompt.
Topics that are covered in this Python Video:
0:25 what is __name__
3:24 Execute python file from a command prompt
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Next Video:
Python Tutorial - 16. Exception Handling: https://www.youtube.com/watch?v=Ia1i5EIGp9k&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=18
Website: https://codebasics.io/
Facebook: https://www.facebook.com/codebasicshub
Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from codebasics · codebasics · 18 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
▶
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
🎓
Tutor Explanation
DeepCamp AI