Finally, I would like to write a Python program in earnest. There are many explanations of basic Python grammar, which can be boring at first.
I would like to try not to get bored by incorporating playful elements while using graphic drawing using Python as appropriate.
You may want to do a lot with Python, but it's still difficult to write a program without an overview of the program's grammar. Let's start with the basics.
Now, the basis of the grammar is ** variables **. Variables appear not only in the Python language, but in any other language, including Java and C, so make sure you understand them.
As I explained in Chapter 1, I was able to add the entered numbers. There are various operators for numbers in Python. Various operators will appear after this, but the following are called ** arithmetic operators **.
operator | meaning | Writing example |
---|---|---|
+ | Addition (addition) | 11 + 3 |
- | Subtraction (subtraction) | 11 -3 |
* | Multiplication | 11 * 3 |
/ | Division (division) | 11 / 3 |
// | Division (division/Merchandise only) | 11 // 3 |
% | Division (division/Surplus only) | 11 % 3 |
** | Exponentiation | 11 ** 3 (113) |
Let's actually check the operation. This time we will run the ** Python Console **. After launching PyCharm, launch the console as described in [01-03].
Please actually enter the calculation formula on the Python console and check the operation. You can try it with your favorite numbers. I think the result is as follows.
>>>11 + 3
14
>>>11 - 3
8
>>>11 * 3
33
>>>11 / 3
3.6666666666666665
>>>11 // 3
3
>>>11 % 3
2
>>>11 ** 3
1331
All operations are used, but in the programming world ** // ** (division quotient only) and **% ** (remainder) are commonly used. For example, when calculating time, it is common to find "minutes" from "seconds". When calculating how many minutes and seconds 3327 seconds is
>>>3327 // 60
55
>>>3327 % 60
27
If you calculate, you can calculate it as 55 minutes 27 seconds.
In the above, we performed the calculation of two numbers, but the calculation can also calculate three or more numbers. For example, the following formula
10 + 2 * 3
If you enter and calculate, ** 16 ** will be output. For this, the same rule applies to the calculation order that has been performed on the desk so far.
There are two main types of numerical values. More on this at the end of Chapter 2, but there are the following types of numbers:
--Integer
Example) 10, -7, 321 etc.
--Floating point number (real number)
Example) 3.14, -2.718, 6.02 × 10 23 </ sup> etc.
To learn programming, you must understand the concept of ** variables **. For example, the following Python program,
x = 25
Let's think about it. In mathematics, ** x means 25 (x is equal to 25) **. However, in programming, the above means to assign (or assign) the number 25 to ** x **. In difficult terms, = (equal) is called the ** assignment operator **.
Also, in the introductory book of programming etc., explain that put 25 in the box named ** x ** as shown in (Fig. 1) </ font> below. It seems that you often do this, but here, add the tag x to the value ** 25 ** (Fig. 2) </ font>.
Please remember this image because it will be easier to understand later when you explain object-oriented programming later. However, for the sake of convenience, we will use the expression assignment. (It's not exactly an assignment)
Now let's actually write a Python program using variables. Try entering the following program from the ** Python Console **.
>>>num1 = 20
>>>num2 = 30
If you do so, you will not see any execution results, but the values are actually assigned. (Values are tagged with num1 and num2 respectively)
Next, enter the following and you will see the values "20" and "30".
>>>num1
20
>>>num2
30
In addition, addition, subtraction, multiplication and division are performed using variables. You can check the calculation result by inputting as follows.
>>>num1 + num2
50
>>>num1 - num2
-10
>>>num1 * num2
600
>>>num1 / num2
0.6666666666666666
I used the names num1 and num2 for the variable names this time, but in reality, you can create your own names. (Some variable names cannot be used, such as ** reserved words **. Please check the details.)
Try entering the following code from the ** Python Console **.
>>>num = 10
>>>num
10
As I explained earlier, this means assigning 10 to a variable called num. It is displayed and the numerical value of 10 is output.
Now, try entering the following code.
>>>num = num + 1
In that state, display the contents of num. Then you will see the following display.
>>>num
11
First, the formula num = num + 1, but it is an equation that does not hold mathematically. However, in the world of Python programs, it is not "equal", and the result of calculating num + 1 is labeled as num. It will be easier to understand if you think as follows.
num ← num + 1 (Substitute the result of adding 1 to num to num)
In addition, this display is also calculated as follows: Try entering the code.
>>>num += 1
In that state, display the contents of num. Then you will see the following display.
>>>num
12
** num + = 1 ** is the same calculation as ** num = num + 1 **. I explained it with addition now, but you can do the same with subtraction, multiplication, and division, so please try it.
>>>num -= 3
>>>num
9
This is the same as calculating ** num = num --3 **.
in this way,
The description of ** variable name = variable name operator value ** or ** variable name operator = value ** is called ** assignment operator **.
It will always appear in the ** control syntax (loop statement, etc.) described later. Please hold it down.
This time, I learned about basic operations and variable handling. As for operators, we introduced ** arithmetic operators ** and ** assignment operators **. The ** assignment operator ** often appears in control syntax described later, so be sure to wear it.
Recommended Posts