Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what is the function of , in python

+8 votes
asked Mar 9, 2023 by Krishaanth S CSA CBE (200 points)

2 Answers

0 votes
answered Mar 14, 2023 by dadsdy (220 points)
Commas are used to seperate arguments passed to a function/method, as well as to seperate arguments in defining of functions/methods. Does that help?
+2 votes
answered Mar 27, 2023 by Paul Bhorjee (180 points)

A comma denotes an instantiation of a tuple.  Most think that it is the parentheses which will define a tuple, but this is incorrect.

#explore the tuple!

single_tuple_error = (10)
print(single_tuple_error)
10

print(type(single_tuple_error))
<class 'int'>
single_tuple = (10, )
print(single_tuple)
(10,)

print(type(single_tuple))
<class 'tuple'>
#no parens!
single_tuple = 10, 
print(single_tuple)
(10,)

print(type(single_tuple))
<class 'tuple'>
print(single_tuple[0])
10
print((0, 1, 2) + (3))
TypeError: can only concatenate tuple (not "int") to tuple

print((0, 1, 2) + (3, ))
(0, 1, 2, 3)
funny_tuple = 0, 1, 2 + 3, 
print(funny_tuple)
(0, 1, 5)
print(type(funny_tuple))
<class 'tuple'>
funny_tuple = 0, 1, 2, + 3,
print(funny_tuple)
(0, 1, 2, 3)
funny_tuple = 0 - 1, 2, + 3,
print(funny_tuple)
(-1, 2, 3)
print(type(funny_tuple))
<class 'tuple'>

#empty tuples are different and do need parens.

#empty tuple #1

empty_tuple = ()
print(empty_tuple)
()

print(type(empty_tuple))
<class 'tuple'>


#empty tuple #2

empty_tuple = tuple()
print(empty_tuple)
()

print(type(empty_tuple))
<class 'tuple'>
# empty tuple error

empty_tuple_error = (,)
# SyntaxError: invalid syntax
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...