Menu

Python Day 7: Positional-Only, Keyword-Only Arguments and Return Statement
📰

Python Day 7: Positional-Only, Keyword-Only Arguments and Return Statement

DEV Community: beginners·Vasanthakumari N·about 5 hours ago
#eXYgbVLm
Long article (315 words) — no AI summary yet.
Reading 0:00
15s threshold

1. Positional-Only Arguments (/)

In Python, / is used as a positional-only argument separator.

It means that the parameters written before / must be passed using their position. We cannot pass them using parameter names (keyword arguments).

Example:

def calculate ( a , b , / ) : print ( a + b )

calculate ( 10 , 20 ) # 30 calculate ( a = 10 , b = 20 ) # TypeError

Enter fullscreen mode

Exit fullscreen mode

TypeError: calculate() got some positional-only arguments passed as keyword arguments

Here, a and b must be passed as positional arguments.

2. Keyword-Only Arguments (*)

In Python, * can be used as a keyword-only argument separator.

It means that the parameters written after * must be passed using their parameter names.

Example:

def calculate ( a , * , b ) : print ( a + b )

calculate ( a = 10 , b = 20 ) # 30 calculate ( 10 , b = 20 ) # 30 calculate ( 10 , 20 ) # Error

Enter fullscreen mode

Exit fullscreen mode

TypeError: calculate() takes 1 positional argument but 2 were given

Here:

a can…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.