In this post we will be looking into a few simple python exercises or challenges that will be very useful in strengthening your concepts.
Find the number of upper case and lower case letters in a string.
For this challenge, we will loop through a string and count the number of upper and lower case letters in it. Then when all the characters have been checked, we will output the numbers.
We will use the string 'The Dev Community is Amazing'.
Text = "The Dev Community is Amazing" #Variables to store the number of upper and lower case UpperCase = 0 LowerCase = 0 #loop through the string for i in Text: #check for upper case if i >= "A" and i <= "Z": UpperCase += 1 #check for lowercase elif i >= "a" and i <= "z": LowerCase += 1 #print the final number. print("UpperCase letters:",UpperCase,"and LowerCase letters:", LowerCase)
The output for this string will be:
UpperCase letters: 4 and LowerCase letters: 20
Write the multiplication table of a number to 5
In this one, we will write the multiplication table of any number input by user.
#user input Number = int(input("Enter a number: ")) #loop from 1 to 5 and write the multiplication for each for i in range(1,6): print(Number, "*",i,"=", Number*i)
Enter a number: 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
Find the number of even numbers in a given range
For this one, we will take a range of numbers from the user and give the number of even numbers in that range.
A number is even if its remainder is 0 when it is divided by 2.
#user inputs the range Number1 = int(input("Enter starting number: ")) Number2 = int(input("Enter last number: ")) #Store number of even numbers Even = 0 #check each number in range for i in range(Number1,Number2+1): #check if number is even if i % 2 == 0: Even += 1 print("Number of even numbers are:",Even)
Enter starting number: 1 Enter last number: 4 Number of even numbers are: 2
These were the 3 simple python exercises every beginner should try to strengthen their concepts and get some useful practice
Like my work? Buy me a coffee!
Until next time,