INTRODUCTION TO PYTHON
2018 EDITION
By
Mark Lassoff
with Julius Hernandez
LearnToProgram, LLC
129 Church Street #230
New Haven, CT 06510
LearnToProgram.tv, Incorporated
129 Church Street #230
New Haven, CT 06510
contact@learntoprogram.tv
(860) 840-7090
2018 by LearnToProgram.tv, Incorporated
All rights reserved. No part of this document may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of LearnToProgram.tv, Incorporated.
Limit of Liability/Disclaimer of Warranty : While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. By following the instructions contained herein, the reader willingly assumes all risks in connection with such instructions. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, exemplary, or other damages resulting in whole or part, from the readers use of, or reliance upon, this material.
Dedication
For my family, friends and colleagues who supported
the development of LearnToProgram.tv and this book.
Table of Contents
Chapter 1 Introduction
The familiar Python logo. Python is a well-supported, open-sourced language. It is freely-available for both personal and commercial use.
Welcome to Python for Beginners! With this book, you will learn the basics of Python, a powerful high-level and object-oriented programming language that is suitable for both beginners and experienced programmers. The goal of Python is to make programming easy to learn. It is open-source and is free to use and distribute, even commercially.
Prior knowledge of Python is not required to learn from this book. Whether you are new to programming or you are someone who has programmed before using another language, you will find this book to be the ideal resource for learning Python.
For beginners who are new to Python, computer science knowledge would be helpful. However, it is not required. It is important to spend time understanding the concepts and doing the coding exercises. This is a sure-fire way to retain knowledge gained from this book.
The concepts discussed in the book will be familiar to someone with prior programming experience. For those readers, it is expected that the book will fast-track their understanding of Python programming concepts and how they differ from the language/s they have used before.
At the end of the course, you will have the basic skills necessary to take your Python programming skills to the next level. Learn to Programs Python for Game Development courses are perfect for that.
Figure 1.2. Learning the basics of Python programming has never been easier with the Python for Beginners video course on Learn to Program TV and this companion book. After you have completed this beginners course, you can take your Python development skills to the next level with our Game Development for Python courses.
What Does the Book Cover?
This book has 12 chapters, including this introductory chapter. We will go through the next chapters briefly below.
Figure 1.3. The home page of the Python.org website, where you can download installers and documentation on the Python programming language. The website also features the latest news and events about Python, and the robust Python user community.
In Chapter 2, you will start by downloading and installing Python (installation packages are available for Windows, Linux and other Unix-based platforms, and Mac OS X). You will then code your first Python program using IDLE , or the Integrated Development and Learning Environment , which is installed with Python. Your very first Python program would be a simple program. However, what better way is there to fire up the excitement of learning a programming language than writing your first program? You will then execute the program from the command line. In this chapter, you will also learn how to use the Python shell window interactively. You will be able to write and run programming commands directly from the shell window. You will then have a coding exercise at the end, that will reinforce the concepts discussed in the chapter.
Figure 1.4. Python includes a cross-platform IDE known as IDLE, for Integrated Development and Learning Environment. IDLE was coded entirely in Python and can be configured to your liking.
In Chapter 3, you will learn how the first program you wrote in Chapter 1 works. This chapter will teach you how to output the results from your program onto the screen, by using the Print function. You will also learn about the proper usage of separators and newlines, which allow a finer grain of control on displaying output on the command line. At the end, you will tie this all together with a coding exercise on the Print function.
Figure 1.5. IDLE has built-in syntax highlighting, and code indentation, among other features, commonly found in commercial IDEs.
Chapter 4 discusses the use of variables in Python. You might remember variables from your algebra class, such as in the expression x + 10 = 15 , what is the value of the variable x ? Variables play an important role in Python, as well as other programming languages. You will learn how to assign variables, and then review the different types of variables you can use in your programs, including numbers, strings, and substrings and how to concatenate them. You will also learn about the different variables that you can use in lists, tuples, and dictionaries (we will discuss these data structures in greater detail in Chapter 10). Finally, you will tie together everything you learned about variables in another coding exercise.
Chapter 5 focuses on the use of operators, which are used to complete expressions made up of variables. For example, in the expression x + 10 = 15 , the symbols + and = are operators (the + sign is a mathematical operator, denoting that the number should be added to the variable x , while the = sign is an assignment operator, which means it assigns the number to the expression x+10 ). The order in which these operators are evaluated works is the same way as in math given an expression, you should solve it in the following order: parentheses, exponents, multiplication, division, addition, and subtraction. In addition to mathematical operators, you will also learn about comparison and logical operators. As the name implies, a comparison operator compares two variables or values, while a logical operator allows you to join expressions. A coding exercise will be given at the end to test your newly-acquired knowledge.