We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 69c860b commit 511164fCopy full SHA for 511164f
Functions/def Functions/Examples/Swap 2 Numbers.py
@@ -1,12 +1,29 @@
1
-### Swap two numbers ###
+### Swap 2 numbers ###
2
3
-def swapnos(n1, n2):
4
- if n1<n2:
5
- print(n2, n1)
6
- else:
7
- print (n1, n2)
+def swap_numbers(a, b):
+ # Print values before swapping
+ print("Before swapping:")
+ print("a =", a)
+ print("b =", b)
8
9
-n1 = int(input("Enter number 1: "))
10
-n2 = int(input("Enter number 2: "))
+ # Swap using a temporary variable
+ temp = a
11
+ a = b
12
+ b = temp
13
-swapnos(n1, n2)
14
+ # Print values after swapping
15
+ print("\nAfter swapping:")
16
17
18
+
19
+swap_numbers(5, 10)
20
21
+''' Output:
22
+Before swapping:
23
+a = 5
24
+b = 10
25
26
+After swapping:
27
+a = 10
28
+b = 5
29
+'''
0 commit comments