filmov
tv
Conditional Statements and Loops | Python for Beginners | Quantra Free Course

Показать описание
Timestamp:
00:14 - 01:11 - Conditional statement
01:11 - 02:00 - 'If' conditional statement in Python
02:00 - 03:57 - Loop statement
02:11 - 03:57 - 'For' loop statement
In the previous section, we have extensively covered the Pandas library in Python. In this section, we are going to study about: Conditional Statements - The ‘if’ statement and Loops - The ‘for’ loop.
Let us begin with conditional statements. What if there were no decisions to be made in life? This is purely a rhetorical question, since there are always decisions to be made, both in real life and, of course, in programming.
Conditional statements help us to make decisions in programming based on conditions. So far we have accomplished predefined tasks, but what makes programming more interesting is the ability to test a variable against a value and to make the program act in a particular way.
We can check if a particular condition is met by the variable, or not and then program responses separately for these two cases. These statements are known as ‘if’ conditional statements. Consider this simple statement: “If it rains today, I will take an umbrella.” Here, umbrella is your variable which you would be taking along, if and only if, it rains. In Python, the syntax for an ‘if ‘ statement is as follows: Let us see this example.
Here, if the “condition 1” is True, the statements in the ‘statement_block_1’ will be executed. If not, then “condition_2” would be checked.
If “condition_2” is evaluated to be true, then the statements in ‘statement_block_2’ would be executed. If “condition_2” is also false, then the “condition_3” would be checked. If “condition_3” is evaluated to be true, then the statements in ‘statement_block_3’ will be executed. In the programming languages, there are many situations when you need to execute a block of code several number of times.
A loop statement allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement. Let us have a look at a ‘for’ loop. The general syntax for a ‘for’ loop is as follows.
Here, the block of statements within the loop will get executed, until all ‘sequence’ elements get exhausted. Once all sequence elements are exhausted, the program will come out of the loop.
For further clarity, consider this simple example of closing prices of Stock ABC. Here, we have created this loop where the closing prices are stored in a list ‘Close_Price_ABC’. This list is our ‘sequence’. Now, let us create a ‘for’ loop.
Here i is the variable which helps you to temporarily store each element of the sequence during a single round of the iteration process.
The variable ‘i’ first stores the value ‘300’ in it and runs it through the loop to execute the statements. Here, we have placed a condition that if ‘i == 300’ we will print “No new positions”. Hence, as you can see, this is the first statement in our output. Now, ‘i’ will run through the sequence and pick the second element of the sequence which is ‘305’. It will run it through the statements of the loop. When i == 305, it will execute the block where ‘if i>300: print (“We Sell”). Check the second output. Similarly, it will keep executing all the elements of the loop. You may pause the videos to check the outputs of all the elements of our sequence.
Finally, it will come out of the loop and execute the other part of the program i.e. print (“ We are now out of the loop”). The above lecture is just an introduction to ‘Conditional Statements’ and ‘Loops’. For more, Stay tuned for the IPython Notebooks. Keep Learning!
Quantra is an online education portal that specializes in Algorithmic and Quantitative trading. Quantra offers various bite-sized, self-paced and interactive courses that are perfect for busy professionals, seeking implementable knowledge in this domain.
Комментарии