Python Programming Exercises to Try for Beginners

python-programming-exercises

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. 

See also  Python or JavaScript: Which To Choose And Why To Choose?

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))

Output:

Put the value of the radius of the circle :  2

See also  51+ Innovative Image Processing Projects Using Python In 2024

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“]

GreenRedYellowBlue

The Python code to get the desired color is:

color_list = [“Green”,”Red”,”Yellow” ,”Blue”]

print( “%s %s”%(color_list[1],color_list[-2]))

Output:

Red Yellow

List of Python programming exercises

Execute the basic Python programming exercises to get desired output

Questions:

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.

Solutions:

Set 1: 

import datetime 

now = datetime.datetime.now() 

print (“Current date & time : “) 

print (now.strftime(“%Y-%m-%d %H:%M:%S”))

Set 2: 

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) ) 

print (n1+n2+n3)

Set 3: 

exam_st_date = (12,11,2021)

print( “The exam will be started from : %i / %i / %i”%exam_st_date)

Set 4: 

from datetime import date 

See also  What is Linked List in Python and How Does it Work?

f_date = date(2021, 2, 3) 

l_date = date(2021, 2, 8) 

delta = l_date – f_date 

print(delta.days)

Set 5: 

pi = 3.1415926535897931 

r= 5.0 

V= 4.0/3.0*π* r**3 

print(‘The Sphere’s volume is: ‘,V)

Set 6: 

def sum_twice(x, y): 

sum = x + y  

if x == y : 

sum = sum * 2 return sum 

print(sum_twice(1, 2)) 

print(sum_twice(2, 2))

Set 7: 

def histogram( items ): 

for n in items: 

output = ” 

times = n 

while( times > 0 ): 

output += ‘x’ 

times = times – 1 

print(output) 

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:

print 8//3

A) 2

B) 2.6

C) 5

D) 4

Set 2: The output of the given code will be:

L = [‘p’,’q’,’r’,’s’]

print(“”.join(L))

A) ERROR

B) pqrs

C) NONE

D) ‘p’,’q’,’r’,’s’

Set 3:  What is the data type of the below code? 

L = [2, 55, ‘hi’, 5]

(A) List

(B) Dictionary

(C) Tuple

(D) Array

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:

def myfunc(x):

    x = x + 3

        x = x * 3

    return x

print myfunc(3)

(A) 18

(B) 8

(C) Runtime Error 

(D) Indentation Error

Solutions: 

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.

Conclusion

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. and also get the best do my python programming assignment and python programming assignment from our experts.