First off what it comes to control flow and python is if statements. This is one of the first things I would want to get solid not just with python, but any new language. They work more or less like if statements in any other language so maybe there is only so much to write about them, but never the less in this section I am going to go over everything that I think is important.
I am not sure if it makes sense to start with studying built in functions first, or flow control. In any case when it comes to getting start with if statements there are a few things that one needs to have at least a basic understand of first. Such as the fact that the word if in python is a keyword, and that in order to use an if statement you need to given it an expression. In addition an expression that is used with an if statement will evaluate to a boolean data type, and that the bool built in function us a way to create a boolean value from another value. I will not be getting into all of this in detail here, however I do think that I should at least touch base on all of it, before even getting to some basic if statement examples.
So in this section I will be going over some very basics of python, and then a few if statement examples.
The expression that is used with an expression should evaluate to a boolean value. It might be a little off topic to get into booleans and the bool built in function, but it might help to gain a batter understanding of if statements to start with this.
Expressions are a topic that deserve a whole other post, and maybe even several posts actually. Expressions can be very simple, however they can also get pretty complex. However for the sake of this post on just control flow in python it might be called for to just stick to some basic expressions.
Some basic expressions might just involve comparing two values to see if one is greater than another, the other way around, or if they are equal to each other. In any case the expressions will evaluate to a true, or false boolean value, and this is how expressions relate to the topic of a bool value that I touched base on.
Here are some basic examples of python expressions to start off with when it comes to what I think is a good starting point for if statements.
So now that we have a basic idea of what a boolean value is and what an expression is we can now move on to some basic examples of if statements in python.
To write an if statement, I just need to start with the if keyword, followed by an expression, and then finish with a colon. Then in the next line I just need to intend to start a block of code that will run if the expression evaluates to true.
Another major part of control flow in a language is loops. There are several kinds of loops that come to mind when it comes to languages, however in this section I will be going over while loops in python. These work just like if statements, only they will keep ruining what is in the block of code following the while statement until the expression is given will evaluate to a false value.
The basic idea of a while loop is then just like an if statement, only I start off by typing while rather than if, however that is the only difference in terms of the syntax. It is important to do something in the block of code that will potentially change the value of the expression from false to true, if not then I might end up having an infinite loop.
So a basic example of a while loop might be to set a variable to a starting value such as zero, and then compare the value to another value in the expression. Inside the block of code I would then just need to step the value in a way in which the expression will end up being true at some point.
In the first section on if statements I mentioned that the numerical value of zero will evaluate to a false boolean value when such a value is covered to a boolean type. This fact can be used to ones advantage when it comes to working out an expression for a while loop.
For example the expression for a while loop can just be a variable that holds an integer value, and inside the body of the while loop I am subtraction from that integer value by one. If the starting value of the variable is greater than zero, then one will keep getting subtracted from the variable until the value of zero is reached which evaluates to false, and thus the loop will stop.
Another kind of loop to work with in python is a for loop. Although it is possible to work with lists, with while loops it might be easier to just use a for loop. So far it would seem that a for loop might be a better option when it comes to working with lists, and sequences. So because this is a post on control flow in python I should go over some examples of for loops also, so lets look at a few examples.
So here is a basic for loop example where I am just looping over a list.
I should at least mention the range built in function when it comes to for loops.
One interesting thing about for loops in python is that they can be used to loop over each character in a string.
There is also having nested for loops also, here is a quick example of using nested for loops to loop over the contents of a list of lists.
It might be a gray area if a function is really a part of control flow or not, however in any case I should take a moment to write about a few quick function examples at least.
A function starts of with the def keyword followed by the name of the function. After the name of the function I then have a set of parentheses in which I can have some parameters for the function. Then just like if statements and loops I end the line with a colon, and define the body of code for the function my indenting each line afterwards. Inside the body of the function I can have an optional return value for the function, for this I just need to type return followed by the value that I want the return value of the function to be.
There is also the break and continue keywords that can be used in conjunction with loops.
Great so not I have the basics and beyond worked out when it comes to control flow, however I still have a lot more to learn about when it comes to python. This might be a boring part of learning a new language, however I am all ready starting to scratch the surface when it comes to more interesting aspects of programing with python. When I work out more examples I will of course come back around to this post and see about expanding what I have wrote here farther as needed.