Why Are Python List Operations/Methods Unlike Python String Operations/Methods?

Problem scenario
List operations such as .sort() will change previous copies of the list.

String operations such as .replace(“old_pattern”, “new_pattern”, count), will not change the variable or its copies; these operations will return a string after the replace operation has run on the original string.

Why is this behavior happening?

Solution
In place changes and mutability are factors.

 » Read more..

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?

 » Read more..

How Do You Pass Two or More Subnets to an “aws eks” Command?

Problem scenario
You want to pass more than one subnet to an “aws eks” command.

You tried to delimit the list with commas (or separate two subnet IDs with commas). You received this error message:

An error occurred (InvalidParameterException) when calling the CreateNodegroup operation: The subnet ID ‘subnet-0abcd1234,subnet-zyxw9876’ does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidSubnetID.NotFound; Request ID: Proxy: null)

What should you do?

 » Read more..

What Does the Number “2” in a man Command Mean?

Question
Normally when you use the man page, you just type man nameofcommand. But you have seen integer numbers between 1 and 9 (inclusive) between the “man” and the command’s name. What do those numbers signify?

Answer
This legend was taken from “man man” on a Linux server:

1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions),  » Read more..

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,

 » Read more..