How Do You Troubleshoot “SyntaxError: invalid syntax” in Python?

Problem scenario
You are running a Python script. You keep getting “SyntaxError: invalid syntax” and a line number. You can find nothing wrong with the syntax on the specified line number. What do you do?

Possible solution #1
Look at the line with code, as opposed to blank line(s), above it. Is there a missing quote, brace, bracket or parentheses?

Possible solution #2
Are you trying to use an “if” statement on one line?

Why in Python Does a Variable Copy Get Changed when You Change Its Source?

Problem scenario
You have a list in Python that you copy into another variable. You change the original list and the copied version changes. Why does the copied version get copied and what can you do to overcome this?

Solution
There is something called a “deep” copy and a “shallow” copy. This program will illustrate both the problem and the solution:

list_a = [3, …

How Do You Use the .copy() Feature in Python?

Problem scenario
You want to use the copy feature in Python. What do you do?

Solution
To see the different types of copying, use a mutable object such as a list. Here is an example:

import copy

var1 = [1, 2, 3, 4]
shallow_copy = var1
deep_copy = copy.deepcopy(var1)
var1.append(999999999999)

print()
print(shallow_copy)
print(” — Shallow copy above and deep copy below —.”)
print(deep_copy)

For most (or perhaps all) intents and purposes,

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 Troubleshoot dpkg Errors from apt Commands like “package post-installation script subprocess returned error exit status 1”?

Problem scenario
On a Debian/Ubuntu server, you are trying to run apt commands. But you get errors like this:

dpkg: error processing package oracle-java11-installer-local (–configure):
installed oracle-java11-installer-local package post-installation script subprocess returned error exit status 1
Setting up nmap-common (7.80+dfsg1-2build1) …
Setting up liblua5.3-0:amd64 (5.3.3-1.1ubuntu2) …
dpkg: dependency problems prevent configuration of default-jre-headless:
default-jre-headless depends on openjdk-11-jre-headless; however:
Package openjdk-11-jre-headless is not installed.

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

What Is Rotating a List in Computer Programming?

Problem scenario
You have read about lists or arrays being rotated. What does this mean in computer programming?

Solution
Lists and arrays can be interchangeable in many contexts. Java supports arrays and Python supports lists. Both are zero-based. That is, the first item in each is indexed as 0. Lists in Python are circular: the last item in the list can be referred to as -1 and the penultimate item can be referred to as index -2.