🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Swapping numbers is a common exercise in programming to understand variable manipulation. While swapping two numbers is straightforward, swapping three numbers introduces a slightly more complex challenge. This task can help us delve deeper into the mechanics of assignment and value swapping in Python.
Swapping involves exchanging the values held by the variables. When we swap three numbers, we shift their values so that each number moves to the position of the next, with the last number going to the position of the first.
2. Program Steps
1. Define three numbers to be swapped.
2. Perform the swapping in a circular fashion.
3. Output the numbers after swapping.
3. Code Program
# Initialize three numbers
a = 10
b = 20
c = 30
# Print original values
print("Original values:")
print("a =", a)
print("b =", b)
print("c =", c)
# Swap the numbers in a circular fashion
# Here, we use Python's ability to assign multiple variables at once
a, b, c = c, a, b
# Print swapped values
print("\nSwapped values:")
print("a =", a)
print("b =", b)
print("c =", c)
Output:
Original values: a = 10 b = 20 c = 30 Swapped values: a = 30 b = 10 c = 20
Explanation:
1. a, b, and c are initialized with the values 10, 20, and 30 respectively.
2. The original values of a, b, and c are printed before the swap takes place.
3. The multiple assignment a, b, c = c, a, b changes the values of a, b, and c in a circular fashion. The value of c goes to a, a to b, and b to c.
4. The new swapped values are then printed, showing that the variables now hold the values 30, 10, and 20 respectively.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment