What are python tuples and * args after all?

After all, what is a tuple?

What is a tuple

Basically, you can think of it as a list whose contents cannot be changed. important point

--The contents of the objects in the tuple can be changed. -Declared with (), but it is recognized as a tuple even if it is separated by a comma.

Looking at the sample code, it was quick to understand.

t = (1, 2, 3)
# t[0] =0 Cannot be changed (cannot append)

#You can put an object and change the contents
t = (1, [1, 2])
t[1][0] = 0
print(t) # (1, [0, 2])

t = 1, 2, 3 #Recognized as tuples separated by commas
print(type(t)) # <class 'tuple'>
# (1)Note that is just a number. If you want to make tuples(1,)

#Tuple packing
t = (1, 2, 3)
#Tuple unpacking
x, y, z = t
print(x, y, z) # 1 2 3

#Can be replaced without tmp
x, y = y, x
print(x, y) # 2 1

What is * args

Based on the above, you can think of it as a method of receiving variable arguments as tuples.

This was also quick to understand when looking at the sample code.

#Tuples are the remaining arguments, dictionaries are keyword arguments
def menu(food, *args, **kwargs):
    print(f'food: {food}')
    print(f'args: {args}')
    for a in args:
        print(a)
    print(f'kwargs: {kwargs}')
    for k, v in kwargs.items():
        print(k, '=', v)

menu('banana', 'apple', 'orange', entree='beef', drink='coffee')
# food: banana
# args: ('apple', 'orange')
# apple
# orange
# kwargs: {'entree': 'beef', 'drink': 'coffee'}
# entree = beef
# drink = coffee

#This is a tuple and a dictionary* **Write using
t = ('apple', 'orange')
d = {'entree': 'beef', 'drink': 'coffee'}
menu('banana', *t, **d)
#Output is the same as above

#By the way* **Must be used*It will be recognized by args
menu('banana', t, d)
# food: banana
# args: (('apple', 'orange'), {'entree': 'beef', 'drink': 'coffee'})
# ('apple', 'orange')
# {'entree': 'beef', 'drink': 'coffee'}
# kwargs: {}

I did a dict with a lot of momentum, but what I was doing was the same.

Recommended Posts

What are python tuples and * args after all?
What are you comparing with Python is and ==?
[Python] * args ** What is kwrgs?
Python list and tuples and commas
[Python beginner] How do I develop and execute Python after all?
Python a + = b and a = a + b are different
Python> these are all considered False:
[Python] Let's master all and any
What are "sudo ln -s" and "ln -s"?
After all, what is statistical modeling?
(Beginner) What are cores and threads?
What should I do with the Python directory structure after all?
Modules and packages in Python are "namespaces"
All Python arguments are passed by reference
Sh and py run after installing Python3
Data analysis, what do you do after all?
UnionFind in python (enhanced version: strings and tuples are allowed for elements)
After all, what should I use to do type comparisons in Python?
Python open and io.open are the same
What I think Python and Ruby (and languages whose types are not specified) are dung
Training data and test data (What are X_train and y_train?) ①
Training data and test data (What are X_train and y_train?) ②
What is "functional programming" and "object-oriented" in Python?
[Python] What are the two underscore (underscore) functions before?
[Mathematics] Let's visualize what are eigenvalues and eigenvectors
How python classes and magic methods are working.
[Road to Intermediate] What are Python's * args, ** kwargs?
What are you using when testing with Python?
What is python
What is Python
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
Save lists, dictionaries and tuples to external files python
Check what the character code is for all files under the directory that is Python and output