Function: A definition of a group of programs
Built-in functions: Functions preset in python itself
Think of it as a program version of a variable
A method is a method that performs processing on a specific value.
Method
value.Method name()
There is also an instance variable that retrieves value elements and so on. Instance variables have no arguments because they are variables, not functions.
Instance variables
value.Instance variable name
Taking append as an example, it is as follows.
append
# append()Is a method to use if you want to add only one new element to the list.
al = ["1","2","3","4","5"]
al.append("f")
print(al)
# ["1","2","3","4","5","f"]Is output
A method for enlarging and counting characters.
code | Contents |
---|---|
upper | Enlargement of characters |
count | Count the number of characters |
format | Type a variable into a string |
city = "Nagoya"
print(city.upper())
#output: 「NAGOYA」
print(city.count("N"))
#output: 「1」
print("I{}birth,{}Birthplace".format("Aichi", "Nagoya"))
#output:"I was born in Aichi and from Nagoya"
#Where to embed{}Specified by. The argument does not have to be a string type
# {}The value to be inserted in can be specified in the insertion order and the same value can be repeated.
print("I{1}birth,{0}Growing up{1}Citizen".format("Aichi", "Nagoya"))
#output:"I was born in Nagoya, raised in Aichi, and a citizen of Nagoya"
code | Contents |
---|---|
index | Returns index number |
count | count |
sort | Rearrange |
reverse | Sort in reverse |
sorted | Sort without changing the original value |
index()
#Method to find out if it is in the index number
count()
#Some methods to output
al = ["0", "1", "2", "3", "3"]
print(al.index("0"))
#output: 「0」
print(al.count("3"))
#output: 「2」
sort()
#This will sort the list in ascending order.
reverse()
#Reverse the order of the elements in the list
# sort()And reverse()The contents of the list are changed directly.
#There is no return value, so print(list.sort())Even if None is returned
soted()
#Built-in function sorted()And the original value remains the same
# sort()Usage example
list = [1, 3, 2, 4]
list.sort()
print(list) # [1, 2, 3, 4]It will be displayed.
# reverse()Usage example
list = ["Or", "Ki", "Ku", "Ke", "This"]
list.reverse()
print(list)
#output: ["This", "Ke", "Ku", "Ki", "Or"]
"Def function name(): 」
def eat():
print ("eat")
#Clarify the scope of processing with indentation
#The function call is "function name"()Will be
eat()
#output:eat
If you set an argument, You can use that value in a function
def function name(argument):
def ii(n):
print(n + "is")
ii("yellow")
#output:is yellow
#If you specify the argument as a variable, you can change the output result each time.
def ii(n):
print(n + "is")
name = "yellow"
ii(name)
#is yellow
name = "black"
ii(name)
#output: black
#Variables defined in the function,Arguments can only be used within a function
Arguments separated by commas Specify more than one
def ii(sei, name):
print("Last name is" + sei + "And the name is" + name + "is.")
ii("j", "bb")
#"Last name is j and first name is bb." Is output.
When you set the initial (default) value If left blank, the initial value is automatically set. Argument = set in the form of initial value
def ii(family="j", first="bb"):
print("Last name is" + family + "And the name is" + first + "is.")
ii("b")
#"Last name is b and first name is bb." Is output.
#If you want to pass only the first argument, ii(first="cc")Designated as
#In addition, it is necessary to always set the initial value in the argument after the argument for which the initial value is set.
#Therefore, it is possible to set the initial value only for the last argument as follows.
def ii(family, first="aa"):
print("Last name is" + family + "And the name is"+ first + "is.")
#When only the previous argument is set, an error will occur if it is not set in the latter argument.
def ii(family="zz", first):
print("Last name is" + family + "And the name is" + first + "is.")
return
Variables and arguments defined in a function cannot be used outside the function return allows you to pass the return value to the caller of the function
def ii(family = "jj", first = "bb"):
comment = "Last name is" + family + "And the name is" + first + "is."
print(ii("ss"))
#None is output
def ii(family = "jj", first = "bb"):
comment = "Last name is" + family + "And the name is" + first + "is."
return comment #Pass the return value to the function
print(ii("ss"))
#"Last name is ss and first name is bb." Is output.
Recommended Posts