Before we start writing
Let's start by learning how to use Python's interactive shell.
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.
(рис 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 *), like so:
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.
In programming (and also in mathematics), whole numbers like 4, 0, and 99 are called integers. Numbers with 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
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.
(рис 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.)
(рис 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, 2.5 are numbers that are not integers.)
This is like how a cat is a type of
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!
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
This may not seem important, but a lot of
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.
When we program, we will often want to save the values that our expressions evaluate to so we can use them later in the program. We can store values in variables.
Think of variables like a box that can hold values. You can store values inside variables with the = sign (called the assignment operator). For example, to store the value 15 in a variable named "spam", enter spam = 15 into the shell:
>>> spam = 15 >>>
You can think of the variable like a box with the value 15 inside of it (as shown in Figure 2.4). The variable name "spam" is the label on the box (so we can tell one variable from another) and the value stored in it is like a small note inside the box.
When you press Enter you won't see anything in response, other than a blank line. Unless you see an error message, you can assume that the successfully. The next >>> prompt will appear so that you can type in the next instruction.
(рис 2.4) Variables are like boxes that can hold values in instruction has been executed them.
This instruction (called an assignment statement) creates the variable spam and stores the value 15 in it. Unlike expressions, statements are instructions that do not evaluate to any value, which is why there is no value displayed on the next line in the shell.
It might be confusing to know which instructions are expressions and which are statements. Just remember that if the instruction evaluates to a single value, it's an expression. If the instruction does not, then it's a statement.
An 15 by itself is an expression. Expressions made up of a single value by itself are easy to evaluate. These expressions just evaluate to the value itself. For example, the expression 15 evaluates to 15!
Remember, variables store values, not expressions. For example, if we had the statement, spam = 10 + 5, then the expression 10 + 5 would first be evaluated to 15 and then the value 15 would be stored in the variable, spam.
The first time you store a value inside a variable by using an
Now let's see if we've created our variable spam into the shell by itself, we should see what value is stored inside the variable spam.
>>> spam = 15 >>> spam 15 >>>
Now, spam evaluates to the value inside the variable, 15.
And here's an interesting twist. If we now enter spam + 5 into the shell, we get the integer 20, like so.
>>> spam = 15 >>> spam + 5 20 >>>
That may seem odd but it makes sense when we remember that we set the value of spam to 15. Because we've set the value of the variable spam to 15, writing spam + 5 is like writing the expression 15 + 5.
If you try to use a variable before it has been created, Python will give you an error because no such variable would exist yet. This also happens if you
We can change the value stored in a variable by entering another
>>> spam = 15 >>> spam + 5 20 >>> spam = 3 >>> spam + 5 8 >>>
The first time we enter spam + 5, the expression evaluates to 20, because we stored the value 15 inside the variable spam. But when we enter spam = 3, the value 15 is replaced, or overwritten, with the value 3. Now, when we enter spam + 5, the expression evaluates to 8 because the value of spam is now 3.
To find out what the current value is inside a variable, just enter the variable name into the shell.
Now here's something interesting. Because a variable is only a name for a value, we can write expressions with variables like this:
>>> spam = 15 >>> spam + spam 30 >>> spam - spam 0 >>>
When the variable spam has the integer value 15 stored in it, entering spam + spam is the same as entering 15 + 15, which evaluates to 30. And spam - spam is the same as 15 - 15, which evaluates to 0. The expressions above use the variable spam twice. You can use variables as many times as you want in expressions. Remember that Python will evaluate a variable name to the value that is stored inside that variable, each time the variable is used.
We can even use the value in the spam variable to assign spam a new value:
>>> spam = 15 >>> spam + 5 20 >>>
The spam = spam + 5 is like saying, "the new value of the spam variable will be the current value of spam plus five." Remember that the variable on the left side of the = sign will be assigned the value that the expression on the right side evaluates to. We can also keep increasing the value in spam by 5 several times:
>>> spam = 15 >>> spam = spam + 5 >>> spam = spam + 5 >>> spam = spam + 5 >>> spam >>>
Overwriting Variables
Changing the value stored inside a variable is easy. Just perform another
>>> spam = 42 >>> print(spam) 42 >>> spam = 'Hello' >>> print(spam) Hello
Initially, the spam variable had the integer 42 placed inside of it. This is why the first print(spam) prints out 42. But when we execute spam = 'Hello', the 42 value is tossed out of the variable and forgotten as the new 'Hello' string value is placed inside the spam variable.
Replacing the value in a variable with a new value is called overwriting the value. It is important to know that the old value is permanently forgotten. If you want to remember this value so you can use it later in your program, store it in a different variable before overwriting the value:
>>> spam = 42 >>> print(spam) 42 >>> oldSpam = spam >>> spam = 'Hello' >>> print(spam) Hello >>> print(oldSpam) 42
In the above example, before overwriting the value in spam, we store that value in a variable named oldSpam.
When we program we won't always want to be limited to only one variable. Often we'll need to use multiple variables.
For example, let's assign different values to two variables named eggs and fizz, like so:
>>> fizz = 10 >>> eggs = 15
Now the fizz variable has 10 inside it, and eggs has 15 inside it.
(рис 2.5) The "fizz" and "eggs" variables have values stored in them.
Without changing the value in our spam variable, let's try assigning a new value to the spam variable. Enter spam = fizz + eggs into the shell then enter spam into the shell to see the new value of spam. Can you guess what it will be?
>>> fizz = 10 >>> eggs = 15 >>> spam = fizz + eggs >>> spam 25 >>>
The value in spam is now 25 because when we add fizz and eggs we are adding the values stored inside fizz and eggs.
In this chapter you learned the basics about writing Python instructions. Python needs you to tell it exactly what to do in a strict way, because computers don't have common sense and only understand very simple instructions. You have learned that Python can evaluate expressions (that is, reduce the expression to a single value), and that expressions are values (such as 2 or 5) combined with operators (such as + or -). You have also learned that you can store values inside of variables in order to use them later on.
In the next chapter, we will go over some more basic concepts, and then you will be ready to program!
Для получения официальных документов о завершении программы дополнительного профессионального образования (удостоверения о повышении квалификации, дипломов о профессиональной переподготовке и MBA) необходимо предоставить:
Внимание! Вы можете не заказывать доставку бумажной версии официального документы, а скачать его в электронном виде и распечатать самостоятельно. Информация о выданном документе в течение 1 месяца загружается в Федеральную информационную систему «Федеральный реестр сведений о документах об образовании и (или) о квалификации, документах об обучении» - ФИС ФРДО.
Доступ на новый сайт осуществляется с использованием адреса электронной почты, который был указан вами при регистрации на "старом". Мы постарались перенести все ваши данные с прежнего ресурса, однако не исключена вероятность потери части информации.
При возникновении проблемы со входом, воспользуйтесь функцией сброса пароля
Если вы обнаружите несоответствия, пожалуйста, сообщите нам.