Powershell Guide to Python a Comparative Approach to Learn Two Scripting Languages at Once

Powershell Guide to Python a Comparative Approach to Learn Two Scripting Languages at Once

PowerShell Guide to Python A Comparative Approach to Learn two Scripting Languages at once. Fast! Prateek Singh © 2018 - 2019 Prateek Singh Tweet This Book! Please help Prateek Singh by spreading the word about this book on Twitter! The suggested tweet for this book is: I just bought this amazing book: #PowerShell guide to #Python! The suggested hashtag for this book is #PS2PY. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: #PS2PY Contents Introduction .................................................. 1 About the Book ................................................ 2 Why did I write this book ........................................ 2 Who is the Target Audience ...................................... 2 Prerequisites ................................................ 3 Feedback .................................................. 3 Chapter 1 - The Basics ............................................ 4 Version ................................................... 4 Case Sensitivity .............................................. 5 Indentation ................................................ 7 Comments ................................................. 9 Variables .................................................. 10 Chapter 2 - Console Input/Output .................................... 14 Printing to Console ............................................ 14 Reading from Console .......................................... 19 Reading from Console Securely .................................... 20 Chapter 3 - Creating your First Script .................................. 23 Saving Commands as a Script File ................................... 23 Running the Script File ......................................... 26 Chapter 4 - Passing Command-Line Arguments ........................... 30 ‘sys.argv’ in Python ........................................... 30 ‘$args’ Automatic Variable in PowerShell .............................. 31 ‘Params()’ in PowerShell ........................................ 32 ‘argparse’ Module in Python ...................................... 34 Parameter Help .............................................. 40 Chapter 5 - Object Introspection ..................................... 43 Class, Object, Property, and Method ................................. 43 ‘Get-Member’ cmdlet in PowerShell ................................. 47 ‘type()’ Method in Python ....................................... 50 CONTENTS ‘dir()’ Method in Python ........................................ 50 ‘inspect’ Module in Python ....................................... 53 Chapter 6 - The Help System ........................................ 60 Get-Help cmdlet in PowerShell .................................... 60 ‘help()’ Method in Python ....................................... 62 Understanding How a Module Works ................................ 64 Chapter 7 - Modules ............................................. 69 Modular Programming ......................................... 69 Importing Modules ............................................ 69 Installing Modules ............................................ 73 Creating Modules ............................................. 74 Chapter 8 - Data Types ........................................... 76 Common Data types ........................................... 76 Checking the Data Type ......................................... 83 Data type casting ............................................. 84 Chapter 9 - String Manipulations and Formatting .......................... 88 Common String Operations ...................................... 88 String Formatting ............................................. 90 Substring or String Slicing ....................................... 92 Testing String Membership ....................................... 94 Built-in methods ............................................. 95 Chapter 10 - Date and Time ........................................ 104 Get Date and Time ............................................104 Convert String to datetime object ...................................111 DateTime formatting ...........................................112 Time Span or Time Delta ........................................113 Chapter 11 - File Handling ......................................... 116 Reading from a File ...........................................116 File Modes .................................................120 Create and write to a text File .....................................121 Appending data to the File .......................................123 Chapter 12 - Arrays, List, ArrayList and Tuples ........................... 125 Arrays ...................................................125 ArrayList (.Net) ..............................................127 Lists .....................................................128 Common PowerShell Array and Python Lists Operations . 129 Tuples ....................................................142 Sets .....................................................143 CONTENTS Chapter 13 - Dictionary and Hashtable ................................. 145 Hash Table .................................................145 Dictionary .................................................145 Chapter 14 - Conditional Statements .................................. 157 If Statement ................................................157 Else Statement ..............................................160 The elif and ifelse statement ......................................163 Nested Conditional Statements ....................................168 Chapter 15 - Loops .............................................. 171 For Loop ..................................................171 ForEach and ForeEach-Object .....................................174 While Loop ................................................176 Loop Control Statements ........................................177 Nested Loops ...............................................183 Chapter 16 - Functions ........................................... 186 Types of Functions ............................................186 Creating a Function ...........................................186 Calling a Function ............................................192 Functions vs Methods ..........................................193 Parameters and Arguments .......................................195 Mandatory and Default parameters ..................................195 The Anonymous Functions .......................................197 Chapter 17 - Handling XML and JSON Data .............................. 199 Difference between JSON and XML .................................199 Reading XML from a file ........................................200 Accessing XML as Objects .......................................203 Accessing XML with XPath ......................................206 Using XML for Object Serialization ..................................209 Parsing JSON ...............................................213 JSON Serialization and De-Serialization ...............................215 Chapter 18 - Reading and Writing CSV Files ............................. 220 What is CSV ................................................220 Reading CSV File .............................................220 Writing CSV File .............................................223 Chapter 19 - Error Handling ........................................ 227 Types of Errors ..............................................227 Raising an Exception or Throwing an error .............................232 Handling Exceptions using Try..catch..finally ............................234 CONTENTS Chapter 20 - Built-In Functions ...................................... 243 Basic Mathematical Operations ....................................243 Selection and Arrangement .......................................253 Union and Intersection .........................................256 Dynamic Execution of Code ......................................259 Introduction This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer period of time. “Use what you know to learn what you don’t.” also known as Associative learning. PowerShell has been my scripting language of choice because of multiple reasons which includes its intuitiveness, command chaining with pipelines, smaller code and overall robustness! Undoubtedly it has shaped my career for good. As a scripting language it is very powerful when it comes to get the job done, although I do not intend to say that it is always the best tool to use. I feel when we love something we develop an deep affinity towards it, which seeds a bias towards it; and it grows into a preference and limits our learning somewhere. Which is just not right and I think we should be open to options and ideas, more specifically scripting/programming languages. While PowerShell is still evolving in last 10 years since its inception, I see a lot of possibilities with more mature languages like Python. Python is one of the top programming languages and in fast changing IT scenarios to DevOps and Cloud to the future - Data Science, Artificial Intelligence (AI) and Machine Learning Python is a must know. But this PowerShell Scripting guide to Python would be very helpful

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    266 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us