0% found this document useful (0 votes)
4 views18 pages

Python 3 Programming Basics and Functions

The document contains a series of Python 3 programming exercises, each with a specific function to implement. These exercises cover various topics including printing, namespaces, mathematical operations, string manipulation, list and set operations, and more. Each section includes a function definition, input handling, and expected output, designed to help learners practice Python programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Python 3 Programming Basics and Functions

The document contains a series of Python 3 programming exercises, each with a specific function to implement. These exercises cover various topics including printing, namespaces, mathematical operations, string manipulation, list and set operations, and more. Each section includes a function definition, input handling, and expected output, designed to help learners practice Python programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Python 3 Programming

1. Print

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'Greet' function below.
#
# The function accepts STRING Name as parameter.
#

def Greet(Name):
# Write your code here
print("Welcome " + Name + ".")
print("It is our pleasure inviting you.")
print("Have a wonderful day.")

if __name__ == '__main__':
Name = input()

Greet(Name)

2. Namespaces 1

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'Assign' function below.
#
# The function accepts following parameters:
# 1. INTEGER i
# 2. FLOAT f
# 3. STRING s
# 4. BOOLEAN b
#

def Assign(i, f, s, b):


# Write your code here
w = i
x = f
y = s
z = b
print(w)
print(x)
print(y)
print(z)
print(dir())

if __name__ == '__main__':

i = int(input().strip())

f = float(input().strip())

s = input()

b = input().strip()

Assign(i, f, s, b)

3. Handson - Python - Get Additional Info

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'docstring' function below.
#
# The function is expected to output a STRING.
# The function accepts STRING x as parameter.
#

def docstring(functionname):
# Write your code here
help(functionname)

if __name__ == '__main__':

x = input()
docstring(x)

4. Name Space 2

#!/bin/python3

import math
import os
import random
import re
import sys
#
# Complete the 'Prompt' function below.
#
#

def Prompt():
# Write your code here
x = input('Enter a STRING:\n')
print(x)
print(type(x))

if __name__ == '__main__':

Prompt()

[Link] - Python - Usage imports

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'calc' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER c as parameter.
#

def calc(c):
# Write your code here
r = c/(2*[Link])
a = r*r*[Link]
x = round(r,2)
y = round(a,2)
return(x,y)

if __name__ == '__main__':
fptr = open([Link]['OUTPUT_PATH'], 'w')

c = int(input().strip())

result = calc(c)

[Link](str(result) + '\n')

[Link]()

6. Python Range1

#!/bin/python3
import math
import os
import random
import re
import sys

#
# Complete the 'func' function below.
#
# The function is expected to print an INTEGER.
# The function accepts following parameters:
# 1. INTEGER startvalue
# 2. INTEGER endvalue
# 3. INTEGER stepvalue
#

def rangefunction(startvalue, endvalue, stepvalue):


# Write your code here
for i in range(startvalue,endvalue,stepvalue):
print(i*i,end = "\t")

if __name__ == '__main__':

x = int(input().strip())

y = int(input().strip())

z = int(input().strip())

rangefunction(x, y, z)

7. Using int

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'Integer_fun' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. FLOAT a
# 2. STRING b
#

def Integer_fun(a, b):


# Write your code here
c = int(a)
d = int(b)
print(type(a))
print(type(b))
print(c)
print(d)
print(type(c))
print(type(d))

if __name__ == '__main__':
a = float(input().strip())

b = input()

Integer_fun(a, b)

8. Using Int operations

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'find' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER num1
# 2. INTEGER num2
# 3. INTEGER num3
#

def find(num1, num2, num3):


# Write your code here
print(num1<num2 and num2 >= num3,end=" ")
print(num1>num2 and num2 <= num3,end=" ")
print(num3 == num1 and num1!=num2,end=" ")

if __name__ == '__main__':

num1 = int(input().strip())

num2 = int(input().strip())

num3 = int(input().strip())

find(num1, num2, num3)

9. Using intMath Operations

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'Integer_Math' function below.
#
# The function accepts following parameters:
# 1. INTEGER Side
# 2. INTEGER Radius
#

def Integer_Math(Side, Radius):


# Write your code here
a = Side * Side
b = Side * Side * Side
c = 3.14 * Radius * Radius
x = round(c,2)
d = (4/3)*3.14*Radius*Radius*Radius
y = round(d,2)
print("Area of Square is "+ str(a))
print("Volume of Cube is "+ str(b))
print("Area of Circle is "+ str(x))
print("Volume of Sphere is "+ str(y))

if __name__ == '__main__':
Side = int(input().strip())

Radius = int(input().strip())

Integer_Math(Side, Radius)

10. Using Float 1

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'tria' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. FLOAT n1
# 2. FLOAT n2
# 3. INTEGER n3
#

def triangle(n1, n2, n3):


# Write your code here
x = round((n1 * n2)/2,n3)
y = round([Link],n3)
return x,y

if __name__ == '__main__':
fptr = open([Link]['OUTPUT_PATH'], 'w')

n1 = float(input().strip())

n2 = float(input().strip())

n3 = int(input().strip())

result = triangle(n1, n2, n3)

[Link](str(result) + '\n')

[Link]()

11. Using float 2

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'Float_fun' function below.
#
# The function accepts following parameters:
# 1. FLOAT f1
# 2. FLOAT f2
# 3. INTEGER Power
#

def Float_fun(f1, f2, Power):


# Write your code here
print("#Add")
print(f1+f2)
print("#Subtract")
print(f1-f2)
print("#Multiply")
print(f1*f2)
print("#Divide")
print(f2/f1)
print("#Remainder")
print(f1%f2)
print("#To_The_Power_Of")
a = f1 ** Power
print(a)
print("#Round")
print(round(a,4))
if __name__ == '__main__':
f1 = float(input().strip())

f2 = float(input().strip())

Power = int(input().strip())

Float_fun(f1, f2, Power)

12. String Operations - 1

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'strng' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING fn
# 2. STRING ln
# 3. STRING para
# 4. INTEGER number
#

def stringoperation(fn, ln, para, number):


# Write your code here
print(fn+'\n'*number+ln)
print(fn+" "+ln)
print(fn*number)
print(f"The sentence is {para}")

if __name__ == '__main__':

fn = input()

ln = input()

para = input()

no = int(input().strip())

stringoperation(fn, ln, para, no)

13. Newline and Tab Spacing

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'Escape' function below.
#
# The function accepts following parameters:
# 1. STRING s1
# 2. STRING s2
# 3. STRING s3
#

def Escape(s1, s2, s3):


# Write your code here
s = "Python\tRaw\nString\tConcept"
print(s1+'\n'+s2+'\n'+s3)
print(s1+'\t'+s2+'\t'+s3)
print(s)
s = r"Python\tRaw\nString\tConcept"
print(s)

if __name__ == '__main__':
s1 = input()

s2 = input()

s3 = input()

Escape(s1, s2, s3)

14. String Operations 2

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'resume' function below.
#
# The function is expected to print a STRING.
# The function accepts following parameters:
# 1. STRING first
# 2. STRING second
# 3. STRING parent
# 4. STRING city
# 5. STRING phone
# 6. STRING start
# 7. STRING strfind
# 8. STRING string1
#

def resume(first, second, parent, city, phone, start, strfind, string1):


# Write your code here
print([Link]().capitalize()+" "+[Link]().capitalize()+"
"+[Link]().capitalize()+" "+[Link]())
print([Link]())
print([Link](start))
print([Link](strfind)+[Link](strfind)+[Link](strfind)
+[Link](strfind))
print([Link]())
print([Link](strfind))

if __name__ == '__main__':

a = input()

b = input()

c = input()

d = input()

ph = input()

no = input()

ch = input()

str = input()

resume(a, b, c, d, ph, no, ch, str)

15. List Operations 1

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'List_Op' function below.
#
# The function accepts following parameters:
# 1. LIST Mylist
# 2. LIST Mylist2
#

def List_Op(Mylist, Mylist2):


# Write your code here
print(Mylist)
print(Mylist[1])
for i in range(len(Mylist)):
if(i==len(Mylist)-1):
print(Mylist[i])
[Link](3)
for i in range(len(Mylist)):
if( i == 3 ):
Mylist[i] = 60
print(Mylist)
print(Mylist[1:5])
[Link](Mylist2)
print(Mylist)
[Link](Mylist2)
print(Mylist)
[Link]()
print(Mylist)
print(len(Mylist))

if __name__ == '__main__':
qw1_count = int(input().strip())

qw1 = []

for _ in range(qw1_count):
qw1_item = int(input().strip())
[Link](qw1_item)

qw2_count = int(input().strip())

qw2 = []

for _ in range(qw2_count):
qw1_item = int(input().strip())
[Link](qw1_item)

List_Op(qw1,qw2)

16. List Operations 2

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'tuplefun' function below.
#
# The function accepts following parameters:
# 1. LIST list1
# 2. LIST list2
# 3. STRING string1
# 4. INTEGER n
#

def tuplefunction(list1, list2, string1, n):


# Write your code here
tuple1 = tuple(list1)
tuple2 = tuple(list2)
tuple3 = tuple1 + tuple2
print(tuple3)
print(tuple3[4])
tuple4 = (tuple1,tuple2)
print(tuple4)
print(len(tuple4))
print((string1,)*n)
print(max(tuple1))
if __name__ == '__main__':

qw1_count = int(input().strip())

qw1 = []

for _ in range(qw1_count):
qw1_item = int(input().strip())
[Link](qw1_item)

qw2_count = int(input().strip())

qw2 = []

for _ in range(qw2_count):
qw1_item = input()
[Link](qw1_item)

str1 = input()

n = int(input().strip())

tuplefunction(qw1,qw2,str1, n)

17. Python - Slicing

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'sliceit' function below.
#
# The function accepts List mylist as parameter.
#

def sliceit(mylist):
# Write your code here
a = slice(1,3)
print(mylist[a])
b = slice(1,len(mylist),2)
print(mylist[b])
c = slice(-1,-4,-1)
print(mylist[c])

if __name__ == '__main__':
mylist_count = int(input().strip())

mylist = []

for _ in range(mylist_count):
mylist_item = input()
[Link](mylist_item)

sliceit(mylist)

18. Python - Range

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'generateList' function below.
#
# The function accepts following parameters:
# 1. INTEGER startvalue
# 2. INTEGER endvalue
#

def generateList(startvalue, endvalue):


# Write your code here
list1 = list(range(startvalue,endvalue+1))
print(list1[:3])
list2 = list1[::-1]
print(list2[0:5])
print(list1[::4])
print(list2[::2])

if __name__ == '__main__':
startvalue = int(input().strip())

endvalue = int(input().strip())

generateList(startvalue, endvalue)

19. Python - Set

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'setOperation' function below.
#
# The function is expected to return a union, intersection, difference(a,b),
difference(b,a), symmetricdifference and frozen set.
# The function accepts following parameters:
# 1. List seta
# 2. List setb
#

def setOperation(seta, setb):


# Write your code here
seta = set(seta)
setb = set(setb)
union = [Link](setb)
intersection = [Link](setb)
diff1 = [Link](setb)
diff2 = [Link](seta)
symdiff = seta.symmetric_difference(setb)
frozenseta = frozenset(seta)
return(union, intersection, diff1, diff2, symdiff, frozenseta )

if __name__ == '__main__':
seta_count = int(input().strip())

seta = []

for _ in range(seta_count):
seta_item = input()
[Link](seta_item)

setb_count = int(input().strip())

setb = []

for _ in range(setb_count):
setb_item = input()
[Link](setb_item)

un, intersec, diffa, diffb, sydiff, frset = setOperation(seta, setb)


print(sorted(un))
print(sorted(intersec))
print(sorted(diffa))
print(sorted(diffb))
print(sorted(sydiff))
print("Returned value is {1} frozenset".format(frset, "a" if type(frset) ==
frozenset else "not a"))

20. Python Dictionary

#!/bin/python3
import math
import os
import random
import re
import sys

from pprint import pprint as print

#
# Complete the 'myDict' function below.
#
# The function accepts following parameters:
# 1. STRING key1
# 2. STRING value1
# 3. STRING key2
# 4. STRING value2
# 5. STRING value3
# 6. STRING key3
#

def myDict(key1, value1, key2, value2, value3, key3):


# Write your code here
dict1 = {key1:value1}
print(dict1)
dict1[key2] = value2
print(dict1)
dict1[key1] = value3
print(dict1)
[Link](key3)
return dict1

if __name__ == '__main__':
key1 = input()

value1 = input()

key2 = input()

value2 = input()

value3 = input()

key3 = input()

mydct = myDict(key1, value1, key2, value2, value3, key3)

print(mydct if type(mydct) == dict else "Return a dictionary")

21. While Loop

#!/bin/python3

import math
import os
import random
import re
import sys
#
# Complete the 'calculateNTetrahedralNumber' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER startvalue
# 2. INTEGER endvalue
#

def calculateNTetrahedralNumber(startvalue, endvalue):


# Write your code here
list1 = list()
i = startvalue
while i<= endvalue:
num = (i*(i+1)*(i+2)/6)
[Link](int(num))
i = i + 1
return list1

if __name__ == '__main__':
fptr = open([Link]['OUTPUT_PATH'], 'w')

startvalue = int(input().strip())

endvalue = int(input().strip())

result = calculateNTetrahedralNumber(startvalue, endvalue)

[Link]('\n'.join(map(str, result)))
[Link]('\n')

[Link]()

22. For Loop:-

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'sumOfNFibonacciNumbers' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.
#

def sumOfNFibonacciNumbers(n):
# Write your code here
first = 0
second = 1
result = 1
if n <= 1:
return 0
else:
for elem in range(2,n):
next = first + second
result = result + next
first = second
second = next
return result

if __name__ == '__main__':
fptr = open([Link]['OUTPUT_PATH'], 'w')

n = int(input().strip())

result = sumOfNFibonacciNumbers(n)

[Link](str(result) + '\n')

[Link]()

23. If Condition

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'calculateGrade' function below.
#
# The function is expected to return a STRING_ARRAY.
# The function accepts 2D_INTEGER_ARRAY students_marks as parameter.
#

def calculateGrade(students_marks):
# Write your code here
list1 = list()
for i in range(len(students_marks)):
count = 0
sum = 0
avg = 0
for j in range(len(students_marks[i])):
count = count + 1
sum = sum + students_marks[i][j]
avg = sum/count
if avg >= 90:
[Link]("A+")
elif avg >= 80:
[Link]("A")
elif avg >= 70:
[Link]("B")
elif avg >= 60:
[Link]("C")
elif avg >= 50:
[Link]("D")
elif avg < 50:
[Link]("F")
return list1

if __name__ == '__main__':
fptr = open([Link]['OUTPUT_PATH'], 'w')

students_marks_rows = int(input().strip())
students_marks_columns = int(input().strip())

students_marks = []

for _ in range(students_marks_rows):
students_marks.append(list(map(int, input().rstrip().split())))

result = calculateGrade(students_marks)

[Link]('\n'.join(result))
[Link]('\n')

[Link]()

You might also like