Python Tutorial 2
Python Tutorial 2
values, and Returns a list Containing Values that are present in Both lists(I,e., the
intersection of the two input lists).
Syntax:
def intersect(listA, listB):
listed = []
for list1 in listA:
for list2 in listB:
if list1 == list2:
listed.append(list1)
return listed
listA=input("Enter list A:").split(",")
listB=input("Enter list B:").split(",")
print(intersect(listA, listB))
Output:
Implement the function pair() That Takes as input two lists of Integers and one
integer n and prints the pair of integers, one from the first input list and the other
from the second input list, that add up to n. Each pair should be printed.
Syntax:
pair(listA,listB,n)
Output:
Implement the function pairSum() that takes as input a list of distinct integers 1st
and an integer n, and prints the indexes of all pairs of values in 1st that sum up to n.
Syntax:
list=[int(i) for i in input("enter the list of distinct
integer:").split(",")]
n=int(input("Enter integer:"))
def pairSum(list, n):
for x in range(0, len(list)):
for y in range( x + 1, len(list)):
if list[x] + list[y] == n :
print(x,y)
pairSum(list,n)
Output:
Write function pay() that takes as input an hourly wage and the number of hours an
employee worked in the last week. The Function should compute and return the
employee’s pay. Overtime work should be paid in this way: Any hours beyond 40
but less than or equal 60 should be paid at 1.5 times the regular hourly wage. Any
hours beyond 60 should be paid at 2 times the regular hourly wage.
>>> pay (10, 35) 350
>>>pay (10, 45) 475
>>> pay (10,61) 720
Syntax:
hourly_wage=int(input("Enter hourly wage:"))
hour=int(input("Enter number of hours:"))
def pay(hourly_wage,hour):
if hour <= 40:
return hourly_wage * hour
elif 40 < hour <= 60:
return 1.5 * (hour - 40) * hourly_wage + 40 *hourly_wage
else:
return 2 * (hour - 60) * hourly_wage + 20 *1.5 *
hourly_wage + 40 * hourly_wage
print(pay(hourly_wage, hour))
Output:
(a). (b).
(c).
Write function case() that takes a string as input and returns ‘capitalized’, ‘not
capitalized’, or ‘uknown’, depending on whether the string starts with an uppercase
letter, lowercase letter, or something other than a letter in the English alphabet,
respectively.
Syntax:
word=input("enter word:")
def case(word):
if word[0] >= "A" and word [0] <= "Z":
return "capitalized"
elif word[0] >= "a" and word [0] <= "z":
return "not capitalized"
else:
return "unknown"
print(case(word))
Output:
(a). (b).
Implement function leap() that takes on input argument--- a year --- and returns
True if the year is a leap year and False otherwise. (A year is a leap year if it is
divisible by 4 but no by 100, unless it is divisible by 400 in which case it is a leap
year. For example, 1700, 1800 and 1900 are not leap years but 1600 and 2000 are.)
>>> leap(2008)
True
>>> leap(1900)
False
>>>leap (2000)
True
Syntax:
year=eval(input("enter year:"))
def leap(year):
if (year % 4 == 0) & ((year % 100 != 0) | (year %
400 == 0)):
return True
return False
print(leap(year))
Output:
(a). (b).
(c).
Rock, Paper, Scissors is a two- player game in which each player chooses one of
three item s. If both player choose the same item, the game is tied. Otherwise, the
rules that determine the winner are:
Implement function rps() that takes the choice (‘R’, ‘P’, or ‘S’) of player 1 and the
choice of player 2, and returns -1 if player 1 wins, 1 if player 2 wins, or 0 if there is
a tie.
Syntax:
player1=input("player 1:")
player2=input("player 2:")
def rps(player1,player2):
if (player1 == "S" and player2 == "P") | (player1 == "R" and
player2 == "S") | (player1 == "P" and player2 == "R"):
return "1"
elif player1 == player2:
return "0"
else:
return "-1"
print(rps(player1,player2))
Output: