Python 3 Programming Tutorial - Frequently asked questions

sentdex · Beginner ·🛠️ AI Tools & Apps ·12y ago

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 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video explains the shebang line and the if __name__ == "__main__" construct in Python 3, covering their purposes and use cases, and demonstrating how to use them in scripts and modules.

Key Takeaways
  1. Understand the purpose of the shebang line
  2. Learn how to use if __name__ == "__main__" to control module execution
  3. Create a Python script with a shebang line
  4. Import a module and use its functions
  5. Use if __name__ == "__main__" to prevent unwanted code execution
💡 The if __name__ == "__main__" construct is used to prevent unwanted code execution when importing modules, by checking if the current script is the main module being run.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →