Practice Code:
def showinfo(name, age):
print(name, age)
login = False
def login_Check():
username = input("Enter your username: ")
pwd = int(input("Enter your password: "))
if username == "ali" and pwd == 1234:
print("Welcome to dashboard")
else:
print("Invalid username or password")
print("Welcome to Website")
print("Please login")
login_Check()
print("Try again")
login_Check()
print("Try Again")
login_Check()
Explanation
What Is a Function in Python?
A function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, we can put it inside a function and run it whenever we need it.
In Python, a function is created using the def keyword.
The basic structure of a function is:
def function_name():
# code to execute
For example:
def welcome():
print("Welcome to Python!")
Here, welcome is the function name. The code inside the function will run when we call the function.
welcome()
The important thing to understand is that defining a function does not execute it. The function runs when we call it.
Creating a Login Function
Let’s look at a simple example:
def login_Check():
username = input("Enter your username: ")
pwd = int(input("Enter your password: "))
if username == "ali" and pwd == 1234:
print("Welcome to dashboard")
else:
print("Invalid username or password")
We created a function called login_Check().
Everything indented inside the function belongs to that function.
Step 1: Create the Function
def login_Check():
The def keyword tells Python that we are creating a function.
login_Check is the name of our function, and () means that this function does not require any parameters.
The colon : indicates that the function body starts after this line.
Step 2: Write Code Inside the Function
Inside the function, we ask the user for a username and password:
username = input("Enter your username: ")
pwd = int(input("Enter your password: "))
The input() function allows the user to enter information.
The password is converted into an integer using int() because we are comparing it with the number 1234.
Step 3: Use a Condition
Next, we check whether the username and password are correct:
if username == "ali" and pwd == 1234:
print("Welcome to dashboard")
else:
print("Invalid username or password")
The if statement checks the condition.
The and operator means that both conditions must be true.
If the username is ali and the password is 1234, Python prints:
Welcome to dashboard
Otherwise, it prints:
Invalid username or password
Step 4: Calling the Function
After creating the function, we can call it using its name:
login_Check()
This tells Python to execute the code inside the login_Check() function.
We can call the same function multiple times:
login_Check()
print("Try again")
login_Check()
print("Try Again")
login_Check()
This is one of the biggest benefits of functions: write the code once and reuse it whenever needed.
Complete Example
Here is the complete program:
def showinfo(name, age):
print(name, age)
login = False
def login_Check():
username = input("Enter your username: ")
pwd = int(input("Enter your password: "))
if username == "ali" and pwd == 1234:
print("Welcome to dashboard")
else:
print("Invalid username or password")
print("Welcome to Website")
print("Please login")
login_Check()
print("Try again")
login_Check()
print("Try Again")
login_Check()
Key Things to Remember
When working with functions in Python, remember these three basic steps:
- Define the function using
def. - Write the required code inside the function.
- Call the function using its name followed by
().
For example:
def hello():
print("Hello!")
hello()
Here, def hello(): creates the function, while hello() calls and executes it.
Practice
The best way to understand functions is to write and modify the code yourself.
You can practice this example using the Programiz Python Online Compiler:
Try creating your own functions such as welcome(), calculate(), or student_info() and call them from your program.
Conclusion
Functions make Python programs more organized, reusable, and easier to understand. The login_Check() example shows the basic idea: create a function, put a specific task inside it, and call that function whenever you need to perform the task.
Once you understand this concept, you can move on to more advanced function concepts such as parameters, arguments, return values, and default parameters.