Python 3 ejercicios para principiantes

Python 3 ejercicios para principiantes

Sobre este libro

Python 3 ejercicios para principiantes propone ejercicios sencillos y ofrece la solución, y enseña a pensar.

El libro está escrito en tres idiomas:

  • Inglés
  • Catalán
  • Castellano

También enseño a crear juegos en modo texto como tres en ralla, hundir la flota, adivina mi número, y gráficos.

Este libro es una traducción al Español de Python 3 Exercises for beginners, que fue originalmente escrito en Inglés.

Sobre el autor

Carles Mateo
Carles Mateo

I’m Carles Mateo, since January 2020 working as Operations Engineer for Blizzard Entertainment in Cork, Ireland.

Previously worked as Cloud Architect and SDM for Sanmina, in Ireland and United States, where I created and I did lead a Python and C project for a Storage Manager for our 4U Cold Storage Servers 1PB and 2U SSD and 1U NVMe with NICs up to 2x100Gbps, which will restore redundancy in the RAID when a drive dies, very quickly, using ZFS and DRAID.

I helped several multinationals:

  • Volkswagen IT (gedas) where I got promoted to head of department of Reusability and Code Components and previously I lead the SEAT's Intranet project, I solved TCP/Ip transmission problems in project PortIc for Barcelona's port (EDI Transfer), I created a project to transfer warranty documents over Web and TCP/IP instead of X.25 expensive lines, and worked for CaixaBank (formerly "la caixa") creating a Documentum solution programing the GUI and DLL's.
  • Winterthur Insurance (Axa) where I was Senior PM and I lead both technically and in functionally, the project to deploy a new Contact Center Software, the e-Commerce web platform and the insurance of health policies (special Data treatment)
  • BBVA Tickets where I was responsible of the platform (Sun Solaris and Oracle)

I worked a lot for CSP (Cloud Service Providers) and ISP, in Linux.

Also I helped many Start ups, e-Commerces, Online travel agencies... as CTO or in hands on positions leading teams and coding myself, in Software Development, SRE and Operation roles.

I was one of the 5 Senior BackEnds that wrote Privalia's new multi-country Website, from the scratch, with inheritance, and I lead a KPI Dashboards for CEO, really learning the internals of all the areas of the company through the data.

The last company I was CTO for, in Barcelona, was sold to airbnb for 45 million euros.

My friends say that everything I touch turns into gold, and my very good friend Jaume (R.I.P.) used to say that I was the most brutal technical guy, solving all the difficult technical issues easily, that he ever met. I've very grateful thoughts and memories for all the people that did speak up how well they considered me, and that were fair Other people tried to take advantage by never telling me how good and helpful I was, so they could pay me less. What they didn't know is that I always had offers paying me double, and I was choosing to help them.

Now I only accept fair agreements. I know my value.

I started programming when I was 5 years old, at 10 I won a contest at Spanish State level, getting a Pc with a green phosphorescent monitor and a floppy drive, and I've lived the times of the BBS, much before Internet.

I've coded many Freeware Software, and Open Source Software, and I created the first system in Europe to sell Tickets for musical events, in 1995, for Tick Tack Ticket. I created this e-Commerce engine, with render template system completely in C, cgi-bin, HTML.

I love Software and Hardware.

I've worked with C, Java, PHP, Python... and my knowledge of PHP Unit Testing really reached its best when I was working for a bank.

I've invented the CSort algorithm.

In my free time I teach some students to get pro in coding. Most of my time now I work with Python 3, Docker Containers and Linux.

You can find information about me in my blog.

Tabla de contenidos

  • Index for version 39. 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 31
  • Access Strings by position 31
  • Exercise 7: Scrap the title of a web 33
  • Exercise 8: Reverse a String 37
  • Exercise 8.1: Reverse a String using for 39
  • Exercise 9: Phrase in diagonal 41
  • Exercise 10: Number Translator 45
  • Exercise 11: Create a function that using .rfind() return the index of the last position in a String passed by parameter 46
  • Exercise 11.1: Create a function that using .find() returns the index position of the first occurrence of the substring searched for 46
  • 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…) 46
  • Exercise 12: The minor number of 2. 47
  • Exercise 13: The minor number of 3. 47
  • Exercise 13: The minor number of n 47
  • Exercise 13.1: The minor number of n 47
  • Exercise 14: Create a function that does nothing and does not generate an error when it's called 47
  • Exercise 15: Import library random and print a random number between 0 and 10. 48
  • Exercise 16: Create a function that gets two parameters, i_min and i_max, and it generates a random number. Return it 48
  • 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 49
  • Exercise 18: Using the previous code, generate an Array of numbers 1,000 numbers between 0 and 255. 49
  • Exercise 19: Numbers in words 51
  • Exercise 20: translate numbers to the name of their position if the most optimal way in terms of usage of memory 53
  • Exercise 21: Convert Bytes to KB 55
  • Exercise 22: Count how many times “a” char appears in a text 56
  • Exercise 23: Remove the accents of a Text 57
  • Exercise 24: Count the char more used in a text, excluding spaces 59
  • Exercise 25: Open a text file and print it’s contents on the screen prepending the line number 61
  • Exercise 26: Calculate the average of several numbers 64
  • Exercise 27: Create a Function to calculate the area of a circle of radius n 65
  • Exercise 28: import the previous file and calculate the areas of circles of radius from 5 to 100 in increments of 5 (5, 10, 15...100). 67
  • Exercise 29: Print the values from a tuple stored in an Array 68
  • Arrays or Lists. Llistes. Listas 69
  • Exercice 1: Print the items of the list, one per line 69
  • Exercici 2: Sort the list using .sort() 71
  • Exercise 2.1 [Difficult]: Sort the list without using .sort() 72
  • Exercise 3: Using .count() method determine how many times a value appears in the Array 74
  • Exercise 3.1 [Difficult]: Using a loop (avoid using .count() method) count how how many times a value appears in the Array 74
  • Exercise 4: Last item of an Array 75
  • Exercise 5: Print a new array that contains the last two items of the previous array using array slice 76
  • Exercise 5: Sorting an Array on numbers with sorted 77
  • Exercise 6: Sorting an String with sorted 77
  • Exercise 7: repeat an array pattern n times 78
  • Exercise 8: Create an Array with the values from 0 to 99. 79
  • Exercise 9: Keep an Array always sorted 80
  • Dictionary / Diccionaris / Diccionarios 81
  • Exercise 1: The code (substitution using a dictionary) 81
  • Exercise 2: Connect to a web page that returns a JSON and get your public Ip 82
  • Exercise 3: Challenge lettersum 83
  • Classes 87
  • Exercise 1: Create a class with a constructor, that does nothing when created, but works 87
  • 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) 88
  • Exercise 3: Draw a map for your games 89
  • Practical problems and Recipes 95
  • Exercise 1: Ask the user for Y or N. 95
  • Exercise 2: Function to ask for user input 96
  • 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 97
  • 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 105
  • Exercise 5: Using Paramiko library, write a function to SSH to a Server and execute a command 106
  • Exercise 6: Getting count, min, max, average 109
  • Exercise 7: Redo the exercise 6 by using Arrays 115
  • Graphics 116
  • Use WordCloud to generate a .PNG cloud of words 116
  • Graphic lines with matplotlib.pyplot 118
  • Draw a pie chart 119
  • Games 121
  • Guess my number / Adivina el número 121
  • Tic Tac Toe / 3 en ratlla / 3 en ralla 123
  • Battleship / Enfonsar la flota / Hundir la flota 127
  • Hard exercises 145
  • 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 145
  • Exercise 1.1: Use the Unix epoch functions written before, to see the time used by the Bubble Sort algorithm 146
  • Exercise 1.2: Using the previous program, remove everything unnecessary, except the last print to show the time 149
  • Exercise 1.3: Using the previous program, replace the Bubble Sort by Python Sort, so: a_i_random.sort() 151
  • Exercise 1.4: Increase the number of numbers generated to 100,000 and observe the differences in time between Bubble Sort and Python .sort() 151
  • 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. 153
  • 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. 154
  • Exercise 3: Create a game program that generates a random number between 1 and 100, and you have to guess it 155
  • Exercise 4: Create a class named MySql to connect, select, insert/update to a MySql Database Server 157
  • 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 159
  • 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 161
  • 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. 163
  • Challenge: Compare two Roman numbers 168
  • Challenge Nonogram 171
  • Quiz 173
  • Q1: Copying an Array and modifying the array from the new variable 173
  • Q2: Accessing global variables from a function 174
  • Q3: Having in count the previous exercise, what will be the output of this code 176
  • Q4: Will this generate a Warning? 177
  • Q5: Can we have a function with more than an input parameter? 178
  • Q6: Can a function or method return more than a parameter? 179
  • Q7: What is the result of int(“1.first”) 180
  • Q8: Can this be done in Python? 182
  • Q9: What will this code print? 183
  • Q10: What will this code print? 185
  • Useful libraries 186
  • Bibliography 191

Leanpub incondicional, sin riesgo, 100% de felicidad garantizada

Durante los primeros 60 días de compra, puedes obtener un reembolso del 100% de cualquier compra Leanpub, en dos clics. Procesamos las restituciones manualmente, así que puede tomarse unos días en aparecer. Véase términos completos.

80% de regalías. Gana $16 en un libro de $20.

Pagamos 80% de regalías. No es un error tipográfico: ganas $16 en una venta de $20. Si vendemos 5000 copias no reembolsadas de tu libro o curso por $20, ganarás $80,000.

(Sí, algunos autores ya han ganado mucho más que eso en Leanpub.)

De hecho, los autores han ganadomás de $12 millonesescribiendo, publicando y vendiendo en Leanpub.

Aprende más sobre cómo escribir en Leanpub

Actualizaciones gratis. Libre de DRM.

¡Si compras un libro Leanpub obtienes actualizaciones gratis siempre y cuando el autor actualice el libro! Varios autores usan Leanpub para publicar sus libros en progreso mientras los escriben. Todos los lectores obtienen actualizaciones gratis, independientemente de cuándo compraron el libro o cuánto pagaron (incluyendo si fue gratis).

La mayoría de los libros Leanpub se encuentran disponibles en PDF (para computadores) y EPUB (para teléfonos, tabletas, y Kindle). Los formatos que un libro incluye se muestran en la esquina superior derecha de esta página.

Finalmente, los libros Leanpub no tienen ninguna de las cosas sin sentido sobre protección de copia DRM, así que puedes leerlos fácilmente en cualquier dispositivo que se soporta.

Aprende más sobre los formatos de Leanpub y dónde puedes leerlos

Escribir y publicar en Leanpub

Los autores y editores usan Leanpub para publicar libros electrónicos sorprendentes en curso y completados, como éste. ¡También puedes utilizar Leanpub para escribir, publicar y vender tu libro! Leanpub es una potente plataforma para autores serios, que combina un flujo de trabajo de escritura y publicación simple y elegante con una tienda centrada en la venta de libros electrónicos en progreso. Leanpub es una máquina de escribir mágica para los autores: basta con escribir en texto plano, y para publicar tu libro electrónico, simplemente haz clic en un botón. Realmente es así de fácil.

Más información sobre cómo escribir en Leanpub