Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Positional Arguments (Complete Guide for Beginners)

In Python, positional arguments are the most basic way to pass values to a function. They are called “positional” because the values are assigned to parameters based on their position (order).

If you change the order of arguments, the result will also change.


🔹 What are Positional Arguments in Python?

Positional arguments are values passed to a function in a specific order, and Python assigns them to parameters from left to right.

👉 First value → first parameter
👉 Second value → second parameter
👉 And so on…


🔹 Syntax of Positional Arguments

def function_name(param1, param2):
# code block
pass

function_name(value1, value2)

🔹 Simple Example of Positional Arguments

def student(name, age):
print("Name:", name)
print("Age:", age)

student("Alex", 20)

🔸 Output:

Name: Alex
Age: 20

🔍 Explanation:

  • "Alex" → assigned to name
  • 20 → assigned to age

🔹 Order Matters in Positional Arguments

def info(name, city):
print(name, city)

info("Phnom Penh", "Sophy")

🔸 Output:

Phnom Penh Sophy

⚠️ Explanation:

  • Values are assigned based on position
  • So output becomes incorrect if order is wrong

🔹 Correct vs Incorrect Order

✔ Correct:

def person(name, age):
print(name, age)

person("John", 25)

❌ Incorrect logic (wrong meaning):

person(25, "John")

🔍 Result:

25 John

🔹 Real-Life Example: User Login

def login(username, password):
print("Username:", username)
print("Password:", password)

login("admin", "1234")

🔸 Output:

Username: admin
Password: 1234

🔹 Real-Life Example: Calculator

def add(a, b):
print("Sum:", a + b)

add(10, 5)

🔸 Output:

15

🔹 Real-Life Example: Product Order

def order(product, quantity, price):
print("Product:", product)
print("Quantity:", quantity)
print("Price:", price)

order("Laptop", 2, 1200)

🔸 Output:

Product: Laptop
Quantity: 2
Price: 1200

🔹 Mixing Positional and Keyword Arguments

You can mix both, but positional must come first:

def user(name, age):
print(name, age)

user("Alex", age=20)

🔸 Output:

Alex 20

🔹 ❌ Invalid Example

def demo(a, b):
print(a, b)

demo(a=10, 20)

❌ This causes an error

🔍 Why?

  • Positional argument cannot come after keyword argument

🔹 Positional Arguments with Default Values

def greet(name, message="Hello"):
print(message, name)

greet("John")
greet("Sophy", "Hi")

🔸 Output:

Hello John
Hi Sophy

🔹 Why Use Positional Arguments?

✔ Simple and easy to use
✔ Fast to write
✔ Good for small functions
✔ Most commonly used in Python basics


🔹 Positional vs Keyword Arguments

FeaturePositionalKeyword
Order matters          Yes           No
Syntax          value only           name=value
Readability          Medium           High
Flexibility          Low           High

🔹 Key Points to Remember

  • Values are assigned based on position
  • Order is very important
  • Common in basic function calls
  • Can be combined with keyword arguments

🚀 Conclusion

Python positional arguments are the simplest way to pass data into functions. They rely on the order of values, making them fast and easy to use, but also sensitive to mistakes if the order is incorrect.

They are widely used in:

  • Basic programming
  • Simple functions
  • Mathematical operations
  • Everyday Python code

 




Post a Comment

0 Comments