Lab3003
  • FacultyAlgorithmChallenge
  • README
  • Question Bank
  • Python Documentation
    • BetterMap
    • Board
    • Card
    • Circle
    • Deck
    • README.md
    • Guide For Bookmarklet
    • Hand
    • HashMap
    • Hist
    • Kangaroo
    • LinearMap
    • Markov
    • Point
    • PokerDeck
    • PokerHand
    • Rectangle
    • State
    • Test
    • ThinkPython
    • ThinkPython2
    • Time
    • Think Python
    • thinkpython
  • Files
  • Image Files
  • lab
    • InsertSortTDD
    • countingBits
    • cs3003mergeSortLectureGuideLine
    • index_min
    • insort
    • One-line factorial function in Python
  • Manual for Python Lab
    • COURSE OBJECTIVES
    • 1_timelines
    • FDPNotes
    • PC-2_Python
    • lab1-kgashok.py
    • Lab 10 :Matrix Multiplication
    • Lab 11: Programs that take command line arguments (word count)
    • Lab 11: Programs that take command line arguments
    • Lab 12: Compute the most frequently used words from the text file
    • Lab 12: Find the most frequent words in a text read from a file
    • Lab 12a: File content sorter
    • Lab 2: Find the square root of a number (Newton’s method)
    • Lab 3: Compute power of a number (Exponentiation)
    • Lab 3: Exponentiation (power of a number)
    • lab4-binaraysearch-anvar.py
    • Solution for Binary Search
    • lab4-joelanandraj-binarysearch.py
    • Lab 4: Linear and Binary Search
    • lab4-lin-anvar.py
    • Linear Searcher
    • Lab 5 : find n prime numbers
    • lab6-kgashok.py
    • lab7-kgashok.py
    • lab8-kgashok.py
    • Lab 8: Selection and Insertion sort using Python
    • Merge Sort
    • Quick Sort
    • labSheet
    • pc0
    • sortPythonic
    • Sorting
  • misc
    • Bookmarklets
    • Guide For Bookmarklet
    • pythonResources
    • FDP for Python
    • Agenda for Workshop
      • Agenda
  • notes
    • Problem Set
    • InsertSortTDD
    • MergeSort
    • cs3003-unit1-notes
    • cs3003-unit3
    • cs3003-unit4-examples
    • Unit 4 Lists, Tuples and Dictionaries
    • cs3003insertsortLectureGuide
    • cs3003mergeSortLectureGuideLine
    • Designing and Delivering Effective Lectures
    • histogram.py
    • selectSortLectureGuide
  • Sandbox to try ThinkPy2 code
    • Code from ThinkPython Github repository
  • Important Topics
    • 3003-syllabus
    • GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY L T P C
    • Unit II Version 2
    • Unit IV material
    • UNIT III CONTROL FLOW, FUNCTIONS
    • Unit 1 Algorithm Problem Solving
    • Unit_V_Notes
    • UpdatedSyllabus
    • glossary
    • glossaryGeneration.py
    • importantTopics-kgashok
    • memCSV
    • Tower of Hanoi
    • Notes for Unit 2 - Illustrative programs
    • unit5-updated_notes
    • unit_1_notes
    • Unit 3 Control Flow Version 1
    • unit 3
      • UNIT III CONTROL FLOW, FUNCTIONS
    • unit_i_img
Powered by GitBook
On this page
  • Program 1
  • Source Code
  • Sample Output
  • Demo
  • Program2
  • Source Code
  • Sample Output
  • Demo
  • Program 3
  • Source Code
  • Sample Output
  • Demo
  • Teaching Assets
  • Bonus Material
  • Swap
  1. Important Topics

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

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

Program 3

Question
Answer

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.

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?

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

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?



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

Teaching Assets

item
link
remarks

Video capture

http://j.mp/distanceThisVideo

demonstrates how to use Memcode and CyberDojo and Slides

Memcode cards

useful for classroom and personal review

CyberDojo link

The super simple testing framework for capturing incremental coding sessions

Google Slides

Or PPTx file is better?

Auto-generated documentation

Bonus Material

Swap

PreviousTower of HanoiNextunit5-updated_notes

Last updated 3 years ago

out1

run python3 swap.py in

out2

run python3 circulate.py in

What is the for defining a point (3, 4) as a tuple in Python?

But we know from

Test yourself at

out3

run python3 distance.py in

doctstring documentation

consolidates all docstrings in one place () in the repo

There are actually 4 ways to do it. Please find all of them.

1 and 4. 
https://repl.it/@ashokb/unit-2-illustrative-programs
https://repl.it/@ashokb/unit-2-illustrative-programs
https://repl.it/@ashokb/unit-2-illustrative-programs
https://www.geeksforgeeks.org/python-program-to-swap-two-variables/
syntax
https://www.mathsisfun.com/algebra/distance-2-points.html
http://j.mp/twoPointsCC
https://www.memcode.com/courses/2288/
http://cyberdojo1.kgfsl.com/kata/edit/Tx2Vqt
PDF version
docs/README.md
distance_between