# 在Python中使用星号

## *和**的一般用法

*用作乘法运算符, 而**用作幂运算符。

``````## declaring variables
a, b = 2, 3

## multiplication
print("Multiplication of 2 and 3 is {}".format(a * b))

## as power operator
print("2 power 3 is {}".format(a ** b))
``````
``````Multiplication of 2 and 3 is 6
2 power 3 is 8
``````

1. 使用*开箱

1.1。结合不同的可迭代

1.2。网站开箱

2.使用**打开包装

3.函数中的星号

3.1。打开功能

3.2。包装功能

4.带有位置参数的仅关键字参数

5.不带位置参数的仅关键字参数

## 1.使用*开箱

``````## general approach
nums = [i for i in range(1, 6)]
a = nums[0]
b = nums[1:]
print(a, b)

print("--------------------------")

## hard approach
squares = [i ** 2 for i in range(1, 6)]
a = squares[0]
b = []
for i in range(1, len(squares)):
b.append(squares[i])
print(a, b)
``````
``````1 [2, 3, 4, 5]
--------------------------
1 [4, 9, 16, 25]
``````

``````nums = [i for i in range(1, 6)]

## a will be 1 and b will be a list containing remaining elements
a, *b = nums
print(a, b)
``````
``````1 [2, 3, 4, 5]
``````

``````## using tuple
## here first element will assign to a and last element to c
a, *b, c = (1, 2, 3, 4, 5)
print(a, b, c)

print("--------------------------")

## using sets
a, *b, c = {1, 4, 9, 16, 25}
print(a, b, c)
``````
``````1 [2, 3, 4] 5
--------------------------
1 [4, 9, 16] 25
``````

### 1.1。结合不同的可迭代

``````## combining a tuple, list, set
nums = [1, 2, 3]
nums2 = (4, 5, 6)
nums3 = {7, 8, 9}

## we convert the combined elements into any iterable we want
## here i am converting into a list
_list = [*nums, *nums2, *nums3]
_tuple = (*nums, *nums2, *nums3)
_set = {*nums, *nums2, *nums3}

print(type(_list))
print(_list)
print("------------------------")
print(type(_tuple))
print(_tuple)
print("------------------------")
print(type(_set))
print(_set)
``````
``````<class 'list'>
[1, 2, 3, 4, 5, 6, 8, 9, 7]
------------------------
<class 'tuple'>
(1, 2, 3, 4, 5, 6, 8, 9, 7)
------------------------
<class 'set'>
{1, 2, 3, 4, 5, 6, 7, 8, 9}
``````

### 1.2。嵌套拆箱

``````languages = ["Python", "HTML", "CSS", "JS"]

## unpacking
[[first_letter, *remaining], *other] = languages

print(first_letter, remaining, other)
``````
``````P ['y', 't', 'h', 'o', 'n'] ['HTML', 'CSS', 'JS']
``````

## 2.使用**打开包装

``````## sample dictionary
person = {"name":"John", "age":19, "year_of_passing":"2021"}
string = "Name:-{name} Year Of Graduation:-{year_of_passing} Age:-{age}".format(**person)
print(string)
``````
``````Name:-John Year Of Graduation:-2021 Age:-19
``````

## 3.函数中的星号

### 3.1。打开功能

*运算符用于通过解压缩可迭代对象来调用函数。假设我们有一个列表, 并且必须将其所有元素分别传递给print()。你会怎么做？我认为你正在考虑一个循环。但是, *使我们很容易做到这一点。

``````nums = [i for i in range(1, 6)]
## passsing list using the *
print(*nums)
``````
``````1 2 3 4 5
``````
``````nums = (i for i in range(1, 6))
## passsing tuple using the *
print(*nums, sep = ", ")
``````
``````1, 2, 3, 4, 5
``````

## 3.2。包装元素

``````def average(*nums):
return sum(nums) / len(nums)
``````

``````## calling the average with some numbers
print(average(1, 2, 3, 4, 5))
``````
``````3.0
``````

``````def _object(name, **properties):
print(name, properties)

_object("Car", color="Red", cost=999999, company="Ferrari")
``````
``````Car {'ceo': 'Louis', 'color': 'Red', 'cost': 999999, 'company': 'Ferrari'}
``````

## 4.带有位置参数的仅关键字参数

``````## name is a positional argument
## color and cost are keyword arguments
def sample(car, color = None, cost = None):
print("Car:-{} Color:-{}, Cost:-{}".format(car, color, cost))
``````

``````sample("Ferrari", "Red", 999999)
``````
``````Car:-Ferrari Color:-Red, Cost:-999999
``````
``````sample("Ferrari", cost = 999999, color = "Red")
``````
``````Car:-Ferrari Color:-Red, Cost:-999999
``````

``````sample("Ferrari", color = "Green")
``````
``````Car:-Ferrari Color:-Green, Cost:-None
``````

``````def keyword_only(*items, _list, default = False):
print(items)
print(_list)
print(default)
``````

``````nums = [i ** 2 for i in range(1, 6)]
## calling the function
keyword_only(1, 2, 3, 4, 5, _list = nums, default = True)
``````
``````(1, 2, 3, 4, 5)
[1, 4, 9, 16, 25]
True
``````

``````nums = [i ** 2 for i in range(1, 6)]
## calling the function will raise an error
keyword_only(1, 2, 3, 4, 5, nums, False)
``````
``````---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-85-bf737d8a8bfc> in <module>()
1 nums = [i ** 2 for i in range(1, 6)]
----> 2 keyword_only(1, 2, 3, 4, 5, nums, False)

TypeError: keyword_only() missing 1 required keyword-only argument: '_list'
``````

## 5.不带位置参数的仅关键字参数

``````def _sample(*, name):
print(name)
``````

``````## calling the function using keyword 'name'
_sample(name = "srcmini")
``````
``````srcmini
``````
``````## calling the function without using keyword 'name'
## we will get a TypeError
_sample("srcmini")
``````
``````---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-12-fe0d3718b56c> in <module>()
1 ## calling the function without using keyword 'name'
2 ## we will get a TypeError
----> 3 samp("srcmini")

TypeError: samp() takes 0 positional arguments but 1 was given
``````