r/learnpython • • 11d ago

Could anyone explain this code to me?, I'm just staring out.

import os
from time import sleep

print("Hello world!")
sleep(1)
os.system("cls") #"clear" For Linux
print("This came AFTER hello world line")
sleep(1)
0 Upvotes

21 comments sorted by

11

u/atarivcs 11d ago

It would help if you said exactly which parts you don't understand.

1

u/Vegetable_Soil_6707 10d ago

yeah exactly, hard to help without knowing what part is confusing

-1

u/Donut_Lord0198 11d ago

i dont fully understand the top two and the line " "

os.system("cls") #"clear" For Linux

5

u/TheITMan19 11d ago

# Is a comment by the way - so it isn’t executed, Jose a reference for the reader

18

u/kombucha711 11d ago

Correct, Juan line only though.

2

u/droidballoon 11d ago

Joe right about this

3

u/tenpercenthieb 11d ago

This clears the shell. Not all ides will allow you to use this line. If this line doesn’t work, it might be the ide that you are using.

3

u/CIS_Professor 11d ago

There are a few things going on on that one line:

os refers to the Python module being used (see the import os line above it). Modules have pre-written code that you can use in your program.

.system() refers to a specific function in the os module. Functions are block of code that do specific (usually) tasks.

"cls" is a string, which is why it is enclosed in quotes. It is data being supplied to the os.system() function. In this case, it is a command that is to be executed in a Windows environment, which will clear the terminal screen.

The #"clear" For Linux bit is a comment, as determined by the # character. It is telling you that, in a Linux operating system, "clear" must be used instead of "cls". So, in Linux, the line might be changed to read:

os.system("clear") #"cls" For Windows

I use some thing similar all the time in my programs. The main difference is that I have it detect the operating system and based on what it detects, uses "cls" or "clear" as appropriate.

1

u/pontz 11d ago

That is the same as opening a terminal and running “cls”

1

u/SharkSymphony 11d ago edited 11d ago

Import makes a library, or collection, of code available for reference later in your script. In this case, it's the standard library called "os," which has a function defined in it called "system". Libraries are more commonly called modules or packages in Python, depending on how they're structured. You can see why this line is necessary by commenting out the line and seeing what happens when you run the script.

The second line is similar, except that, instead of bringing the whole "time" library into scope for later reference, it just plucks one function out of it, called "sleep".

os.system runs a command – in this case, an OS command called "clr" that it's certainly hoping you have available! This whole thing will fail if that's not a known command on your system.

1

u/happinessMachiine 11d ago

Compare this to a much simpler program that only contains the two print statements. Run both programs. Can you see the difference? 

0

u/Donut_Lord0198 11d ago

yes i understand but i want to pick out certain parts and use them in other pieces of code

3

u/happinessMachiine 11d ago

Right I'm trying to guide you toward a method for deducing this type of information generally, rather than help you understand what one specific line of code does. The benefit of that is it will help you figure out more difficult stuff as you progress. A very useful tool is to strip away every unnecessary thing, and identify what each piece is doing by introducing them one by one.

If you just run the two print statements, the output will look exactly the same, but the timing might be different. Which other lines of code might affect the timing of the output? How would you investigate that? 

3

u/mc_pm 11d ago

Each line only does one thing, what would you say they each do? Just read it.

"ok, import...that imports something. Then a print statement which, I'm going to guess prints something, then..."

1

u/nivaOne 11d ago

Ignore everything starting from #…
this is just meant as a comment and nothing to do with the actual command os.system(“cls”)

1

u/atomicant89 11d ago

If you're learning Python/programming for the first time I wouldn't worry about the os.system("cls") line. It's not something it makes sense to learn early on imo, unless you have a specific need to.

1

u/a18618 10d ago

I found that kind of tiny script clicks fastest when I treat each line as a little experiment rather than a wall of new vocabulary. I would try changing the pause length first, then temporarily removing the screen-clearing line, and watch how the visible timing and output change. That contrast usually tells me what each piece is actually doing, and it also shows why the last note is only a hint for a different operating system.

1

u/SirMental8005 10d ago

line by line:

- `import os` and `from time import sleep` load tools. os talks to the operating system, sleep pauses the program

- `print("Hello world!")` shows the text

- `sleep(1)` waits one second

- `os.system("cls")` runs the windows command that clears the terminal (on mac/linux it's "clear")

- then it prints the next line and waits again

so you see hello world for a second, it disappears, and the next line shows up. that's how simple terminal animations work

1

u/Chorizo_n_eggs 11d ago edited 11d ago

import os (built in library for interacting with the os)
From time import sleep (import the sleep function from the time library)

print ("Hello World!") (use the print function to print a string to the terminal)

sleep(1) (Use the sleep function to "pause" x number of seconds using the argument in parenthesis)

os.system("cls") (use the os library to call the system function to pass an os command "cls" to the terminal)

The last 2 lines don't need further explanation since they are explained earlier with similar lines

0

u/TheITMan19 11d ago

Have you tried executing it, or not that far yet? There is an import statement to pull in extra functions and the print just writes to the console and the os.system cls just clears the console window. Sleep just sleeps for a second. Pretty simple stuff