[Python Programming Basics to Advanced]:Positional Keyword Optional Variable-length Arguments|Lab 27

preview_player
Показать описание
This Python programming playlist is designed to take beginners with zero programming experience to an expert level. The course covers installation, basic syntax, practical scenarios, and efficient logic building. The course material includes PDF handouts, review questions, and covers a wide range of topics, from data types to advanced functions like Lambda and Recursive functions, Generators, and JSON data parsing.

In this lesson we will study about different properties of the input arguments of the functions we can set while defining the function. These include:
- Default value of an Input Argument (Optional input argument)
- Positional-only and Keyword-only Input Arguments
- Variable length Input Arguments

The link for proper format of the Doc String:

Complete Playlist:

If you have the basic programming knowledge and interested to learn Object-Oriented Programming in Python, check out this playlist:

Lab Manual 27 can be downloaded from here:

Review Question:
1- Polygon is a planar closed shape with straight line segments. A polygon can have minimum 3 sides and there is no limit for the maximum sides. If a polygon has n sides, it will have n vertices. A vertex on XY plane can be represented as a Tuple with two values (x and y). As an example a polygon can have five vertices as:
v1=(2,4)
v2=(1,3)
v3=(5,-2)
v4=(3,1)
v5=(6,5)

Your task is to create a user-defined function polyParameter which could take any number of vertices as the input argument and will return the Perimeter of the Polygon. For above case, the function will be used like this:
polyParameter(v1,v2,v3,v4,v5)

To find the parameter you need to find the sum of length of all sides of the polygon. It will be better to create a function dist(v1,v2) which will take two vertices as input argument and returns the length of the line segment.

#PythonProgramming #python #pythontutorial
Рекомендации по теме
Комментарии
Автор

from math import sqrt
def dist(a, b):
return
def polyParameter(*a):
perimeter=sum([dist(a[vertices], a[vertices-1]) for vertices in range(len(a))])
return perimeter
v1=v1, v2, v3, v4, v5=(2, 4), (1, 3), (5, -2), (3, 1), (6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

abdurrehmansarwar
Автор

from math import sqrt
def dist(a, b):
'Tells the distance between two vertices'
return
def polyperimeter(*x):
'Calculates the sum of sides of the polygon'
sides=[]
for i in range(len(x)):
if (i+1)<len(x):
s=dist(x[i], x[i+1])
sides.append(s)
else:
s=dist(x[-1], x[0])
sides.append(s)
return sum(sides)
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(f'The perimeter is:{polyperimeter(v1, v2, v3, v4, v5)}')

farazshah
Автор

from math import sqrt as sq
def dist(x, y):
return
def polyParameter(*a):
p=round(sum([dist(a[i], a[i-1]) for i in range(len(a))]), 2) # by usig comprehension method
return 'The required answer is:'+str(p)

##Main programme.

p1=(3, 1)
p2=(4, -3)
p3=(1, 5)
p4=(8, 2)
p5=(-4, 4)
p6=(3, 7)
p7=(9, -9)
print(polyParameter(p1, p2, p3, p4, p5, p6, p7))

jahanzaibaslamsiddique
Автор

2019-MC-17


from math import sqrt
def dist(a, b):
'Tells the distance between two vertices'
return
def polyperimeter(*x):
'Calculates the sum of sides of the polygon'
sides=[]
for i in range(len(x)):
if (i+1)<len(x):
s=dist(x[i], x[i+1])
sides.append(s)
else:
s=dist(x[-1], x[0])
sides.append(s)
return sum(sides)
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(f'The perimeter is:{polyperimeter(v1, v2, v3, v4, v5)}')

MuhammadHassan-ldyt
Автор

Assalam-o-Alaikum Sir!
from math import*

def dist(a, b):

return

def polyParameter(*a):

sum=0

for i in range(len(a)):

print(i)

c=a[i]

d=a[i-1]

e=dist(c, d)

sum+=e

return sum
v1=(2, 4)

v2=(1, 3)

v3=(5, -2)

v4=(3, 1)

v5=(6, 5)

print(polyParameter(v1, v2, v3, v4, v5))

zainzakir
Автор

from math import*
def dist(a, b):
return
def polyParameter(*a):
sum=0
for i in range(len(a)):
print(i)
c=a[i]
d=a[i-1]
e=dist(c, d)
sum+=e
return sum
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

waleedraza
Автор

from math import *
def polyPerimeter(*vertices):
perimeter=0
def dist(vert1, vert2):
return
for i in range(len(vertices)):
j=i+1
if i==len(vertices)-1:
j=0
distanc=dist(vertices[i], vertices[j])
perimeter+=distanc
return perimeter

MuhammadAhmad-dshu
Автор

from math import sqrt
def distance(a, b):
return
def poly_parameter(x):
c=0
for i in x:
try:
d=distance(x[i], x[i+1])
except:
d=distance(x[0], x[-1])
c+=d
return c
x=[(2, 4), (1, 3), (5, -2), (3, 1), (6, 5)]

aliraza-zlft
Автор

from math import sqrt
def dist(x, y):
return
def polyParameter(*a):
s=0
for i in range(len(a)):
if i+1<len(a):
s+=dist(a[i], a[i+1])
else:
s+=dist(a[-1], a[0])
return s
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

yusrakashif
Автор

Review Question:

from math import *
def dist(a, b):
return
def polyParameter(*d):
s=0
for i in range(len(d)):
h=d[i]
j=d[i-1]
k=dist(h, j)
s+=k
return s

v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

zoniseithzoniseith
Автор

from math import sqrt
def dist(v1, v2):
return sqrt ((v2[0]-v1[0])**2 + (v2[1]-v1[1])**2)
def polyParameter(r):
a = []
for i in range(len(r)):
if(i+1 < len(r)):
s = dist(r[i], r[i+1])
a.append(s)
else:
f = dist(r[i], r[0])
a.append(f)
return sum(a)

'''Main Program'''
e = eval(input('How many vertices you want to give: '))
r = []
for i in range(e):
s = eval(input(f' Vertex-{i+1}: '))
r.append(s)
print(f'The Parameter of the polygon is = {polyParameter((r))}')

ahmedimran
Автор

from math import sqrt as s

def dist(a, b):

x1, y1=a

x2, y2=b

return s((x2-x1)**2+(y2-y1)**2)

def polyParameter(*k):

@/this List will include the distance between the last and the first element as 0-1=-1 (which is the index of last elemnt)

return sum([dist(k[v], k[v-1]) for v in range(len(k))])

v1, v2, v3, v4, v5=(2, 4), (1, 3), (5, -2), (3, 1), (6, 5)

print(polyParameter(v1, v2, v3, v4, v5))

haris
Автор

from math import sqrt as s
def dist(a, b):
x1, y1=a
x2, y2=b
return s((x2-x1)**2+(y2-y1)**2)
def polyParameter(*k):
#This List will include the distance between the last and the first element as 0-1=-1 (which is the index of last elemnt)
return sum([dist(k[v], k[v-1]) for v in range(len(k))])
v1, v2, v3, v4, v5=(2, 4), (1, 3), (5, -2), (3, 1), (6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

amaanmajid
Автор

@/review Question:
from math import *
def dist(a, b):
return
def polyParameter(*d):
s=0
for i in range(len(d)):
h=d[i]
j=d[i-1]
k=dist(h, j)
s+=k
return s
@/main Programme is
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

shahzaibaliafaq
Автор

from math import *
def dist(a, b):
return
def polyParameter(*d):
s=0
for i in range(len(d)):
h=d[i]
j=d[i-1]
k=dist(h, j)
s+=k
return s
@/main Programme
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

abdulrehmansajid
Автор

Review Question:
from math import *
def dist(a, b):
return
def polyParameter(*d):
s=0
for i in range(len(d)):
h=d[i]
j=d[i-1]
k=dist(h, j)
s+=k
return s
​@/main​ Programme
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

wasifalii
Автор

#Review_Question
from math import*
def dist(a, b):
return
def polyParameter(*a):
sum=0
for i in range(len(a)):
print(i)
c=a[i]
d=a[i-1]
e=dist(c, d)
sum+=e
return sum

v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

abdulrehmankhalid
Автор

from math import sqrt
def dist(x, y):
return
def polyParameter(*a):
p=sum([dist(a[i], a[i-1]) for i in range(len(a))])
return p

v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

noorulhuda
Автор

#Review_Question
from math import sqrt
def dist(a, b):
'Tells the distance between two vertices'
return
def polyperimeter(*x):
'Calculates the sum of sides of the polygon'
sides=[]
for i in range(len(x)):
if (i+1)<len(x):
s=dist(x[i], x[i+1])
sides.append(s)
else:
s=dist(x[-1], x[0])
sides.append(s)
return sum(sides)
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(f'The perimeter is:{polyperimeter(v1, v2, v3, v4, v5)}')

nukhbaiqbal
Автор

##Review
from math import *
def dist(a, b):
return
def polyParameter(*a):
sum=0
for i in range(len(a)):
print(i)
c=a[i]
d=a[i-1]
e=dist(c, d)
sum+=e
return sum
v1=(2, 4)
v2=(1, 3)
v3=(5, -2)
v4=(3, 1)
v5=(6, 5)
print(polyParameter(v1, v2, v3, v4, v5))

dawood