Hi Bumsfeld, thanks for sharing, I haven't come across this before

# works on Windows or Linux, also Vista
import os
os.system(['clear','cls'][os.name == 'nt'])

Can please tell me how is the following code is working. I mean,

>>> ['clear','cls'] [True]
'cls'
>>> ['clear','cls'] [False]
'clear'
>>> ['a','b']['some_string' == r'some_string']
'b'
>>> ['a','b']['some_string' == r'somestring']
'a'
>>>

is list and [True] is also another list, what is happening here? I have no knowledge on this, could please let me know what is happening?

Thanks in advance,
regards,
kath.

Sorry kath, moved this out of 'Starting Python' the rules there are ...

The idea of this thread is to help the beginning Python programmer with hints and helpful code. Please feel free to contribute! If you have any questions start your own thread!

vegaseat

Recommended Answers

All 6 Replies

Hi Bumsfeld,

# works on Windows or Linux, also Vista
import os
os.system(['clear','cls'][os.name == 'nt'])

I got it.. I was thinking ['clear','cls'] is list of string and [True] is a list of boolean. But the later is wrong. Later is a index, True will cast into 1 , hence that can deduced as

>>os.system(['clear','cls'][os.name == 'nt'])
>>'cls'
# [os.name == 'nt'] this part will result in either True or False
# hence we can rewrite the above line as
>>os.system(['clear', 'cls'][True]) # for eg:
# which is nothing but os.system(['clear', 'cls'])[1]
>>'cls'

Thanks a lot for this piece of code.. really I enjoyed it..


Cheers,
kath :icon_smile:

Nice deduction there kath! Python does equate True with 1 and False with 0.

Hi Vegaseat,

Sorry kath, moved this out of 'Starting Python' the rules there are ...
vegaseat

No need to be sorry... Well, thanks for you to reminding me.

Cheers,
kath.

How can i control the position of the cursor ? something like this maybe:

print  "\n"*500
setCursor(0, 0)

Thanks, That's is helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.