HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
def / returndefault parameter*args / **kwargslambda
📁 File & Error
🔄 Type & Conversion
Python›REFERENCE›Function
🧩

Function

Defining and reusing your own commands.

CommandDescription
def / returnDefines your own function (command).
default parameterGives a parameter a default value.
*args / **kwargsTakes a variable number of arguments.
lambdaA short one-line function.

def / return

Try it yourself →

Defines your own function (command).

Syntax

def name(param):
    return value

Description

Binds a repeated task to a name. It takes data via parameters and returns a result with return. If there's no return, it returns None.

Parameters

parameter(optional)The input(s) given to the function.

Returns

The value specified with return.

Example

def square(x):
    return x * x
print(square(5))

Expected output

25

When to use?

Avoiding code repetition, breaking the program into readable pieces.

Common mistakes

  • ⚠️ If you don't write return, the function returns None; print and return are different.

default parameter

Try it yourself →

Gives a parameter a default value.

Syntax

def name(x, y=default):

Description

If no argument is given at the call, the default is used; it lets you write flexible functions.

Example

def greet(name, message="Hello"):
    return f"{message} {name}"
print(greet("Alex"))
print(greet("Alex", "Good morning"))

Expected output

Hello Alex
Good morning Alex

When to use?

Optional settings, arguments that are the same most of the time.

*args / **kwargs

Try it yourself →

Takes a variable number of arguments.

Syntax

def name(*args, **kwargs):

Description

*args collects an indefinite number of positional arguments into a tuple, **kwargs collects named arguments into a dictionary.

Example

def addUp(*numbers):
    return sum(numbers)
print(addUp(1, 2, 3, 4))

Expected output

10

When to use?

Helper functions where it's unclear how many arguments will come.

lambda

Try it yourself →

A short one-line function.

Syntax

name = lambda p: expression

Description

A small, anonymous, single-expression function. Usually passed as an argument to places like sort/map/filter.

Example

square = lambda x: x * x
print(square(6))
print(sorted([3,1,2], key=lambda n: -n))

Expected output

36
[3, 2, 1]

When to use?

A sort key, short transformations; for bigger logic, prefer a normal def.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code