- 1

Paython

I need the explanation of the below question: x = ‘a’ If (x < ‘c’) X+= ‘b’ If (x > ‘z’): X+ = ‘c’ Print (x)

3rd Jun 2022, 1:18 PM
Mohamed Adli
4 RĂ©ponses
+ 1
X is not x, they are two different variables. x = 'a' (line one). The rest is there to confuse you. But it could also be the keyboard helping you with caps at the beginning of a sentence.
3rd Jun 2022, 5:56 PM
Paul
Paul - avatar
+ 2
Mohamed Adli x = 'a' if (x < 'c'): x += 'b' if (x > 'z'): x += 'c' print (x) x is less than c because Of ASCII value of a is less than c so x = ab Now 'ab' > 'z' here comparison will be on first character ASCII value of a is less than z so this if block will not execute So finally x = 'ab'
3rd Jun 2022, 1:29 PM
AÍąJ
AÍąJ - avatar
+ 2
Hi! And also, in Python we can skip parentheses in the conditions (if they are not needed i.g. to group terms or to order operations): if x < 'c': x += 'b'
3rd Jun 2022, 2:51 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Thank you
3rd Jun 2022, 1:40 PM
Mohamed Adli