Control Structures in PythonMost programs don't operate by carrying out a straightforward sequence of statements. A code is written to allow making choices and several pathways through the program to be followed depending on shifts in variable values. All programming languages contain a pre-included set of control structures that enable these control flows to execute, which makes it conceivable. This tutorial will examine how to add loops and branches, i.e., conditions to our Python programs. Types of Control StructuresControl flow refers to the sequence a program will follow during its execution. Conditions, loops, and calling functions significantly influence how a Python program is controlled. There are three types of control structures in Python:
SequentialSequential statements are a set of statements whose execution process happens in a sequence. The problem with sequential statements is that if the logic has broken in any one of the lines, then the complete source code execution will break. Code Output: The result of the subtraction is: 10 The result of the addition is : 30 The result of the multiplication is: 200 Selection/Decision Control StatementsThe statements used in selection control structures are also referred to as branching statements or, as their fundamental role is to make decisions, decision control statements. A program can test many conditions using these selection statements, and depending on whether the given condition is true or not, it can execute different code blocks. There can be many forms of decision control structures. Here are some most commonly used control structures:
Simple ifIf statements in Python are called control flow statements. The selection statements assist us in running a certain piece of code, but only in certain circumstances. There is only one condition to test in a basic if statement. The if statement's fundamental structure is as follows: Syntax These statements will always be executed. They are part of the main code. All the statements written indented after the if statement will run if the condition giver after the if the keyword is True. Only the code statement that will always be executed regardless of the if the condition is the statement written aligned to the main code. Python uses these types of indentations to identify a code block of a particular control flow statement. The specified control structure will alter the flow of only those indented statements. Here are a few instances: Code Output: The initial value of v is 5 and that of t is 4 5 is bigger than 4 The new value of v is 3 and the t is 4 3 is smaller than 4 the new value of v is 4 The value of v, 4 and t, 4, are equal if-elseIf the condition given in if is False, the if-else block will perform the code t=given in the else block. Code Output: The value of v is 4 and that of t is 5 v is less than t RepetitionTo repeat a certain set of statements, we use the repetition structure. There are generally two loop statements to implement the repetition structure:
For LoopWe use a for loop to iterate over an iterable Python sequence. Examples of these data structures are lists, strings, tuples, dictionaries, etc. Under the for loop code block, we write the commands we want to execute repeatedly for each sequence item. Code Output: 2, 4, 7, 1, 6, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, While LoopWhile loops are also used to execute a certain code block repeatedly, the difference is that loops continue to work until a given precondition is satisfied. The expression is checked before each execution. Once the condition results in Boolean False, the loop stops the iteration. Code Output: 2 3 4 5 6 7 8 While loop is completed |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India