30 - Understanding the Basics of Python Programming Flashcards
__ is one of the most common programming languages in the world. It is used by computer programmers and by people in various professions that are interested in automatic and scripting tasks and making their work more efficient.
Python
Why Learn Python?
- Interpreted scripting language
- Low barrier to entry compared to other languages
- Can be used to write various types of Python apps
- Python execution engine exists on most Linux distributions, including network operating systems such as NX-OS
There are two versions of Python currently in widespread use:
- Python 2.x
* Python 3.x
What Python version is this? o No longer under active development, but supported by the Python community o Better library support o Default on Linux and Mac o Supported by Cisco NX-OS
Version 2
What Python version is this? o Under active development o Designed to be easier to learn o Fixed major issues in 2.x o Not backwards compatible
Version 3
What are the two main ways to execute Python code?
- Using the Python dynamic interpreter (shell)
* Writing Python scripts
The Python interactive interpreter, more commonly known as the __, is used to write a test code in real time without having to write a full program or script. The __ is found natively on nearly all Linux distributions and Cisco switch platforms, such as Nexus
Python shell
To open the Python shell, enter the __ command either in a Linux terminal or from the CLI of a Cisco Nexus switch.
python
Python scripts can be written in any text editor and saved locally with the __ file extension. When running a script in this way, the script can be invoked simply by invoking the python statement and the filename.
.py
Which Pyton helper utility returns the python built-in documentation about the object?
Help()
Which Pyton helper utility returns the attributes (and methods) of the object or module?
Dir()
Which Pyton helper utility returns the type of object?
Type()
__ Python programming is concerned with readability and uniformity. There are many things to consider when writing __ Python, often referred to as being Pythonic.
Idiomatic
What are a few things to consider when writing Python to achieve readability and uniformity?
- Single line comments – simply begin the line with the hash # character and the comment automatically terminate at the end of the line
- Multi-line comments – use a delimiter (unique character not in the comment itself) to define the beginning and end of the comment
- Whitespace – characters used for spacing i.e spaces and tabs
- Indentation – leading whitespace at the beginning of the line
- The Python Style Guide (PEP8) – a list of coding conventions for the Python code
Python makes use of many data types:
- Strings
- Numbers
- Lists
- Dictionaries
- Booleans
- Files
__ are simply a sequence of characters that are surrounded by either quotes or double quotes. Without quotes, you are referencing an object. With quotes, it is a __.
Strings
There are two different ways of printing strings:
- Using the print statement to print strings – prints the rendered string
- Typing in the variable name on the Interpreter – prints the value as a string literal
You can add strings together (concatenate) using the __ command.
+
When working with strings, there are functions to modify or verify data within the string. These functions are called __, and there are many __ available.
methods
Use ___ command on any object to see its available built-in methods.
dir()
Common methods: Displays the string in either uppercase or lowercase
.upper() / .lower()
Common methods: Returns a string where a specified value is replaced with a specified value
.replace()
Common methods: Returns true if the string starts with the specified value
.startswith()
Common methods: Returns true if the string ends with the specified value
.endswith()
Common methods: Formats specified values in a string
.format()
Common methods: Splits the string at the specified separator, and returns a list
.split()
Common methods: Searches the string for a specified value and returns the position of where it was found
.index()
Common methods: Searches the string for a specified value and returns the position of where it was found
.find()
Common methods: Returns True if all characters in the string are alphanumeric
.isalnum()
Common methods: Returns True if all characters in the string are in the alphabet
.asalpha()
Assign a value to a variable using the _ sign.
equals (=)
What are the Boolean data types?
AND – all must match for value to be true
OR – any must match for value to be true
NOT – takes the inverse
In Python, __ statements are used to perform different computations or actions depending on whether a condition evaluates to true or false
conditional
What are the comparison operators?
==. !=, >, =
What are the logical operators
and, or, not
What are the membership operators?
in, not in
What are the identity operators?
is, is not.
In Python, conditional statements end with a __ and must maintain consistent __ to work properly
colon, indentation
The three conditional statements are:
- The if statement states a conditional to be evaluated
- Elif if an optional condition that can be used numerous times to check for multiple expression of true
- Else is the statement that contains the code to execute some action.
Writing scripts is one of the basic programming skills used in Python. Python is an __ programming language, which means that you need only create the file and then the file can be run. The file does not need to be compiled into a form that the machine understands, Python can interpret the file itself.
interpreted
Things to remember about creating Python scripts:
- Include shebang (used by shell)
- Define an entry point
- Code is the same as you have seen on the Python interactive interpreter
- Python files often end with a .py file extension
- Python scripts are executed as: python [scriptname
It is recommended to use a __ as the first line in your script – while not required, it does aid in enforcing which Python version is used by your shell environment when executing a given script.
shebang
A shebang, when used, must be the __ line in the script. It is also the only line that can start with a __ that is not a comment.
first, #
An example shebang is #!/usr/bin/env python. When running this script, it will run the version of Python that is configured in the ___ (env).
environment
It is recommended to define an __ point for Python scripts. It means you can explicitly define where the code begins to get executed when the Python file is executed as a standalone program/script.
entry
How is a Python script executed?
python [scriptname] .py
One thing to note is that when scripts are executed, you may often see a __ file in the same directory as your original script. Even though Python is an interpreted language, it still compiles byte level code into a __ upon execution for speed.
.pyc
There are several key areas to cover when examining code. It is a good idea to pay attention to the following when getting started:
- Accessing data in lists (index) and dictionaries (key)
- Know your data types
- Variable scoping
- Code blocks and indentation with conditionals