I learned Python basic grammar

Python basic grammar

There are four basic grammars that Python beginners should learn. ・ Print ·comment(#) -Variable assignment (a = value) ・ Four arithmetic operations (+,-, *, /,%)

Print

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.

comment(#)

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.

Variable assignment (variable name = value)

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".

Four arithmetic operations (+,-, *, /,%)

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.

Python article summary

Recommended Posts

I learned Python basic grammar
Python3 basic grammar
Python basic grammar / algorithm
Python basic grammar (miscellaneous)
[Python] I personally summarized the basic grammar.
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
Basic Python 3 grammar (some Python iterations)
Refactoring Learned in Python (Basic)
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
What I learned in Python
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Java and Python basic grammar comparison
I learned about processes in Python
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Basic grammar of Python3 system (dictionary)
RF Python Basic_01
python grammar check
I started python
Basic Python writing
[Basic grammar] Differences between Ruby / Python / PHP
Python grammar notes
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
RF Python Basic_02
I tried to touch Python (basic syntax)
Basic grammar of Python3 system (included notation)
VBA user tried using Python / R: basic grammar
[Go] Basic grammar ① Definition
Python basic course (12 functions)
Python I'm also basic
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Basic sorting in Python
Python basic course (9 iterations)
[python] class basic methods
I tried Python> autopep8
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python3 cheat sheet (basic)
Python Basic Course (Introduction)
I implemented Python Logging
Python basic memorandum part 2
python basic on windows ②
[Go] Basic grammar ③ Pointer
Python basic memo --Part 2
Relearn Python (Algorithm I)
Python basic course (13 classes)
Basic Python command memo
Basic knowledge of Python
OpenCV basic code (python)
I tried Python> decorator
Python basic memo --Part 1
Why I chose Python
python memorandum super basic