Python Basic Exercise for Beginners (2023)

This Python essential exercise is to help Python beginners to learn necessary Python skills quickly. Practice Python basic concepts such as loops, control flow, data types, operators, list, strings, input-output, and built-in functions.

Also, See:

  • Python Quizzes
  • Python Basics

What questions are included in this Python fundamental exercise?

  • The exercise contains 15 programs to solve. The hint and solution is provided for each question.
  • I have added tips and required learning resources for each question, which helps you solve the exercise. When you complete each question, you get more familiar with the basics of Python.

Use Online Code Editor to solve exercise questions.

Also, try to solve the basic Python Quiz for beginners

This Python exercise covers questions on the following topics:

  • Python for loop and while loop
  • Python list, set, tuple, dictionary, input, and output

Exercise 1: Calculate the multiplication and sum of two numbers

Given two integer numbers return their product only if the product is equal to or lower than 1000, else return their sum.

Given 1:

number1 = 20number2 = 30

Expected Output:

The result is 600

Given 2:

number1 = 40number2 = 30

Expected Output:

The result is 70


  • Accept user input in Python
  • Calculate an Average in Python
Show Hint
  • Create a function that will take two numbers as parameters
  • Next, Inside a function, multiply two numbers and save their product in a product variable
  • Next, use the if condition to check if the product >1000. If yes, return the product
  • Otherwise, use the else block to calculate the sum of two numbers and return it.
Show Solution
def multiplication_or_sum(num1, num2): # calculate product of two number product = num1 * num2 # check if product is less then 1000 if product <= 1000: return product else: # product is greater than 1000 calculate sum return num1 + num2# first conditionresult = multiplication_or_sum(20, 30)print("The result is", result)# Second conditionresult = multiplication_or_sum(40, 30)print("The result is", result)
(Video) Python Exercises for Beginners - Exercise #1

Exercise 2: Print the sum of the current number and the previous number

Write a program to iterate the first 10 numbers and in each iteration, print the sum of the current and previous number.

Expected Output:

Printing current and previous number sum in a range(10)Current Number 0 Previous Number 0 Sum: 0Current Number 1 Previous Number 0 Sum: 1Current Number 2 Previous Number 1 Sum: 3Current Number 3 Previous Number 2 Sum: 5Current Number 4 Previous Number 3 Sum: 7Current Number 5 Previous Number 4 Sum: 9Current Number 6 Previous Number 5 Sum: 11Current Number 7 Previous Number 6 Sum: 13Current Number 8 Previous Number 7 Sum: 15Current Number 9 Previous Number 8 Sum: 17

Referencearticle for help:

  • Python range() function
  • Calculate sum and average in Python
Show Hint
  • Create a variable called previous_num and assign it to 0
  • Iterate the first 10 numbers one by one using for loop and range() function
  • Next, display the current number (i), previous number, and the addition of both numbers in each iteration of the loop. At last, change the value previous number to current number ( previous_num = i).
Show Solution
print("Printing current and previous number and their sum in a range(10)")previous_num = 0# loop from 1 to 10for i in range(1, 11): x_sum = previous_num + i print("Current Number", i, "Previous Number ", previous_num, " Sum: ", x_sum) # modify previous number # set it to the current number previous_num = i

Exercise 3: Print characters from a string that are present at an even index number

Write a program to accept a string from the user and display characters that are present at an even index number.

For example, str = "pynative" so you should display ‘p’, ‘n’, ‘t’, ‘v’.

Expected Output:

Orginal String is pynativePrinting only even index charspntv

Referencearticle for help: Python Input and Output

Show Hint
  • Use Python input() function to accept a string from a user.
  • Calculate the length of string using the len() function
  • Next, iterate each character of a string using for loop and range() function.
  • Use start = 0, stop = len(s)-1, and step =2. the step is 2 because we want only even index numbers
  • in each iteration of a loop, use s[i] to print character present at current even index number
Show Solution

Solution 1:

# accept input string from a userword = input('Enter word ')print("Original String:", word)# get the length of a stringsize = len(word)# iterate a each character of a string# start: 0 to start with first character# stop: size-1 because index starts with 0# step: 2 to get the characters present at even index like 0, 2, 4print("Printing only even index chars")for i in range(0, size - 1, 2): print("index[", i, "]", word[i])

Solution 2: Using list slicing

# accept input string from a userword = input('Enter word ')print("Original String:", word)# using list slicing# convert string to list# pick only even index charsx = list(word)for i in x[0::2]: print(i)

Exercise 4: Remove first n characters from a string

Write a program to remove characters from a string starting from zero up to n and return a new string.

For example:

  • remove_chars("pynative", 4) so output must be tive. Here we need to remove first four characters from a string.
  • remove_chars("pynative", 2) so output must be native. Here we need to remove first two characters from a string.

Note: n must be less than the length of the string.

Show Hint

Use string slicing to get the substring. For example, to remove the first four characters and the remeaning use s[4:].

Show Solution
def remove_chars(word, n): print('Original string:', word) x = word[n:] return xprint("Removing characters from a string")print(remove_chars("pynative", 4))print(remove_chars("pynative", 2))
(Video) 6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)

Also, try to solvePython String Exercise

Exercise 5: Check if the first and last number of a list is the same

Write a function to return True if the first and last number of a given list is same. If numbers are different then return False.


numbers_x = [10, 20, 30, 40, 10]numbers_y = [75, 65, 35, 75, 30]

Expected Output:

Given list: [10, 20, 30, 40, 10]result is Truenumbers_y = [75, 65, 35, 75, 30]result is False
Show Solution
def first_last_same(numberList): print("Given list:", numberList) first_num = numberList[0] last_num = numberList[-1] if first_num == last_num: return True else: return Falsenumbers_x = [10, 20, 30, 40, 10]print("result is", first_last_same(numbers_x))numbers_y = [75, 65, 35, 75, 30]print("result is", first_last_same(numbers_y))

Exercise 6:Display numbers divisible by 5 from a list

Iterate the given list of numbers and print only those numbers which are divisible by 5

Expected Output:

Given list is [10, 20, 33, 46, 55]Divisible by 5102055
Show Solution
num_list = [10, 20, 33, 46, 55]print("Given list:", num_list)print('Divisible by 5:')for num in num_list: if num % 5 == 0: print(num)

Also, try to solvePython list Exercise

Exercise 7: Return the count of a given substring from a string

Write a program to find how many times substring “Emma” appears in the given string.


str_x = "Emma is good developer. Emma is a writer"

Expected Output:

Emma appeared 2 times
Show Hint

Use string method count().

Show Solution

Solution 1: Use the count() method

str_x = "Emma is good developer. Emma is a writer"# use count method of a str classcnt = str_x.count("Emma")print(cnt)

Solution 2: Without string method

def count_emma(statement): print("Given String: ", statement) count = 0 for i in range(len(statement) - 1): count += statement[i: i + 4] == 'Emma' return countcount = count_emma("Emma is good developer. Emma is a writer")print("Emma appeared ", count, "times")

Exercise 8:Print the following pattern

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Hint: Print Pattern using for loop

Show Solution
for num in range(10): for i in range(num): print (num, end=" ") #print number # new line after each row to display pattern correctly print("\n")
(Video) Python for Beginners - Learn Python in 1 Hour

Exercise 9:Check Palindrome Number

Write a program to check if the given number is a palindrome number.

A palindrome number is a number that is same after reverse. For example 545, is the palindrome numbers

Expected Output:

original number 121Yes. given number is palindrome numberoriginal number 125No. given number is not palindrome number
Show Hint
  • Reverse the given number and save it in a different variable
  • Use the if condition to check if the original number and reverse number are the same. If yes, return True.
Show Solution
def palindrome(number): print("original number", number) original_num = number # reverse the given number reverse_num = 0 while number > 0: reminder = number % 10 reverse_num = (reverse_num * 10) + reminder number = number // 10 # check numbers if original_num == reverse_num: print("Given number palindrome") else: print("Given number is not palindrome")palindrome(121)palindrome(125)

Exercise 10: Create a new list from a two list using the following condition

Create a new list from a two list using the following condition

Given a two list of numbers, write a program to create a new list such that the new list should contain odd numbers from the first list and even numbers from the second list.


list1 = [10, 20, 25, 30, 35]list2 = [40, 45, 60, 75, 90]

Expected Output:

result list: [25, 35, 40, 60, 90]
Show Hint
  • Create an empty list named result_list
  • Iterate first list using a for loop
  • In each iteration, check if the current number is odd number using num % 2 != 0 formula. If the current number is an odd number, add it to the result list
  • Now, Iterate the first list using a loop.
  • In each iteration, check if the current number is odd number using num % 2 == 0 formula. If the current number is an even number, add it to the result list
  • print the result list
Show Solution
def merge_list(list1, list2): result_list = [] # iterate first list for num in list1: # check if current number is odd if num % 2 != 0: # add odd number to result list result_list.append(num) # iterate second list for num in list2: # check if current number is even if num % 2 == 0: # add even number to result list result_list.append(num) return result_listlist1 = [10, 20, 25, 30, 35]list2 = [40, 45, 60, 75, 90]print("result list:", merge_list(list1, list2))

Note: Try to solve Python list Exercise

Exercise 11: Write a Program to extract each digit from an integer in the reverse order.

For example, If the given int is 7536, the output shall be “6 3 5 7“, with a space separating the digits.

Show Solution

Use while loop

number = 7536print("Given number", number)while number > 0: # get the last digit digit = number % 10 # remove the last digit and repeat the loop number = number // 10 print(digit, end=" ")

Exercise 12: Calculate income tax for the given income by adhering to the below rules

Taxable IncomeRate (in %)
First $10,0000
Next $10,00010
The remaining20

Expected Output:

For example, suppose the taxable income is 45000 the income tax payable is


10000*0% + 10000*10% + 25000*20% = $6000.

Show Solution
income = 45000tax_payable = 0print("Given income", income)if income <= 10000: tax_payable = 0elif income <= 20000: # no tax on first 10,000 x = income - 10000 # 10% tax tax_payable = x * 10 / 100else: # first 10,000 tax_payable = 0 # next 10,000 10% tax tax_payable = 10000 * 10 / 100 # remaining 20%tax tax_payable += (income - 20000) * 20 / 100print("Total tax to pay is", tax_payable)

Exercise 13: Print multiplication table form 1 to 10

Expected Output:

1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 

See: How two use nested loops in Python

Show Hint
  • Create the outer for loop to iterate numbers from 1 to 10. So total number of iteration of the outer loop is 10.
  • Create a inner loop to iterate 10 times.
  • For each iteration of the outer loop, the inner loop will execute ten times.
  • In the first iteration of the nested loop, the number is 1. In the next, it 2. and so on till 10.
  • In each iteration of an inner loop, we calculated the multiplication of two numbers. (current outer number and curent inner number)
Show Solution
for i in range(1, 11): for j in range(1, 11): print(i * j, end=" ") print("\t\t")

Exercise 14: Print downward Half-Pyramid Pattern with Star (asterisk)

* * * * * * * * * * * * * * *

Hint: Print Pattern using for loop

Show Solution
for i in range(6, 0, -1): for j in range(0, i - 1): print("*", end=' ') print(" ")

Exercise 15: Write a function called exponent(base, exp) that returns an int value of base raises to the power of exp.

Note here exp is a non-negative integer, and the base is an integer.

Expected output

Case 1:

base = 2exponent = 52 raises to the power of 5: 32 i.e. (2 *2 * 2 *2 *2 = 32)

Case 2:

base = 5exponent = 45 raises to the power of 4 is: 625 i.e. (5 *5 * 5 *5 = 625)
Show Solution
def exponent(base, exp): num = exp result = 1 while num > 0: result = result * base num = num - 1 print(base, "raises to the power of", exp, "is: ", result)exponent(5, 4)

Next Steps

I want to hear from you. What do you think of this basic exercise? If you have better alternative answers to the above questions, please help others by commenting on this exercise.

I have shown only 15 questions in this exercise because we have Topic-specific exercises to cover each topic exercise in detail. Please have a look at it.

(Video) Python Tutorial - Python Full Course for Beginners


How can I practice Python as a beginner? ›

11 Beginner Tips for Learning Python Programming
  1. Make It Stick. Tip #1: Code Everyday. Tip #2: Write It Out. ...
  2. Make It Collaborative. Tip #6: Surround Yourself With Others Who Are Learning. Tip #7: Teach. ...
  3. Make Something. Tip #10: Build Something, Anything. Tip #11: Contribute to Open Source.
  4. Go Forth and Learn!

What should a beginner learn in Python? ›

  1. Getting Started.
  2. Keywords and Identifiers.
  3. Statements & Comments.
  4. Python Variables.
  5. Python Datatypes.
  6. Python Type Conversion.
  7. Python I/O and import.
  8. Python Operators.

Where can I Practise Python for free? ›

Top 10 Free Python Courses
  • Google's Python Class.
  • Microsoft's Introduction to Python Course.
  • Introduction to Python Programming on Udemy.
  • Learn Python 3 From Scratch by Educative.
  • Python for Everybody on Coursera.
  • Python for Data Science and AI on Coursera.
  • Learn Python 2 on Codecademy.

Can I learn Python in 3 days? ›

On average, it can take anywhere from five to 10 weeks to learn the basics of Python programming, including object-oriented programming, basic Python syntax, data types, loops, variables, and functions.

What are the 5 easy steps to learn Python? ›

How To Learn Python In 5 Easy Steps?
  1. Figure Out What Python Is And What Motivates You To Learn It. ...
  2. Master Basic Syntaxes And Concepts. ...
  3. Install Python And Discover Different Frameworks. ...
  4. Work On Python Projects. ...
  5. Keep Challenging Yourself Every Day.
7 Oct 2022

Can I learn Python in 15 days? ›

If you're looking for a general answer, here it is: If you just want to learn the Python basics, it may only take a few weeks. However, if you're pursuing a data science career from the beginning, you can expect it to take four to twelve months to learn enough advanced Python to be job-ready.

Can I learn Python in 3 weeks? ›

In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes.

What are the basic skills of Python? ›

7 Essential Python Skills for Research
  • Know the fundamental data science libraries. ...
  • Master the command line. ...
  • Learn git and GitHub. ...
  • Use a code formatter. ...
  • Organize and standardize your file system. ...
  • Use notebooks to explore and full programs for experiments. ...
  • Enhance reproducibility using command line arguments.
4 Oct 2021

Can I learn Python in a month? ›

It takes about 65 hours to complete all the courses in the Learn Programming with Python track. If you can spare three hours a day, you will complete the entire track in 22 days. Thus, you can finish it up in a month.

How do I start coding? ›

How to Start Coding
  1. Figure out why you want to learn to code.
  2. Choose which coding language you want to learn first.
  3. Take online courses.
  4. Watch video tutorials.
  5. Read books and ebooks.
  6. Use tools that make learning to code easier.
  7. Check out how other people code.
  8. Complete coding projects.
29 Jun 2022

Can I learn Python in a day? ›

Yes, one can learn Python in one day! First, I explained the various aspects of Python. Then, I designed a coding problem to practice for each topic. By practicing those problems, the learner will learn the basics of Python deeply.

How can I practice coding? ›

Top websites for practicing your coding skills
  1. CodeChef. CodeChef lets you choose among thousands of problems to practice skills like sorting, data structures, and dynamic programming. ...
  2. Coderbyte. ...
  3. Codewars. ...
  4. CodinGame. ...
  5. Geektastic. ...
  6. HackerRank. ...
  7. LeetCode. ...
  8. Project Euler.
25 Apr 2022

How long will it take to learn Python? ›

If you have a full-time job or you are a student, you can finish it in 5 months. After coming back from your work/school, spend 2–3 hours to learn python. Your goal will be to learn one day and practice the next day.

Is Python or C++ better? ›

C++ is faster than Python because it is statically typed, which leads to a faster compilation of code. Python is slower than C++, it supports dynamic typing, and it also uses the interpreter, which makes the process of compilation slower.

Should I start C++ or Python? ›

Deciding whether to learn Python or C++ first is a matter of preference for most people. Learn more about the pros and cons of each before you make a decision. Both Python and C++ are popular, easy programming languages for beginners, and choosing the one to learn first is often a matter of personal preference.

Is Python easier than Java? ›

Java and Python are two of the most popular programming languages. Of the two, Java is the faster language, but Python is simpler and easier to learn. Each is well-established, platform-independent, and part of a large, supportive community.

How can I learn Python in 10 days? ›

Learn Python Basics in 10 Days
  1. Day 2- Variables and Data-types: ...
  2. Day 3- Keyword and operators: ...
  3. Day 4- String Methods: ...
  4. Day 5- Control Structure: ...
  5. Day 6- Functions and Modules: ...
  6. Day 7- Exception Handling: ...
  7. Day 8- File Operation: ...
  8. Day 9- Oops Concepts:

How fast can a beginner learn Python? ›

While you can start to write small scripts in Python after just a few days of study, you'll probably spend around four months gaining an essential ability in programming with Python. You'll have to spend years and build many projects to become a Python expert in even just one field.

Is Python enough to get a job? ›

Python is used in many different areas. You can search for a job as a Python developer, data scientist, machine learning specialist, data engineer, and more. These jobs are interesting and in-demand. And, like other Python jobs, they pay good salaries.

Can I learn Python in 2 hours? ›

Python Basics for Absolute Beginners

6 within just 2 hours. The Basics of Python course covers, the concepts of Python Programming in 2 hours, and then you'll be creating your own applications, working with coding quizzes and challenges to excel what you learned.

Will learning Python get me a job? ›

Python is easy to understand and once you do, you can use those skills to land a wonderful career in the rapidly developing data science industry. Even better, your career will thrive as the demand for Python programmers grows with the new applications for machine learning that arise every day.

Which Python course is best for job? ›

The Best Python Courses of 2022
  • Best Overall: 2022 Complete Python Bootcamp From Zero to Hero in Python.
  • Best In-Depth Option: Python for Everybody Specialization.
  • Best for Beginners: Crash Course on Python.
  • Best for Advanced Training: Pluralsight.
  • Best Online Bootcamp: CodingNomads Python Bootcamp Online.
3 Nov 2022

Why is Java better than Python? ›

Python and Java are two of the most popular and robust programming languages. Java is generally faster and more efficient than Python because it is a compiled language. As an interpreted language, Python has simpler, more concise syntax than Java. It can perform the same function as Java in fewer lines of code.

What is the hardest programming language? ›

Malbolge: One esoteric programming language is Malbolge.

The fact that it took at least two years to complete developing the first Malbolge code indicates that it is by far the toughest programming language to learn.

What are the 4 main uses of Python? ›

Python is commonly used for developing websites and software, task automation, data analysis, and data visualization. Since it's relatively easy to learn, Python has been adopted by many non-programmers such as accountants and scientists, for a variety of everyday tasks, like organizing finances.

What are the 7 features of Python? ›

Python Features
  • 1) Easy to Learn and Use. Python is easy to learn as compared to other programming languages. ...
  • 2) Expressive Language. ADVERTISEMENT. ...
  • 3) Interpreted Language. ...
  • 4) Cross-platform Language. ...
  • 5) Free and Open Source. ...
  • 6) Object-Oriented Language. ...
  • 7) Extensible. ...
  • 8) Large Standard Library.

What are 5 features of Python? ›

The following are some of the features in Python that are discussed below:
  • Easy to Code. Python is a very high-level programming language, yet it is effortless to learn. ...
  • Easy to Read. ...
  • Free and Open-Source. ...
  • Robust Standard Library. ...
  • Interpreted. ...
  • Portable. ...
  • Object-Oriented and Procedure-Oriented. ...
  • Extensible.
28 Oct 2022

Do I need to learn HTML before Python? ›

There's no definite rule that states what programming language you learn first. Both HTML and Python are easy to learn, and you can choose to get started with either of these programming languages depending on the area of development you want to focus on.

Is it too late to learn Python? ›

Let's get this out of the way: no, you are not too old to program. There isn't an age limit on learning to code, and there never was. But all too often, insecurity and uncertainty compel older adults to put a ceiling on their achievement potential.

What is the best way to learn Python? ›

One of the best places on the internet to learn Python for free is Codecademy. This e-learning platform offers lots of courses in Python, both free and paid. Python 2 is a free course they provide, which is a helpful introduction to basic programming concepts and Python.

What is coding step by step? ›

CodeStepByStep is an online coding practice tool that has thousands of exercises to help you learn and practice programming in a variety of popular languages.

Can I teach myself code? ›

Teach Yourself to Code

It's true that you can learn programming languages on your own, but it won't be easy. Coding is a highly technical job that entails different algorithms and complex data structures. On the flip side, learning by yourself allows you to set the pace of your education.

Which coding is easy for beginners? ›

Python is always recommended if you're looking for an easy and even fun programming language to learn first. Rather than having to jump into strict syntax rules, Python reads like English and is simple to understand for someone who's new to programming.

How long will it take to learn Python for beginners? ›

While you can start to write small scripts in Python after just a few days of study, you'll probably spend around four months gaining an essential ability in programming with Python. You'll have to spend years and build many projects to become a Python expert in even just one field.

Can I learn Python in 2 weeks? ›

If you're interested in learning the fundamentals of Python programming, it could take you as little as two weeks to learn, with routine practice. If you're interested in mastering Python in order to complete complex tasks or projects or spur a career change, then it's going to take much longer.

Which Python is easiest to learn? ›

If you want the short answer, here it is: You should learn Python 3 because it's the version most relevant to today's data science projects. Plus, it's easy to learn, and there are few compatibility issues to worry about.


1. The ultimate introduction to Python in 2022 [ + Exercises ]
(Clear Code)
2. 5 Mini Python Projects - For Beginners
(Tech With Tim)
3. Learn Python - Full Course for Beginners [Tutorial]
4. Python Functions | Python Tutorial for Absolute Beginners #1
(Programming with Mosh)
5. Solving Beginner Python Coding Challenges
6. Python For Loops - Python Tutorial for Absolute Beginners
(Programming with Mosh)
Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated: 04/11/2023

Views: 5974

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.