Python 3 Exercises for beginners
Python 3 Exercises for beginners
Easy exercises for starting coders
About the Book
This book pretends to fill a gap between the Python books.
Written by Carles Mateo, with Sandra Soriano and Albert Ballart as coautors, when Carles was teaching to different students he realized that there is a gap, between the moment the person is starting to program and finds no exercises easy enough to do. They are too difficult. Like in hackerrank.
This book provides very easy exercises to learn easily, and explanations abut the solutions achieved.
Then the difficulty increases, and the explanations provide a very valuable knowledge absorbed quickly and easily. Also examples that will clarify very basic doubts and not so basic ones.
It also provides useful recipes and samples, like building text menu applications, receiving input from keyboard, or using library paramiko to SSH to a Server with username and password and execute a command. Also I show how I created a Battleship game, a Guess my number game, a Tic Tac Toe, graphics generation...
This book is written in three languages:
- English (Main)
- Catalan
- Spanish
Everything is available in the three languages.
Bundles that include this book
Table of Contents
- Index for v.45 (2022-02-12) .... 3
- Contact me .... 7
- Receiving Free Updates .... 8
- If you’re not satisfied .... 9
- Some notes / Algunes notes / Algunas anotaciones .... 10
- MT Notation .... 11
- MT Notation (English) .... 11
- Just execute / Només executa / Solo ejecuta .... 12
- Exercise 1: index returned by .find() .... 12
- Exercise 2: for in range .... 13
- Exercise 3: New instance of a class .... 14
- Exercise 4: New instance of a class with Parameters .... 15
- Exercise 5: Math Operations order .... 16
- Basic exercises / Exercicis bàsics / Ejercicios básicos .... 18
- Exercise 1: Print numbers from 0 to 9. Use for and range. .... 18
- Exercise 1.1: Do the same with while instead of for and range. .... 19
- Exercise 2: Num1 exp Num2 .... 20
- Exercise 3: Prints characters from "a" to "z". .... 23
- Ascii Table .... 25
- Exercise 4: Print even numbers for 0 to 20 .... 27
- Exercise 4.1: Do it using while instead of for .... 28
- Exercise 5: Locate the index of a Substring in a String .... 30
- Exercise 6: Create a function that gets a String and prints each character in a line .... 33
- Access Strings by position .... 33
- Exercise 7: Scrap the title of a web, manually .... 35
- Exercise 7.1: Scrap the title of a web, using Beautiful Soup package .... 39
- Exercise 8: Reverse a String .... 42
- Exercise 8.1: Reverse a String using for .... 44
- Exercise 9: Phrase in diagonal .... 46
- Exercise 10: Number Translator .... 50
- Exercise 10.1: Import the file from the exercise 10, and make a new program that prints all the numbers in a cipher in a single line .... 54
- Exercise 11: Create a function that using .rfind() return the index of the last position in a String passed by parameter .... 55
- Exercise 11.1: Create a function that using .find() returns the index position of the first occurrence of the substring searched for .... 55
- Exercise 11.2: Create a simple code that prints the index position of the first occurrence of the character “a” in the String without using any function (not .find(), etc…) .... 55
- 3 / 202Exercise 12: The minor number of 2 .... 56
- Exercise 13: The minor number of 3 .... 56
- Exercise 13: The minor number of n .... 56
- Exercise 13.1: The minor number of n .... 56
- Exercise 14: Create a function that does nothing and does not generate an error when it's called .... 56
- Exercise 15: Import library random and print a random number between 0 and 10 .... 57
- Exercise 16: Create a function that gets two parameters, i_min and i_max, and it generates a random number. Return it .... 57
- Exercise 17: Create a function that gets three parameters, i_min and i_max and i_numbers and it generates a list with i_numbers random numbers. Returns that array .... 58
- Exercise 18: Using the previous code, generate an Array of numbers 1,000 numbers between 0 and 255 .... 59
- Exercise 19: Numbers in words, with negative numbers .... 60
- Exercise 20: translate numbers to the name of their position if the most optimal way in terms of usage of memory .... 62
- Exercise 21: Convert Bytes to KB .... 64
- Exercise 22: Count how many times “a” char appears in a text .... 65
- Exercise 23: Remove the accents of a Text .... 66
- Exercise 24: Count the char more used in a text, excluding spaces .... 68
- Exercise 25: Open a text file and print it’s contents on the screen prepending the line number .... 70
- Exercise 26: Calculate the average of several numbers .... 73
- Exercise 27: Create a Function to calculate the area of a circle of radius n .... 74
- Exercise 28: import the previous file area_of_a_circle.py and calculate the areas of circles of radius from 5 to 100 in increments of 5 (5, 10, 15...100) .... 76
- Exercise 29: Print the values from a tuple stored in an Array .... 77
- Arrays or Lists. Llistes. Listas .... 78
- Exercice 1: Print the items of the list, one per line .... 78
- Exercici 2: Sort the list using .sort() .... 80
- Exercise 2.1 [Difficult]: Sort the list without using .sort() .... 81
- Exercise 3: Using .count() method determine how many times a value appears in the Array .... 83
- Exercise 3.1 [Difficult]: Using a loop (avoid using .count() method) count how how many times a value appears in the Array .... 83
- Exercise 4: Last item of an Array .... 84
- Exercise 5: Print a new array that contains the last two items of the previous array using array slice .... 85
- Exercise 5: Sorting an Array on numbers with sorted .... 86
- Exercise 6: Sorting an String with sorted .... 86
- Exercise 7: repeat an array pattern n times .... 87
- Exercise 8: Create an Array with the values from 0 to 99 .... 88
- Exercise 9: Keep an Array always sorted .... 89
- Dictionary / Diccionaris / Diccionarios .... 90
- Exercise 1: The code (substitution using a dictionary) .... 90
- Exercise 2: Connect to a web page that returns a JSON and get your public Ip .... 91
- Exercise 3: Challenge lettersum .... 92
- Classes .... 95
- Exercise 1: Create a class with a constructor, that does nothing when created, but works .... 96
- Exercise 2: Create a class named MyCar with a constructor, with two parameters, one String, and the other Integer, that sets two properties (class variables) .... 97
- Exercise 3: Draw a map for your games .... 98
- Practical problems and Recipes .... 104
- Exercise 1: Ask the user for Y or N .... 104
- Exercise 2: Function to ask for user input .... 105
- Exercise 3: Create a function that will ask a user for a number from Keyboard input, and return the result, only when the value is between the accepted ranges .... 106
- Exercise 4: Write a program that will keep the Unix Epoch time when the program starts, and when the program ends, and it will show the time spent by the program .... 114
- Exercise 5: Using Paramiko library, write a function to SSH to a Server and execute a command .... 115
- Exercise 6: Getting count, min, max, average .... 118
- Exercise 7: Redo the exercise 6 by using Arrays .... 124
- Graphics .... 125
- Use WordCloud to generate a .PNG cloud of words .... 125
- Graphic lines with matplotlib.pyplot .... 127
- Draw a pie chart .... 128
- Games .... 130
- Guess my number / Adivina el número .... 130
- Tic Tac Toe / 3 en ratlla / 3 en ralla .... 132
- Battleship / Enfonsar la flota / Hundir la flota .... 136
- Hard exercises .... 154
- Exercise 1: Using the previous code to generate random numbers, generate and array of 10,000 random Integer numbers and sort the array using the Algorithm Bubble Sort .... 154
- Exercise 1.1: Use the Unix epoch functions written before, to see the time used by the Bubble Sort algorithm .... 155
- Exercise 1.2: Using the previous program, remove everything unnecessary, except the last print to show the time .... 158
- Exercise 1.3: Using the previous program, replace the Bubble Sort by Python Sort, so: a_i_random.sort() .... 160
- Exercise 1.4: Increase the number of numbers generated to 100,000 and observe the differences in time between Bubble Sort and Python .sort() .... 160
- Exercise 1.5: Now repeat the exercise with the Python Sort and Bubble Sort, but instead of a range between 0 and 255, use 10,000,000 as max. Generate 10,000 numbers only. Note the times and your conclusions. .... 162
- Exercise 2: Create a dictionary that contains three places, and the continent, population, and a particularity. Then print all of them using a for loop. .... 163
- Exercise 3: Create a game program that generates a random number between 1 and 100, and you have to guess it .... 164
- Exercise 4: Create a class named MySql to connect, select, insert/update to a MySql .... 164
- Database Server .... 166
- Exercise 5: Define the SQL for MySQL, for creating a table named car_queue that keeps the list of a car manufacturer to be built .... 168
- Exercise 6: write a class named CarDO that keeps the Data for an object Car, and provides some getters and functions to retrieve the Data visually nicelly .... 170
- Exercise 7: Using the MySQL Lib you created, and the table you created, and the CarDO write a CLI program to List, Insert, Delete registers, and Search by Id .... 172
- Challenge: Compare two Roman numbers .... 177
- Challenge Nonogram .... 180
- Quiz .... 181
- Q1: Copying an Array and modifying the array from the new variable .... 182
- Q2: Accessing global variables from a function .... 183
- Q3: Having in count the previous exercise, what will be the output of this code .... 185
- Q4: Will this generate a Warning? .... 186
- Q5: Can we have a function with more than an input parameter? .... 187
- Q6: Can a function or method return more than a parameter? .... 188
- Q7: What is the result of int(“1.first”) .... 189
- Q8: Can this be done in Python? .... 191
- Q9: What will this code print? .... 192
- Q10: What will this code print? .... 194
- Useful libraries .... 195
- Bibliography .... 200
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Do Well. Do Good.
Authors have earned$11,721,492writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
Recipes for Decoupling
Matthias NobackWrite software that survives
CQRS by Example
Carlos Buenosvinos, Christian Soronellas, and Keyvan Akbary- Leverage your Software Architecture skills by learning everything about CQRS in detail with lots of examples
- Develop faster applications by applying CQRS and fostering Read Models and Projections
- Learn how to apply CQRS into a brownfield project from a pragmatic approach
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Maîtriser Apache JMeter
Philippe Mouawad, Bruno Demion (Milamber), and Antonio Gomes RodriguesToute la puissance d'Apache JMeter expliquée par ses commiteurs et utilisateurs experts. De l'intégration continue en passant par le Cloud, vous découvrirez comment intégrer JMeter à vos processus "Agile" et Devops.
If you're looking for the newer english version of this book, go to Master JMeter : From load testing to DevOps
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Jetpack Compose internals
Jorge CastilloJetpack Compose is the future of Android UI. Master how it works internally and become a more efficient developer with it. You'll also find it valuable if you are not an Android dev. This book provides all the details to understand how the Compose compiler & runtime work, and how to create a client library using them.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
Mastering STM32 - Second Edition
Carmine NovielloWith more than 1200 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the most complete guide around introducing the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL and STM32CubeIDE development environment.
Rector - The Power of Automated Refactoring
Matthias Noback and Tomas VotrubaLearn how to automatically and continuously upgrade and improve your PHP code base
The Hundred-Page Machine Learning Book
Andriy BurkovEverything you really need to know in Machine Learning in a hundred pages.
Top Bundles
- #2
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #4
Software Architecture
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #5
Static Analysis and Automated Refactoring
2 Books
As PHP developers we are living in the "Age of Static Analysis". We can use a tool like PHPStan to learn about potential bugs before we ship our code to production, and we can enforce our team's programming standards using custom PHPStan rules. Recipes for Decoupling by Matthias Noback teaches you in great detail how to do this, while also... - #9
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon...