Predict the output of the following
>>> a = ‘python’ < ‘program’
>>> print (a)
Answers
QuestioN :
Predict the output of the following
>>> a = ‘python’ < ‘program’
>>> print (a)
ANswer :
The output would be 'False'.
When comparing two strings in Python, their ASCII values are compared.
Each character in Python has its own ASCII value, a language that is interpreted by the system.
- Characters from 'A - Z' have ASCII values from 65 - 90.
- Characters from 'a - z' have ASCII values from 97 - 122.
- Characters from 0 - 9 have ASCII values from 49 - 57.
- Special characters [@, !, &, %, #, *, ... etc] also have their own ASCII values.
Now, when we look at the given operation, the 'p' from 'python' and the 'p' from 'program' would be equivalent since they're the same character, indicating that they have the same ASCII value. The interpreter then moves on to the next character in each string, the 'y' from 'python' and the 'r' from 'program'.
Since 'y' has a higher ASCII value when compared to that of 'r', and the operation given is like saying 'y' < 'r', it would render as False.
Hence a = False.
QuestioN :
Predict the output of the following
>>> a = ‘python’ < ‘program’
>>> print (a)
ANswer :
The output would be 'False'.
When comparing two strings in Python, their ASCII values are compared.
Each character in Python has its own ASCII value, a language that is interpreted by the system.
- Characters from 'A - Z' have ASCII values from 65 - 90.
- Characters from 'a - z' have ASCII values from 97 - 122.
- Characters from 0 - 9 have ASCII values from 49 - 57.
- Special characters [@, !, &, %, #, *, ... etc] also have their own ASCII values.
Now, when we look at the given operation, the 'p' from 'python' and the 'p' from 'program' would be equivalent since they're the same character, indicating that they have the same ASCII value. The interpreter then moves on to the next character in each string, the 'y' from 'python' and the 'r' from 'program'.
Since 'y' has a higher ASCII value when compared to that of 'r', and the operation given is like saying 'y' < 'r', it would render as False.
Hence a = False.