Python: Control Statements
In this article, we take a look at the major control statements in Python. Primary we look at the control statements if-elif-else, for,while and control exit statement break . We list sample code for each of these and use in-line comments to explain what the code line does. Note that we declare the same functions in a class to adhere to object-oriented semantics. You can easily copy/paste the code in your Python code. if-elif-else statement class DemoClass: def IfThenElse(self): # Using integer inp = int(input("Enter a number: ")) if inp < 0: print("Number is negative") elif inp == 0: #Else-if print("Number is 0") else: print("Number is positive") # Using a string input inp = input("Enter your name: ") if inp == "Albert": print("Are you Albert Einstein ?") elif inp == "Blaise"