pynative.com
Open in
urlscan Pro
2606:4700:10::6816:3ad2
Public Scan
Submitted URL: https://pynative.com/python-basic-exercise-for-beginners/#pd:desktop#source:www.google.com#browser:chrome#topic:tech#...
Effective URL: https://pynative.com/python-basic-exercise-for-beginners/
Submission: On September 04 via api from SG
Effective URL: https://pynative.com/python-basic-exercise-for-beginners/
Submission: On September 04 via api from SG
Form analysis
1 forms found in the DOMGET https://pynative.com/
<form class="is-search-form is-form-style is-form-style-3 is-form-id-0 " action="https://pynative.com/" method="get" role="search"><label for="is-search-input-0"><span class="is-screen-reader-text">Search for:</span><input type="search"
id="is-search-input-0" name="s" value="" class="is-search-input" placeholder="Search here..." autocomplete="off"></label><button type="submit" class="is-search-submit"><span class="is-screen-reader-text">Search Button</span><span
class="is-search-icon"><svg focusable="false" aria-label="Search" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24px">
<path
d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z">
</path>
</svg></span></button></form>
Text Content
PYnative Python Programming * Learn Python * Exercises * Quizzes * Code Editor * Tricks * Home » Python Exercises » Python Basic Exercise for Beginners PYTHON BASIC EXERCISE FOR BEGINNERS Updated on: June 20, 2021 | 255 Comments This Python essential exercise is to help Python beginners to learn necessary Python skills quickly. Practice Python Basic Concepts such as Loops, Control structure, List, Strings, input-output, and built-in functions. Also See: * Python Quizzes * Python Basics What Questions included in this Python fundamental exercise? * The exercise contains 15 questions. The solution is provided for each question. * Each question contains the necessary skills you need to learn. 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 a control structure, loops, string, and list. 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: GIVEN TWO INTEGER NUMBERS RETURN THEIR PRODUCT. IF THE PRODUCT IS GREATER THAN 1000, THEN RETURN THEIR SUM Reference article for help: * Accept user input in Python * Calculate an Average in Python Given 1: number1 = 20 number2 = 30 Expected Output: The result is 600 Given 2: number1 = 40 number2 = 30 Expected Output: The result is 70 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 condition result = multiplication_or_sum(20, 30) print("The result is", result) # Second condition result = multiplication_or_sum(40, 30) print("The result is", result) Run EXERCISE 2: GIVEN A RANGE OF THE FIRST 10 NUMBERS, ITERATE FROM THE START NUMBER TO THE END NUMBER, AND IN EACH ITERATION PRINT THE SUM OF THE CURRENT NUMBER AND PREVIOUS NUMBER Reference article for help: * Python range() function * Calculate sum and average in Python 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 Show Solution def sumNum(num): previousNum = 0 for i in range(num): sum = previousNum + i print("Current Number", i, "Previous Number ", previousNum," Sum: ", sum) previousNum = i print("Printing current and previous number sum in a given range(10)") sumNum(10) Run EXERCISE 3: GIVEN A STRING, DISPLAY ONLY THOSE CHARACTERS WHICH ARE PRESENT AT AN EVEN INDEX NUMBER. For example, str = "pynative" so you should display ‘p’, ‘n’, ‘t’, ‘v’. Reference article for help: Python Input and Output Expected Output: Orginal String is pynative Printing only even index chars p n t v Show Solution def printEveIndexChar(str): for i in range(0, len(str)-1, 2): print("index[",i,"]", str[i] ) inputStr = "pynative" print("Orginal String is ", inputStr) print("Printing only even index chars") printEveIndexChar(inputStr) Run EXERCISE 4: GIVEN A STRING AND AN INTEGER NUMBER N, REMOVE CHARACTERS FROM A STRING STARTING FROM ZERO UP TO N AND RETURN A NEW STRING For example, removeChars("pynative", 4) so output must be tive. Note: n must be less than the length of the string. Show Solution def removeChars(str, n): return str[n:] print("Removing n number of chars") print(removeChars("pynative", 4)) Run Also try to solve Python String Exercise EXERCISE 5: GIVEN A LIST OF NUMBERS, RETURN TRUE IF FIRST AND LAST NUMBER OF A LIST IS SAME Expected Output: Given list is [10, 20, 30, 40, 10] result is True Given list is [10, 20, 30, 40, 50] result is False Show Solution def isFirst_And_Last_Same(numberList): print("Given list is ", numberList) firstElement = numberList[0] lastElement = numberList[-1] if (firstElement == lastElement): return True else: return False numList = [10, 20, 30, 40, 10] print("result is", isFirst_And_Last_Same(numList)) Run EXERCISE 6: GIVEN A LIST OF NUMBERS, ITERATE IT AND PRINT ONLY THOSE NUMBERS WHICH ARE DIVISIBLE OF 5 Expected Output: Given list is [10, 20, 33, 46, 55] Divisible of 5 in a list 10 20 55 Show Solution def findDivisible(numberList): print("Given list is ", numberList) print("Divisible of 5 in a list") for num in numberList: if (num % 5 == 0): print(num) numList = [10, 20, 33, 46, 55] findDivisible(numList) Run Also try to solve Python list Exercise EXERCISE 7: RETURN THE COUNT OF SUB-STRING “EMMA” APPEARS IN THE GIVEN STRING Given: str = "Emma is good developer. Emma is a writer" Expected Output: Emma appeared 2 times Show Solution sampleStr = "Emma is good developer. Emma is a writer" # use count method of a str class cnt = sampleStr.count("Emma") print(cnt) Run Solution 2: Without string function 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") Run EXERCISE 8: PRINT THE FOLLOWING PATTERN 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Reference article for help: 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") Run EXERCISE 9: REVERSE A GIVEN NUMBER AND RETURN TRUE IF IT IS THE SAME AS THE ORIGINAL NUMBER Expected Output: original number 121 The original and reverse number is the same original number 125 The original and reverse number is not same Show Solution def reverseCheck(number): print("original number", number) originalNum = number reverseNum = 0 while (number > 0): reminder = number % 10 reverseNum = (reverseNum * 10) + reminder number = number // 10 if (originalNum == reverseNum): return True else: return False print("The original and reverse number is the same:", reverseCheck(121)) Run EXERCISE 10: GIVEN A TWO LIST OF NUMBERS CREATE A NEW LIST SUCH THAT NEW LIST SHOULD CONTAIN ONLY ODD NUMBERS FROM THE FIRST LIST AND EVEN NUMBERS FROM THE SECOND LIST Expected Output: list1 = [10, 20, 23, 11, 17] list 2 = [13, 43, 24, 36, 12] result List is [23, 11, 17, 24, 36, 12] Show Solution def mergeList(listOne, listTwo): print("First List ", listOne) print("Second List ", listTwo) thirdList = [] for num in listOne: if (num % 2 != 0): thirdList.append(num) for num in listTwo: if (num % 2 == 0): thirdList.append(num) return thirdList listOne = [10, 20, 23, 11, 17] listTwo = [13, 43, 24, 36, 12] print("result List is", mergeList(listOne, listTwo)) Run Note: Try to solve Python list Exercise Also, Test you basic concepts using Basic Python Quiz for Beginners. EXERCISE 11: WRITE A CODE TO EXTRACT EACH DIGIT FROM AN INTEGER, IN THE REVERSE ORDER Expected Output: If the given int is 7536, the output shall be “6 3 5 7“, with a space separating the digits. Show Solution 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=" ") Run EXERCISE 12: CALCULATE INCOME TAX FOR THE GIVEN INCOME BY ADHERING TO THE BELOW RULES Taxable IncomeRate (in %)First $10,0000Next $10,00010The remaining20 Expected Output: For example, suppose that the taxable income is $45000 the income tax payable is $10000*0% + $10000*10% + $25000*20% = $6000. Show Solution income = 45000 taxPayable = 0 print("Given income", income) if income <= 10000: taxPayable = 0 elif income <= 20000: taxPayable = (income - 10000) * 10 / 100 else: # first 10,000 taxPayable = 0 # next 10,000 taxPayable = 10000 * 10 / 100 # remaining taxPayable += (income - 20000) * 20 / 100 print("Total tax to pay is", taxPayable) Run 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 Show Solution for i in range(1, 11): for j in range(1, 11): print(i * j, end=" ") print("\t\t") Run EXERCISE 14: PRINT DOWNWARD HALF-PYRAMID PATTERN WITH STAR (ASTERISK) * * * * * * * * * * * * * * * Reference article for help: Print Pattern using for loop Show Solution for i in range(6, 0, -1): for j in range(0, i - 1): print("*", end=' ') print(" ") Run 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 = 2 exponent = 5 2 raises to the power of 5: 32 i.e. (2 *2 * 2 *2 *2 = 32) Case 2: base = 5 exponent = 4 5 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) Run 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. Filed Under: Python, Python Basics, Python Exercises Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources. TweetF sharein shareP Pin ABOUT VISHAL Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors! RELATED TUTORIAL TOPICS: Python Python Basics Python Exercises 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 Exercises Quizzes Posted In Python Python Basics Python Exercises TweetF sharein shareP Pin Python Exercises * Python Exercises Home * Basic Exercise for Beginners * Input and Output Exercise * Loop Exercise * Functions Exercise * String Exercise * Data Structure Exercise * List Exercise * Dictionary Exercise * Set Exercise * Tuple Exercise * Date and Time Exercise * OOP Exercise * Python JSON Exercise * Random Data Generation Exercise * NumPy Exercise * Pandas Exercise * Matplotlib Exercise * Python Database Exercise ALL PYTHON TOPICS Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON ABOUT PYNATIVE PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills. EXPLORE PYTHON * Learn Python * Python Basics * Python Databases * Python Exercises * Python Quizzes * Online Python Code Editor * Python Tricks FOLLOW US To get New Python Tutorials, Exercises, and Quizzes * Twitter * Facebook * Sitemap LEGAL STUFF * About Us * Contact Us We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy. Copyright © 2018-2021 · [pynative.com] Search for:Search Button