30 - Understanding the Basics of Python Programming Flashcards

1
Q

__ 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.

A

Python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why Learn Python?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

There are two versions of Python currently in widespread use:

A
  • Python 2.x

* Python 3.x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
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
A

Version 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
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
A

Version 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two main ways to execute Python code?

A
  • Using the Python dynamic interpreter (shell)

* Writing Python scripts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

Python shell

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

To open the Python shell, enter the __ command either in a Linux terminal or from the CLI of a Cisco Nexus switch.

A

python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which Pyton helper utility returns the python built-in documentation about the object?

A

Help()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which Pyton helper utility returns the attributes (and methods) of the object or module?

A

Dir()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which Pyton helper utility returns the type of object?

A

Type()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

__ Python programming is concerned with readability and uniformity. There are many things to consider when writing __ Python, often referred to as being Pythonic.

A

Idiomatic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are a few things to consider when writing Python to achieve readability and uniformity?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Python makes use of many data types:

A
  • Strings
  • Numbers
  • Lists
  • Dictionaries
  • Booleans
  • Files
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

__ 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 __.

A

Strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

There are two different ways of printing strings:

A
  1. Using the print statement to print strings – prints the rendered string
  2. Typing in the variable name on the Interpreter – prints the value as a string literal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

You can add strings together (concatenate) using the __ command.

A

+

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

When working with strings, there are functions to modify or verify data within the string. These functions are called __, and there are many __ available.

A

methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Use ___ command on any object to see its available built-in methods.

A

dir()

21
Q

Common methods: Displays the string in either uppercase or lowercase

A

.upper() / .lower()

22
Q

Common methods: Returns a string where a specified value is replaced with a specified value

A

.replace()

23
Q

Common methods: Returns true if the string starts with the specified value

A

.startswith()

24
Q

Common methods: Returns true if the string ends with the specified value

A

.endswith()

25
Q

Common methods: Formats specified values in a string

A

.format()

26
Q

Common methods: Splits the string at the specified separator, and returns a list

A

.split()

27
Q

Common methods: Searches the string for a specified value and returns the position of where it was found

A

.index()

28
Q

Common methods: Searches the string for a specified value and returns the position of where it was found

A

.find()

29
Q

Common methods: Returns True if all characters in the string are alphanumeric

A

.isalnum()

30
Q

Common methods: Returns True if all characters in the string are in the alphabet

A

.asalpha()

31
Q

Assign a value to a variable using the _ sign.

A

equals (=)

32
Q

What are the Boolean data types?

A

AND – all must match for value to be true
OR – any must match for value to be true
NOT – takes the inverse

33
Q

In Python, __ statements are used to perform different computations or actions depending on whether a condition evaluates to true or false

A

conditional

34
Q

What are the comparison operators?

A

==. !=, >, =

35
Q

What are the logical operators

A

and, or, not

36
Q

What are the membership operators?

A

in, not in

37
Q

What are the identity operators?

A

is, is not.

38
Q

In Python, conditional statements end with a __ and must maintain consistent __ to work properly

A

colon, indentation

39
Q

The three conditional statements are:

A
  • 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.
40
Q

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.

A

interpreted

41
Q

Things to remember about creating Python scripts:

A
  • 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
42
Q

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.

A

shebang

43
Q

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.

A

first, #

44
Q

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).

A

environment

45
Q

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.

A

entry

46
Q

How is a Python script executed?

A

python [scriptname] .py

47
Q

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.

A

.pyc

48
Q

There are several key areas to cover when examining code. It is a good idea to pay attention to the following when getting started:

A
  • Accessing data in lists (index) and dictionaries (key)
  • Know your data types
  • Variable scoping
  • Code blocks and indentation with conditionals