Showing posts with label Python Functions. Show all posts
Showing posts with label Python Functions. Show all posts

Creating a simple Function in python || python code for creating function

← Back to Home

๐Ÿ”น Functions in Python

A function is a block of code which only runs when it is called.

we can pass data, known as parameters, into a function.

A function can return data as a result.


# In Python a function is defined using the def keyword:

๐Ÿ”ธ Example : Creating a Function

def my_function():
  print("Hello function" )



๐Ÿ”ธ  Calling a Function

To call a function, we use the function name followed by parenthesis

 Example :


def my_function():
  print("Hello from function")

my_function()    # Here is Function call by its name


Python Functions Explained: A Beginner’s Guide with Code Examples

← Back to Home

๐Ÿ” Understanding Python Functions – A Beginner’s Guide

Functions are one of the most powerful and useful features in Python. They allow you to group code into reusable blocks, making your programs more organized, readable, and efficient.

Functions are the heart of clean, efficient Python programming. If you're a beginner, think of functions as named blocks of code that do something specific — like a recipe in a cookbook.


๐Ÿง  What is a Function?

A function is a reusable block of code that  that performs a specific task and runs only when called.  You define it once and call it whenever needed. 

You can pass data into it using parameters, and get a result back using the return keyword.

๐Ÿ“Œ Key Points:

  • A function is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a function.
  • A function can return data as a result.
  • In Python, a function is defined using the def keyword:

See example below:

def my_function():
  print("Hello from a function")

๐Ÿ”ง Basic Anatomy of a Function

def function_name(parameters):
    # code block
    return result

๐Ÿ‘️ Code Diagram

+---------------------------+
| def greet(name):         | <- Function Definition
|     print("Hello", name) | <- Function Body
+---------------------------+

        ⬇ Function Call

+--------------------+
| greet("Alice")     | <- Output: Hello Alice
+--------------------+

✅ Why Use Functions?

Functions help you:

๐Ÿงฉ Break complex tasks into manageable chunks
๐Ÿ” Reuse code instead of repeating
๐Ÿ“š Improve readability of your code
๐Ÿ› ️ Debug and maintain programs easily


✍️ Creating Your First Function

Let’s write a simple function:

def my_function():
    print("Hello from a function")

✍️ Defining and Calling a Function

Let’s write a above simple function again:

def my_function():
    print("Hello from a function!")

# Call the function
my_function()

๐Ÿ–ฅ️ Output:

Hello from a function!


๐Ÿงพ Function with Parameters

def greet(name):
    print(f"Hello, {name}!")
    
greet("Alice")

๐Ÿ–ฅ️ Output:

Hello, Alice!

๐Ÿ“Œ You can pass any data—strings, numbers, lists, etc.—as parameters.



๐Ÿ” Reusability at Work:

Want to greet different users? Use parameters.

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")
greet("Bob")

๐Ÿ–จ️ Output:

Hello, Alice!
Hello, Bob!

๐Ÿ”„ Returning Values from Functions

Some functions return a result:

def add(a, b):
    return a + b

sum = add(4, 6)
print(sum)

๐Ÿ–จ️ Output:

10


๐Ÿ”„ Function with Conditional Logic

def is_even(num):
    if num % 2 == 0:
        return True
    else:
        return False

print(is_even(4))  # True
print(is_even(5))  # False

๐Ÿ“Œ You can use functions to implement logic and decision-making.


๐Ÿงญ Flowchart: How a Function Works

[Start]
   ↓
Define Function → [Call Function?] — No → [End]
                          ↓ Yes
                    Execute Code Block
                          ↓
                     Return (Optional)
                          ↓
                        [End]

See the flowchart in picture below:




๐ŸŽฅ Watch and Learn

Want to see it in action?
๐Ÿ‘‰ Check out the video below: “What is a Function in Python” – ideal for visual learners!



๐ŸŽฏ Final Tip

Functions help you write clean, modular, and testable code. The more you use them, the better your Python skills will become!


๐Ÿš€ Your Turn

Now that you know the basics of Python functions, try creating your own! Whether it’s a calculator or a greeting app, functions will help you build smarter and cleaner code.


Python Functions || learn python programming || Python for Beginners

Python Functions
This video tutorial contains complete explanation from part 1, 2 and 3 of Python Functions.

๐Ÿง  What You'll Learn

This complete video tutorial combines all key concepts from our Python Functions series (Part 1, 2, and 3). It’s perfect for beginners who want to understand functions from scratch and become confident using them in real projects.

  • What functions are and why they are used in Python
  • How to define and call functions
  • Understanding function parameters and return values
  • Default parameters, *args and **kwargs
  • Lambda functions and variable scope

๐Ÿ“ฝ️ Video Walkthrough

This all-in-one video recaps every important topic about Python functions. Whether you're revising or learning for the first time, this session will give you a clear and practical understanding of how functions work in Python, with examples and code demonstrations explained in Hindi.

  • Defining and calling functions using the def keyword
  • Different types of arguments: positional, keyword, default
  • Returning single and multiple values
  • Working with *args and **kwargs
  • Introduction to lambda functions and scope handling

✅ Key Takeaways

  • Functions make your code reusable, organized, and easy to debug.
  • Arguments and return values allow data flow between functions.
  • *args and **kwargs give flexibility to accept variable inputs.
  • Understanding scope and lambda functions is key for writing efficient Python code.

Python Functions - PART -3 || Python tutorial || Python for Beginners ||...

๐Ÿง  What You'll Learn

In Part 3 of our Python Functions tutorial series, we’ll explore advanced concepts that help you write more flexible and powerful functions.

  • Understanding default parameter values
  • Using variable-length arguments (*args and **kwargs)
  • Scope of variables inside and outside functions
  • Introduction to lambda (anonymous) functions

๐Ÿ“ฝ️ Video Walkthrough

This video explains how to enhance your Python functions by adding default parameters, accepting a variable number of arguments, and understanding the scope of variables. You will also get a brief introduction to lambda functions, which provide a compact way to write small anonymous functions.

  • Setting default values for function parameters
  • Using *args for non-keyword variable arguments
  • Using **kwargs for keyword variable arguments
  • Variable scope: local vs global variables
  • Writing simple lambda functions

✅ Key Takeaways

  • Default parameters allow functions to have optional arguments.
  • *args and **kwargs enable flexible argument passing.
  • Understanding variable scope is crucial to avoid bugs.
  • Lambda functions offer concise syntax for small anonymous functions.

Python Function Part-2 || Python Tutorial ||Python for beginners

๐Ÿง  What You'll Learn

In Part 2 of our Python Functions series, we dive deeper into how functions work by exploring different types of arguments and return values. This lesson is perfect for beginners aiming to write more flexible and reusable code.

  • Types of function arguments in Python
  • Positional vs keyword arguments
  • Returning single and multiple values
  • Practical examples of using return values

๐Ÿ“ฝ️ Video Walkthrough

This tutorial continues from Part 1 and shows how different kinds of arguments work in functions. You'll also learn how to return values and how to use them in your program logic.

  • Function calls with positional arguments
  • Using keyword arguments for clarity
  • Returning multiple values as tuples
  • Best practices with return statements

✅ Key Takeaways

  • Function arguments allow you to pass data into functions for reuse.
  • Return values are used to send data back from a function.
  • Python supports returning multiple values easily using tuples.
  • Understanding argument types improves your function design.

Python Function (part-1)|| Python for beginners || Learn Python

๐Ÿง  What You'll Learn

In this first part of the Python Functions series, you will learn the basics of defining and using functions in Python. This tutorial is perfect for beginners who want to understand how functions help organize code, make it reusable, and improve readability.

  • What is a function in Python?
  • How to define a function using def keyword
  • Understanding function arguments and parameters
  • Calling functions and passing data
  • Using return statements to get results

๐Ÿ“ฝ️ Video Walkthrough

The video explains function basics with simple examples and live coding in Python. You’ll see how to write your own functions, how arguments work, and how to return values. This step-by-step demonstration helps you build a strong foundation for using functions effectively in your Python programs.

  • Syntax of Python functions
  • Defining and calling functions
  • Positional and keyword arguments
  • Using return to send back results
  • Practical examples and exercises

✅ Key Takeaways

  • Functions help organize code into reusable blocks.
  • Use def to create a function.
  • Parameters allow functions to accept input data.
  • Return statements send results back to the caller.
  • Understanding functions is essential for writing clean, maintainable Python code.

Featured Post

GROUP BY, HAVING, and Aggregations in SQL Server Explained

Part 9: GROUP BY, HAVING, and Aggregations in SQL Server Microsoft SQL Server Tutorial Series: Beginner to Expert Welcome to Part 9 of...

Popular Posts