Python problems and solutions

Python Basic Exercise for Beginners

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.

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.
Читайте также:  Вывод таблицы sql java

Use Online Code Editor to solve exercise questions.

This Python exercise covers questions on the following topics:

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.

Expected Output:

Expected Output:

  • 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.
def multiplication_or_sum(num1, num2): # calculate product of two number product = num1 * num2 # check if product is less then 1000 if product 

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: 0 Current Number 1 Previous Number 0 Sum: 1 Current Number 2 Previous Number 1 Sum: 3 Current Number 3 Previous Number 2 Sum: 5 Current Number 4 Previous Number 3 Sum: 7 Current Number 5 Previous Number 4 Sum: 9 Current Number 6 Previous Number 5 Sum: 11 Current Number 7 Previous Number 6 Sum: 13 Current Number 8 Previous Number 7 Sum: 15 Current Number 9 Previous Number 8 Sum: 17

Reference article for help:

  • 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 ).
print("Printing current and previous number and their sum in a range(10)") previous_num = 0 # loop from 1 to 10 for 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 pynative Printing only even index chars p n t v

Reference article for help: Python Input and Output

  • 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
# accept input string from a user word = input('Enter word ') print("Original String:", word) # get the length of a string size = 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, 4 print("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 user word = input('Enter word ') print("Original String:", word) # using list slicing # convert string to list # pick only even index chars x = 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.

  • 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.

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

def remove_chars(word, n): print('Original string:', word) x = word[n:] return x print("Removing characters from a string") print(remove_chars("pynative", 4)) print(remove_chars("pynative", 2)) 

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 True numbers_y = [75, 65, 35, 75, 30] result is False
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 False numbers_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 5 10 20 55
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 solve Python 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:

Solution 1: Use the count() method

str_x = "Emma is good developer. Emma is a writer" # use count method of a str class cnt = 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 count count = 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
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")

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 121 Yes. given number is palindrome number original number 125 No. given number is not palindrome number
  • 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 .
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]
  • 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
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_list list1 = [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.

number = 7536 print("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

Expected Output:

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

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

income = 45000 tax_payable = 0 print("Given income", income) if income  

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

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

base = 2 exponent = 5 2 raises to the power of 5: 32 i.e. (2 *2 * 2 *2 *2 = 32)
base = 5 exponent = 4 5 raises to the power of 4 is: 625 i.e. (5 *5 * 5 *5 = 625)
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.

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

Оцените статью