While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. PYTHON: TIPS AND TRICKS TO PROGRAMMING CODE WITH PYTHON First edition. January 26, 2017. Copyright 2017 Charlie Masterson. ISBN: 978-1386338864 Written by Charlie Masterson. 10 9 8 7 6 5 4 3 2 1
Python: Tips and Tricks to Programming Code with Python Charlie Masterson
Table of Contents
Copyright 2017 by Charlie Masterson - All rights reserved. The contents of this book may not be reproduced, duplicated or transmitted without direct written permission from the author.
Under no circumstances will any legal responsibility or blame be held against the publisher for any reparation, damages, or monetary loss due to the information herein, either directly or indirectly. Legal Notice: This book is copyright protected. This is only for personal use. You cannot amend, distribute, sell, use, quote or paraphrase any part or the content within this book without the consent of the author. Disclaimer Notice: Please note the information contained within this document is for educational and entertainment purposes only. Every attempt has been made to provide accurate, up to date and reliable complete information.
No warranties of any kind are expressed or implied. Readers acknowledge that the author is not engaging in the rendering of legal, financial, medical or professional advice. The content of this book has been derived from various sources. Please consult a licensed professional before attempting any techniques outlined in this book. By reading this document, the reader agrees that under no circumstances are is the author responsible for any losses, direct or indirect, which are incurred as a result of the use of information contained within this document, including, but not limited to, errors, omissions, or inaccuracies.
C ONGRATULATIONS ON downloading Python: Tips and Tricks to Programming Code with Python and thank you for doing so.
The following chapters will discuss some tips and tricks to help you get the most out of Python. This book will go over things that can improve your programming outcome with Python. All of you work will improve with this book. Each chapter will cover a different tip with coding that you can use today. In the first chapter you will learn how to change the color of your images. In chapter two you will learn how to make better data graphics.
In chapter three you will learn how to use code to send and check your e-mails, as well as receive text messages. In chapter four you will learn how to manipulate your images. In chapter five you will learn how to schedule things for you program to do. In chapter six you will learn how to make an animated banner. In chapter seven you will learn how to code games. This book can provide any programmer with some interesting and fun codes to learn from.
This book will help the already experienced programmer more, but a beginner can learn a lot from it too. Youre sure to find lots of helpful information within these pages, so lets begin. There are plenty of books on this subject on the market, thanks again for choosing this one! Every effort was made to ensure it is full of as much useful information as possible, please enjoy!
Image Colors L ETS START THIS WITH some fun image programming. Its always nice to have some fun programming up your sleeve, and this code will give you just that. In this chapter we will look at codes to help you change the color of an image, so you can make it look the way you want it to. This code will give you some fun things to do to your images when coding.
Its also just a fun thing to play around with. 1. Type the following in a Python IDLE window. CODE: from ImageLibrary import * myfile=pickAFile() pict=makePicture(myfile) show(pic) This tells Python to use every function that is in your image library. This should not be done in a main window. You need to write this in a new window.
Do this by choosing, file -> New Window and then paste the code in an empty window, then pick run. You will then be asked if want to save your file. The file needs to be saved in an arbitrary location. The from ImageLibrary import* needs to be placed into every IDLE window. myfile=pickAFile() lets the computer know you need a window that you can pick the .jpg image file that you want to play with. pickAFile() will give you a return of the full name of your image file. 2. 2.
Ask Python to print myfile so you will be able to see how the image looks. As a reminder, print command looks like print myfile. The string should look like: C:/Users/Public/Pictures/image.jpg That means myfile is a string that states the picture that you want. You could use a string so you wont have to call pickAFile() each time that you need anything new. Like this: pic=makePicture(/users/nameGoesHere/Pictures/image.jpg) Make your picture: from ImageLibrary import * filename=pickAFile() pic=makePicture(filename) show(pic) This will load you picture into memory so you can change it. You dont need to call makePicture(myfile) unless you plan on putting the results in a useful place such as assigning it to a variable such as pic .
Make sure that you get the lower and upper case letters right because Python is case sensitive. Whenever you change something about the picture, you have to call show(pic) so you will be able to see your changes. Your changes do not update automatically when they are changed. 3. Open the image and display it CODE: from ImageLibrary import * myfile=pickAFile() pic=makePicture(myfile) show(pic) First thing you are going to do is make every red pixel level to 255: for p in getPixels(pict): setRed(p,255) show(pic) 3a. Change red, green, and blue levels until the image is black, then white, the green.
Another neat function is setColor(pixel,color) will change a pixel color. The list of predefined colors are cyan, magenta, pink, orange, yellow, darkGrey, lightGrey, grey, blue, green, red, black, white. You can also use this function to change a pixel color to orange instead of the regular blue, red, and green colors. 3b. Change the picture color using the for loop, setColor (pixel, color), and then you can use the colors that have already been defined. You can also use makeColor(redValue, greenValue, blueValue).
This lets you create any color you want. To make purple you use: Purple=makeColor (255, 0, 255) 3c. Play with r, g, b values to figure out how to create yellow color. Use the earlier code to make the change to the complete picture. You can use the makeLighter (color) function to lighten parts of color. It actually subtracts 40 from each main color green, red, and blue.