变量位置并打印值 [英] variable positional and printing the values

查看:39
本文介绍了变量位置并打印值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def minimum (*n):
        print(n)
minimum(1)
minimum(1,2)
    
    
def func(*args):
     print(args)
    
values1 = (1,2)
values2 = ((1,2), (3,4))
func(values1)
func(values2)

OUTPUT:
(1,)
(1, 2)
((1, 2),)
(((1, 2), (3, 4)),)

Process finished with exit code 0

第一个 O/p:我认为python期望传递多个参数,因此1之后有一个逗号(,).?

First O/p: I think python is expecting multiple arguments to be passed so there is a comma (,) after 1. ?

第二个O/p:现在 python 看到多个参数被传递,没有逗号.它将 args 存储为元组?

Second O/p: Now the python sees multiple arguments being passed there is no comma. It stores the args a tuple?

第三个 O/p 和第四个 O/p:为什么还有逗号?即使在我传递了 2 个元组之后,假设 python 需要像上面这样的多个元组?

Third O/p and Fourth O/p: Why is there still a comma? even after I passed 2 tuples assuming that python is expecting multiple tuples like the above?

帮助我理解这一点.

推荐答案

第一个输出显示一个逗号,因为没有它,1 是唯一的元素,(1)将只是一个整数(括号将表达式 1 包裹起来),(1,) 用于区分元组和简单括号.

The first output shows a comma because without it, 1 being the only element, (1) would be just a integer (parentheses are wrapping the expression 1), (1,) is shown to differentiate tuples and simple parentheses.

在第二个中,不需要尾随逗号来区分元组,因为有多个元素.

in the second one, no trailing comma is needed to differentiate tuples, since there are more than one element.

在第三个 O/p 中,您没有传递 12,而是传递了整个 (1,2),因此它只显示元组中的一项(即 (1,2)),并添加了一个额外的逗号.第四个相同:您传递了整个 ((1,2), (3,4)).

In the third O/p, you are not passing 1 and 2, but instead you're passing the whole (1,2), so it shows only one item (which is (1,2)) in a tuple, and adds an extra comma. Same for the fourth: your passing the entire ((1,2), (3,4)).

这篇关于变量位置并打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆