[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable

[Python] The role of the asterisk in front of the variable. Split input value

Confirm that it is executed with the following notation.

a,*b = [1,2,3,4,5]

** ▼ Processing content ** Substitute the very first element for a. Substitute after that for b.

python


a,*b = [1,2,3,4,5]
print(a)
print(b)

#output
1
[2,3,4,5]

** ▼ Basic confirmation ** If you prepare variables for each element, you can store them one by one.
a,b,c=1,2,3
print(a)
print(b)
print(c)

#output
1
2
3

▼ Error if the number of elements and the number of variables do not match

a,b=1,2,3
print(a)
print(b)

#output
too many values to unpack (expected 2)

** ▼ Asterisk eliminates the need to match the number of variables and elements **

・ Can be used at the beginning or in between.

a,*b,c,d=1,2,3,4,5,6,7,8,9
print(a)
print(b)
print(c)
print(d)

#output
1
[2, 3, 4, 5, 6, 7]
8
9
*a,b,c,d=1,2,3,4,5,6,7,8,9
print(a)
print(b)
print(c)
print(d)

#output
[1, 2, 3, 4, 5, 6]
7
8
9

Application example

name, *line = input().split()

--Divided input values including spaces and TAB into a list. --Substitute the first element for name --Substitute the following elements into line

python



    n = int(input())

#Define dictionary type
    student_marks = {}
    for _ in range(n):

#The first element is name. After that, store in line
        name, *line = input().split()
#Make the number stored in line a float and make it a list type
        scores = list(map(float, line))
#Name the key,Store the value as scores in a dictionary
        student_marks[name] = scores
    query_name = input()

#The dictionary type for statement retrieves the key.
    for key in student_marks:
        if key==query_name:
#Sum to find the average()/len()
         ave = sum(student_marks[key])/len(student_marks[key])

#Two decimal places display. use f string
         print(f'{ave:.2f}')

f'string' f'{variable}' Value: .nf → Display up to n decimal places

Recommended Posts

[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
How to check the memory size of a variable in Python
How to input a character string in Python and output it as it is or in the opposite direction.
Python Note: The mystery of assigning a variable to a variable
The result of making a map album of Italy honeymoon in Python and sharing it
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Get the value of a specific key up to the specified index in the dictionary list in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
What seems to be a template of the standard input part of the competition pro in python3
I tried to create a Python script to get the value of a cell in Microsoft Excel
An easy way to view the time taken in Python and a smarter way to improve it
How to determine the existence of a selenium element in Python
How to check the memory size of a dictionary in Python
If you want to assign csv export to a variable in python
Convert the result of python optparse to dict and utilize it
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
The story of returning to the front line for the first time in 5 years and refactoring Python Django
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
I set the environment variable with Docker and displayed it in Python
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
Use Cloud Dataflow to dynamically change the destination according to the value of the data and save it in GCS
[Python] How to save the installed package and install it in a new environment at once Mac environment
How to embed a variable in a python string
Get the caller of a function in Python
Make a copy of the list in Python
[Note] About the role of underscore "_" in Python
Output in the form of a python array
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
To output a value even in the middle of a cell with Jupyter Notebook
How to count the number of elements in Django and output to a template
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Japanese translation of self-study "A Beginner's Guide to Getting User Input in Python"
Build a python environment to learn the theory and implementation of deep learning
Define a division value in Django and easily reflect it on the screen
Python script to get a list of input examples for the AtCoder contest
How to get a list of files in the same directory with python
[Python & SQLite] I tried to analyze the expected value of a race with horses in the 1x win range ①
How to save the feature point information of an image in a file and use it for matching
An engineer who has noticed the emo of cryptography is trying to implement it in Python and defeat it
A Python script that crawls RSS in Azure Status and posts it to Hipchat
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
How to retrieve the nth largest value in Python
I want to embed a variable in a Python string
How to get the variable name itself in python
[Python3] Take a screenshot of a web page on the server and crop it further
Get the value of a specific key in a list from the dictionary type in the list with Python
How to store Python function in Value of dictionary (dict) and call function according to Key
How to identify the element with the smallest number of characters in a Python list?
[Python] Change the text color and background color of a specific keyword in print output
[Selenium] Open the link in a new tab and move it [Python / Chrome Driver]
I want to replace the variables in the python template file and mass-produce it in another file.
How to check in Python if one of the elements of a list is in another list
[Mac] A super-easy way to execute system commands in Python and output the results
A story about trying to introduce Linter in the middle of a Python (Flask) project
A reminder about the implementation of recommendations in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
It is easy to execute SQL with Python and output the result in Excel