From the course: Programming Foundations: Fundamentals

Basic statements and expressions - Python Tutorial

From the course: Programming Foundations: Fundamentals

Basic statements and expressions

- You could have all of the ingredients to bake a cake, but if you don't have a recipe, then there's no cake. It's the same way with statements. Statements are the building blocks of any program and are the individual actions that you want your program to take. Without statements, there's no program. Each statement can be made up of keywords, expressions, and operators. We'll talk more about keywords later. For now, we'll focus on expressions and operators. Operators are symbols that tell the computer to perform an action with some input, and the good news is, you're already familiar with many of the operators that are used in programming from your primary school education. In this table, you can see a partial listing of some operators that should be familiar to you. The plus symbol used for addition, minus for subtraction, an asterisk or a star to represent multiplication, and finally, a forward slash for division. These are known as arithmetic operators, as they take numbers for their input and perform an arithmetic operation. Flash back to primary school. When we have three plus two, the plus sign is the operator and three and two are the input, also known as operands. In programming, the combination of operators and operands that break down to a single value are called expressions. Let's look at a few examples. If we take the following, 10 plus two, what does that equal? 12, correct. 10 plus two is our expression and the 12 is what our expression evaluated to or equaled. Let's try another one. 10 divided by two plus three. What does this expression evaluate to? Yes, it's eight. Now let's look at another expression where the answer may surprise you. Two plus three times six. What do you think this expression evaluates to? It's 20. Now if you thought 30, that's understandable. But in programming, the default order of operations is the same as in mathematics. The plus or addition operator in our example is evaluated after the asterisk or multiplication operator. That's why we get 20 instead of 30. If you're from the United States like me, you may have been taught the phrase, please excuse my dear aunt Sally as a way of remembering the order of operations or PEMDAS. That stands for P, parentheses first, E, exponents, MD, multiplication and division from left to right, and finally, AS, addition and subtraction, left to right. It's important to keep the order of operations in mind as your program could behave unexpectedly based on how an expression is evaluated. If we go back to our last expression, it evaluated to 20 because of the order of operations. But what if we, as the programmer, wanted it to process the addition first? In that case, we would add in the parentheses around our addition operation and then we would get the result that we wanted, 30. Computer programs are made up of statements, statements that can perform a mathematical calculation, display something to the screen, or even make a choice between two code paths. As we continue to explore the fundamentals of programming, you'll see just how powerful this concept really is.

Contents