Python basics: functions

Functions, built-in functions

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

Method

A method is a method that performs processing on a specific value.

Method


value.Method name()

Instance method

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

String method

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"


List type method

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

function

"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

argument

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

Multiple arguments

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.

Initial value of argument

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

Python basics: functions
# 4 [python] Basics of functions
Python basics ⑤
Python basics ④
Python functions
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
[Beginner] Python functions
Python array basics
Python profiling basics
Python Easy-to-use functions
Python #Numpy basics
#Python basics (class)
Python basics summary
Python Beginner's Guide (Functions)
Python basic course (12 functions)
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
Python basics 8 numpy test
[Python] Memo about functions
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
Python built-in functions ~ Zip ~
Wrap Python built-in functions
Basics of python: Output
Curry arbitrary functions with Python ....
Python> lambda> tiny functions / callback functions
Getting Started with Python Functions
Python3 programming functions personal summary
Python
Overriding library functions in Python
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Keyword arguments for Python functions
Python for super beginners Python #functions 1
Paiza Python Primer 7: Understanding Functions
Python 3 sorted and comparison functions
Python functions learned in chemoinformatics
Basics of Python × GIS (Part 1)
Python higher-order functions and comprehensions
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Python control syntax, functions (Python learning memo ②)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
Azure Functions: Try Durable Functions for Python
SNS Python basics made with Flask