Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Lists Shopping List Take Three

Prasanna kadel
PLUS
Prasanna kadel
Courses Plus Student 1,057 Points

didnt understand the shopping list take 3

Why did he import os ?

Shana HT
Shana HT
3,292 Points

He uses it in this function:

def clear_screen():
  os.system("cls" if os.name == "nt" else "clear")

It calls the operating system to clear the console screen, depending on which operating system the user is running. This function is called before a list is printed. The way he uses the if statement, is like a shortcut.

This function would do the same thing

def clear_screen():
  if (os.name == "nt"):
    os.system("cls")
  else:
   os.system("clear")

1 Answer

Dylan Jackson
PLUS
Dylan Jackson
Courses Plus Student 1,906 Points

NT is the windows kernel, so CLS would clear the screen. *nix kernels have different names and commands, so you would use "clear" to clear the terminal screen in a *nix OS, such as Apple's OSX, or Debian, Mint, RHEL, etc.

The OS package that was imported lets him check the name of the kernel for comparison.