Python-programming-exercises icon indicating copy to clipboard operation
Python-programming-exercises copied to clipboard

q12: Alternative solution

Open CHAMP-123 opened this issue 6 years ago • 6 comments

CHAMP-123 avatar Jul 13 '19 02:07 CHAMP-123

l=[] for i in range(1000,3001): t=[int(j)%2 for j in list(str(i))] tp=list(set(t)) if tp.count(1)==0: l.append(str(i)) print(','.join(l))

how about this?

CHAMP-123 avatar Jul 13 '19 02:07 CHAMP-123

a='' for i in range(1000,3001): if i%2==0: a=a+str(i) a=a+str(',') print(a) it works

venkatasai1432 avatar Jul 22 '19 08:07 venkatasai1432

a=[] for i in range(1000,3001): if i%2==0: a=a+[str(i)] print(','.join(a))

Is it ok OR can you want another one ?

venkatasai1432 avatar Jul 22 '19 08:07 venkatasai1432

You can try this

Result = [ i for in range(1000, 3001) if i%2 == 0] print(Result)

Manish-Ranjan avatar Sep 08 '19 08:09 Manish-Ranjan

Hi, It returns syntax error because i is not declared there the below one return the output as expected

Result = [ i for i in range(1000, 3001) if i%2 == 0] print(Result)

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free. www.avg.com http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Sun, Sep 8, 2019 at 1:36 PM Manish-Ranjan [email protected] wrote:

You can try this

Result = [ i for in range(1000, 3001) if i%2 == 0] print(Result)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/zhiwehu/Python-programming-exercises/issues/80?email_source=notifications&email_token=AMVU576A4X6JZEVFDTK7DVTQISXBRA5CNFSM4ICU6BL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6FKM2A#issuecomment-529180264, or mute the thread https://github.com/notifications/unsubscribe-auth/AMVU577ZTG2QMGCS6IKN7NDQISXBRANCNFSM4ICU6BLQ .

venkatasai1432 avatar Sep 12 '19 05:09 venkatasai1432

Hi, i didn't get any type of error. you don't have to declare variable i .Range function always gives you integer but if you want a list of string values then you simply typecast int into string ( str(i) for i in range(1000, 3001) if i%2 == 0]

Manish-Ranjan avatar Sep 14 '19 05:09 Manish-Ranjan