+91 81900 98289

Python Arguments

This Python Arguments lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A parameter can carry a default. The caller may skip that argument. A later argument can be passed by name, so the order of those names does not matter.

Positional arguments come first. Keyword arguments follow them.

Example 1: Start here

A default belongs on the trailing parameters. Putting a required parameter after a default is a syntax error.

def greet(name, city="Chennai"): # define the function; it runs when you call it return name + " from " + city # send this value back to the caller print(greet("Meera")) print(greet("Arun", city="Madurai")) # name= sets that argument by its name

Example 2: Positional order

The first value goes to the first name.

def show(city, seats): # define the function; it runs when you call it print(city, seats) # a comma prints each value, with a space between show("Chennai", 24) # call the function

Example 3: Names at the call

Named arguments can be written in any order.

def show(city, seats): # define the function; it runs when you call it print(city, seats) # a comma prints each value, with a space between show(seats=24, city="Chennai") # call the function

Example 4: A default at the end

Put parameters with defaults after parameters that require a value.

def show(city, seats=20): # define the function; it runs when you call it print(city, seats) # a comma prints each value, with a space between show("Chennai") # call the function
A default belongs on the trailing parameters. Putting a required parameter after a default is a syntax error.
Call Now 81900 98289