Home › Python with Chai › Lesson 16

Default values: less sugar

Python with Chai, lesson 16 of 20. A short Hinglish video, simple English notes and code you can run, free.

Open the interactive lesson with Run button

The idea

If a function needs a value and you don't give one, Python stops with a TypeError (missing argument).

A default value fixes that: def chai_banao(cheeni=1): uses 1 when you say nothing, and your value when you pass one.

Parameters with defaults go last: def chai_banao(size, cheeni=1) works, (cheeni=1, size) is a SyntaxError.

The code

def chai_banao(cheeni=1):
    print(f"Cheeni: {cheeni} chammach")

chai_banao()
chai_banao(0)
chai_banao(3)

Output:

Cheeni: 1 chammach
Cheeni: 0 chammach
Cheeni: 3 chammach

Try it yourself

Remember

Default = the answer Python uses when you say nothing.

← range(): 10 cups at once · Sets: how many customers? →

All lessons: Python with Chai