Magic Spell of Python

Coding CS Learning Study

Python code snippet is given below:

if <condition>:
    print("Hello")
else:
    print("World")

What’s the <condition> so that the code snippet prints “Hello World” ?

.

.

.

.

.

.

.

.

.

Solution:

The below program prints “Hello World”

if print("Hello", end =" "):
    print("Hello")
else:
    print("World")

Explanation:

Here, <condition> = print("Hello", end =" ")

In Python : print() function doesn't return anything (so it's None).

By default Python’s print() function ends with a newline. You can end a print statement with any character/string using this parameter. Let's see

# default end parameter '\n'
print("Hello")
print("World")
-- Output is --
Hello
World

# Now change end parameter value
# ends the output with a <space>
print("Hello " , end = ' ')
print("World" , end = ' ')
-- Output is --
Hello World

Now, what is the output of below code?

print(print())
print("***")
print(print("Hello World"))
print("***")
print(print(print("Hello World")))

You will get

None
***
Hello World
None
***
Hello World
None
None