Swap Values of 3 Variables

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to swap the values of three variables in a Python program. This is a fundamental programming concept that is often used in various algorithms and data structures.

👀 Preview

$ python3 swap.py
1 2 3
3 1 2

$ python3 swap.py
3 1 4
4 3 1

ðŸŽŊ Tasks

In this project, you will learn:

  • How to take three integer inputs from the user
  • How to swap the values of the three variables
  • How to output the new values of the variables after the swap

🏆 Achievements

After completing this project, you will be able to:

  • Understand the logic behind swapping variable values
  • Implement a simple program to swap the values of three variables
  • Apply the swapping technique in more complex programming problems

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302780{{"`Swap Values of 3 Variables`"}} python/variables_data_types -.-> lab-302780{{"`Swap Values of 3 Variables`"}} python/numeric_types -.-> lab-302780{{"`Swap Values of 3 Variables`"}} python/conditional_statements -.-> lab-302780{{"`Swap Values of 3 Variables`"}} python/tuples -.-> lab-302780{{"`Swap Values of 3 Variables`"}} python/function_definition -.-> lab-302780{{"`Swap Values of 3 Variables`"}} python/build_in_functions -.-> lab-302780{{"`Swap Values of 3 Variables`"}} end

Understand the Problem

In this step, you will understand the problem of swapping the values of three variables.

The problem is to implement a program that takes three input integers, a, b, and c, and swaps their values. The goal is to give b the original value of a, give c the original value of b, and give a the original value of c. Finally, the program should output the new values of a, b, and c.

Implement the Swapping Logic

Before you start coding, you need to set up the project environment. The swap.py file is provided for you, and it is located in the /home/labex/project directory.

Now, it's time to implement the swapping logic. In the swap_numbers() function, you need to swap the values of the three variables a, b, and c.

  1. Open the swap.py file.
  2. Complete the swap_numbers() function to the file.
def swap_numbers():
    ## Input three integers separated by spaces
    a, b, c = map(int, input().split())

    ## Swap the values of the variables
    a, b, c = c, a, b

    ## Output the swapped values separated by spaces
    print(a, b, c)


if __name__ == "__main__":
    swap_numbers()

This code defines a function swap_numbers() that takes the three input integers, swaps their values, and then prints the new values.

Test the Program

After implementing the swapping logic, you can test the program by running it in the terminal.

Open a terminal and navigate to the /home/labex/project directory. Then, run the following command to execute the swap.py file:

python3 swap.py

The program will prompt you to enter three integers separated by spaces. Enter the values and press Enter. The program should then output the new values of a, b, and c after the swap.

Try the program with different input values to ensure that it works correctly.

$ python3 swap.py
1 2 3
3 1 2

$ python3 swap.py
3 1 4
4 3 1
âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like