3 Simple Python Exercises

Hi guys!

In this post we will be looking into a few simple python exercises or challenges that will be very useful in strengthening your concepts.

Exercise – 1

Find the number of upper case and lower case letters in a string.
For this c…


This content originally appeared on DEV Community and was authored by Saleh Mubashar

Hi guys!

In this post we will be looking into a few simple python exercises or challenges that will be very useful in strengthening your concepts.

Exercise - 1

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

Exercise - 2

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)

Output:

Enter a number: 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25

Exercise - 3

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)

Output:

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

I hope you all found them useful.
Check out my other tutorials on hubpages.
Also follow me on twitter.

Like my work? Buy me a coffee!

Buy Me A Coffee

Until next time,
Cheers :)


This content originally appeared on DEV Community and was authored by Saleh Mubashar


Print Share Comment Cite Upload Translate Updates
APA

Saleh Mubashar | Sciencx (2021-11-14T16:59:30+00:00) 3 Simple Python Exercises. Retrieved from https://www.scien.cx/2021/11/14/3-simple-python-exercises/

MLA
" » 3 Simple Python Exercises." Saleh Mubashar | Sciencx - Sunday November 14, 2021, https://www.scien.cx/2021/11/14/3-simple-python-exercises/
HARVARD
Saleh Mubashar | Sciencx Sunday November 14, 2021 » 3 Simple Python Exercises., viewed ,<https://www.scien.cx/2021/11/14/3-simple-python-exercises/>
VANCOUVER
Saleh Mubashar | Sciencx - » 3 Simple Python Exercises. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/14/3-simple-python-exercises/
CHICAGO
" » 3 Simple Python Exercises." Saleh Mubashar | Sciencx - Accessed . https://www.scien.cx/2021/11/14/3-simple-python-exercises/
IEEE
" » 3 Simple Python Exercises." Saleh Mubashar | Sciencx [Online]. Available: https://www.scien.cx/2021/11/14/3-simple-python-exercises/. [Accessed: ]
rf:citation
» 3 Simple Python Exercises | Saleh Mubashar | Sciencx | https://www.scien.cx/2021/11/14/3-simple-python-exercises/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.