Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Functions

Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Functions

Python Functions

In programming, we often perform a task multiple times. Writing the same code again and again isn’t smart — and that’s where functions come in.

A function is a named block of code designed to do one job. Whenever you need that task, just call the function, and it runs for you. This keeps your code organized and easy to manage.

Types of Functions

Python supports two main types of functions:

1. Built-in Functions

These are the functions that Python provides by default. You’ve likely used many of them without even realizing.

Some common built-in functions:

  • len() → returns the length of a collection

  • sum() → adds all items in a list or tuple

  • type() → tells the data type

  • print() → displays output on screen

  • list(), set(), dict(), etc.

Example:
				
					items = [12, 45, 33, 21]
print("Total items:", len(items))
print("Sum of items:", sum(items))


				
			

2. User-Defined Functions

You can define your own function to perform a task your way. These are called user-defined functions.

Syntax:
				
					def function_name(parameters):
    # code block

				
			
  • def is the keyword used to define a function.

  • You can include parameters inside the parentheses if needed.

  • The body of the function must be indented.

  • To run the function, call it by name.

Example: Create and Call a Function

Let’s make a function that greets a user with their full name.

				
					def greet_user(first, last):
    print("Welcome,", first, last)

greet_user("Arshyan", "Ali")

				
			

Output:

				
					Welcome, Arshyan Ali

				
			

You can also create functions without parameters if you want a fixed output:

				
					def show_institute():
    print("Learn With Arshyan - Smart Coding Starts Here")

show_institute()

				
			

Output:

				
					Learn With Arshyan - Smart Coding Starts Here

				
			

Summary

  • Use functions to reuse blocks of code.

  • Python gives you built-in functions like print() and sum().

  • You can define your own functions using def.

  • Functions make big programs easier to manage.

Scroll to Top