0% found this document useful (0 votes)
72 views

Python

The document contains examples of code snippets that take user input and perform operations like calculating totals, areas, sorting arrays. It also contains examples demonstrating if-else conditional statements and for loops to iterate over arrays/lists.

Uploaded by

Deepak Murugesan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Python

The document contains examples of code snippets that take user input and perform operations like calculating totals, areas, sorting arrays. It also contains examples demonstrating if-else conditional statements and for loops to iterate over arrays/lists.

Uploaded by

Deepak Murugesan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

I/O – Session 1

Binita was traveling from Chennai


tot = int(input(''))
hrs = int(tot/60)
mins = int(tot % 60)
print(hrs, 'Hours and', mins,'Minutes')

Compute the area of a triangle


import math
s1=float(input())
s2=float(input())
s3=float(input())
s=(s1+s2+s3)/2
area =math.sqrt(s*(s-s1)*(s-s2)*(s-s3))
print('The area of triangle is {}'.format(area))

Vetrivel wants to convert the


days=int(input())
hours=int(input())
minutes=int(input())
seconds=int(input())
result=(days*24*3600)+(hours*3600)+(minutes*60)+seconds
print(result)

Timothy boom having the first name


fname=input()
lname=input()
reversed_name= lname + " " + fname
print("Hello",reversed_name)

The electricity officer has mention


uc = int(input())
cpu = int(input())
tb = int(uc ** cpu)
print(tb)

What is the sum of the digits


def calculate(n, power):
return sum([int(i) for i in str(pow(n, power))])
n=2
power = int(input(''))
print (calculate(n,power))

Arif planned to make a room


length=float(input())
width=float(input())
area=length*width
print(round(area,2))

Laasya bought a new volley ball


r = float(input(''))
pi = 3.14
volume = (4.0/3.0)*pi*r**3
print(volume)

In geometry, the area enclosed


r = float(input(""))
pi = 3.142
area = pi * r**2
print("The area of the circle with radius",r,"is:",area)

Pinnaccio went to the school


G=int(input())
S=int(input())
print("The Range of given data =",G-S)

Sajid was booking a train ticket


num1,num2,num3=input().split()
total=int(num1)+int(num2)+int(num3)
print(total)

Selvan was playing with an object


l = int(input(''))
w = int(input(''))
h = int(input(''))
sa = (2*((w*l)+(l*h)+(h*w)))
print(sa)

Athika and ritu got a nice job


bpa = float(input(''))
hra = bpa*0.8
da = bpa*0.4
total = hra+da+bpa
print('{:.02f}'.format(total))

Janaki wants to find the distance


import math
x1=int(input())
y1=int(input())
x2=int(input())
y2=int(input())
d=math.sqrt((x2-x1)**2+(y2-y1)**2)
print(d)

Ratika a young millionaire


p=float(input())
i=float(input())
t=int(input())
interest=(p*i*t)/100
amount=p+interest
print("Interest after {c} Years = ${a:.2f}\nTotal Amount after {c} Years =
${b:.2f}".format(a=interest,b=amount,c=t))

Compute the area of triangle


import math
s1=float(input())
s2=float(input())
s3=float(input())
s=(s1+s2+s3)/2
area=math.sqrt (s*(s-s1)*(s-s2)*(s-s3))
print("The area of triangle is",area)

Nancy bought apples


num1 = int(input())
num2 = int(input())
quotient = round(num1/num2)
remainder = num1%num2
print("Quotient:",end="")
print(quotient)
print("Remainder:",end="")
print(remainder)
Flow control - Session 2

Caleb and Salima are living in interior village

x=int(input())

y=int(input())

if x>y:

print(x-y)

else:

print(x+y)

Shree and Harry was living in the town

num1=float(input())

num2=float(input())

if num1-num2==0.5 or num2-num1==0.5:

print("Approximate Number")

else:

print("Not an Approximate Number")

Mr.Issac the head of Tamilnadu

f=float(input())

c=(f-32)*(5/9)

print(round(c,2),"Centigrade")

if c>=150:

print("Very Hot")

elif 150>=c>=100:

print("Hot")

else:

print("Moderate")

Adhi and Tara travel frequently

m=int(input())
r=float(input())

d=int(input())

if m==4 or m==5:

r1=r*d

e=(r1*20)/100

print("Rs."+"%.2f"%(r1+e))

else:

print("Rs."+"%.2f"%(r*d))

Mrs.Swathy is a principal

s1=int(input())

s2=int(input())

s3=int(input())

s4=int(input())

s5=int(input())

t=s1+s2+s3+s4+s5

p=t/5

print("%.2f"%p,"Percent")

if p>=90:

print("Grade A")

elif 90>=p>=80:

print("Grade B")

elif 80>=p>=70:

print("Grade C")

elif 70>=p>=60:

print("Grade D")

elif 60>=p>=40:

print("Grade E")

else:

print("Grade F")

A team from royal squatraclub


a1=int(input())

w1=int(input())

if a1>=18 and w1>=50:

print("Eligible for Donation")

else:

print("Not Eligible for Donation")

Arav and Aaron are participating

a1=int(input())

a2=int(input())

if a1>a2:

print(a1-a2)

else:

print(a2-a1)

A paytm announced a cash back

n=int(input())

a=n%10

if a%2==0:

print("Even Currency")

else:

print("Odd Currency")

Three brothers want to take a photo

b1=int(input())

b2=int(input())

b3=int(input())

if b1>b2 and b1>b3:

print(b1)

elif b2>b1 and b2>b3:

print(b2)
else:

print(b3)

Selvan is working as a QC

a=input()

if a.isdigit():

print("NOT AN ALPHABET")

else:

print("ALPHABET")

Laasya looking at the friends birthday

a=int(input())

if a%4==0:

print("LEAP YEAR")

else:

print("NOT A LEAP YEAR")

The election commission of India

age = int(input())

if age >= 18:

print("Eligible")

else:

print("Not Eligible")

Caleb and Irfan are purchasing

apple1 = int(input())

apple2 = int(input())

apple3 = int(input())

if apple2 > apple1 and apple3 > apple2:

print("Fit into Budget")

else:
print("Doesn't fit into Budget")

Atifa would like to withdraw

wamt=int(input())

inamt=float(input())

if wamt%5==0:

if wamt>inamt:

print("Invalid Withdrawal Request")

print("Initial Balance :",format(inamt,".2f"))

else:

x=(inamt-wamt)-0.5

print("Current Balance :",format(x,".2f"))

print("Initial Balance :",format(inamt,".2f"))

Yasir techie is working in a military

num = int(input())

if(num>0):

print("POSITIVE")

else:

print("NEGATIVE")

Simon was working in a casa grand

Arunmozhivarman a famous skill trainer

operator=input()

n1=float(input())

n2=float(input())

sum=n1+n2

difference=n1-n2

product=n1*n2

divison=n1/n2
if operator=='+':

print(sum)

elif operator=='-':

print(differnce)

elif operator=='*':

print(product)

elif operator=='/':

print(divison)

else:

print("Invalid Input")
Arrays and Loops – Session 3
Akash the die heart

s=int(input())

for i in range(s,0,-1):

for j in range(i):

print(i,end=" ")

print()

Euler asked the question to ramanujam

def sum_of_multiples(N):

total = 0

for i in range(N):

if i % 3 == 0 or i % 5 == 0:

total += i

return total

N = int(input()) # get input from user

print(sum_of_multiples(N)) # calculate and print the sum of multiples

Matrix is a rectangular arrangement

r=int(input())

c=int(input())

matrix=[]

for i in range(r):

a =[]

for j in range(c):

a.append(int(input()))

matrix.append(a)

for i in range(r):

for j in range(c):

print(matrix[i][j], end = " ")


print()

In our institutions celebrates

a=int(input())

c=[]

for i in range(a):

c.append(a)

for j in range(i + 1):

if j == i:

print(chr(97 + j), end=" ")

else:

print(chr(97 + j), end=" ")

print()

2520 is the smallest number

import math

# Returns the lcm of first n numbers

def lcm(n):

ans = 1

for i in range(1, n + 1):

ans = int((ans * i)/math.gcd(ans, i))

return ans

if 1>2:

a=5

# main

n = int(input())

print (lcm(n))

Pentagonal numbers are generated


length = int(input())

for n in range(1,length):

p = round(n*((3*n)-1)/2)

print(p)

Mukesh and salima was looking

a=int(input())

b=[]

for i in range(a):

c=int(input())

b.append(c)

x=[]

y=[]

for i in b:

if i not in x:

x.append(i)

elif i in x:

y.append(i)

else:

break

print(len(y))

Thanos and iron man had a challenge

a=int(input())

b=[]

for i in range(a):

c=int(input())

b.append(c)

e=[]

o=[]

for i in b:
if i%2==0:

e.append(i)

else:

o.append(i)

for i in e:

print(i,end=' ')

print()

for i in o:

print(i,end=' ')

Matrix is a rectangular arrangement

r=int(input())

c=int(input())

matrix=[]

for i in range(r):

a =[]

for j in range(c):

a.append(int(input()))

matrix.append(a)

for i in range(len(matrix)):

k=0

for j in matrix:

print(matrix[k][i], end = " ")

k+=1

print()

In the battle of kurukshestra

n=int(input())
m=1

for i in range(n):

h='* '

print(h*m)

m+=1

n = int(input()) # get input from user

# iterate over each row

for i in range(n):

# print the appropriate number of stars for this row

for j in range(i+1):

print("* ", end="")

print() # move to the next line

Marvel Studios is known

n = int(input())

# Read the character names

names = []

for i in range(n):

names.append(input())

# Sort the names in alphabetical order

names.sort()

# Display the sorted names

for name in names:

print(name)

Mathematics teacher gave the home

r=int(input())

c=int(input())

matrix=[]
for i in range(r*2):

a =[]

for j in range(c):

a.append(int(input()))

matrix.append(a)

matrix1=matrix[:r]

matrix2=matrix[r:]

for i in range(r):

for j in range(c):

print(matrix1[i][j]+matrix2[i][j],end=' ')

print()
Strings – Session 4
Arif likes to play valley ball

def determine_winner(match):

arif_score = 0

opponent_score = 0

score_set = set() # use a set to keep track of the scores

for i in range(len(match)):

if match[i] == '1':

arif_score += 1

else:

opponent_score += 1

if (arif_score >= 11 or opponent_score >= 11) and abs(arif_score - opponent_score) >= 2:

if arif_score > opponent_score:

return "WIN"

else:

return "LOSS"

score_set.add((arif_score, opponent_score)) # add the current score to the set

# check if the match ended with a score less than 11-10

last_score = list(score_set)[-1]

if last_score[0] >= 11 or last_score[1] >= 11:

if last_score[0] > last_score[1]:

return "WIN"

else:

return "LOSS"

else:

return "LOSS"

# main function

if __name__ == "__main__":

t=int(input()) # number of test cases


for i in range(t):

matchscenario=str(input()) # input string describing match

result = determine_winner(matchscenario)

print(result) # print the winner of the match

Janaki set the password

s=str(input())

# Printing all substrings of the string s

for i in range(len(s)):

for j in range(i+1, len(s)+1):

print(s[i:j])

Lokesh usually likes to play

def can_be_identical(n, s1, s2):

s1_count = s1.count('1')

s2_count = s2.count('1')

if s1_count != s2_count:

return "NO"

else:

return "YES"

t = int(input())

for i in range(t):

n=int(input())

s1=str(input())

s2=str(input())

print(can_be_identical(n, s1, s2))

Janaki has the extra ability

t=int(input())

# Loop through each test case


for i in range(t):

# Get the string

test_string=str(input())

# Reverse the string

string_reversed = test_string[::-1]

# Print the reversed string

print(string_reversed)

A long time age, there resided

t = int(input())

# Looping through each test case

for i in range(t):

# Reading the input string

str1=str(input())

# Initializing the vowel count

count = 0

# Looping through each character in the string

for j in range(len(str1)):

# Checking if the character is a vowel

if str1[j] in "aeiouAEIOU":

count += 1

# Printing the vowel count for the current string

print(count)

Sudan and siva are classmates

# Taking input for the number of test cases

t = int(input())

# Looping through each test case

for i in range(t):
# Taking input for the string

str1=str(input())

# Sorting the characters in the string using the sorted() function and the key parameter

sorted_string = sorted(str1, key=lambda x: x.isupper())

# Joining the sorted characters back to form the final string

final_string = ''.join(sorted_string)

# Printing the final string

print(final_string)

Aaron has a number D containing

def check_all_same(s):

# Count number of 0s and 1s

count_0 = s.count('0')

count_1 = s.count('1')

# If all digits are already same, return Yes

if count_0 == len(s) or count_1 == len(s):

return "YES"

# If there is only one 0 or one 1, return Yes

if count_0 == 1 or count_1 == 1:

return "YES"

# If there is more than one 0 and more than one 1, return No

if count_0 > 1 and count_1 > 1:

return "NO"

# If there is exactly one 0 and more than one 1, or exactly one 1 and more than one 0, return Yes

else:

return "YES"

if __name__ == "__main__":
# Get number of test cases

T = int(input())

# Loop over all test cases

for i in range(T):

# Get input string

s=str(input())

# Call function to check if all digits can be made same by flipping one digit

result = check_all_same(s)

# Print the result

print(result)

You might also like