Notes for Unit 2 - Illustrative programs
Program 1
Source Code
print("Enter two values to swap")
a = int(input())
b = int(input())
# using a temporary variable
print("Swapping using temporary variable...")
t = a
a = b
b = t
print("Value of a is {a} and\nValue of b is {b}".format(a=a, b=b))
# using XOR operator, eliminating the need for 3rd variable
print("Swapping using XOR operator...")
a = a ^ b
b = a ^ b
a = a ^ b
print("Value of a is {a} and\nValue of b is {b}".format(a=a, b=b))
def swap_func():
global a, b
print("swap_func function called")
a, b = b, a
swap_func() # a and b values get swapped again
print("Value of a is {a} and\nValue of b is {b}".format(a=a, b=b))
Sample Output

Demo
run python3 swap.py
in https://repl.it/@ashokb/unit-2-illustrative-programs
Program2
Source Code
def circulate(a, b, c):
i = 3
while i:
t = a
a = b
b = c
c = t
print(a, b, c)
i -= 1
print("Enter 3 values")
a, b, c = input().split()
print()
circulate(a, b, c)
def circulate_list(alist):
i = len(alist)
while i:
first = alist.pop(0)
alist.append(first)
print(*alist, sep=", ")
i -= 1
alist = input("enter multiple values\n").split()
print()
circulate_list(alist)
Sample Output

Demo
run python3 circulate.py
in https://repl.it/@ashokb/unit-2-illustrative-programs
Program 3
Coordinate Geometry Terms
Coordinate Geometry Definition - It is one of the branches of geometry where the position of a point is defined using coordinates.
What are the Coordinates?
What are the Coordinates?
Coordinates are a set of values which helps to show the exact position of a point in the coordinate plane.
Coordinate Plane Meaning - A coordinate plane is a 2D plane which is formed by the intersection of two perpendicular lines known as the x-axis and y-axis.
What is the Distance Formula?
What is the Distance Formula?
It is used to find the distance between two points situated in A(x1,y1) and B(x2,y2)
What is the most logical way to represent a cartesian point in Python?
A. Use two variables, for e.g. x1 and y1 or x2 and y2
B. Use a single tuple variable
C. None of the above
Choice B.
Tuple is the logical way of representing a point in Python.
What is the syntax for defining a point (3, 4)
as a tuple in Python?
A. (3, 4)
B. tuple(3, 4)
C. Both the above are valid
Choice C.
(3, 4) and tuple(3, 4) are equivalent to each other.
How can you deconstruct a tuple that represents a point?
How do you deconstruct a tuple?
If
pointA = (6, 8) # construction
then
x1, y1 = pointA # deconstruction
print(x1, y1) # 6, 8
x1, y1, x2, y2 = (1, 2, 3, 4)
What is the value of x1
and y2
?
1 and 4.
What is the distance between (4, 0) and (0, 0)?
4
d = (x1 - x2)
= 4 - 0
= 4
What is the distance between (4, 3) and (4, 0)?
3
d = (x1 - x2) + (y1 - y2)
= (4 - 4) + (3 - 0)
= 0 + 3
= 3
What is the distance between (4, 3) and (0, 0)?
Using previous formula, we get
d = (x1 - x2) + (y1 - y2)
= (4 - 0) + (3 - 0)
= 4 + 3
= 7
But we know from https://www.mathsisfun.com/algebra/distance-2-points.html
If (x1 - x2) + (y1 - y2) is giving a higher value than what is correct,
What can the distance formula be?
Why not square the x component and square the y component, and then reduce it down by using square root?
The distance formula is as follows:
What is the distance between (2, -1) and (5, 3)?
Are you confident of writing the code for Distance between Two Points?

Test yourself at http://j.mp/twoPointsCC
Source Code
from math import sqrt
def distance_between(pointA, pointB):
x1, y1 = pointA
x2, y2 = pointB
distx = x1 - x2
disty = y1 - y2
return sqrt(distx**2 + disty**2)
# main program
# tuple packing allows for assigning
# multiple inputs in one statement
print("Enter coordinates for Point A")
pointA = int(input()), int(input())
print("Enter coordinates for Point B")
pointB = int(input()), int(input())
print("distance between {0} and {1} is {2}"
.format(pointA, pointB, distance_between(pointA, pointB))
)
Sample Output

Demo
run python3 distance.py
in https://repl.it/@ashokb/unit-2-illustrative-programs
Teaching Assets
Video capture
http://j.mp/distanceThisVideo
demonstrates how to use Memcode and CyberDojo and Slides
CyberDojo link
The super simple testing framework for capturing incremental coding sessions
Auto-generated documentation
distance_between doctstring documentation
consolidates all docstrings in one place (docs/README.md) in the repo
Bonus Material
Swap
There are actually 4 ways to do it. Please find all of them. https://www.geeksforgeeks.org/python-program-to-swap-two-variables/
Last updated