Comments are from #
to the end of the line.
However, #
written in the character string does not function as a comment.
Enclose it in single quotes '
or double quotes "
to get a string.
"sample" #This is a string
Use 3 double quotes.
Below is the content of the edit request from * shiracamus * Reflected.
sample = """line1
line2
line3"""
In the above description, the variable sample
is associated with the string"line1 \ nline2 \ nline3"
.
Use the +
symbol.
sample = "a" + "b"
print(sample) #=> ab
Call the replace
method on the string.
sample = "a" + "b"
print(sample) # => ab
sample.replace("b", "c") #=> ac
Call the split
method on the string.
sample = "a" + "1" + "b"
print(sample)
a1b
print(sample.split("1")) #=>["a", "b"]
A container that can store multiple data. Think of something like a long box with a partition.
What's in the box starts at 0, which is attached to the partition. Identified by ascending number.
The list where the strings "0", "1" and "2" are in the dividers 0, 1 and 2 is as follows: It can be created by writing.
["0", "1", "2"]
Enclose the list (box) in []
and separate it with commas.
The components in the list can be described as they are.
If you want to put 0 in the character string, write "0", and if you want to put 0 in the numerical value, write 0
.
The beginning of the component can be accessed by the number 0 (index) The next component can be accessed by the number 1 and the next component by the number 2.
Access example by index when the list is assigned to the variable a
a = ["0", "1", "2"]
print(a[0]) #=> "0"
a[0] = "a"
print(a[0]) #=> a
For things (objects) that you want to access in order like a list When accessing in order, there is a convenient description method.
a = ["0", "1", "2"]
for i in a:
print(i)
#Execution result
# 0
# 1
# 2
First, associate the first element (index 0) of the list (object) of a with the variable i. Execute print (i). Then associate the next element (index 1) in the list of a with the variable i Execute print (i). Finally, associate the next element (index 2) in the list of a with the variable i. Execute pirint (i).
A name tag attached to a container to put a value in.
Name the container ʻaand enter the value
contents`.
a = "contents"
print(a) #=>contents
There are no constants in python. By convention, all variables that do not change their value are written in uppercase and are written as constants.
SAMPLE_VALUE = 986
print(SAMPLE_VALUE) # => 986
What you can call a process with a name.
Use def
.
def sample():
print("hello")
sample() #=> hello
The above defines a method named sample. The content of the method is the print ("hello") part.
In Python, the beginning and end of a block is marked by the :
at the end of the line and the presence or absence of indentation from the next line.
Express.
The indentation depth of the next line (in Python, the standard is four spaces, representing one indent) When a line appears that is indented shallower than the standard, Judge that the line before that is a block. In other languages, there are characters, strings, and expressions that represent blocks, In the case of Python, it is very characteristic to distinguish blocks by the depth of indentation.
It feels strange, but once you get used to it, it doesn't bother you at all. If you don't mind it, this one will be cleaner and easier to see.
In Python, when defining and calling a method, You must put the parentheses firmly.
What is written in parentheses after the method name It is called an argument.
Formal argument, when defining the method The argument when calling the method is called the actual argument.
Formal arguments are at the stage of defining the method (temporary), so I don't know the specific value (it hasn't been decided). Specify the variable name in the formal argument.
Since the actual argument is specified at the stage of calling the method, You will specify the actual value that is specifically determined.
def sample(string): #string is a formal argument
print(string)
sample("hello") # "hello"Is the actual argument
#Execution result
# hello
The output from the method is received as a return value
.
The return value is returned using return
in the method.
def sample(string) :
return string
a = sample("hello")
print(a) #=> hello
In other languages, blocks are represented by enclosing them in {
, }
symbols.
def sample() {
puts "this is sample1"
puts "this is sample2"
}
In python, the block structure is represented by indentation.
def sample() :
print("this is sample1") # => this is sample1
print("this is sample2") # => this is sample2
Indentation is the distance from the left side of the source.
The same block must be indented the same number of rows on each row in the block.
def sample():
print("sample1")
print("sample2") #Wrong because the number of indents is different from the number of indents in the line above
Recommended Posts