PYTHON DICTIONARY
CommentsData typesVariablesIf ElseIf Elif ElseFunctionsFor loopWhile loopData structuresString slicingUser defined functionLocal/Global variableValidation and TryMethodsFile I/OPseudocodeModules |
What is a For Loop?
The "in" and "not in" operatorsPython uses lots of different "operators"
There are mathematical operators like + / % Comparison operators like > < == Below is the "in" and "not in" operator. The "in" operator is used a lot with For loops. Run the code below to see what it does. Try changing the first numbers and run it again. "in" really means "is a member of". "not in" really means "is NOT a member of". So, "in" and "not in" are called "Membership operators" range() function introducedWelcome to the range function
Run the code below Change the number in the range brackets, then run it again See how the "in" operator works with the range function A for loop using an inputIn this example, a name is being asked to be inputted and is used in the body of the loop. How many times will the name be displayed? Run the code then change the 3 to a 2 and run it again.
Python challenge - For loop with range() functionEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
In this example, the variable called "counting" is holding each element of "range(10)". Range(10) are the numbers 0 to 9. First it holds the first element "1" then then "2" and so on. In the loop body, each number is to be displayed on screen one at a time. Try the code out in a code editor to see the result.
range (Start, Stop)
range (Start, Stop, Step)
Python challenge - For loop with list data structureEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
In this example, a list called animals is declared and it holds five animals. The variable called "nextAnimal" is holding each element of the list called "animals". First the variable holds the first element "cat" then then "dog" and so on. In the loop body, the sentence "the animal is a" and then each animal is to be displayed on screen one at a time. Try the code out in a code editor to see the result.
Python challenge - For loop with list data structure and string slicingEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
In this example a list is declared that contains six animals. The code then enters a loop with a condition that specifies that the first letter in each animal must be a letter "c". If this condition is met, then only the animals that begin with a "c" will be displayed on screen. Try the code out in a code editor to see the result.
Python challenge - For loop with list data structure and variablesEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
In this example, a list is declared with some prices in it. A loop then declared and in the loop body each item has a 50% discount applied to it and then the discount is subtracted from the item and stored in a variable called newPrice. This new price is then displayed on the screen. Try the code out in a code editor to see the result.
Index in rangeEver had an "index out of range" error? It means you are attempting to access an element in a list e.g. accessing a 6th element in a list with 5 elements.
|