There are four basic grammars that Python beginners should learn. ・ Print ·comment(#) -Variable assignment (a = value) ・ Four arithmetic operations (+,-, *, /,%)
The first thing to remember is the print. If you want to output on the screen, enter the following.
python
print("Hello World")
Hello World will be displayed on the screen.
If you want to output the "Hello" is
python
print("Hello")
Characters can be output by enclosing them in "". You can also handle "numerical values". Numerical values, unlike strings, do not need to be enclosed in quotation marks. Numerical values can be added and subtracted using the same symbols "+" and "-" as in mathematics, as shown in the figure on the left. All numbers and symbols are written in half-width characters. Also, it is not necessary to have a half-width space before and after the symbol, but it will be easier to see the code if you include it.
If you add #, it will be commented out. It is not executed as a program statement.
python
print("Hello World")#If you want to output to the screen, use print.
By entering a comment, you will be able to understand it immediately when you look back at the comment later.
It is also useful when sharing with others.
Next is variable assignment.
A variable is like a box that holds data (values). By giving a name (variable name) to this box (variable), you can use that name to put a value in the variable or retrieve the value from the variable.
python
name = 'Kita Taro'
Variables are defined by "variable name = value" like this. Variable names do not need to be quoted.
Keep in mind that the "=" in programming does not mean "equal", but "substitutes the right side for the left side".
The four arithmetic operations are as follows.
Calculation | symbol |
---|---|
addition | + |
subtraction | – |
multiplication | * |
division | / |
Remainder calculation | % |
python
print(1 + 1)
print(8 - 4)
print(5 * 5)
print(5 / 2)
print(5 % 2)
#Enclose in quotation marks
print('9 + 3')
>_Output result
2
4
25
2.5
1
#⬇️ This is what happens when you enclose it in quotation marks
9 + 3
In this way, "1 + 1" outputs the calculation result "2". On the other hand, if you enclose it in quotation marks like "'9 + 3'", it will be interpreted as a character string and "9 + 3" will be output as it is. In this way, in the programming world, strings and numbers are treated as completely different things. In programming, you can also perform calculations other than addition and subtraction. Multiplication is represented by "*" and division is represented by "/". You can also calculate the remainder of the division with "%". These three symbols are a little different from the symbols used in mathematics, so be sure to remember them.
Recommended Posts