1. Trang chủ
  2. » Tất cả

234844490-Python-Book

231 1 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Python for Beginners i ii Python for Beginners Python for Beginners Alex Bowers LearnToProgram, Inc Vernon, Connecticut iii LearnToProgram.tv, Incorporated 27 Hartford Turnpike Suite 206 Vernon, CT06066 contact@learntoprogram.tv (860) 840-7090 ©2013 by LearnToProgram.tv, Incorporated ISBN-13: 978-0-9888429-7-7 ISBN-10: 0988842971 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 Mark Lassoff, Publisher Kevin Hernandez, VP/ Production Alison Downs, Copy Editor Alexandria O’Brien, Book Layout Ardit Sulce, Technical Writer Jeremias Jimenez, Technical Editor iv Python for Beginners Dedication To the team at LearnToProgram: thank you for your trust and faith in me Working alongside you has been incredible A special thank you to Mark for his guidance and assistance throughout the entire process Finally, a thank you to my parents, Caroline and Lee, and my brother Jonathan, for putting up with my seclusion while I harbored and honed my passion for programming v vi Python for Beginners Table of ConTenTs Getting Started 11 1.1 Acquiring the Tools 11 1.2 Hello World in Python 19 The Basics 23 2.1 Getting Started with Variables 23 2.2 Data Types 26 2.3 Arithmetic Operators 34 2.4 Understanding the Order of the Operators 36 2.5 Comments 37 2.6 Variable Scope 38 Chapter Lab Exercise 40 Conditionals 43 3.1 If Statements 43 3.2 Else Statements 46 3.3 Elif Statements 47 3.4 Switch Statements Workaround 49 3.5 Inline if 51 Chapter Lab Exercise 53 # AND Truth Table: 55 # OR truth table: 55 # NOT truth table: 56 # Main Challenge: 56 Looping 60 4.1 While Loops 60 4.2 For Loops 64 4.3 Try, Except, Finally 68 4.4 Break, Continue, and Else 72 Chapter Lab Exercise 76 Lists 80 5.1 A Closer Look at Lists 80 5.2 Modifying Lists 84 5.3 Sorting Lists 89 Chapter Lab Exercise 93 Receiving Inputs 98 6.1 “Press any key to continue” 98 6.2 Data Input 100 Chapter Lab Exercise 104 Predefined String Functions 106 7.1 Using Strings as Objects 106 7.2 Splitting and Joining Strings 111 Chapter Lab Exercise 115 Custom Functions 118 vii 8.1 Syntax Overview 118 8.2 More Parameters 121 8.3 More on Returns 124 Chapter Lab Exercise 127 Classes 130 9.1 Overview of Classes and Objects 130 9.2 Using “class” 132 9.3 Using Methods 136 9.4 Using Object Data 141 9.5 Inheritance 146 Chapter Lab Exercise 151 File Handling 154 10.1 Opening Files 154 10.2 Reading and Writing Text Files 157 10.3 Reading and Writing Binary Files 161 Chapter 10 Lab Exercise 162 Database Handling 166 11.1 Creating the Database 166 11.2 Inserting and Updating Records 170 11.3 Retrieving and Deleting Records 173 Chapter 11 Lab Exercise 177 Modules 180 12.1 Using Standard Libraries 180 12.2 Creating a Module 185 Chapter 12 Lab Exercise 188 Debugging 190 13.1 Dealing with Syntax Errors 190 13.2 Dealing with Errors at Runtime 196 Chapter 13 Lab Exercise 201 Answer Key: Python for Beginners .212 Appendix 221 viii Python for Beginners About the Author Alex Bowers’ programming career began when he learned PHP and MySQL for a small project that he wanted to complete Two years later, the project was finished and Alex had become a bona fide PHP and mySQL expert He was hooked—he just couldn’t get enough of coding! This was the beginning of a lifelong passion for programming Since then, Alex has dedicated his time to teaching others In 2009, he began teaching PHP and MySQL on YouTube under the name “TheTutSpace.” He has since moved on to teach jQuery, Javascript and HTML/CSS; manage a hugely successful forum, PHPacademy; and author and produce content for various other educational sources all over the web Alex’s primary expertise is in web development, but he has also dabbled in Android and iOS Alex’s style is very much compatible with LearnToProgram’s mission: to provide easy-to-access technical education for students worldwide For Alex, traditional classroom teaching is passé: his goal is to teach thousands of students he has never met from all countries and cultures ix

Ngày đăng: 13/04/2019, 01:34