Before proceeding to Python programming exercises, let’s know some important details about Python. You might have wondered why Python is slower than any other programming language. The answer to this is quite simple: Python is one of the interpreted programming languages, whereas languages like C are compiled languages.
It has been observed that interpreted codes are always much slower than direct machine codes as it executes several instructions to implement an interpreted instruction. Therefore, Python instructions need to be understood by the CPU before the working of the actual program. Python interpreter always analyzes every statement with the Python programming rules like straining out comments and spaces from the given program, allocating the memory to store the variables, and much more. In this process, each program line repeats and adds significantly to execute the program furthermore.
Reasons: Why programmers select Python after even being slower speed?
Readable and Concise Application Code
With Python, programmers can easily express concepts with readable and concise codes. Moreover, the Python syntax rules’ simplicity makes it possible to learn and use it for beginners without effort and time.
High-level Programming Language
Being a high-level programming language, Python allows developers to write application programs flexibly. To develop software in the Python language, the developers require to focus on the actions only that are being performed.
Multiple Popular Programming Paradigms
Python supports various programming paradigms, like functional, procedural, object-oriented, and imperative. Moreover, Python is a general-purpose programming language that features an automatic memory management system and a dynamic type system.
A robust Library
Python offers a comprehensive and robust stand library as compared to the other programming languages. Developers can use any of the libraries to make their work easier without writing lengthy codes.
Create Complex Applications
Being a general-purpose language, Python supports multiple platforms, such as Mac OS X, UNIX, Windows, and Linux. Therefore, it becomes much easier for the developers to execute GUI applications on different platforms.
Apart from this, there are several reasons why developers select Python, even being a slower programming language. Therefore, it is always beneficial for beginners to learn this programming language. So, let’s check some of the Python programming exercises that help you to learn more about Python programs. But before that, let’s discuss some examples of Python programming (basic).
Some of the basics examples of Python programming
Example 1: Write a program to calculate the circumference of the circle.
Solution: The formula for the circumference of the circle is: (2* pi* r) or (pi*d). Take the value of pi = 3.14159.
The Python code to calculate the circumference of the circle is:
from math import pi
r = float(input (“Put the value of the radius of the circle : “))
print (“The circumference of the circle with a radius ” + str(r) + ” is: ” + str(2*pi*r))
Put the value of the radius of the circle : 2
The circumference of the circle with a radius 2.0 is: 12.566370614359172
Example 2: Write a program to represent the second and second last colors from the given list.
color_list = [“Green“,”Red“,”Yellow” ,”Blue“]
The Python code to get the desired color is:
color_list = [“Green”,”Red”,”Yellow” ,”Blue”]
print( “%s %s”%(color_list,color_list[-2]))
List of Python programming exercises
Execute the basic Python programming exercises to get desired output
Set 1: Write a program to present the current date & time.
Set 2: Write a program that accepts an integer’s value (n) to compute the value of n+nn+nnn.
Set 3: Write a program to show the exam schedule. (get the date from exam_st_date).
exam_st_date = (12, 11, 2021)
Set 4: Write a program to count the number of days between the two different dates.
Set 5: Write a program to calculate the volume of a sphere with a radius of 5.
Formula: The volume of a sphere is: V = 4/3 × pi × r^3 = pi × d^3/6.
Set 6: Write a program to get the sum of two numbers; if the given numbers are equal, give their sum twice.
Set 7: Write a program to generate a histogram from the provided list of integers.
now = datetime.datetime.now()
print (“Current date & time : “)
print (now.strftime(“%Y-%m-%d %H:%M:%S”))
x = int(input(“Put the value of an integer : “))
n1 = int( “%s” % x )
n2 = int( “%s%s” % (x,x) )
n3 = int( “%s%s%s” % (x,x,x) )
exam_st_date = (12,11,2021)
print( “The exam will be started from : %i / %i / %i”%exam_st_date)
from datetime import date
f_date = date(2021, 2, 3)
l_date = date(2021, 2, 8)
delta = l_date – f_date
pi = 3.1415926535897931
V= 4.0/3.0*π* r**3
print(‘The Sphere’s volume is: ‘,V)
def sum_twice(x, y):
sum = x + y
if x == y :
sum = sum * 2 return sum
def histogram( items ):
for n in items:
output = ”
times = n
while( times > 0 ):
output += ‘x’
times = times – 1
histogram([1, 2, 2, 1])
Python programming exercises: Multiple Choice Questions (MCQs)
Answer the output of the given python programs
Set 1: The output of the given code will be:
Set 2: The output of the given code will be:
L = [‘p’,’q’,’r’,’s’]
Set 3: What is the data type of the below code?
L = [2, 55, ‘hi’, 5]
Set 4: Check which number is not complex no.?
(A) k = 2 + 3j
(B) k = complex(2, 3)
(C) k = 2 + 3l
(D) k = 2 + 3J
Set 5: The output of the given code will be:
x = x + 3
x = x * 3
(C) Runtime Error
(D) Indentation Error
|Set 1||(A)||The operator ‘//’ in Python will return the floating number’s integer part.|
|Set 2||(B)||“” represents a null string & it joins all the list’s element function into a string.|
|Set 3||(A)||[ ] represents a list.|
|Set 4||(C)||l (or L) refers to long.|
|Set 5||(D)||Python generates a particular code block that depends on the code’s indentation. Thus, a new indent specifies the new scopes.|
Python is one of the slow programming languages, even though several developers and programmers use it. Python programming language supports multiple programming paradigms, like imperative, object-oriented, and procedural styles or functional programming. It has features like automatic memory allocation, a dynamic type system, and has a comprehensive and large library. Therefore, each programmer must learn this language to do their work with ease. The best method to learn Python programming is “PRACTICE.” Therefore, practice all above mentioned Python programming exercises and improve your coding skills. Moreover, you can ask us for more practice exercises, and we will provide you the number of exercises immediately. So keep programming and polish your coding skills.