Python 3 Programming Tutorial - Frequently asked questions
Key Takeaways
The video covers Python 3 programming basics, specifically the shebang line and the if __name__ == "__main__" construct, explaining their purposes and use cases.
Full Transcript
Hello everybody and welcome to another Python basics tutorial video. In this video, I'm actually just going to answer a couple of questions that I see people ask quite often. And the first one is going to be the shebang line. Uh that's this line. You'll see pound sign exclamation mark USR uh and then something like bin Python. Now, if you're on Windows, this is really useless. U but you'll still see it on people's uh modules sometimes or maybe on some example code and you might not actually know what that means. Um and this is referred to this as the shebang line. And basically what it's going to do is if you're on Linux it notifies Linux of the path to Python. So you can actually run this script as an executable. Um but on Windows again it's there's really no use for it here. You could use this on Windows and still tell Windows sort of but it doesn't work the same. Uh but anyway, that is why you are going to see something very similar to this quite often in scripts. People just put them up there. Now, the next thing that you're going to see quite often in scripts and especially modules is the following. So on people's modules, a lot of times you're going to see the following. So uh let's say you've got this module that you've downloaded and this module is epic. And what epic does is just print, wow, this is great. Right? And then in on the module itself um maybe or the script that someone wrote but it's for the purposes of a module um you'll see something like uh if name equals main print that. Okay. So, you'll see something like this. Um, and so we'll run this script and you'll see it says such great module. Okay. Uh, and the question that people ask is why do people write this? Why do people saying if name equals main? Well, the reason why people say if name equals main then run this stuff is so people can use this code as a module. Now let me show you. So now uh in your script um for example my script is called um epic thing.py. Now in that same directory that you have epic thing.py make another uh like another script. Okay. So, let me uh bring my folder over here. There's epic thing.py. Let's go ahead and copy and paste epic thing.py, another script, and we'll just call that um another script. We're very fancy in our name calling. And we'll open it with idle. And again, this was just a copy of epic thing. So, it's got the same code. But let's say instead you wanted to import epic thing.py. You want to say import epic thing like so that's how you can import um another script. Now epic thing.py has a function called epic. So what we could do in our script is we could do epic oops epic thing.epic. Okay save that and we can run this and we get wow this is great. Okay, so we're treating this like a module. Uh like I had mentioned before that you can do so enjoy. Now the reason why it says if name equals main is the following. If uh we go to epic thing.py, right, the module that we're importing and we get rid of if name equals main and we bring print over and we save. We run epic thing.py and we get the same outcome that we had before. It does such a great module. Um, but what ends up happening is if we come into to our other script that's importing epic thing and then all we wanted to do was just do epic thing. Epic but instead what do we get? Well, we get such great module and then wow this is great. Why do we get that? Well, the reason why we get that is at the instant that we imported epic thing, we run epic thing kind of like it basically what happens is when you import the module, it's going to run through that module to load it in just like it um Python would load uh your functions and it loads things in order. So, uh in the order that it got it, it imported epic thing. It stores the function and then it's going to go ahead and print such great module. So, that's why we use if name equals main. that will stop that from occurring. Okay, so we do it again and we don't get that. Now, why does that stop that from occurring? Well, if_ame equals main, basically what it's asking is, is this the main um file that's being run? So, basically asks, are we actually running epic thing.py or are we running some other file? So, as long as we're running some other file, it's okay. And if we are running some other file, this will uh return a false uh and this won't run. And so that's why people use if name equals main. So hopefully that was informative for some of you guys. If you have any questions or comments, feel free to leave them below. As always, thanks for watching. Thanks for all the support and subscriptions. Until next
Original Description
Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M
In this python 3 programming tutorial video, the following topics are discussed:
#!/usr/bin/python placed at the top of scripts and if __name__ == "__main__" for the main loops.
#!/usr/bin/python is a shebang line, which notifies Linux of the python path. This allows the script to be executable in Linux.
if __name__ == "__main__" is used by people who distribute their code to others. This allows the code to either be used as a standalone, or to be imported by another script without running the mainloop.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
🎓
Tutor Explanation
DeepCamp AI