Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
solutions for exercise on site https://www.practicepython.org/
import datetime
now = datetime.datetime.now()
print("Enter your name and age")
name = input('Name:')
age = int(input('Age :'))
remainingYears = 100 - age
print("Enter the no. of times you want to see the output")
messageCount = int(input("-->"))
message = "In "+str(remainingYears + now.year)+" you'll be 100 year old"
print(messageCount * (message+"\n"))
print("Enter a number to check if it is odd or even")
num = int(input('-->'))
if num%4==0:
print(str(num)+' modulo 4 is 0.')
elif num%2==0:
print(str(num)+' is an even number' )
else:
print(str(num)+' is an odd number' )
print("Enter two number to check if second is evenly divide into first")
num1 = int(input('num1:'))
num2 = int(input('num2:'))
if num1 % num2==0:
print(str(num1)+ ' is evenly divisble by '+str(num2))
else:
print(str(num1)+ ' is not evenly divisble by '+str(num2))
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [element for element in a if element<5]
print(b)
print('Enter the number')
maxNumber = int(input("-->"))
userList = [element for element in a if element < maxNumber]
print(userList)
print("Enter any number")
num = int(input('num:'))
count =0
divisibles = []
for i in range(1,int(num/2)+1):
if num%i==0 : divisibles.append(i)
print(divisibles)
import random
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = list(set(a) & set(b))
print('Common elements: '+str(c))
a = random.sample(range(0,40),10)
b = random.sample(range(0,40),10)
c = list(set(a) & set(b))
print("list a:"+str(a))
print("list b:"+str(b))
if len(c) > 0:
print('Common elements: '+str(c))
else:
print('No common elements in random list')
print("Enter a string to check if it's palindrome or not")
givenString = input("-->").lower()
if givenString == givenString[::-1]:
print("String is palindrome")
else:
print("String is not palindrome")
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
evens = [num for num in a if num%2==0]
print(evens)
print("Let's play Rock Paper Scissors")
choices = ["Rock", "Paper", "Scissors"]
play = True
p1Wins = "Player 1 wins"
while(play):
p1Answer = input("P1 Choose between Rock Paper Scissors")
p2Answer = input("P2 Choose between Rock Paper Scissors")
if p1Answer in choices and p2Answer in choices:
if p1Answer == "Rock" and p2Answer == "Scissors" : print(p1Wins)
elif p1Answer == "Scissors" and p2Answer == "Paper" : print(p1Wins)
elif p1Answer == "Paper" and p2Answer == "Rock" : print(p1Wins)
elif p1Answer == p2Answer : print("Draws")
else : print("Player 2 Wins")
print("Want a rematch, yes or no?")
answer = input("").lower()
if answer == "yes" : play = True
else : play = False
else:
if p1Answer in choices: print("P2 available choices are : Rock Paper Scissors")
else: print("P1 available choices are : Rock Paper Scissors")
import random
play = True
while play:
randomNum = random.randint(1,10)
guessNum = input("Guess a number between 1-10\n->")
if str(randomNum) == guessNum:
print("Your guess is right. It is " + guessNum)
elif guessNum == 'exit':
print("Game over")
play=False
else:
print("Sorry the number is "+str(randomNum)+".Try again")
import random
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
commonSet = [num for num in set(a) if num in b]
if len(commonSet) > 0:
print("Common Set: "+ str(commonSet))
else :
print("Nothing is commont")
# Randomly generated List
a = random.sample(range(0,90),15)
b = random.sample(range(0,80),10)
commonSet = [num for num in set(b) if num in a]
print("Random List a: "+str(a))
print("Random List b: "+str(b))
if len(commonSet) > 0:
print("Common Set in random generated list: "+ str(commonSet))
else :
print("Nothing is commont")
def isPrime(num) :
if num == 1 :return False
divisibles = [i for i in range(1,int(num/2)+1) if num%i==0]
return len(divisibles) == 1
num = int(input("Enter the number\n->"))
if isPrime(num):
print(str(num) + " is a prime number")
else:
print(str(num) + " is not a prime number")
def endpoints(numList):
b = [numList[0],numList[len(numList)-1]]
return b
a = [5, 10, 15, 20, 25]
print(endpoints(a))
def getFibonacci(length) :
series = [1,1]
head = 1
tail = 1
while length > 2 :
series.append(head + tail)
pointer = head
head += tail
tail = pointer
length -= 1
return series
num = input("How many numbers you want to generate?\n")
print(getFibonacci(int(num)))
import random
def removeDuplicateUsingSet(inputList):
return (list(set(inputList)))
def removeDuplicateUsingLoop(list):
removeDuplicatesList = []
for a in list:
if not a in removeDuplicatesList:
removeDuplicatesList.append(a)
return removeDuplicatesList
a = [0, 12, 11, 17, 10, 2, 16, 6, 8, 11, 17]
print(removeDuplicateUsingSet(a))
print(removeDuplicateUsingLoop(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment