314x Filetype PDF File size 0.22 MB Source: static1.squarespace.com
Basic python programming questions a Free coding exercises for Python programmers. Tutorials include Python basics, data structure and data analysis. There are currently 18 exercises on this page. What's Included in These Python Exercises? Each exercise contains specific Python questions to practice and solve. These free exercises are nothing more than practice exercises in Python where you have to solve different programs and challenges. All exercises are tested on Python 3. Each exercise has 10-20 questions. Every question has a solution. Practice each exercise in the online code editor These Python programming exercises are suitable for all Python programmers. If you are a beginner, after solving these exercises you will have a better understanding of Python. Below is a list of exercises. Select the task you want to solve. Practice and quickly learn essential Python skills by solving simple questions and problems. Topics: variables, operators, loops, strings, numbers, list. Solving input and output operations in Python. We also practice file handling. Topics: print() and input(), File I/O This Python looping exercise is designed to help programmers practice branching and looping methods in Python. Topics: if-else statements, the loop, and the while loop. Practice creating functions, nesting functions, and using efficient function arguments in Python by solving various questions. Topics: Function arguments, built-in functions. Solve the Python String exercise to learn and practice string operations and manipulation. Practice common Python types such as Python list, set, dictionary, and tuple operations This Python list exercise is designed to help Python programmers learn and practice list operations. This Python dictionary exercise is designed to help Python programmers learn and practice dictionaries. This exercise is designed to help Python programmers learn and practice set operations. This exercise is designed to help Python programmers learn and practice stack operations. This oneis designed to help Python programmers learn and practice date and time and timestamp questions and problems. Topics: date, time, date and time, calendar. This object-oriented programming (OOP) Python tutorial is designed to help Python programmers learn and practice OOP concepts. Topics: object, classes, inheritance, practice and learning, creation, manipulation, encoding, decoding and parsing in Python Practice NumPy questions such as manipulating arrays, numeric ranges, slicing, indexing, searching, sorting and dividing, and more. Practice data analysis with Python Pandas. Practice dataframe, data selection, grouping by, series, sorting, searching and statistics. Practice data visualization with Matplotlib in Python. Line Chart, Style Properties, Multi-Line Chart, Scatter Chart, Bar Chart, Histogram, Pie Chart, Sub Chart, Stacked Chart. Practice and learn various techniques for generating random data in Python. Topics: random module, secret module, UUID module Practice your Python database programming skills by solving questions step by step. Use any MySQL, PostgreSQL, SQLite to solve exercises. Exercises for Intermediate Programmers The following practice questions are intended for intermediate Python programmers. If you haven't solved the above exercises, complete them to understand and practice each topic in detail. Then you can quickly solve the following questions. Exercise 1: Rotate each word in a string Data: str = 'My name is Jessa' Expected result yM emaN si asseJ Show help Use the split() method to split a string into a list of words. Finally, reverse each word in the list, use join() to convert the list to a string View the solution Steps to solve this question: Split the given string into a list of words using the split() method Use list comprehensions to create a new list by reversing each word in the list. Use join() to convert the new list to a string Display the resulting string Solution: # Exercise to reverse each word of the stringreverse_words(Sentence): # Split string into whitespace = Sentence.split(" ") # Run through the list and reverse each word using ::-1 new_wordlist = [word[::-1] for word in words] # Append do res_str = " ".join(new_word_list) return res_str # Given string str1 = "My name is Jessa" print(reverse_words(str1)) Exercise 2. Load a text file into a variable and replace all new lines with a space Given: Suppose you have the following text file (sample.txt). Line1 Line2 Line3 Line4 Line5 Expected result: Line1 Line2 Line3 Line4 Line5 Show Hint Read the text file first. Then use the string replace() function to replace all newlines () with a space (' '). Show solution Steps to solve this problem: - First open the file in read mode. Then read the entire contents of the file with the read() function and assign it to a variable. Then use the string replace() function to replace all newlines () with a space (' '). Display the final string with open('sample.txt', 'r') as a file: data = file.read().replace('', ' ') print(data) Exercise 3. Removing items from a list during iteration Description : In this question, you need to remove items from the list while iterating, but without creating another copy of the list. Remove numbers greater than 50 Given: number_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Expected result: - [10, 20, 30, 40, 50] Show hint Download size of the list Go through the list with a while loop Check if the number is greater than 50 If so, remove the element with the del keyword Reduce the size of the list Show solution Solution 1. Use a while loop number_list = [10, 20, 30, 40, 50, 60 , 70, 80 , 90, 100] i = 0 # get list size n = len(number_list) # traverse the list until i is less than n while i < n: # check if number is greater than 50 if number_list [i] > 50: # remove the current index from the list del number_list[i] # reduce the size of the list n = n - 1 else: # go to the next element i = i + 1 print(list_number) Solution 2: Use a for and range loop () number_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] for andrange(len(number_list) - 1, -1, -1): if number_list[i] > 50: del number_list[i] print(number_list) Exercise 4: Reverse Dictionary Mapping Data: ascii_dict = {'A': 65 , 'B': 66, 'C': 67, 'D': 68} Expected result: {65: 'A', 66: 'B', 67: 'C', 68: 'D'} Show solution ascii_dict = {'A': 65, 'B': 66, 'C': 67, 'D': 68} # Reverse mapping new_dict = {value: key by key, value in ascii_dict.items()} print(new_dict) Exercise 5: show all duplicate items from list Data: sample_list = [10, 20, 60, 30, 20, 40, 30, 60, 70, 80] Expected result: - [20, 60, 30] Show hint Use counter() collection module method. Create a dictionary that will store the count of each item in the list. Then get all keys with value greater than 2. Show Solution Solution 1:- Using collections.Counter() import collections sample_list = [10, 20, 60, 30, 20, 40, 30, 60, 70, 80 ] duplicates = [ ] for items, count in collections. Counter(sample_list).items(): if count > 1: duplicates.add(item) print(duplicates) Solution 2: - sample_list = [10, 20, 60, 30, 20, 40 , 30, 60, 70, 80] exists = {} duplicates = [] for x in random_list: if x does not exist: exists [x] = 1 else: duplicates.append(x) print(duplicates) Exercise 6: Filter a dictionary containing keys from the given list Data : # Dictionary d1 = {'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69, 'F' : 70} # Filter the dict using the following keys l1 = ['A', 'C', 'F'] Expected result: - new dict {'A': 65, 'C': 67, 'F': 70} Show solution # Dictionary d1 = {'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69, 'F': 70 } # Filter the dict with the following keys l1 = ['A', 'C', ' F'] new_di ct = {key: d1[key] for key in l1} print(new_dict) Exercise 7: Print the following sample numbers 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 To solve this question, see Print samples in Python. Show hint set x = 0 Use two for loops Outer loop is reversed for loops 5 to 0 Increment x by 1 on each iteration of outer loop Inner loop will iterate from 0 to iouter loop Print the value of x in each iteration of the inner loop Print a new line at the end of each outer loop Show solution lines = 5 x = 0 # reverse loop from 5 to 0 for i in range (rows, 0, -1) : x += 1 for j in range(1, i + 1): print(x, end=' ') print('\r') Exercise 8: Create an inner function Question description: - Create an outer function that will accept two strings, x and y. (x= "Emma" and y = "Kelly". Create an inner function inside an outer function that combines x and y. Finally, the outer function appends the word "programmer" to it. Expected result: - EmmaKellyDevelopers Show Solution def manipulate(x, y): # combine two strings def inner_fun(x, y): return x + y z = inner_fun(x, y) return z + 'Developer's results' = manipulate('Emma', 'Kelly') print(result) Exercise 9: Edit item list nested inside the following list Change item 35 to 3500 Given: list1 = [5, [10, 15, [20, 25, [30, 35], 40], 45], 50] Expected result: - [5, [10, 15, [20, 25, [30, 3500], 40], 45], 50] See solution list1 = [5, [10, 15, [20, 25, [30], 35] , 40], 45] , 50] # modify element list1[1][2][2][1] = 3500 # display final result print(list1) # print(list1[1] ) = [10, 15, [ 20, 25, [30, 400], 40], 45] # print(list1[1][2]) = [20, 25, [30, 400], 40] # print(list1 [1][2] [2]) = [ 30, 40] # print(list1[1][2][2][1]) = 40 Exercise 10: Accessing a nested key increment from t then working dictionary Data: emp_dict = { "company": { "employee": { "name": "Jess", "paid": { "salary": 9000, "increment": 12 } } } } View solution emp_dict = { "company": { "employee": { " name": "Jess", "payment": { "salary": 9000, "increment": 12 } } } } print(emp_dict['company']['employee' ]['paid']['increment'] ) In the Exercises section: - This basic Python exercise is designed to help Python beginners quickly learn essential Python skills. Practice basic Python concepts such as loops, control flow, data types, operators, lists, strings, I/O, and built-in functions. See also: Python QuizzesPythonWhat questions are covered in this basic Python tutorial? The exercise contains 15 programs to be solved. Help and solutions are available for each question. I have added tips and required study materials to each question to help you solve the exercises. By answering each question, you'll learn the basics of Python. Use the online code editor to solve practice questions. Also try the Python Basics Quiz for Beginners. Exercise 1: Calculate the multiplication and sum of two numbers Given two integers, return the product only if the product is equal to or less than 1000, otherwise return their sum. Input 1: number1 = 20 number2 = 30 Expected result: Result is 600 Input 2: number1 = 40 number2 = 30 Expected result: Output is 70 Link: Accept user input in Python Calculate average in Python View help Create a function that takes two numbers as parameters Then inside the function, multiply the two numbers and store their product in the variable product. Then use an if condition to check if the product is >1000. If so, return the product, otherwise use the else block to calculate the sum of the two numbers and return it. Display solution def multiplication_or_sum(num1, num2): # calculate product of two numbers product = number1 * number2 # check if product is less than 1000, if product 0 : result = result * base num = num - 1 print(base, "increases se high", 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 specific topic exercises that cover each topic exercise in detail. Please take a look. it.
no reviews yet
Please Login to review.