Lecture 4 - Control Structures in Python PDF
Lecture 4 - Control Structures in Python PDF
Python.
Loops
Conditional
• In Python, If Statement is used for decision making. It will run the body of
code only when IF statement is true.
• When you want to justify one condition while the other condition is not
true, then you use "if statement".
• The easiest method for making a decision in Python is by using the if
statement. The if statement enables you to select the actions to perform if
the evaluated condition is true.
• The syntax of the if statement is:
• In Python, If Statement is used for decision making. It will run the body of
code only when IF statement is true.
• When you want to justify one condition while the other condition is not
true, then you use "if statement".
• The easiest method for making a decision in Python is by using the if
statement. The if statement enables you to select the actions to perform if
the evaluated condition is true.
• The syntax of the if statement is:
• The if…elif statement in Python is used when you need to choose between
more than two alternatives.
• The elif clause can be used to create a number of conditions that can be
evaluated.
• The syntax of the statement is:
• Note how the lines above are indented. The indentation is important
because it informs Python that the indented statement is the second-level
statement.
• The start argument is the starting number. The stop argument is the last
number (which is not included). The step argument is optional and
defines the difference between each number in the sequence.
• Note: If the step argument is omitted, it defaults to 1. If
the start argument is omitted, it defaults to 0. Note that all arguments
have to be integers.
• The simple example above asks the user to enter a number and then prints
all numbers less than that number. The process will continue until the user
enters 0: