Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.