I've been in the Ai classroom lately I will write a summary.
The unspoken output. The quotes used can be single or double
print('Hello World')
#comment
It can be calculated as follows. You can use it like this
・ Addition: "+" ·subtraction:"-" ·multiplication:"*" ·division:"/" ・ Calculation of remainder: "%" ・ Exponentiation: "**"
print(3+6)
#Output result
9
If you enclose it in quotation marks It will be a character string.
print('3+6')
#Output result
3+6
Next, the basic variables
n = 3
#n is the variable name, 3 is the value to enter
print(n)
#3 and output
print(n+5)
#It can be calculated in the same way, and 8 is output.
It's not unusable, but Japanese is OK But not very popular as a definition
Delete it in the following form.
del variable name
You can connect as follows.
n = "Tanaka"
print("Name is" + n + "is")
##output:The name is Tanaka.
There are types such as letters and numbers. The types are as follows
str type: string int type: integer float type: floating point
print(type(Variable name))
age = 17
print(type(age)) #When you want to know the type of age
#Output result
<class 'int'>
If you try to connect with different types An error will occur, so convert as follows.
str (): Convert to string int (): Convert to integer value float (): Convert to numeric type including decimal point
height = 150
print("How tall are you" + str(height) + "cm.")
Write as below and return true or falsa.
a == b # a and b are equal a! = b # a is not equal to b (inverted) a> b # a is greater than b a> = b # a is greater than or equal to b a <b # a is less than b a <= b # a is less than or equal to b
if: Executes the following processing that is 4 indented when the conditions on the left are met. else: Execute if if and elif do not hold elif: Write if you want to process and condition separately between if and else.
Can be executed with only basic if Added else and elif in some cases
n = 2
if n == 1: #If n is equal to 1, perform the following processing
print("This is the first process.")
elif n == 2: #If n is equal to 2, perform the following processing
print("This is the second process.")
elif n == 3: #If n is equal to 3, perform the following processing
print("This is the third process.")
else: #If all the above three conditional expressions are not satisfied, the following processing is performed.
print("This is the fourth and subsequent processes.")
Conditional expression A and conditional expression B # If both conditional expressions of A and B are True, processing is performed. Conditional expression A or conditional expression B # If either conditional expression is True, processing is performed. not Conditional expression # Returns False when the conditional expression is True, True when the conditional expression is False.
Example of use
n_1 = 15
n_2 = 29
print(n_1 > 8 and n_1 < 14)
# true
print(not n_1 ** 2 < n_2 * 5)
# true
Recommended Posts