Skip to main content

FOR LOOP IN PYTHON



FOR LOOP IN PYTHON

Loops are used to execute a piece of code or set of statements multiple times in a program as long as the condition is true. For loop is the one of the loop structure in Python. It has the ability to iterate over the items of any sequence using “range”, “in” and some other predefined keywords.

Syntax:

For iterating_var in sequence:
            Statement(s)


Here, a sequence is created which have an expression either defined by list, tuple and string. The first item of that sequence is assigned for iteration to iterating_var. After that, the block of statements are executed. The loop will iterate or execute until the complete sequence is over. When the sequence is completed, program comes out of the loop and remaining statements are executed. In Python language, during any of the decision making, control statements and looping indentation is very important because, it specifies the set of statements to be a part of single code or block. Since no brackets are used to specify the complete code with in, indentation is used.

Flowchart:

   
Watch my video to get the detailed view of how to use while loop with a practical explanation.


Comments

Popular posts from this blog

DECISION MAKING STATEMENTS

DECISION MAKING STATEMENTS During the execution of the program, when there is a specific application to check the condition and make a proper decision, Decision making statements are used to anticipate specific actions which has to be taken depending upon the condition. Decision making statements are evaluated using TRUE and FALSE values. For example, if a particular condition is TRUE what outcome is expected and when, FALSE what outcome is expected. Depending on the application of program, set of statements are written into the code to perform a particular task. Python provides following types of decision making statements: Ø If statement: It is normal If statement which has condition in terms of Boolean expression, comparison operators, etc. Ø If..else statement: In this case, an If is followed by an else statement. It means ‘IF’ condition turns to be FALSE than ‘ELSE’ will be executed automatically Ø Ladder If.. else if .. else if: This is an extension o...

COMPARISON, LOGICAL, ASSIGNMENT AND BITWISE OPERATORS

COMPARISON, LOGICAL, ASSIGNMENT AND BITWISE OPERATORS In this blog we will see about Arithmetic operators and understand how to use it with Python IDLE to perform certain calculations. PYTHON COMPARISON OPERATORS OPERATOR DESCRIPTION EXAMPLE        == Checks whether LHS is equal to RHS, then the condition becomes true 10==10 is true        != Checks whether LHS is not equal to RHS, then the condition becomes true 2!=3 is true        > Checks whether LHS is greater than RHS, then the condition becomes true 6>3 is true        < Checks whether LHS is less than RHS, then the condition becomes true 6<10 is true        >= Checks whether LHS is gre...

DATATYPE – PYTHON LISTS AND SLICING

DATATYPE – PYTHON LISTS AND SLICING Python lists are the most versatile and useful datatype. Lists can store a sequence of data within it, which are separated by comma and enclosed inside square brackets. The good feature of Python Lists is, the data or items stored can be of mixed data types like strings, integer, float, etc. which can be stored into a single sequence. Example: List1 = ["A", 21, 78, "Red", 50, "Apple"] In this way a list is created, items inside the list are separated by a comma and string is always stored into double quote as explained earlier. Usually when there is multiple data stored inside an array, list, tuple, etc. the indexing of it starts from zero. For example List1[0] = ‘A’ and it ends on n-1. There are many other ways of accessing items inside a list as explained in my video. You can perform addition of two or more lists. Lets see something more about Lists in detail. Some functions to be used with Lis...