Here, we will introduce the grammar and syntax that people who have started studying the programming language Python should first learn. It is assumed that you are using Python 3 series.
comment.py
#Comment on the first line#It is also possible to write a comment from the middle of the line.
Comment on the second line#An error will occur.
'''
Multi-line comment
Multi-line comment
'''
Note that if you break a comment that starts with'#', the second and subsequent lines will be recognized as the execution range of the program, and an error may occur or unexpected behavior may occur. It is recommended that one line be 79 characters or less, including the script of the program execution range. You can write a comment as a memo of the script, and you can also use it to prevent the script you do not want to execute from being executed (comment out). When you write a "multi-line comment" in a function, it becomes an object that shows the explanation of the function, and it has a slightly different form from the comment mentioned here.
Characters obtained as a result of executing the program can be output by using the print function.
print.py
print('Hello, World!') # ă€ŒHello, World!Is output.
Like other programming languages, Python has data types. If you do not write the script while being aware of this data type, the program will not work and an error will occur, or unexpected behavior will occur.
Literally, it refers to the general "letter".
string.py
print('Hello, World!')
print("Hello, World!")
#It can be enclosed in single quotes or double quotes, but it is common to enclose it in single quotes.
#It is NG to start with single quotes and close with double quotes (or vice versa).
print('Hello, World!") #An error will occur.
print("Hello, World!') #An error will occur.
print('Say "Hello, World!"') #This is OK. If you want to use more quotation marks in the string, enclose them in quotation marks that you haven't used yet.
print("Say 'Hello, World!'") #This is also OK. However, as mentioned above, it is common to enclose the character string in single quotation marks, so it is more common to write it one line above.
It refers to a "numerical value" that can be calculated arithmetically. Arithmetic calculations cannot be performed if the character string is a "number".
number.py
print(123) #Write the numbers as they are, without enclosing them in quotation marks. This is an integer type.
print(123.4567) #You can also write with a decimal point (called a floating point number type).
print('123') #If you write it like this, it will be a "character string of numbers" instead of numbers (arithmetic operations cannot be performed accurately).
print(str(123)) #Conversion from number to string. The output result is the same as the one above.
print(int('123')) #Convert from a character string to a number (integer). int is an abbreviation for integer.
print(float('123.4567')) #Convert from string to floating point type. float means floating point.
print(int(123.4567)) #Floating point types cannot be converted to integer types, resulting in an error.
Data type conversion is confusing in the above example, but it often comes up when dealing with variables. For example, if a number is stored as a value in a variable that is inevitably defined as a character string, and you want to treat it as a number.
It can be either'True'or'False'.
boolean.py
print(True) #"true"
print(False) #"false"
No quotations required. Write only the first letter in uppercase and the second and subsequent letters in lowercase. It is closely related to the if statement.
Indentation has the effect of making it easier to see the mass of scripts (processes). Python is a programming language that emphasizes indentation, and it should be noted that an error will occur if there is excess or deficiency of indentation. This is especially important for if and for statements, and the behavior may change depending on the presence or absence of indentation.
indent.py
print('Hello, World!') # OK
print('Hello, World!') #An example of writing with indentation for 4 single-byte characters. Simply writing this much will result in an error.
Here, we explained how to output the execution result of the program and the data type. If you are new to programming, you should understand it well.
Surprisingly few! ?? "Minimum" knowledge required for programming in a pharmaceutical company
Recommended Posts