What is the output of the following code and Why: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the output of the following code and Why:

x='a' if(x<'c'): x+='b' if(x>'z'): x+='c' print (x)

27th Dec 2022, 8:22 AM
Amit Joe
Amit Joe - avatar
11 Answers
+ 3
The ascii value of 'a' is less than ascii value of 'c' x becomes 'ab' 'ab' > 'z' is false (it checks by ascii value of first character) Therefore x remains 'ab' You can check ascii value by ord()
27th Dec 2022, 9:01 AM
Purva
Purva - avatar
+ 3
You can find the output by **running** the code: Go to Code section, click +, select the programming language, insert your code, click Run. Check the spelling of print()
27th Dec 2022, 8:40 AM
Lisa
Lisa - avatar
+ 2
The output would be ab
27th Dec 2022, 8:51 AM
Purva
Purva - avatar
+ 2
You can remove the other print() when you have understood how the code works
27th Dec 2022, 8:58 AM
Lisa
Lisa - avatar
+ 1
put a print() in each if-block to see which one is executed
27th Dec 2022, 8:52 AM
Lisa
Lisa - avatar
+ 1
Ya as 'a' is the first character
27th Dec 2022, 9:04 AM
Purva
Purva - avatar
0
Output is ab But How ??? It must be 'abc'
27th Dec 2022, 8:50 AM
Amit Joe
Amit Joe - avatar
0
Ok I understand it check only a of 'ab' in condition x>'c'
27th Dec 2022, 8:52 AM
Amit Joe
Amit Joe - avatar
0
Thanks,I understand but i only want to print the x value at last
27th Dec 2022, 8:54 AM
Amit Joe
Amit Joe - avatar
0
Ok
27th Dec 2022, 8:59 AM
Amit Joe
Amit Joe - avatar
0
Purva Bharadiya it only check the value of a in 'ab'.m'I right?
27th Dec 2022, 9:03 AM
Amit Joe
Amit Joe - avatar