ebook img

Introduction to Python Programming PDF

465 Pages·2019·14.11 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Introduction to Python Programming

Introduction to Python Programming Introduction to Python  Programming Gowrishankar S. Veena A. CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2019 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Printed on acid-free paper International Standard Book Number-13: 978-0-8153-9437-2 (Hardback) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www. copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging‑in‑Publication Data Names: S, Gowrishankar, author. | A, Veena author. Title: Introduction to Python programming / Gowrishankar S, Veena A. Description: Boca Raton : Taylor & Francis, a CRC title, part of the Taylor & Francis imprint, a member of the Taylor & Francis Group, the academic division of T&F Informa, plc, 2018. | Includes bibliographical references and index. Identifiers: LCCN 2018046894 | ISBN 9780815394372 (hardback : alk. paper) | ISBN 9781351013239 (ebook) Subjects: LCSH: Python (Computer program language) Classification: LCC QA76.73.P98 S2325 2018 | DDC 005.13/3--dc23 LC record available at https://lccn.loc.gov/2018046894 Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Dedicated to my wife Roopa K. M. and to my sister Ashwini S. Nath. —Dr. Gowrishankar S. I would love to dedicate this book to my parents, and my family for their love, support and encouragement. —Veena A. Contents Preface ...........................................................................................................................................xiii Acknowledgment .......................................................................................................................xvii Authors .........................................................................................................................................xix 1. Introduction .............................................................................................................................1 1.1 What Is a Program? ....................................................................................................1 1.2 Programming Languages ..........................................................................................2 1.2.1 Machine Language ......................................................................................2 1.2.2 Assembly Language ....................................................................................3 1.2.3 High-Level Language .................................................................................3 1.3 Software Development ...............................................................................................5 1.4 History of Python Programming Language ...........................................................7 1.5 Thrust Areas of Python..............................................................................................8 1.5.1 Academia ......................................................................................................9 1.5.2 Scientific Tools ...........................................................................................10 1.5.3 Machine Learning .....................................................................................10 1.5.4 Natural Language Processing .................................................................10 1.5.5 Data Analysis .............................................................................................10 1.5.6 Statistics ......................................................................................................11 1.5.7 Hypertext Transfer Protocol (HTTP) Library ........................................11 1.5.8 Database Connectors/ORM/NoSQL Connectors ................................11 1.5.9 Web Frameworks .......................................................................................11 1.5.10 Cloud Computing......................................................................................11 1.5.11 Python Distributions ................................................................................12 1.5.12 IDE Available .............................................................................................12 1.5.13 Community ................................................................................................12 1.5.14 Python Stack in Industry .........................................................................12 1.6 Installing Anaconda Python Distribution ............................................................13 1.7 Installing PyCharm IDE to Set Up a Python Development Environment .......16 1.8 Creating and Running Your First Python Project ................................................19 1.9 Installing and Using Jupyter Notebook.................................................................23 1.9.1 Starting Jupyter Notebook .......................................................................24 1.10 Open Source Software .............................................................................................27 1.10.1 Why Do People Prefer Using Open Source Software? ........................28 1.10.2 Doesn’t “Open Source” Just Mean Something Is Free of Charge? .....29 1.10.3 Open Source Licenses ...............................................................................29 1.11 Summary ....................................................................................................................32 Multiple Choice Questions ...................................................................................................32 Review Questions ..................................................................................................................34 2. Parts of Python Programming Language ........................................................................35 2.1 Identifiers ...................................................................................................................35 2.2 Keywords ...................................................................................................................36 vii viii Contents 2.3 Statements and Expressions ....................................................................................36 2.4 Variables .....................................................................................................................37 2.4.1 Legal Variable Names ...............................................................................37 2.4.2 Assigning Values to Variables .................................................................37 2.5 Operators ....................................................................................................................38 2.5.1 Arithmetic Operators ................................................................................39 2.5.2 Assignment Operators ..............................................................................40 2.5.3 Comparison Operators .............................................................................42 2.5.4 Logical Operators ......................................................................................43 2.5.5 Bitwise Operators ......................................................................................44 2.6 Precedence and Associativity .................................................................................47 2.7 Data Types ..................................................................................................................48 2.7.1 Numbers .....................................................................................................48 2.7.2 Boolean .......................................................................................................48 2.7.3 Strings .........................................................................................................48 2.7.4 None ............................................................................................................49 2.8 Indentation .................................................................................................................49 2.9 Comments ..................................................................................................................50 2.9.1 Single Line Comment ...............................................................................50 2.9.2 Multiline Comments .................................................................................50 2.10 Reading Input ............................................................................................................50 2.11 Print Output ...............................................................................................................51 2.11.1 str.format() Method ....................................................................................51 2.11.2 f-strings .......................................................................................................53 2.12 Type Conversions ......................................................................................................54 2.12.1 The int() Function ......................................................................................54 2.12.2 The float() Function....................................................................................55 2.12.3 The str() Function ......................................................................................55 2.12.4 The chr() Function .....................................................................................56 2.12.5 The complex() Function .............................................................................56 2.12.6 The ord() Function .....................................................................................57 2.12.7 The hex() Function .....................................................................................57 2.12.8 The oct() Function ......................................................................................57 2.13 The type() Function and Is Operator ......................................................................58 2.14 Dynamic and Strongly Typed Language ..............................................................58 2.15 Summary ....................................................................................................................59 Multiple Choice Questions ...................................................................................................60 Review Questions ..................................................................................................................65 3. Control Flow Statements .....................................................................................................67 3.1 The if Decision Control Flow Statement ................................................................68 3.2 The if…else Decision Control Flow Statement .......................................................69 3.3 The if…elif…else Decision Control Statement ........................................................71 3.4 Nested if Statement ...................................................................................................73 3.5 The while Loop ...........................................................................................................74 3.6 The for Loop ...............................................................................................................79 3.7 The continue and break Statements ..........................................................................81 Contents ix 3.8 Catching Exceptions Using try and except Statement ..........................................84 3.8.1 Syntax Errors ..............................................................................................84 3.8.2 Exceptions ..................................................................................................84 3.8.3 Exception Handling Using try…except…finally .....................................85 3.9 Summary ....................................................................................................................89 Multiple Choice Questions ...................................................................................................90 Review Questions ..................................................................................................................93 4. Functions ................................................................................................................................95 4.1 Built-In Functions .....................................................................................................95 4.2 Commonly Used Modules .......................................................................................97 4.3 Function Definition and Calling the Function .....................................................99 4.4 The return Statement and void Function ..............................................................103 4.5 Scope and Lifetime of Variables ...........................................................................106 4.6 Default Parameters .................................................................................................108 4.7 Keyword Arguments ..............................................................................................109 4.8 *args and **kwargs ..................................................................................................110 4.9 Command Line Arguments ..................................................................................112 4.10 Summary ..................................................................................................................113 Multiple Choice Questions .................................................................................................113 Review Questions ................................................................................................................117 5. Strings ...................................................................................................................................119 5.1 Creating and Storing Strings .................................................................................119 5.1.1 The str() Function ....................................................................................120 5.2 Basic String Operations ..........................................................................................120 5.2.1 String Comparison ..................................................................................122 5.2.2 Built-In Functions Used on Strings ......................................................122 5.3 Accessing Characters in String by Index Number .............................................123 5.4 String Slicing and Joining ......................................................................................124 5.4.1 Specifying Steps in Slice Operation ......................................................126 5.4.2 Joining Strings Using join() Method .....................................................127 5.4.3 Split Strings Using split() Method .........................................................127 5.4.4 Strings Are Immutable ...........................................................................128 5.4.5 String Traversing .....................................................................................128 5.5 String Methods ........................................................................................................131 5.6 Formatting Strings ..................................................................................................138 5.6.1 Format Specifiers .....................................................................................140 5.6.2 Escape Sequences ....................................................................................141 5.6.3 Raw Strings ..............................................................................................142 5.6.4 Unicodes ...................................................................................................142 5.7 Summary ..................................................................................................................143 Multiple Choice Questions .................................................................................................143 Review Questions ................................................................................................................146 6. Lists........................................................................................................................................149 6.1 Creating Lists ...........................................................................................................149 6.2 Basic List Operations ..............................................................................................151 6.2.1 The list() Function ....................................................................................151

Description:
This book presents an intuitive approach to the concepts of Python Programming for students. It is appropriate for courses generally known as “Introduction to Python Programming.” We have tried to write a book that assists students in discovering the power of Python programming. We have taken in
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.