![]() ![]() They tend to have strong opinions about what looks good and what doesn’t, and they don’t like to be shoehorned into a specific choice. Many programmers don’t like to be forced to do things a certain way.Use of indentation to define blocks forces you to maintain code formatting standards you probably should be using anyway.This sort of mistake is virtually impossible to make in Python. It’s possible to write code that is indented in a manner that does not actually match how the code executes, thus creating a mistaken impression when a person just glances at it. In programming languages that do not use the off-side rule, indentation of code is completely independent of block definition and code function. ![]() Python’s use of indentation is clean, concise, and consistent.Debate about the merits of the off-side rule can run pretty hot. On the whole, programmers tend to feel rather strongly about how they do things. Which Is Better?īetter is in the eye of the beholder. Other languages, such as Algol and Pascal, use keywords begin and end to enclose blocks. Compound if Statement in C/C++, Perl, and Java Thus, a compound if statement in Python looks like this:Ĭ/C++, Java, and a whole host of other languages use curly braces in this way. In a Python program, contiguous statements that are indented to the same level are considered to be part of the same block. Now you know why: indentation is used to define compound statements or blocks. Recall from the previous tutorial on Python program structure that indentation has special significance in a Python program. Python is one of a relatively small set of off-side rule languages. (The term is taken from the offside law in association football.) Languages that adhere to the off-side rule define blocks by indentation. Python follows a convention known as the off-side rule, a term coined by British computer scientist Peter J. ![]() Virtually all programming languages provide the capability to define blocks, but they don’t all provide it in the same way. When it is the target of an if statement, and is true, then all the statements in the block are executed. A block is regarded syntactically as a single entity. The usual approach taken by most programming languages is to define a syntactic device that groups multiple statements into one compound statement or block. There needs to be some way to say “If is true, do all of the following things.” In all the examples shown above, each if : has been followed by only a single. (If the weather isn’t nice, then I won’t do any of these things.) Remove ads Grouping Statements: Indentation and Blocksīut let’s say you want to evaluate a condition and then do more than one thing if it is true: Lastly, you’ll tie it all together and learn how to write complex decision-making code.Next, using the if statement as a model, you’ll see why control structures require some mechanism for grouping statements together into compound statements or blocks.First, you’ll get a quick overview of the if statement in its simplest form.The outline of this tutorial is as follows: It allows for conditional execution of a statement or group of statements based on the value of an expression. In a Python program, the if statement is how you perform this sort of decision-making. (It’s implied that if the weather isn’t nice, then I won’t mow the lawn.) If the weather is nice, then I’ll mow the lawn. In the real world, we commonly must evaluate information around us and then choose one course of action or another based on what we observe: ![]() Here’s what you’ll learn in this tutorial: You’ll encounter your first Python control structure, the if statement. A control structure directs the order of execution of the statements in a program (referred to as the program’s control flow). That is where control structures come in. Frequently, a program needs to skip over some statements, execute a series of statements repetitively, or choose between alternate sets of statements to execute. Everything you have seen so far has consisted of sequential execution, in which statements are always performed one after the next, in exactly the order specified.īut the world is often more complicated than that. Watch it together with the written tutorial to deepen your understanding: Conditional Statements in Python (if/elif/else)įrom the previous tutorials in this series, you now have quite a bit of Python code under your belt. Watch Now This tutorial has a related video course created by the Real Python team. ![]()
0 Comments
Leave a Reply. |