1. Home
  2. Docs
  3. Learn Python By Examples
  4. Learn Python by examples – Ex-31-35

Learn Python by examples – Ex-31-35

Learn Python by examples – Ex-31-35

Note there are multiple ways to solve a problem.

Here is one solution.

Exercise 31– Write a program to take a comma separated words from the user, print alphabetically sorted words.

Some, Like, It, Hot, => Hot, It, Like, Some

 

Solution:
Concept:

Split the words by comma, and then sorting the items.

# split(), sort(), sorted()

def arrange_words(input_str):
    
    words = input_str.split(",")
    print(words)
    #words.sort()
    
    print(sorted(words))
    print(words)
    
str1 = input("Enter the string :>")

arrange_words(str1)

Output:

Enter the string :>Some, Like, It, Hot,
#Using list method sort
['Some', ' Like', ' It', ' Hot', '']

#Using built-in sorted function
['', ' Hot', ' It', ' Like', 'Some']
#original list
['Some', ' Like', ' It', ' Hot', '']


 

Exercise 32– Write a program to take numbers from the user, until the user says done

Concept:

Usage of infinite loop, break when user string matches ‘done’

Solution:

#continuous loop

numbers = []

while True:
    num = input('Enter a number, type "done" to quit :>')
    if not num.lower() == 'done':
        numbers.append(int(num))
    else:
        break
    
print("You have entered:", numbers)

Output:

Enter a number, type "done" to quit :>10
Enter a number, type "done" to quit :>20
Enter a number, type "done" to quit :>30
Enter a number, type "done" to quit :>40
Enter a number, type "done" to quit :>50
Enter a number, type "done" to quit :>done
You have entered : [10, 20, 30, 40, 50]




Exercise 33– Write a program accept a sequence of lines from user, and print lines with title cased.

the shawshank redemption
the godfather
the dark knight
the Godfather
pulp fiction
12 angry men …..

Solution:

Concept:

Use title() method of string on every input.

 

##continuous loop

user_lines = []

while True:
    line = input('Enter a line :>')
    if line:
        #line.title()
        user_lines.append(line)
    else:
        break
    
print("You have entered :",user_lines)

Output:

Enter a line :>the shawshank redemption
Enter a line :>the godfather
Enter a line :>the dark knight
Enter a line :>the Godfather
Enter a line :>pulp fiction
Enter a line :>12 angry men
Enter a line :>
You have entered : ['The Shawshank Redemption', 'The Godfather', 'The Dark Knight', 'The Godfather', 'Pulp Fiction', '12 Angry Men']


 

Exercise 34– Write a program to print numbers between 2000, 2100, who’s digits are even numbers

Ex: [86,86,45,85,5,83,23,45] => should get 86, 5

Solution:

Concept:

Using modulus operation check each digit for even or odd.


def even(digit):
    return int(digit) % 2  == 0

for num in range(2000, 2050):
    s =str(num)
    
    if even(s[0]) and even(s[1]) and even(s[2]) and even(s[3]):
        print(num)
   

Output:

2000
2002
2004
2006
2008
2020
2022
2024
2026
2028
2040
2042
2044
2046
2048

 

Exercise 35– Write a program accept a sequence of lines or numbers, and print them separately

bend
it
20
like
30
beckham
40

Solution:

 

Concept:
Using string method, isdigit() seperate numbers and strings.

 

words = []
numbers = []

while True:
    user_input = input("Enter some thing :>")
    if user_input:
        if user_input.isdigit():
            numbers.append(user_input)
        else:
            words.append(user_input)
    else:
        break
            
print("Entered words :", words)

print("Entered numbers :", numbers)
    
Output:

Enter some thing :>bend
Enter some thing :>it
Enter some thing :>20
Enter some thing :>like
Enter some thing :>30
Enter some thing :>Beckham
Enter some thing :>40
Enter some thing :>
Entered words : ['bend', 'it', 'like', 'Beckham']
Entered numbers : ['20', '30', '40']