Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals

Similar presentations


Presentation on theme: "Programming Fundamentals"— Presentation transcript:

1 Programming Fundamentals
dnav3-intro-python Setup: <Insert your name, title, presentation data, and complete the social slide at the end of the presentation. Then export the slides to PDF and share them with your audience in the event Webex Teams space BEFORE you start your presentation.> Giving the participants access to these reference slides before you start will enable participants to come along at varying paces, and will give them a reference to use throughout the event. Matthew Developer Advocate June 9, 2018

2 learninglabs.cisco.com/modules/intro-python
This is the module that we will be working in today.

3 Intro to Coding and APIs
Learning to code and work with programmable infrastructures will require an investment of your time. You start small and see initial and incremental returns on your investments. As you begin this journey, let's take a moment to understand the value of the investment you are making.

4 The Human Interaction Challenge
Cisco Live 2016 11/6/2018 The Human Interaction Challenge Software displays results in User Interface (UI) User asks for data or takes action by interacting with UI You have an infrastructure full of products; designed for use by you - a human. I know it may not always seem that way, but human operators are the target users of the command line interfaces and web interfaces that you work with. Which means that when you need to get something done via these interfaces, you (or some other human) has to do the work. You won't have to think back too far to remember the last time that you needed to complete some bulk-task on a computer. The task probably involved a lot of clicking, typing, copying-and-pasting, or other mind-numbing repetitions. These human interfaces (and the paradigm of having humans do the work) are to blame for the bulk-work that we sometimes have to do to complete a task. Computers are great at bulk-work, but if you want your laptop to talk to your infrastructure to do some piece of work for you, you are going to need a machine-to-machine interface or API (Application Programming Interface); an interface designed for computer interaction.

5 “It’s a way for two pieces of software to talk to each other”
Application Programming Interface (API)

6 The Value-Proposition for APIs
>>> do(“repetitious work…”) Done. request response OK! Request actions be performed Get information Store information We can ”ask” an API to: Take some action Give us some piece of information Store some piece of information We use these machine-to-machine APIs to make simple requests of our infrastructure, which in aggregate, enable us to complete powerful tasks. Using APIs to make simple requests like... Get the status for interface X Get the last-change time for interface X Shutdown interface X Set the description of interface X to "Interface disabled per Policy" ...enables you to complete a powerful task like: "Disable all ports that have been inactive for 30 days." Sure, you could do this manually, but wouldn't it be better to codify the process (write it once) and then let your computer run this task whenever you need it done? Modern APIs make it easy for you to make requests of your apps and infrastructure. If you need some information, ask for it. Want something done? Make the request. Using a machine-to-machine API means your request will complete, your data retrieved, or you will receive notification to the contrary - all done in a way that enables you to automate the interaction. Okay, APIs make it easy for me to make requests of my infrastructure, but what makes it easy for me to codify my processes?

7 The Value-Proposition for Programmability
Coding is the process of writing down instructions, in a language a computer can understand, to complete a specific task. Q: What task? A: Your task. for switch in my_network: for interface in switch: if interface.is_down() and interface.last_change() > thirty_days: interface.shutdown() interface.set_description("Interface disabled per Policy") Coding is the process of writing down instructions, in a language a computer can understand, to complete a specific task. Q: What task? A: Your task. For example, here is some sample code that could complete the task "Disable all ports that have been inactive for 30 days": You may not understand the language's syntax (yet - don't worry we'll teach you), but hopefully you can see the simple codified process that we are asking the computer to follow: For each switch in my network... For each interface in the switch... If the interface is down, and hasn't changed states in more than thirty days, then: Shutdown the interface. Update the interface's description. This is essentially the process that you as a human would go through to complete the same task. By taking the time to codify it (write it down in a machine interpretable language), you can now ask the computer to do the task whenever you need it done. You the human are providing the intelligence: what needs to be done and how it should be done, while letting the computer do the boring and repetitious work - which is what it does best - win-win. 🙌 That sounds great, but programming languages aren't as simple as what you showed above. While the code sample above is a snippet of a larger script, and is calling other functions (like interface.last_change() and interface.shutdown()), implementing the utility functions is straightforward and the code shown is actual valid Python code that would complete the task. The core logic is that simple.

8 What Changed? API & Language Maturity Online Communities RESTful APIs
Expressive Modern Languages Online Communities Open Source Social Code Sharing (GitHub) Public Package Repositories $ pip install requests Collecting requests Using cached <-- output omitted for brevity --> $ python >>> import requests >>> requests.get(" <Response [200]> Step 2. What Changed? APIs and programming languages aren't new, so, why the recent hype? They have matured! Modern Programming Languages & Tools Modern programming languages like JavaScript, Python, Go, Swift, and others are less cumbersome and more flexible than their predecessors. It used to be that you had to write 10,000 lines of C++ code to do anything useful, but with these modern languages (and packages and libraries available from their developer communities) you can do powerful things in less than 300 lines of code. Which is probably shorter, or on par with, most Cisco IOS configurations that you have worked with. These languages, when combined with other modern developer tools like: Git repositories Package management systems Virtual environments Integrated Development Environments (IDEs) Equip you with powerful development tools that enable you to automate your tasks and processes and begin creating your own set of powerful tools and workflows. While these tools are great, and are now bringing rich value to the systems engineering discipline, we are also benefiting from another maturing area of the software development industry... Online Communities In the past, when you set out to create some script or program, you often had to start "from scratch," working with low-level standard libraries included with your programming language and toolset of choice. This created a high barrier to entry (and massive global repetition) as software developers had to write the same "heavy lifting" modules to get common tasks done. Take for example making a HTTPS web request (they had to write code to): Open a TCP connection on port 443 Handle TLS negotiation and exchange certificates Validate the certificates Manage the TCP connection (and any connection pooling) Format HTTP requests Interpret HTTP responses That is a lot of work when all the developer wanted to do was get or send some data to or from some remote server; this is why we engineers left this work to the software developers. Now, thanks to: the Open Source community, social code-sharing and collaboration sites like GitHub, and public package repositories, the developer communities around these new modern programming languages are building and sharing Open Source software libraries that help to encourage reuse and reduce duplicate work. Leveraging these community-created libraries can save you tremendous amounts of time and effort, and they enable you to focus your time and effort on what you want your code to do - your codified process. You can make an HTTPS request, without much personal investment, because of the work done by these online communities (reference code sample). What you are seeing here: We installed a community library from a public package repository. pip install requests We entered a Python interactive shell. python We imported the library into our Python code. import requests We made an HTTPS request to requests.get(" Which was successful. <Response [200]> Starting with installing the requests package on our machine: in four typed lines in a terminal, we were able to download and install the package and use it to make an HTTPS request (without having to think about the steps involved with making the HTTPS request). Now that the languages and tools have evolved to be useful to infrastructure engineers, let's see how APIs have become easier to work with. API Maturity Gone are the days where it took an expert programmer to work with a product's API. Previous API standards like SOAP proved themselves to be not so "Simple," and easier to use API models like RESTful APIs have taken their place. Now thanks to RESTful APIs and standardized data formats like JSON (we'll cover these in more depth later), you can make requests of your infrastructure with the same ease these modern programming languages provide. Summary APIs and programming languages have evolved and matured to the point of being useful and applicable to the domains of infrastructure engineers. The net-effect being that you can get powerful things done with relatively small amounts of code; and by so doing, you can automate the repetitious and/or labor intensive parts of your job freeing you up to focus your time and effort on tasks deserving of your intellect. You can get powerful things done with relatively small amounts of code!

9 Why Python? Domain Applicability Established online DevOps Community
Power and Flexibility Create & Work With: Shell Scripts, Back-end Web APIs, Databases, Machine Learning, … Platform Flexibility Run Your Code: Laptop, Server, VM, Container, Cloud, Cisco IOS Device We Like It! We have: Laptop Stickers, T-Shirts, Social Profiles, and Emotional Connections to Our Code Why Python? We could have chosen one of several languages for this track, but we chose Python. Here's why: Domain Applicability - For network and IT infrastructure engineers, there is a well established online community of infrastructure engineers using Python to deploy and maintain IT systems. This means you can leverage (and contribute to) the work done by this collective community to save yourself (and others) time as we all strive to complete common tasks together. Yes, there are infrastructure engineers using other languages, and those languages are in no way inferior to Python. Python appears to have the broadest community (to date) for our domain. Power and Flexibility - As already discussed, Python is a modern language that brings coding into the level-of-effort range of being useful to infrastructure engineers. Python is a "batteries included" language with a rich and diverse developer ecosystem. This means that it will serve you well as you progress in your journey. If your work leads to you needing to: create a backend web service, interact with a data base, do some data analysis, work with machine learning algorithms, or create a simple (or powerful) command line tool, Python and its developer community have you covered. You shouldn't have to "start from scratch," and you should be able to get powerful tasks done with reasonable investments of effort. Platform Flexibility - Your Python scripts can run: on a computer (Windows, macOS, or Linux), on a server, in a VM, in a container, in the cloud, and on some Cisco IOS devices. This means you can use Python to create scripts that can run in a variety of places to fit the needs of your use case. Personal Bias - The proctors and presenters of this event are a little biased towards Python. We have stickers on our laptops, t-shirts, social profiles, and emotional connections to our code that make us a little partial. 😁 If you are still with us, you are ready to start learning to code!

10 A Brief Introduction to Git
Git is one of those developer tools that has enabled this modernization; and before we dive into Python, we want to give you a very brief introduction to the Git Version Control System.

11 The Need for Version Control
How do I make incremental changes and share my work with others? How do I go back to the version of this file from (yesterday, last week, last year, ...)? What changed between version X and version Y of a file? People have been making changes to the same file (or set of files)... How do I reconcile and merge all these changes? Whether you come from a coding background or not, chances are, you have run into the need for a Version Control System (VCS). Ever had any of these questions? How do I make incremental changes and share my work with others? How do I go back to the version of this file from (yesterday, last week, last year, ...)? What changed between version X and version Y of a file? People have been making changes to the same file (or set of files)... How do I reconcile and merge all these changes? If you have ever asked yourself any of these questions, you have a need for a Version Control System. Version Control Systems address problems like those described above and others that you probably haven't thought of yet (and we aren't including here!). They are powerful tools that help you share files, track changes to those files, and manage changes and contributions from authors and contributors. Today, we are going to use Git and GitHub to: Download a copy of the Sample Code you are going to work with to your laptop. Get you started with a basic Git workflow.

12 Git vs. GitHub Git Git is an open source (git-scm.com) Distributed Version Control System, while GitHub is a commercial company that runs GitHub.com, based on Git VCS tech. Git is the underlying VCS technology and utility that is tracking changes and providing the tools to work with the files under "version control." GitHub is the cloud service we have chosen to host our sample code repository. GitHub enables content authors and contributors with both a place to host their version controlled files and powerful collaboration tools that support the content development and maintenance processes. GitHub also enables sharing and distribution of your version controlled files including mechanisms and tools for formal release versioning, content documentation and even static web hosting for your project.

13 Basic Git Terminology Repository (Repo) - A vault for storing version controlled files Working Directory – The visible directory and its contents Versioned Files – Files you have asked Git to track Un-Versioned Files – Files in your working directory not tracked by Git Commit – Snapshot in time (of your version controlled files) Branches – A safe place for you to work Repository (Repo) - A repository is essentially what its name describes: a vault (or repository) for storing the version-controlled files and data. On your computer a Git repository will look like a regular folder or directory, with one important difference: the repository directory will have a hidden .git/ subdirectory. This subdirectory is where Git stores the committed version controlled files and other repository data. You don't have to worry about or work with this hidden subdirectory, but now you know where the magic ✨ is! Working Directory - This is what you see on your computer when you look in the repository directory on your computer - the visible directory and its contents - these are your version-controlled and un-versioned files and folders. Versioned Files - Files that you have asked git to track. Un-Versioned Files - Files in your working directory that you haven't asked git to track. Commit - A commit is a snapshot in time of your version controlled files. Once committed, this snapshot is (almost) indelibly locked into the repository - always available for future retrieval and comparison. Branches - Branches enable parallel work within a repository. We create new branches to split-off work done by different people, to experiment with changes we might want to back out later, or to develop new features. Git provides tools to help visualize, reconcile and merge together changes made in different branches.

14 Image Source: http://git-scm.com
A Peak Under the Hood Commits contain Trees Trees contain links to Files Git stores full copies of all Changed Files When you commit changes to version controlled files, Git stores full copies of all the changed files. It also stores a tree which contains links to all the changed files and previously-committed-unchanged-files in the current commit. Git computes a SHA1 hash of all stored files, trees and commits, and then uses the commit hashes to uniquely refer to individual commits. By computing and storing these hashes, git can detect changes to files and assure that the files retrieved from the repository are exactly as they were when committed to the repository. "It's impossible to get anything out of Git other than the exact bits you put in." git-scm.com - Data Assurance Image Source:

15 What All New Git Users Do
Image Source: xkcd.com

16 Useful Git Commands Setup Tell git who you are
one-time setup git config --global user.name “your name” git config --global user. Clone Clone (“download”) a git repository git clone url Status Check the Status of your local repository git status Checkout A Branch Create and Checkout a local Branch Creates a “safe place” for your changes git checkout –b new-branch-name Add Add a file to your next commit. git add filename Commit Commit your changes. git commit –m “Your commit message.” A File Checks-out a file from the last commit. Reverts any changes you have made, and restores the last committed version of a file. git checkout filename Commit Authors - Telling Git Who You Are (one-time setup) When you make a commit to a Git repository, Git automatically includes (in the indelibly hashed and stored commit) the name and address of the person that made the commit. You need to tell git who you are before you can commit any changes to a repository. Cloning a Repository When you want to work with someone else's code, you first have to get it on your machine. We do this with the git clone command. For example, when you were preparing your machine for this Python Fundamentals module, we instructed you to clone (download) our dnav3-codesample code repository to your machine with the following command: Note: You should have already cloned the sample code to your computer. Please don't do it more than once. Having multiple copies of the code samples on your computer can cause confusion as to which one you are working with. When you ran this command, the git client on your laptop connected to the URL provided and cloned a full copy of the dnav3-code repository to your machine. By the way, when we say "clone," we mean it. When you clone a repo you are indeed getting a complete copy of the repository and all of its contents. From the first commit to the last, you now have locally on your computer all of the files that have ever been committed to the repository. Don't believe me? Try running git log sometime (note if you do this in a repo with many commits, you can press space to page through the commits and q to quit). Now that you have our dnav3-code repo on your computer, let's get it ready for you to edit and commit your changes. Prepare the Repo for Your Changes (Creating a Branch) When you first clone a repository, you should be viewing and working on the "master" branch, which just by convention of naming is the default branch for a repository. You can verify what branch you are working on, and the current status of your working tree with the git status command. If you started editing files on the master branch and committing your changes, your local repository would immediately be out of sync with the remote server, which isn't a problem... until it is. If someone else commits some changes to the master branch, and pushes their changes to the server before you push yours... You will have to merge (reconcile) their changes with your own before you can push your changes to the server. In fact, you cannot even get updates from the server on this branch until you reconcile the discrepancy. Sigh. Wait... I thought this whole version control things was supposed to make collaborating on files easier!?! It does, but you need to create a branch! You use the git checkout command to switch between branches, and adding the -b option to the checkout command causes Git to create a new branch - and then switch to it. When you create a branch on your local repository, you are creating a safe place for you to make changes to the repo. Any changes you make and commit are committed locally to this new branch. Since you aren't making any commits to the branches that are synced with the remote server, any updates made to these other branches ("master" or otherwise) on the remote server, can easily be pulled down to your computer. Making a Commit Committing your changes to a repository is a two-step process: Add - Stage files to be added to the commit with git add. Commit - Commit the files to the repository with a commit message git commit. The -m option lets you supply a short commit message right there on the command line. Without the -m option, Git would open your environment's default text editor (which on many systems is vim) so that you can enter a detailed and potentially lengthy commit message. You should make it a personal practice to "commit often." Creating a commit is simple for you and a lightweight task for your computer. Committing often means that you are capturing more of the history of your code. You can go back and view commits and past changes anytime. Yeah! My code (or some portion of it) is working!! Commit. Reverting Changes in Your Working Directory Need: I made changes to a file, and now I want to revert those changes and get the original (last committed version) of the file back. Solution: You can "checkout" the last version of the file to overwrite the changes you have made and restore the file to its last committed version. Learn More: git --help and man git

17 DevNet Sample-Code Workflow
git clone Sample Code (mycode) git checkout def f(x): ... Edit git commit (master) git add Step Action Git Command 1. Clone the Remote Repository git clone url 2. Create and Checkout a Local Branch git checkout –b new-branch-name 3. Incrementally Commit Changes git add filename git commit -m “Commit message” Using these commands, you know know how to do a high-level git workflow. Anytime you need to: Download (clone) a local copy of a remote repository to your workstation. Create a "safe place" (branch) for you to make edits. Save (commit) your incremental changes. ...you now have a workflow to get that done.

18 Intro to Python | Part 1

19 Python Scripts Text Files (UTF-8)
May contain Unicode Some editors / terminals don’t support Unicode Use any Text Editor Using a Python-aware editor will make your life better No Need to Compile Them Python scripts are simple (UTF-8) text files. You could write or edit one with any text editor. You don't compile them. How are you going to execute them? Answer: with a Python interpreter!

20 Using a Python Interpreter

21 Know Thy Interpreter What interpreter are you using?
python python2 python3 python3.5 python3.6 other What version is it? $ python -V Where is it? $ where command Which interpreter are you using? The one installed on your developer workstation of course! (snark removed) Seriously though, you can have multiple interpreters on your workstation so it is important to know which interpreter you are using at any point in time. Outside of a virtual environment, your interpreter could be called by many names: python py -2 (windows only) py -3 (windows only) python2 python3 python3.5 python3.6 etc. Verify your Interpreter If there is ever any confusion about which interpreter you are using, the following commands will help you out: $ python -V Will give you the version info for the interpreter. $ which python Will give you the full path to the interpreter. Are you using the one that you expected? You should be using at least v3.5+ It should be the one in your virtual environment.

22 What is a Virtual Environment?
Directory Structure Usually associated with a Project An isolated environment for installing and working with Python Packages $ python3 -m venv venv $ $ tree -L 1 venv/ venv/ ├── bin ├── include ├── lib └── pyvenv.cfg $ source venv/bin/activate (venv) $ Python Virtual Environments Without virtual environments, when you pip install packages on your workstation, all of those packages (and their dependencies) get installed "globally." Over time this can clutter up your global Python environment, and eventually you run into version conflicts where Project A needs version X.X of a particular package and Project B need Y.Y. Virtual environments are an elegant way to uncluttered your global Python environment, and isolate projects from each other. At first, they might sound somewhat mysterious, but they are conceptually simple. When you create a virtual environment (like you did with the python -m venv environment-name command), the virtual environment module venv creates a new folder using the name you provide. Looking inside that folder, you will see something like the following: Now, when working inside an activated virtual environment and you pip install some library, where do you think that gets installed? That's right! ...in the /lib directory. What about if you pip install some executable script? Also correct! ...in the /bin directory. You see, a virtual environment is essentially redirecting where packages get installed and where Python looks to find them. Instead of installing packages in the global environment, they are instead installed in the virtual environment directory that you told venv to create. You can create these virtual environments in the same directory as your project files (though you usually exclude them from version control), and then everything stays nice and neat. When you go to work on a project, you can simply activate its virtual environment and you have all the packages you need to work on that project. When you want to switch from working on Project A to working on Project B, you deactivate Project A's virtual environment and then activate Project B’s. Note: Use the deactivate command to exit out of an active virtual environment. When you are done with a Project and perhaps delete it from your workstation, the virtual environment directory and the packages installed in are also deleted. #NoMoreClutter Also, no matter how many Python interpreters you have installed on your workstation - when you are working in your virtual environment, the python command will always refer to the pythoninterpreter located within the virtual environment - which is a symbolic link to the interpreter that you used to create the virtual environment. Result, as long as you are working in your virtual environment, the python command will point to the interpreter of your choosing (the one you used to create the environment).

23 Activating a Python Virtual Environment
Remember source environment-name/bin/activate The activation script will modify your prompt. Inside a virtual environment your interpreter will always be `python`. $ source venv/bin/activate (venv) $ (venv) $ deactivate $ Remember: You need to commit to memory how to "activate your virtual environment." You must have your virtual environment activated before "pip installing" any packages or running any scripts in this module. You will need to activate your virtual environment anytime you open a new Terminal; virtual environment activation isn't persisted when a Terminal session is closed. Reminder: To activate your virtual environment: Open your Terminal CD to the root directory of the dnav3-code repository you cloned to your computer - this is where you created your venv virtual environment Run the following command to activate your virtual environment: $ source venv/bin/activate You can always tell when you are working in an activated virtual environment. The activation script prefixes your command prompt with the name of your activated virtual environment (in parentheses). Inside a virtual environment, your interpreter will always go by the name python, which is much easier to remember and work with. Note: Use the deactivate command to exit out of an active virtual environment. Or simply close your Terminal – virtual environment activations are not persistent between terminal sessions. You must remember activate your virtual environment when you start working on a project.

24 PIP Installs Packages Included with Python v3+ Coupled with a Python installation; may be called pip3 outside a venv Uses the open PyPI Repository Python Package Index Installs packages and their dependencies You can post your packages to PyPI! (venv) $ pip install requests Collecting requests  Downloading <-- output omitted for brevity --> Installing collected packages: idna, certifi, chardet, urllib3, requests Successfully installed certifi chardet idna-2.6 requests urllib3-1.22 (venv) $

25 Using your Python Interpreter
How to… Command Access the Python Interactive Shell $ python Running a Python script $ python script.py Running a script in ‘Interactive’ mode Execute the script and then remain in the Interactive Shell $ python -i script.py Accessing the Python Interactive Shell Python comes equipped with a Read Evaluate Print Loop (REPL) (boring name) Interactive Shell! Which is awesome for playing with Python syntax, incrementally testing commands, playing with APIs and data, and so much more. To access Python's interactive shell, just call the interpreter without providing any options or arguments. Running a Script Python files (again just text files) by convention end with the extension .py. To run a Python file, just pass it to your interpreter like so: Run this on your workstation: $ python intro-python/part1/hello.py Note: You can also instruct the interpreter to run your script and then stay in the interactive shell. This can be useful to incrementally build or debug Python scripts. We'll talk more about this in Part 2.

26 Python’s Interactive Shell
Accepts all valid Python statements Use It To: Play with Python syntax Incrementally write Code Play with APIs and Data To Exit: Ctrl + D or exit() (venv) $ python Python (default, Apr , 15:31:03)[GCC (Red Hat )] on linuxType "help", "copyright", "credits" or "license" for more information. >>> Do this on your workstation: $ python You can enter any valid Python statement, and the interactive shell will execute it and display it's output - if it has any output. We'll use this quite a bit as we explore some of Python's basic syntax and data types. For now, just remember that you always have this tool at your fingertips anytime you want to play with or test out some Python syntax. Note: To exit the interactive shell, press  Ctrl-D or call the exit() function.

27 Basic Python Syntax Now that you know how to use your interpreter, let's dive-in and start learning Python. In all of the sections that we go through, please take a minute to experiment with the Python syntax in your interactive shell. You aren't going to break anything (probably), and getting your hands on the syntax is the best way to learn it and get comfortable with it. Don't feel like you have to type in any of our examples verbatim - try your own. See what works and what doesn't - you'll learn from both.

28 Basic Data Types Python type() Values (examples) int -128, 0, 42 float
-1.12, 0, bool True, False str “Hello 😎” Can use ‘’, “”, and “””””” bytes b”Hello \xf0\x9f\x98\x8e” >>> type(3) <class ‘int’> >>> type(1.4) <class ‘float’> >>> type(True) <class ’bool’> >>> type("Hello") <class ’str’> >>> type(b"Hello") <class ‘bytes’> Note: These are best introduced by briefly using the Python interactive shell and demoing using the built-in type() function. As you would expect, Python has a basic data type to store any value you need. Python is introspective. You can see what type of data something is with the type()function. Can't remember what Python's string data type is called? Just ask it, type("What is this?") - it will tell you.

29 Numerical Operators Math Operations Addition: + Subtraction: -
Multiplication: * Division: / Floor Division: // Modulo: % Power: ** >>> 5 + 2 7 >>> 9 * 12 108 >>> 13 / 4 3.25 >>> 13 // 4 3 >>> 13 % 4 1 >>> 2 ** 10 1024 It also has a full set of operators to work with these numeric and string types. Try playing with some of these in your Interactive Interpreter. You could use the Python interactive shell as a powerful calculator. Python is used by mathematicians and scientists, and is capable of doing high-precision and complex mathematical operations (or you could use it to calculate the tip for your pizza order). Want some geek fun? Try this one in your interactive shell: >>> 1024 ** 15700 It might take your workstation a few seconds to calculate that one, but it will get it right. Feel free to check it by hand if you don't trust it yet.

30 Variables Names Created with the = assignment operator
Cannot start with a number [0-9] Cannot conflict with a language keyword Can contain: [A-Za-z0-9_-] Recommendations for naming (variables, classes, functions, etc.) can be found in PEP8 Created with the = assignment operator Can see list of variables in the current scope with dir() >>> b = 7 >>> c = 3 >>> a = b + c >>> a 10 >>> string_one = "Foo" >>> string_two = "Bar" >>> new_string = string_one + string_two >>> new_string 'FooBar' Defining Variables Python is not a strongly typed language. That means, unlike some other languages, you don't have to declare a variable before using it. Also, a variable name could point to one type of data one minute and then point to a different type of data another. To define a variable in Python, simply use the assign = operator to assign a value to the variable name. Note: In the interactive interpreter, typing a variable name at the prompt and pressing Enter displays the current value of the variable. Also note that while this is a valid Python statement, if you only put a variable name on a line in your script, it won't display the output to the user. You have to use the print() function in a script to display output to the user (you learn how to do this shortly). Variable Names Cannot start with a number [0-9] Cannot conflict with a language keyword Can contain: [A-Za-z0-9_-] Recommendations for naming (variables, classes, functions, etc.) can be found in PEP8.

31 In Python, Everything is an Object!
Use . (dot) syntax to access “things” inside an object. Terminology When contained inside an object, we call… Variable  Attribute Function  Method >>> a = 57 >>> a.bit_length() 6 >>> "WhO wRoTe THIs?".lower() 'who wrote this?' Everything is an Object! In Python everything is an object. What does this mean? Unlike some languages that have different types of data (value types, structures, classes and objects, etc.), in Python everything is an object. What are objects, and what does this mean for me? I'm glad you asked. Objects (over simplified explanation) are purpose-built groupings of variables (called attributes) and functions (called methods) that work together to do something useful. Creating your own classes of objects in Python is easy, but due to time constraints and the fact that you won't need to create your own classes to complete any of our labs today, we aren't going to cover classes here. However, you do need to know how to work with objects - especially since "everything is an object". Fortunately doing so is also straightforward, and you have already seen this in action in the when we looked at how to work with strings. To access the attributes (variables) or methods (functions) contained within an object, all you need to remember is that you use "." dot-syntax to do so. Putting a period (dot) after any object lets you access the attributes and methods available within the object. Examples – See the right panel Note: Python is introspective. To look inside an object and see what names (attributes and methods) are defined inside it, just pass the object to the dir(object) function. Check an object’s type with type(object) Look inside an object with dir(object)

32 Working with Strings String Operations Some Useful String Methods
Concatenation: + Multiplication: * Some Useful String Methods Composition: “{}”.format() Splitting: “”.split() Joining: “”.join() >>> "One" + "Two" 'OneTwo' >>> "Abc" * 3 'AbcAbcAbc' >>> "Hi, my name is {}!".format("Chris") 'Hi, my name is Chris!' >>> "a b c".split(" ") ['a’, 'b’, 'c'] >>> ",".join(['a’, 'b’, 'c']) 'a,b,c' Strings In Python v3 all strings (str) are Unicode strings and therefore can store and work with multi-byte Unicode characters. Note that not all Terminals or platforms support these extended character sets, so use them at your own risk, but Python supports them - hopefully everything else will catch up. You can use any of the following to open and close a string: Single Quotes ' ' Double Quotes " " Triple Single Quotes ''' ''' Triple Double Quotes """ """ Why all the options? The ability to use single or double quotes as string delineators makes it easier for you to create strings that may contain quotation marks. For example, if you want to include double quotes inside your string, open and close the string with single quotes and vice versa. Without the option to use the other quotation mark style to open and close the string, you would have to \ escape all the quotation marks inside the string. What about the triple quotes? These let your string break across lines, and cause the string to include \n newline characters everywhere where there are line breaks in the multiline string - essentially you can write multiline strings just by using the triple opening and closing quotes. Note: A triple quoted string at the beginning of a Python file, function or class has special meaning. These are called docstrings, and they are a great way to document what the Python file, function or class does and how it should be used. String Operators Working with strings in Python is easy. Want to put two strings together (concatenate)? Just add them. Note: This doesn't insert any space between the strings. It simply joins the two strings together into a new string. Weird enough, you can multiply strings. This is actually useful when, for example, you want to repeat a character a certain number of times. There are also some powerful "convenience" methods available with string objects (more on objects later): "{}".format() - The .format() method lets you insert named or unnamed placeholders {}in a string and then use the .format() method to insert values into those placeholders. "".split() - The .split() method lets you split a string into multiple strings using a separator that you provide as the delimiter. "".join() - The .join() method lets you put a sequence of strings together using, joining them with a separator that you provide. Examples – See the right panel

33 Basic I/O Get Input with input() Display Output with print()
Pass it a prompt string It will return the user’s input as a string You can convert the returned string to the data type you need int(), float(), etc. Display Output with print() Can pass multiple values It will concatenate those values with separators in between (default = spaces) It will add (by default) a newline (‘\n’) to the end >>> print(‘a’, ‘b’, ‘c’) a b c >>> i = input(“Enter a Number: ”) Enter a Number: 1 >>> int(i) 1

34 Conditionals Syntax: Indentation is important!
if expression1: statements… elif expression2: else: Indentation is important! 4 spaces indent recommended You can nest if statements Comparison Operators: Less than < Greater than > Less than or equal to <= Greater than or equal to >= Equal == Not Equal != Contains element in Combine expressions with: and, or Negate with: not Conditionals With variables comes the possibility (likelihood) that you may not know what they point to, or perhaps you want to branch your code and take different paths based on some condition. That's why we have if statements. Python if Syntax Indentation is important! In Python, indentation is syntactically significant. Python uses indentation to identify the block of statements that are associated with a preceding statement. By PEP8 convention, indentation should be four (4) spaces. Most "Python aware" text editors and IDEs will automatically insert four spaces when you press the Tab key. The above if syntax reads: If expression1 is True, do the following block of indented statements Else if expression2 is True, do the following block of indented statements Else do the following block of indented statements Note: In this syntax, the expressions can be any Python expression that evaluates to a True or False value (see Python Truth Value Testing for more details). A conditional need not have all of these elements. You could have a single if statement, with some set of statements to run if it evaluates to True. You could have many else-if elifstatements. You may only have one default or catch-all else statement. Note: Python evaluates the if and elif statements starting with the first and proceeding to the last. If any of expressions evaluate to True, it runs the block of statements associated with that if/elif clause and then skips the rest of the clauses and continues with the next statement after the if-block. You can combine expressions with and or or. Negate an expression with not.

35 Conditionals | Examples
>>> b = 5 >>> if b < 0: print("b is less than zero") ... elif b == 0: print("b is exactly zero") ... elif b > 0: print("b is greater than zero") ... else: print("b is something else") ... b is greater than zero >>> words = "Foo Bar" >>> if "Bar" in words: print("words contains 'Bar'") ... elif "Foo” in words: print("words contains 'Foo'") ... words contains 'Bar'

36 Functions | Don’t Repeat Yourself
Modularize your code Defining your own Functions (optionally) Receive arguments (optionally) Return a value Syntax: def function_name(arg_names): statements… return value ... function_name(arg_values) >>> def add(num1, num2): result = num1 + num2 return result ... >>> >>> add(3, 5) 8 >>> def say_hello(): print("Hello!") >>> say_hello() Hello! "Don't Repeat Yourself (DRY)” Find yourself writing blocks of identical or nearly identical code in several places throughout your script (or in several of your scripts)? It's time to create a function. Functions let you write a piece of code once, give it a name, and then call that piece of code whenever you need it. They can (optionally) accept input arguments and return an output allowing you to create operations that take some input(s) and return some output according to your needs. Python Function Syntax: def function_name(arg_1, arg_2):statements...return valueAgain indentation is important! You use the def keyword to define a function, and you must follow the same rules when naming functions as you do when naming variables: Cannot start with a number [0-9] Cannot conflict with a language keyword Can contain: [A-Za-z0-9_-] Recommendations for naming (variables, classes, functions, etc.) can be found in PEP8. When defining a function, arguments are variable names that you create. Lateron when you call your function, you will pass in values for each of the arguments and these values will be assigned to the variable names you defined. These variables can be used within the body of your function to do as you need to carry out the purpose of your function. Your function may optionally return some value. Example: def add(num1, num2):...result = num1 + num2...return result...>>>>>>add(3, 5)8Note: Defining a function (def) only "creates" it for later use. Your function's code doesn't actually run until you call it (see the add() example above). No addition was performed until we called the add function and supplied values for its arguments with the add(3, 5)statement. You typically define a function once and then call it many times; passing in arguments and getting outputs each time. A function doesn't have to have any arguments or return any values. You could simply be calling a function to do some predefined set of actions. >>>def say_hello():...print("Hello!")>>>>>>say_hello()Hello!Note: A function implicitly returns None if it reaches the end of the function's block of statements. None is a special Python singleton value. It essentially means "no value." Noneis the closest thing Python has to a null value that is present in some other languages. Next: Hands-On Exercise

37 Intro to Python – Part 1 Exercise Estimated Time: 15 minutes
Practice what we have learned: Inspecting Variable Types Writing Conditionals (if-statements) Writing Functions Calling a Function There are multiple ways you could complete these items – any functional way works! Use the interactive shell if needed to test out some syntax. This hands-on exercise contains a few small coding challenges that will provide you with the opportunity to use the basic syntax that we have covered here in Part 1. Please complete each of the TODO items, and ensure your script runs successfully. This script uses a couple of random variables. Try running your script multiple times to see the changes in your scripts flow (and output) in response to the random values assigned to the variables. Exercise Open intro-python/part1/hands_on_exercise.py in your editor. Complete each of the TODO tasks. Run the script in your Terminal. Solution

38 Intro to Python | Part 2 Community developed Python takes a "batteries included" approach to the language and its included Standard Library. This means that many of the things that you might want to do are probably already included in the language's native syntax, data types or libraries. This can not only save you time, but help you use and leverage higher quality and "battle hardened" code and functionality that has been created by the Python community. You have already seen some examples of this. Like the string's native .format(), .split(), and .join() methods and there are more where those came from. In 'Part 2' here, we will take a look at some of the "batteries included" collection data types included in Python, and powerfully simple loops and tools that we can use to work with these collections.

39 Python Collections & Loops

40 Data Structures / Collection Data Types
Name type() Notes Example list Ordered list of items Items can be different data types Can contain duplicate items Mutable (can be changed after created) [‘a’, 1, 18.2] tuple Just like a list; except: Immutable (cannot be changed) (‘a’, 1, 18.2) dictionary dict Unordered key-value pairs Keys are unique; must be immutable Keys don’t have to be the same data type Values may be any data type {“apples”: 5, “pears”: 2, “oranges”: 9} Python comes fully equipped with a full set of native data structures. We are going to take a look at the three most commonly used data structures: lists, tuples, and dictionaries. As you work through this step, open your Python interactive interpreter and try creating each of the data types. Try accessing and updating individual elements of the collections. As you move on to working with more complex programs and APIs (and the data they return), it is essential that you be comfortable with these basic concepts. Lists A list is an ordered list of items. Items can be different data types. You may have duplicates of an item in a list. Lists are immutable, meaning they can be changed after they have been created. You can update the elements of a list. You can append new items to a list. You can remove items from a list. You can change the order of items in a list. In short you can change them as needed. Tuples Tuples are essentially just list lists, with one major exception: they are immutable, meaning they cannot be changed after they have been created. They are “frozen” as they were initially created. Dictionaries Dictionaries store unordered key-value pairs. They are mutable, so you can add and remove items as needed. You may at any time store a value for a key, and then later retrieve that value by providing the key. Dictionary keys must be an immutable data type, but values can be any data type. As an FYI, all of the python basic data types that we have looked at (int, float, bool, str, and etc.) are immutable and may be used as dictionary keys. While it may at times (python v3.6+) seem like Python is maintaining the order that items were added to a dictionary… It isn’t. Regular `dict` dictionaries are not guaranteed to maintain the order of items added to a dictionary. If you need to maintain the order, use an OrderedDict (more on these in a bit).

41 Working with Collections
Name type() Creating Accessing Indexing Updating list l = [‘a’, 1, 18.2] >>> l[2] 18.2 >>> l[2] = 20.4 >>> l [‘a’, 1, 20.4] tuple t = (‘a’, 1, 18.2) >>> t[0] ‘a’ You cannot update tuples after they have been created. dict d = {“apples”: 5, “pears”: 2, “oranges”: 9} >>> d[“pears”] 2 >>> d[“pears”] = 6 >>> d {“apples”: 5, “pears”: 6, “oranges”: 9} Creating Lists – You create lists by enclosing the list of items within square braces `[ ]` and separating the items with commas `,`. Tuples – You create tuples by enclosing the items within parentheses `( )` and separating the items with commas `,`. Dictionaries – You create dictionaries by enclosing the items within curly braces `{ }`, separating the keys and values with colons `:`, and separating the key-value pairs with commas `,`. Accessing Elements in Collections (Indexing) You access the elements within a collection using the square braces `[ ]`. We call this indexing. Lists and Tuples, are ordered sequences, and like all good programming languages indexes start with zero `0`. So, the first element in a list is at index `0`, the next element is at index `1`, and so forth. To access an element in a list you simply provide the numerical index for the element you want in square braces. Dictionaries are indexed by their keys. To access an element within a dictionary, simply provide the key in square braces `[ ]` for the value you want to retrieve from the dictionary. Updating Elements in a Collection To update elements in a collection (change their values), assign the new value to the collection’s index that you want to change. Note, again, tuples are immutable and cannot be updated after they have been created.

42 Dictionary Methods Some useful dictionary methods: {}.items()
{}.keys() {}.values() There are many more! 😎 >>> d = {"a": 1, "b": 2, "c": 3} >>> d.items() dict_items([('a’,1), ('b’,2), ('c',3)]) >>> d.keys() dict_keys(['a’, 'b’, 'c’]) >>> d.values() dict_values([1, 2, 3]) When working with dictionaries, there are at least a few things that you are very likely to want to do: get a list of all of the items (key-value pairs) in a dictionary, or get a list of all the keys, or get a list of all the values… Fortunately, Python has you covered. Just use the items(), keys(), and values() methods to get what you need. Oh… and there are more where those came from.  Since Python is a “batteries included” language, much of the functionality you are looking for is built-in to the language and its standard library.

43 Other “Batteries Included” Collections
Description namedtuple() factory function for creating tuple subclasses with named fields deque list-like container with fast appends and pops on either end ChainMap dict-like class for creating a single view of multiple mappings Counter dict subclass for counting hashable objects OrderedDict dict subclass that remembers the order entries were added defaultdict dict subclass that calls a factory function to supply missing values UserDict wrapper around dictionary objects for easier dict subclassing UserList wrapper around list objects for easier list subclassing UserString wrapper around string objects for easier string subclassing Learn docs.python.org What if you need something more? Python may you have covered… There are a number of additional collections included in the Python language and Standard Library. To keep this content as concise and focused as possible, we didn’t cover all of the language’s built-in data structures. We hit the most common three (lists, tuples, and dictionaries), but there are other powerful data structures (like sets) built-in to the language and the standard library. For example, these are some additional collections that are available in the `collections` module in the Python Standard Library. These are installed and available on every Python installation (of a recent enough version). The good news here is that these are all at your fingertips when and where you need them. Remember: Google is your friend! When you find yourself needing a tool to solve a particular problem, google it. Chances are, between the Python docs and StackOverflow, you’ll see something that either does exactly what you need or gets you well on your way to solving the problem. …and when it comes to working with Collections, you already have the basic skills you need to work with data no matter what data type you are working with.

44 OrderedDict Collection
>>> from collections import OrderedDict >>> od = OrderedDict() >>> od["apples"] = 5 >>> od["pears"] = 2 >>> od["oranges"] = 9 >>> >>> od["pears"] 2 >>> od["bananas"] = 12 >>> od OrderedDict([('apples',5), ('pears',2), ('oranges',9), ('bananas',12)]) Example: OrderedDict Collection Ordered Dictionaries work almost exactly like the native dict data structures, with one enhancement: they maintain the order of the items added to them. Since this collection isn't a native data structure, but one that is included in the Standard Library (meaning it should be installed and available on every computer that has a recent enough version of Python installed), we do have to import it into our script to enable us to use it - we'll learn more about imports shortly. Check out this example of using an OrderedDict. This is doing the exact same thing we did with the dict example in the previous step. This OrderedDict worked just like the dict data structure with the following differences: We did have to import it before we could use it. We added the key-value pairs to it incrementally, to demonstrate how it maintains the order in which they were added. When you look at the contents of the OrderedDict it looks a little different OrderedDict(<list of tuples>). Other than these differences, they work exactly the same.

45 Loops Iterative Loops Conditional Loops
for individual_item in iterator: statements… Conditional Loops while logical_expression: statements… >>> names = ["chris", "iftach", "jay"] >>> for name in names: print(name) ... chris iftach jay >>> i = 0 >>> while i < 5: print(i) i += 1 ... 1 2 3 4 Iterative Loops A for loop iterates through a sequence or collection, essentially taking one item at a time (assigning it to the variable name that you provide) and running the block of statements until all of the items have been iterated through (or you manually break out of the loop from within the code block). Python for Loop Syntax In this syntax individual_item is just a variable name of your choosing, and an iterator is something that can be iterated. Many things can be iterated, but for our purposes you should know that lists, tuples, and dictionaries can all be iterated. Choosing Variable Names You can make your code readable by choosing your variable names wisely: By naming my list of names "names" (plural) and then choosing "name" (singular) as my loop variable, this code reads as follows: "Create a list of names. For each name in the names-list, print the name," and that is just what it does. Conditional Loops Conditional loops (while loops in Python) evaluate an expression before each iteration of the loop. If the expression evaluates to True, the loop statements are executed. If the expression evaluates to False, the loop statements are not executed and the script continues with the first line after the loop block. Python while Loop Syntax In this syntax, expression can be any Python expression that evaluates to a True or False value (see Python Truth Value Testing for more details). Note: You can use Ctrl-C to break of infinite loops or hung scripts.

46 Unpacking Q: What if you wanted to break out a collection to separate variables? A: Unpack them! >>> a, b, c = [1, 2, 3] >>> a 1 >>> b 2 >>> c 3 Python has a feature where you can assign a collection of items to a set of variables - this is called unpacking. We’ll use this a little later in combination with a a for loop to help us iterate through a dictionary. What’s a for loop? I’m glad you asked…

47 Iterating through a Dictionary
Use the dictionary .items() method, which returns a “list of tuples” Unpack each tuple into variable names of your choosing to use within your block of statements Method returns dictionary items as a list of (key, value) tuples, which the for loop will iteratively unpack into your variable names. >>> for fruit, quantity in fruit.items(): print("You have {} {}.".format(quantity, fruit)) ... You have 5 apples. You have 2 pears. You have 9 oranges. Here is the trick to iterate through a dictionary of key-value pairs: Iterate the dictionary items() method, which returns a list of (key, value) tuples. Use unpacking to unpack the tuple’s (key, value) pair into variable names of your choosing. This code reads as follows: “For each fruit (key), quantity (value) pair in the fruit.items() list, print this string inserting the current values of fruit and quantity into the placeholders.”

48 Python Script Structure and Execution
When you call the Python interpreter and tell it to run a script, the interpreter does so "synchronously," meaning that it starts at the top of your script and goes line by line executing each statement one at a time. If a particular statement takes a while to compute or is waiting on a response from some external source (like making an API call and waiting on the response), Python stops and waits for the current statement to finish executing before moving on to the next one. This makes understanding the flow of your script and how to troubleshoot it much easier as everything is predictably and sequentially loaded and executed. This sequential execution also brings with it some considerations, like the fact that you must define a function before you can use it. Considerations like these have given rise to common patterns for structuring Python files.

49 Importing and Using Packages & Modules
Import “other people’s” code into your script. Syntax: import module from module import thing Tons of Packages: Python Standard Library Python Package Index GitHub >>> import requests >>> requests.get(' <Response [200]> >>> response = requests.get(' >>> response.status_code 200 At the top of most Python files you will a set of import statements. You use import statements to bring in code from other Python modules and packages into your script. Q: Since everything in Python is an object, yes even imported modules, how do you think you access the functions, classes, and variables inside an imported module? A: Using dot-syntax `.` of course! The from module import thing syntax provides a way for us to pull in only the functionality we need, and simplify the names of deeply nested resources.

50 Variable Scope Open in Your Editor: Code Review:
intro-python/part2/variable_scope.py Code Review: Module-scoped “Global” Variables Argument Variables Local Variables #!/usr/bin/env python """Demonstrate module vs. locally scoped variables.""" # Create a module variable module_variable = "I am a module variable." # Define a function that expects to receive a value for an argument variable def my_function(argument_variable): """Showing how module, argument, and local variables are used.""" # Create a local variable local_variable="I am a local variable." print(module_variable, "...and I can be accessed inside a function.") print(argument_variable, "...and I can be passed to a function.") print(local_variable, "...and I can ONLY be accessed inside a function.") # Call the function; supplying the value for the argument variable my_function(argument_variable="I am a argument variable.") # Let's try accessing that local variable here at module scope print("\nTrying to access local_variable outside of its function...") try: print(local_variable) except NameError as error: print(error) Variable Scope A quick note on variable scoping. Variables are said to be defined in the scope in which they are created. Module Scope Variables created outside of any function or class, are defined at "module-scope" and can be accessed by every function and class created within that module. Local Scope Variables created inside of a function or class, are defined only within the "local scope" in which they have been created. They may only be accessed by statements executing within the local scope in which they were created. Note: Function arguments are locally scoped variables. Variable Scoping Example Review the following code example, which illustrates variables being created at local and module-level scopes. Note the Following: The module_variable is created outside of any function or class. The local_variable is created inside a function. The argument_variable's name is defined as part of the function definition, but its value isn't assigned until the function is called. Now, run this script on your developer workstation to see the output: $ python intro-python/part2/variable_scope.py

51 Python Script Structure and Execution
Open in Your Editor: intro-python/part3/structure.py Code Review: Structure Flow Execution #!/usr/bin/env python # """Module docstring.""" # Imports import os import sys # Module Constants START_MESSAGE = "CLI Inspection Script" # Module "Global" Variables location = os.path.abspath(__file__) # Module Functions and Classes def main(*args): """ My main script function. Displays the full patch to this script, and a list of the arguments passed to the script. """ print(START_MESSAGE) print("Script Location:", location) print("Arguments Passed:", args) # Check to see if this file is the "__main__" script being executed if __name__ == '__main__’: _, *script_args = sys.argv main(*script_args) Now that we are familiar with import statements, and combining all the knowledge that we have gained thus far, let’s take a look at the structure, flow, and execution of a Python script. Note: A Python file may be referred to by a couple of different names. It may be called a script (usually denoting that it is intended to be executed), or it may be called a module(denoting that its contents are meant to be imported and used by another calling script). Sample Script Take a look at the following sample script (intro-python/part2/structure.py in the sample code repository), then let's take a look at its structure and the Python interpreter's execution process running this script. Structure and Execution When we run this script with from the Terminal, the Python interpreter will start with the first line and execute each statement in succession. Flow: The ["shebang"]( Line_ - The first statement in many executable Python scripts isn't meant for the Python interpreter. This line tells the shell attempting to run the script what interpreter should be used to "execute" the script. The Python interpreter sees this line as a comment and ignores it. The Module Docstring - The triple-quoted string at the beginning of this Python script is a module docstring, which serves as a built-in mechanism for documenting the purpose-of and the functionality-provided-by the module. The interpreter will save this string as a special __doc__ variable for the module. Import Statements - Import statements import other code into your script so that you can use their functionality. When the interpreter encounters an import statement, it opens the module being imported and (starting with the first line of that module) executes each of the statements in that file. The contents of that module are then available to your script through the module's name, using dot-syntax, to access the variables, functions, and classes within the module. Module "Constants" - Module constants, named by convention using all-CAPS variable names, are simple variable definitions. Nothing in the Python language makes these "constant." Their values can be changed. As a community we recognize an all-CAPS variable name as something that we probably shouldn't change. The interpreter creates variables with these names and values. Module-level "Global" Variables - Every function and class within the module will have at least "read access" to these variables as they exist at the top-level "global" scope within a module. The interpreter creates variables with these names and values. Module Functions and Classes - As the interpreter encounters new function or class definitions, it creates and stores them in memory to make them available to subsequent portions of your code. Note that the statements within a function (or class) definition aren't "executed" when they are defined. They are "loaded" and made available for future use in your script. You must call a function (supplying any needed arguments) to execute its block of statements. The if __name__ == '__main__' Block - Some Python files could serve as both a script (to be executed) and a module (which could be imported). When a Python script is executed, it is given the internal __name__ of __main__ by the Python interpreter. All other modules, when they are imported by some calling script, will see their __name__ as their own module name. This allows us to use the __name__ variable to determine (at run time) if our Python file is being executed or imported, and we can use if-clauses like this to take actions, execute functions, and etc. if our file is the one being executed. This allows us to parse any command line parameters, initialize any global variables, etc., and then call our script's "main" function. Note: You can call your "main" function whatever you like. There is no special significance of the function name main(). Order Why use this order for structuring a Python file? The linear execution order of statements within a Python file drives this ordering convention. Modules have to be imported before you can use them. Your script must create module-level constants and variables before using them. Functions and classes have to be defined before they can be referenced and used. ...and so on. This linearity creates a chain of prerequisites that dictates the order in which things must be created.

52 Debugging Basics Add print() statements Comment and uncomment them to “enable and disable your debugging” Understand how to read a Python Stake Trace Last Line First Top to Bottom Stop when you reach someone else’s code Run a script and then stay in the Python Interactive Shell Use the python –i option If you are human, you are going to make mistakes (and discover the mistakes made by other humans). When things go wrong, the Python interpreter actually provides pretty useful information about what happened - if you know how to read what it is telling you. How to Read a Stack Trace When an error occurs while running a Python script (and isn't handled or resolved by the script), it will be raised or "bubbled-up" and displayed to the user in the form of a "stack trace". A stack trace shows the calling "stack" of statements all the way from the top-level script that is being executed down to the statement that has produced the error. We’ll look at one of these in a moment. For now, I want you to remember this pattern of how to read one: Last Line First Top to Bottom Stop when you reach someone else’s code Reading a stack trace will help you locate approximately where (in your code) something is going wrong, and it should give you some idea as to what has gone wrong. Using print() Statements Then you can use print() statements to further narrow down where the issue lies, and to enable you to look inside your variables and program flow. Feel free to add them wherever you like to have your script print messages letting you know where it is at in the execution of your script and to print the contents of variables at key points to let you see what is going on while your script is running. Note: You can always comment and uncomment your print() statements to disable or enable your “debugging.” Python Interactive Shell Finally, you can use the Python interactive shell to interactively inspect what your code is doing. Passing the –i option to the interpreter when you run your script, will cause the interpreter to execute the script and then remain in the Python interactive shell – with all of your module variables and defined functions loaded and at your fingertips to play with. You can use the shell to interactively inspect module-level variables, make function calls, and test modifications to your code. Once you have figured out what the problem is, you can go back to your editor and make the needed changes to your script.

53 Reading a Python Stack Trace
fortune_cookie.py main() create_fortune_cookie_message() $ python intro-python/part2/fortune_cookie.py Get your fortune cookie! How many lucky numbers would you like? 5 Traceback (most recent call last): File "intro-python/part2/fortune_cookie.py", line 56, in <module> main() File "intro-python/part2/fortune_cookie.py", line 50, in main fortune_cookie_message = create_fortune_cookie_message(qty_lucky_numbers) File "intro-python/part2/fortune_cookie.py", line 38, in create_fortune_cookie_message raise NotImplementedError() NotImplementedError How to Read a Stack Trace When an error occurs while running a Python script (and isn't handled or resolved by the script), it will be raised or "bubbled-up" and displayed to the user in the form of a "stack trace". A stack trace shows the calling "stack" of statements all the way from the top-level script that is being executed down to the statement that has produced the error. Let’s look at a stack trace for a script (fortune_cookie.py) which calls a main() function, which in turn calls a create_fortune_cookie_message() function, which raises an error... The call stack looks like this: fortune_cookie.py  main()  create_fortune_cookie_message() …and the stack trace looks like this: <Example Output> Let’s read it: Last Line First Top to Bottom Stop when you reach someone else’s code Now that we have an idea of what the problem is (something hasn’t been implemented) and where the problem is (about line 38 in the create_fortune_cookie_message function). Let’s open the code up in our editor and take a look. <Open the code, locate the function and the NotImplementedError, and then advance to the hands-on exercise on the next slide>

54 Intro to Python – Part 2 Exercise Estimated Time: 15 minutes
Practice what we have learned: Understanding Script Execution & Flow Variable Scope Practice Basic Debugging There are multiple ways you could complete these items – any functional way works! You only need to do work inside the create_fortune_cookie_message() function. Feel free to add debug print() statements wherever you like. In this exercise, you are going to write the code for the create_fortune_cookie_message() function. You will make a couple of calls to other “helper functions” to construct and return and fortune cookie message – complete with lucky numbers – which the script will use to create fortune cookie messages for the users. This exercise will provide you an opportunity to review and validate your understanding of a script’s execution flow, and possibly an opportunity to test our the basic debugging skills you just learned. Exercise Open intro-python/part2/fortune_cookie.py in your editor. Complete the TODO task. Run the script in your Terminal. Solution

55 Parsing JSON with Python
When it comes to parsing JSON, Python takes care of the hard part and you get to reap all the benefits! In this section we will learn how to parse JSON text into native Python data and how to work with results; including: The need for structured data formats. How to read-from and write-to files using Python. JSON’s lightweight syntax. How to use Python’s JSON module to “load” and “dump” data. How to access data elements within nested data.

56 What is JSON? Standardized format for passing data as text.
JavaScript Object Notation Looks strikingly similar to Python’s syntax for dictionaries, lists, strings and number types! …BUT… JSON is just text! Q: Why store and share data in text format? A: What other format are you going to use? Working with data within your own code is great (using your language's native syntax and data types), but what about when you need to share data between applications? How are you going to format the data so that the other app will understand it? You could export data in some binary format, but doing so tends to be very proprietary and difficult to work with. Using a structured text format provides a few immediate benefits: Structured Text is: Human Readable (some formats more so than others) Easily Transportable (many methods exist to send/receive text) Machine Parsable (an app can parse the text to extract the structured data) When needing to send data between applications, we could invent our own formats for how we want to structure the data we are sharing as text, but this doesn't scale - everyone wanting to get data from you would have to learn "your format." Standardized Data-Interchange Formatsallow us to learn one, okay a few, data formats that we can all use to send data back and forth between applications. Benefits of Standardized Data-Interchange Formats: We don't have to invent them when we want to share data. We don't have to write the code to parse these formats, others have already done this for us. Once you understand their syntax, you (as a human) can read and write them yourself (it's just text). There are several prominent standardized data-interchange formats in use today, with the following being the most pervasive: JSON (JavaScript Object Notation) XML (eXtensible Markup Language) YAML (YAML Ain't Markup Language) JSON stands for JavaScript Object Notation, and it is a standardized data-interchange format used to exchange data as structured text between many applications and programming languages. You don't have to know any JavaScript to use and understand JSON data. JSON syntax is relatively lightweight (compared to XML) and easy to learn. In fact, as a Python coder you already know most of the syntax.

57 JSON Syntax vs. Python Syntax
{ "ietf-interfaces:interface": { "name": "GigabitEthernet2", "description": "Wide Area Network", "enabled": true, "ietf-ip:ipv4": { "address": [ "ip":" ", "netmask":" " } ] { 'ietf-interfaces:interface': { 'name’: 'GigabitEthernet2’, 'description’: 'Wide Area Network’, 'enabled’: True, 'ietf-ip:ipv4': { 'address': [ 'ip':' ’, 'netmask':' ’, }, ], } Take a look a look at the following JSON data, and compare it to the exact same data in parsed into native Python data structures. Look familiar? It should! JSON syntax is almost identical to syntax used by Python's native data structures; however, there are a few important differences. Can you spot the differences? You might have noticed: The use of single vs. double quotes. Python doesn't care, but JSON does. JSON strings must be delineated using double quotes " ". The capitalization of the boolean value true. Python uses True with a capital T and Falsewith a capital F, while JSON uses an all-lowercase convention of true and false. Trailing commas! This is another difference where Python doesn't care (and will ignore any trailing commas), but JSON does care and will complain if you accidentally leave some trailing commas in your JSON data. Some things that you wouldn't have noticed (because they weren't visually contrasted in the like-for-like example above): The outer most element of a JSON data structure must be an "object" (which is the JSON name for a key-value pair data structure, like Python's dictionary). Python can use any immutable and hash-able data type as a key in a dictionary. JSON keys must be strings. Knowing those differences: if you can write Python dictionaries, lists, and strings, integers, floats, and booleans, then you can read and write structured JSON text. Note: Neither Python nor JSON care about whitespace within (between the elements of) their data structures. You will see Python and JSON data without all of the whitespace between the elements as shown above. It is syntactically the same, but harder for humans to read. We will try to always show you pretty-printed data so that us humans can more readily understand what we are looking at. Now that you know how to read and write structured JSON text, let's talk about how we are going to parse that text into data inside your script. JSON Python

58 Reading-from and Writing-to Files
Use the python open() function. open(file_path, mode=‘r’) File Methods: .read() .write() .close() >>> file = open("demo.txt") >>> contents = file.read() >>> print(contents) It's easy to work with files in Python! >>> file.close() >>> with open("demo.txt") as file: print(file.read()) ... Reading-from and writing-to text files is easy with Python's built-in open() function. It returns a file object that we can use to read-from and/or write-to the file that we opened. Open Syntax Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised. file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened. mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode. Other common values are 'w' for writing (truncating the file if it already exists). Note: Don't forget to .close() files that you open. If you don't want to have to remember to close the files, use the with statement and let it manage the context for your open file. The with Statement Python provides a powerful (under the hood), but easy to understand, context manager capability. In this context, we want to be able to open a file, work with its contents, and then have Python remember to close it when we are done. We instruct Python to do this by using the with statement. This example does exactly the same thing that we did previously, only we don't have to remember to .close() the file when we are done with it Use the with statement if you don’t want to have to remember to close the file after you are done working with a file.

59 Parsing JSON Parsing: Converting the text-based JSON data to native Python data types - things you can work with! Python provides a native JSON parser for you! import json data = json.load(file) data = json.loads(string) string = json.dump(file) string = json.dumps(data) >>> string = '{"pets": ["cat", "dog"]}’ >>> type(string) <class'str'> >>> import json >>> data = json.loads(string) >>> type(data) <class'dict'> >>> data["pets"][1] 'dog' Parsing is the process of analyzing text into its logical syntactic components, and writing code that parses text into data is about as much fun as this definition sounds. Imagine trying to write a bit of code just to extract the name and value pair "enabled": true,from the above JSON text. You would first have to locate where this key-value pair begins. Then you would have to write code to recognize the key as being contained within the opening and closing quotation marks. Then you would have to tell it to recognize that the value starts after the colon, and ends where? When it reaches a comma? What if it was the last element in the set of key-value pairs? No trailing commas! ... #Pain Writing flexible and reliable parsers is hard. Fortunately for you and I, this has already been done for us by the wonderful Python community. Using the Python json Module The json module in the Python Standard Library contains encoding and decoding functions that "do the hard work for you," when it comes to parsing and working with JSON data. The module's load-from-string json.loads() function parses JSON text contained within a string and returns the Python-native data structures. The module's dump-to-string json.dumps() function takes your Python-native data structures and attempts to convert them into a JSON string. It can even make the JSON human-friendly, if you pass it the argument indent=4. Note: Not all Python data structures can be parsed into JSON notation. For example, if you created your own class, Python would have no knowledge as to how objects created from your class should be represented in JSON format. It is best to stick with Python's native data structures (dict, list, etc.) when you want your data to be able to be encoded as JSON text.

60 Nested Data What is nested data? Imagine that you have:
A string which is the value of a dictionary key-value pair. That dictionary is one element of a list. That list is the value of a dictionary key-value pair. That dictionary is the value of dictionary key-value pair. That might seem excessive, but that is precisely the case with the ip address in the JSON example we looked at previously.

61 Accessing Nested Data Indexing into Nested Data
Start with the outermost data structure. “Extract” from it what we want. Repeat. Play with data in the Python Interactive Shell Takes practice. How would you access the “ip” address in this example? json_data = { 'ietf-interfaces:interface': { 'name’: 'GigabitEthernet2’, 'description’: 'Wide Area Network’, 'enabled’: True, 'ietf-ip:ipv4': { 'address': [ { 'ip':' ’, 'netmask':' ’, }, ], } Indexing Nested Data Reminder: We access an element within a Python data structure using the square braces [ ] and the index of the item we want to "extract." Dictionaries use keys as their index, while lists use the numerical index of where each element sits in the ordered sequence (starting at zero). To index into a nested Python data structure to get to the data element that we want: We start with the outermost data structure ”Extract" from it the element that we are interested in Repeat the extraction process for each nested data structure, until we reach the element that we want. This takes a little practice; however once you have mastered this skill, you can quickly and easily access any piece of information within a data structure in a single line of code! Which, beats the heck out having to write a parser to extract some piece of data from a blob of text! Let’s Practice It Here Together: To get to the ip address for this interface, we are going to repeat the indexing / "extraction process" and chain the index operations until we have arrived the value we want. <Have the audience step through each data structure providing you the indexes need to get down to the IP address. Use a whiteboard if you have it. The solution is on the next slide.>

62 Solution >>> json_data["ietf-interfaces:interface"]["ietf-ip:ipv4"]["address"][0]["ip"] ' ' Confirm that the audience’s solution was correct, and show that it does indeed work. 

63 Parsing JSON with Python
Estimated Time: 15 minutes Parsing JSON with Python Practice what we have learned: Parsing JSON with the json module Indexing Nested Data Iterating through a Dictionary There are multiple ways you could complete these items – any functional way works! Remember your basic debugging tools. You will need to index to the data structure you want to iterate. In this exercise you will use the Python JSON module to parse some JSON data from a .json text file, use indexing to access a list of interfaces, iterate the list of interfaces, and use indexing to print out some details from each interface. Exercise Open  intro-python/parsing-json/nested_data.py  in your editor. Complete each of the TODO tasks. Run the script in your Terminal. Solution

64 Wrap Up

65 What you learned in this module…
How to clone a git repo, create branches, and make commits. Core Python syntax, operators, conditionals, and functions. Python container data types and syntax for Python’s loops. Python script structure, execution, and variable scoping and passing. How to parse JSON (aka. convert to native Python data types) and extract and reference it’s data. <Review the items covered in this module.>

66 …where to go to continue learning:
DevNet (always!) Git git-scm.com Tutorials GitHub GitHub Guides Python edx.org Python Courses coursera.com Python Courses codecademy.com Learn Python Need a challenge? <Direct the participants to these resources to learn more.>

67 <Insert your photo and social details so your fans can follow you
<Leave this slide up at the conclusion of your presentation.>

68


Download ppt "Programming Fundamentals"

Similar presentations


Ads by Google