Опубликован: 12.07.2013 | Уровень: специалист | Доступ: платный
Лекция 2:

The Interactive Shell

< Лекция 1 || Лекция 2: 12 || Лекция 3 >
Аннотация: Before we start writing computer games, we should learn some basic programming concepts first. These concepts are values, operators, expressions, and variables.

Topics Covered In This Chapter:

  • Integers and Floating Point Numbers
  • Expressions
  • Values
  • Operators
  • Evaluating Expressions
  • Storing Values in Variables

Before we start writing computer games, we should learn some basic programming concepts first. These concepts are values, operators, expressions, and variables. We won't start programming in this chapter, but knowing these concepts and the names of things will make learning to program much easier. This is because most programming is built on only a few simple concepts combined together to make advanced programs.

Let's start by learning how to use Python's interactive shell.

Some Simple Math Stuff

To open IDLE on Windows, click on Start > Programs > Python 3.1 > IDLE (Python GUI). With IDLE open, let's do some simple math with Python. The interactive shell can work just like a calculator. Type 2+2 into the shell and press the Enter key on your keyboard. (On some keyboards, this is the RETURN key.) As you can see in Figure 2.1, the computer should respond with the number 4; the sum of 2+2.

Type 2+2 into the shell.

Рис. 2.1. Type 2+2 into the shell.

As you can see, we can use the Python shell just like a calculator. This isn't a program by itself because we are just learning the basics right now. The + sign tells the computer to add the numbers 2 and 2. To subtract numbers use the - sign, and to multiply numbers use an asterisk (*), like so:

Таблица 2.1. The various math operators in Python.
2+2 addition
2-2 subtraction
2*2 multiplication
2/2 division

When used in this way, +, -, *, and / are called operators because they tell the computer to perform the specified operation on the numbers surrounding them.

Integers and Floating Point Numbers

In programming (and also in mathematics), whole numbers like 4, 0, and 99 are called integers. Numbers with fractions or decimal points (like 3.5 and 42.1 and 5.0) are not integers. In Python, the number 5 is an integer, but if we wrote it as 5.0 it would not be an integer. Numbers with a decimal point are called floating point numbers. In mathematics, 5.0 is still considered an integer and the same as the number 5, but in computer programming the computer considers any number with a decimal point as not an integer.

Expressions

Try typing some of these math problems into the shell, pressing Enter key after each one.

2+2+2+2+2
8*6
10-5+6
2  +      2

Figure 2.2 is what the interactive shell in IDLE will look like after you type in the instructions above.

What the IDLE window looks like after entering instructions.

Рис. 2.2. What the IDLE window looks like after entering instructions.

These math problems are called expressions. Computers can solve millions of these problems in seconds. Expressions are made up of values (the numbers) connected by operators (the math signs). Let's learn exactly what values and operators are.

As you can see with the last expression in the above example, you can put any amount of spaces in between the integers and these operators. (But be sure to always start at the very beginning of the line, with no spaces in front.)

An expression is a made up of values and operators.

Рис. 2.3. An expression is a made up of values and operators.

Numbers are a type of value. Integers are a type of number. But, even though integers are numbers, not all numbers are integers. (For example, fractions and numbers with decimal points like 2.5 are numbers that are not integers.)

This is like how a cat is a type of pet, but not all pets are cats. Someone could have a pet dog or a pet lizard. An expression is made up of values (such as integers like 8 and 6) connected by an operator (such as the * multiplication sign). A single value by itself is also considered an expression.

In the next chapter, we will learn about working with text in expressions. Python isn't limited to just numbers. It's more than just a fancy calculator!

Evaluating Expressions

When a computer solves the expression 10 + 5 and gets the value 15, we say it has evaluated the expression. Evaluating an expression reduces the expression to a single value, just like solving a math problem reduces the problem to a single number: the answer.

The expressions 10 + 5 and 10 + 3 + 2 have the same value, because they both evaluate to 15. Even single values are considered expressions: The expression 15 evaluates to the value 15.

However, if you just type 5 + into the interactive shell, you will get an error message.

>>> 5 +
SyntaxError: invalid syntax

This error happened because 5 + is not an expression. Expressions have values connected by operators, but the + operator always expects to connect two things in Python. We have only given it one. This is why the error message appeared. A syntax error means that the computer does not understand the instruction you gave it because you typed it incorrectly. Python will always display an error message if you enter an instruction that it cannot understand.

This may not seem important, but a lot of computer programming is not just telling the computer what to do, but also knowing exactly how to tell the computer to do it.

Expressions Inside Other Expressions

Expressions can also contain other expressions. For example, in the expression 2 + 5 + 8, the 2 + 5 part is its own expression. Python evaluates 2 + 5 to 7, so the original expression becomes 7 + 8. Python then evaluates this expression to 15.

Think of an expression as being a stack of pancakes. If you put two stacks of pancakes together, you still have a stack of pancakes. And a large stack of pancakes can be made up of smaller stacks of pancakes that were put together. Expressions can be combined together to form larger expressions in the same way. But no matter how big an expression is it also evaluates to a single answer, just like 2 + 5 + 8 evaluates to 15.

< Лекция 1 || Лекция 2: 12 || Лекция 3 >