You are on page 1of 31

Contents

Print: ....................................................................................................................................................... 2
Namespace1: .......................................................................................................................................... 3
Namespace2: .......................................................................................................................................... 4
Python Range1: ....................................................................................................................................... 5
Get additional info:.................................................................................................................................. 6
usage imports:......................................................................................................................................... 7
Using Int: ................................................................................................................................................. 8
Int Operations: ........................................................................................................................................ 9
Int math: ............................................................................................................................................... 10
Using float1: .......................................................................................................................................... 11
Using float2: .......................................................................................................................................... 12
String 1: ................................................................................................................................................. 13
Newline and tab spacing ........................................................................................................................ 14
String2:.................................................................................................................................................. 15
List1: ..................................................................................................................................................... 17
List2: ..................................................................................................................................................... 18
Slicing:................................................................................................................................................... 20
Range: ................................................................................................................................................... 21
Sets: ...................................................................................................................................................... 22
Dict: ...................................................................................................................................................... 24
while: .................................................................................................................................................... 25
For: ....................................................................................................................................................... 26
Handson23 ............................................................................................................................................ 27
assessment:........................................................................................................................................... 28
Correct answers:................................................................................................................................ 28
Wrong answers: ................................................................................................................................ 31

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,end = '')
print('.')
print('It is our pleasure inviting you.')
print('Have a wonderful day.')

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

Greet(Name)

2
Namespace1:
#!/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(i)
print(f)
print(s)
print(b)
print(dir())
if __name__ == '__main__':

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

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

s = input()

b = input().strip()

Assign(i, f, s, b)

3
Namespace2:
#!/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:")
print('')
print(x)
print(type(x))

if __name__ == '__main__':

Prompt()

4
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)

5
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)

6
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
radius=0.0
radius=c/(2*math.pi)
radius1=round(radius,2)
area=round((math.pi*radius*radius),2)
return(radius1,area)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

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

result = calc(c)

fptr.write(str(result) + '\n')

fptr.close()

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
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 & num2>=num3,end=' ')
print(num1>num2 & num2<=num3,end=' ')
print(num3==num1 & num1!=num2,end=' ')

if __name__ == '__main__':

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

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

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

find(num1, num2, num3)

9
Int math:
#!/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
pi=3.14
asa=round((Side**2),2)
vc=round((Side**3),2)
ac=round((pi*Radius**2),2)
vs=round(((4/3)*pi*Radius**3),2)
print('Area of Square is',asa)
print('Volume of Cube is',vc)
print('Area of Circle is', ac)
print('Volume of Sphere is',vs)

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

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

Integer_Math(Side, Radius)

10
Using float1:
#!/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
pi=round(math.pi,n3)
area=round((n1*n2/2),n3)
return(area,pi)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

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

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

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

result = triangle(n1, n2, n3)

fptr.write(str(result) + '\n')

fptr.close()

11
Using float2:
#!/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")
print(f1**Power)
print("#Round")
print(round(f1**Power,4))

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

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

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

Float_fun(f1, f2, Power)

12
String 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,end='')
print('\n'*number,end='')
print(ln)
print(fn, ln)
print(fn*number)
print('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
print(s1)
print(s2)
print(s3)
print(f"{s1}\t{s2}\t{s3}")
s="Python\tRaw\nString\tConcept"
print(s)
print(repr(s)[1:-1])

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

s2 = input()

s3 = input()

Escape(s1, s2, s3)

14
String2:
#!/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
count=0
first=first.strip().title()
second=second.strip().title()
parent=parent.strip().title()
city=city.strip()
print(f"{first} {second} {parent} {city}")
if phone.isnumeric():
print(True)
else:
print(False)
if start in phone[:len(start)]:
print(True)
else:
print(False)
string=first+second+parent+city
for i in string:
if i==strfind:
count+=1
print(count)
print(string1.split())
print(city.index(strfind))

if __name__ == '__main__':

a = input()

15
b = input()

c = input()

d = input()

ph = input()

no = input()

ch = input()

str = input()

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

16
List1:
#!/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])
print(Mylist[-1])
Mylist.append(3)
Mylist[3]=60
print(Mylist)
print(Mylist[1:5])
Mylist.append(Mylist2)
print(Mylist)
Mylist.extend(Mylist2)
print(Mylist)
Mylist.pop()
print(Mylist)
print(len(Mylist))

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

qw1 = []

for _ in range(qw1_count):
qw1_item = int(input().strip())
qw1.append(qw1_item)

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

qw2 = []

for _ in range(qw2_count):
qw1_item = int(input().strip())
qw2.append(qw1_item)

List_Op(qw1,qw2)

17
List2:
#!/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)
concatedtuple=tuple1+tuple2
print(concatedtuple)
print(concatedtuple[4])
nestttuple=(tuple1,tuple2)
print(nestttuple)
print(len(nestttuple))
reptuple=tuple((string1,)*n)
print(reptuple)
print(max(tuple1))

if __name__ == '__main__':

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

qw1 = []

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

18
qw1.append(qw1_item)

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

qw2 = []

for _ in range(qw2_count):
qw1_item = input()
qw2.append(qw1_item)

str1 = input()

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

tuplefunction(qw1,qw2,str1, n)

19
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
print(mylist[1:3])
print(mylist[1::2])
print(mylist[-1:-4:-1])
if __name__ == '__main__':
mylist_count = int(input().strip())

mylist = []

for _ in range(mylist_count):
mylist_item = input()
mylist.append(mylist_item)

sliceit(mylist)

20
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
l=[]
for i in range(startvalue,startvalue+3):
l.append(i)
print(l)
l=[]
for i in range(endvalue,endvalue-5,-1):
l.append(i)
print(l)
l=[]
for i in range(startvalue,endvalue,4):
l.append(i)
print(l)
l=[]
for i in range(endvalue,startvalue-1,-2):
l.append(i)
print(l)

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

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

generateList(startvalue, endvalue)

21
Sets:
#!/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), differen
ce(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)
s1=seta.union(setb)
s2=seta.intersection(setb)
s3=seta-(setb)
s4=setb-seta
s5=seta.symmetric_difference(setb)
seta=frozenset(seta)
return s1,s2,s3,s4,s5,seta

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

seta = []

for _ in range(seta_count):
seta_item = input()
seta.append(seta_item)

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

setb = []

for _ in range(setb_count):
setb_item = input()
setb.append(setb_item)

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


print(sorted(un))
print(sorted(intersec))
print(sorted(diffa))
print(sorted(diffb))

22
print(sorted(sydiff))
print("Returned value is {1} frozenset".format(frset, "a" if type(frset) == froze
nset else "not a"))

23
Dict:
#!/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
d={key1:value1}
print(d)
d[key2]=value2
print(d)
d[key1]=value3
print(d)
del d[key3]
return d

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")

24
while:
#!/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
a=[]
for n in range(startvalue,endvalue+1):
t=int((n*(n+1)*(n+2))/6)
a.append(t)
return a

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

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

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

result = calculateNTetrahedralNumber(startvalue, endvalue)

fptr.write('\n'.join(map(str, result)))
fptr.write('\n')

fptr.close()

25
For:
#!/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
l=[]
a=0
b=1
for i in range(1,n):
c=a+b
a=b
b=c
l.append(a)
y=sum(l)
return y

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

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

result = sumOfNFibonacciNumbers(n)

fptr.write(str(result) + '\n')

fptr.close()

26
Handson23

27
assessment:
Correct answers:

28
29
30
Wrong answers:

31

You might also like