Why Should You Use virtualenv when Using Python?

Question
You were told to write a Python program in virtualenv (a layer of virtualization). Why are you being asked to do this?

Answer
Isolation of dependencies is one of the dozen factors in 12 Factor App. Using pip or pip3 on an entire Linux system to install packages for PyPI is inadvisable (according to page 32 of Expert Python Programming).

How Does a Python Docstring Work?

Problem scenario
You know that Python has a feature for documentation strings that can print out when your program runs. How does it work as comments and as something that is displayed at the time the code is executed?

Solution
To best illustrate how one works, create a file called test.py with this code and run it:

def cool_function(n):
”’This is an example of a docstring. …

How Do You Use Boto3 to Create EC-2 Instances?

Problem scenario
You want to create EC-2 instances using an SDK (a program that automatically generates AWS resources). How do you do this?

Solution
Prerequisites

You have Boto3 installed. If you need assistance, see this posting if you have CentOS/RHEL/Fedora or this posting for Debian/Ubuntu.

Procedures
Run this program to create a RHEL server (but you can replace the “ami-0a54aef4ef3b5f881” with the image ID of your choice):

# Replace AKIAabcdefghijk with your AWS access key ID
# Replace 1a2b3c4d5e6f7g8h9/i0j0k9l8m7n6o5p4q3r2s1tuvwxyz with your AWS Secret Access key
# Replace us-west-1 with the region of your choice
# Replace ami-0a54aef4ef3b5f881 with the image ID of your choice. …

What Is The Difference between Iterable and Subscriptable in Python?

Question
You have seen messages when coding in Python about iterable and subscriptable. What is the difference between these two?

Answer
A subscriptable does not necessarily use the iter method, but an iterable always does.

A subscriptable object in Python leverages the getitem() method, but it may or may not use the iter method.

How Do You Display a Fixed Number of Numbers after the Decimal Point in Python?

Problem scenario
You want to display a specific number of places after the decimal (e.g., one, two or three) with numeric values in Python. You want the decimal value to be rounded if it is too long. How do you always show (including trailing zeroes) a specific number of places to the right of the decimal point?

Solution
This Python 3 program will illustrate how:

x=123.45678
y=”{:.3f}”.format(x)
print(y)

a=123
b=”{:.1f}”.format(a)
print(b) …

How Do You Troubleshoot the pip Message ‘sys.stderr.write(f”ERROR: {exc}”)’?

Problem scenario
You run a pip command (e.g., pip –version), but ou get an error like this:

Traceback (most recent call last):
File “/home/mike/.local/bin/pip”, line 7, in
from pip._internal.cli.main import main
File “/home/mike/.local/lib/python3.*/site-packages/pip/_internal/cli/main.py” , line 60
sys.stderr.write(f”ERROR: {exc}”)

You no longer want to use Python 2 in on the server. What should you do?

Solution

1.a.

How Do You Use a Class in Python to Test The __init__ Constructor’s Default Settings?

Problem scenario
You want to instantiate a class with default values if the class was called to create an object without all the settings. You want to verify you know how to code this basic object-oriented programming feature. You want to print out specific member variables of an object. How do you do this?

Solution
Use this program:

#!/bin/python3

class Computer(object):
def __init__(self, …

How Do You Troubleshoot the Python Error “built-in method strip of str object at 0x7fc87bf4a0f0”?

Problem scenario
You are running a Python program with the “strip” string manipulation method. You get this problem:

<built-in method strip of str object at 0x7fc87bf4a0f0>

What should you do?

Solution
Did you remember to use the parentheses “()” with your strip call?

print(x.strip) can return “<built-in method strip of str object at 0x7fc87bf4a0f0>”

print(x.strip()) will return the intended value. …

How Do You Use the Bisect Module in Python?

Problem scenario
You want to use bisect in Python. How do you do this?

Solution

# This program was adapted from code found
# https://docs.python.org/3/library/bisect.html

import bisect
def grade(score, breakpoints=[60, 70, 80, 90], grades=’FDCBA’):
i = bisect.bisect(breakpoints, score)
return grades[i]

x = [grade(score) for score in [71, 88, 94]]
print(x)

import bisect
def whether(temperature, breakpoints=[38, 70, 80, 100], descriptors=[”cold”, “cool”, “warm”, …