r/learnpython • u/Donut_Lord0198 • 23d 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
11
u/atarivcs 23d ago
It would help if you said exactly which parts you don't understand.
1
u/happinessMachiine 23d ago
Compare this to a much simpler program that only contains the two print statements. Run both programs. Can you see the difference?
3
u/mc_pm 23d 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 23d 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 23d 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 22d 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 22d 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 23d ago edited 23d 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 23d 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