Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
if __name__ == '__main__' for Python beginners ๐Ÿ“ฅ
Play lesson

Python tutorial for beginners ๐Ÿ - if __name__ == '__main__' for Python beginners ๐Ÿ“ฅ

4.0 (0)
8 learners

What you'll learn

This course includes

  • 14.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

# if __name__ == __main__: (this script can be imported OR run standalone) # Functions and classes in this module can be reused without the main block of code executing # Good practice (code is modular, helps readability, leaves no global variables, avoids unintended execution) Ex. library = Import library for functionality. When running library directly, display a help page. # ---------- script1.py ---------- # This file can run standalone or be imported def favorite_food(food): print(f"Your favorite food is {food}") def main(): print("This is script1") favorite_food("pizza") print("Goodbye!") if __name__ == '__main__': main() # ---------- script2.py ---------- # This file should run only standalone from script1 import * def favorite_drink(drink): print(f"Your favorite drink is {drink}") print("This is script2") favorite_food("sushi") favorite_drink("coffee") print('Goodbye!')

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere