+ 1
Trying to understand while loop logic
x = 1 while x < 10: if x%2 == 0: print(str(x) + " is even") else: print(str(x) + " is odd") x += 1 Answer/Output is 1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd So my questions as follows. 1. what is the meaning of if x%2 == 0: print(str(x) + " is even") else: print(str(x) + " is odd") ? 2. Is it possible to pass parameter after print statement in python? if yes what are the scenarios?
3 Antworten
+ 4
% this operator returns the remainder so if x % 2 == 0 this means that after dividing x by 2, if remainder is 0 then that means it's even.
Some other parameters for print are: end, sep, file and flush. If you're interested you can check this out:
https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/built-in/print
+ 4
1. x%2==0 means on dividing x by 2 ,the remainder is 0 or not.
It is a famous condition for checking whether a number is even or odd.[Even nos are divisible by 2].
So if that condition is true then x is even.else print odd.
2. U first go learn the syntax of while loop.which is the parameter u are mentioning.?
+ 3
1. X%2 just means the remainder of x after it has been divided by 2. Assume x is 10. It would be 10%2 which is equal to zero, thus ten is even, but if x was 11, 11%2 would be 1 so x is odd.
2. It is possible to pass the print statement and move on with the rest of the code. If you want to exit the loop and go on with your code, use the "break" statement after the print statement. But if you don't want to leave the loop and just want to move on to the next line, use "continue" the same way as "break"