Find Max


4. Write a Python program to get the smallest number from a list.

# max_num_in_list([1, 2, -8, 0])

#Solution:

def max_num_in_list( list ):
    max = list[0]
    for a in list:
        if a>max:
            max = a
    return max
print(max_num_in_list([1, 2, -8, 0]))



Diff between 2


3. Write a python program to get the difference between the two list
          # Input
          # list1 = [1, 2, 3, 4]
         # list2 = [1, 2]

        #Solution:

list1 = [ 1, 2, 3, 4]
list2 = [1, 2]
print(list(set(list1) - set(list2)))





Common Items


2. Write a python program to find common items from two lists.
# input
# color1 = "Red", "Green", "Orange", "White"
# color2 = "Black", "Green", "White", "Pink"


#Solution

color1 = "Red", "Green", "Orange", "White"
color2 = "Black", "Green", "White", "Pink"
print(set(color1) & set(color2))





Characters to string

01. Convert a list of character into a string

#Solution:

s = ['a', 'b', 'c', 'd']
str1 = ''.join(s)
print(str1)



vowel or consonal


12. Write a python program to check whether an alphabet is a vowel or consonant

#Solution:

l = input("Input a letter of the alphabet:  ")
if l  in ('a','e','i','o','u'):
    print("%s is a vowel. " % l)
elif l == 'y':
    print("Sometimes letter y stand for vowel, sometimes stand for consonangt.")
else:
    print("%s is a consonant." %l)






Triangle types


11. Write a Python program to check a triangle is equilateral, isosceles or scalene
# Note :
# An equilateral triangle is a triangle in which all three sides are equal.
# A scalene triangle is a triangle that has three unequal sides.
# An isosceles triangle is a triangle with (at least) two equal sides.


Solution:
print("Input lengths of the triangle sides: ")
x = int(input("x: "))
y = int(input("y: "))
z = int(input("z: "))

if x == y == z:
    print("Equilateral triangle")
elif x != y != z:
    print("Scalene triangle")
else:
    print("isoseles triangle")




Guess Game


10. Generate random numbers between 1 and 9(including 1 and 9).
#Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right.


#Solutions:
import random
number = random.randint(1,9)
guess = 0count = 0while guess != number and guess != "exit":
    guess = input("What' your guess?")

    if guess == "exit":
       break
    guess = int(guess)
    count += 1
    if guess < number:
        print("Too low!")
    elif guess > number:
        print("Too high!")
    else:
        print("You got it!")
        print("And it only took you",count,"tries")


Related Posts Plugin for WordPress, Blogger...