In Python Are Dictionaries Much Slower in Performance Compared to Lists or Tuples?

Problem scenario
You want to know how dictionaries perform as iterables in Python. In Python for printing the values of every key-value pair in a dictionary, is it faster or slower than printing every item in a list? How does it compare to a tuple?

Solution
For this example we use integer keys in the dictionary. Keys can be strings or other objects.

 » Read more..

How Do You Upgrade to Python 3.x on Ubuntu 16?

Problem scenario
You are using Python 2.7.12 on Ubuntu 16. You want to upgrade to Python 3.x You are having problems. You tried several things as follows:
sudo apt-get -y install python 3.7
But you see this:

python is already the newest version (2.7.12-1~16.04).
python set to manually installed.
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.

 » Read more..

How Do You Write a Python Program to Accept Input in a Non-interactive Way at Run-time?

Problem scenario
You are used to using “input” with Python 3 to prompt the user for a value. How do you pass input when you execute a Python program?

Solution
1. Use import sys

2. Use sys.argv[1] to refer to the first argument passed when you run your program like this:

python3 goodprog.py foo

The program will see “sys.argv[1]” as the string foo.

 » Read more..

How Do You Upgrade Python 2.x to Python 3.7 in Debian or Ubuntu Linux?

Problem scenario
You have a virtual server running Ubuntu 18.x in Azure. It has Python 2.x. How do you upgrade to Python 3.x?

Solution
1. Run these two commands:
sudo apt -y update
sudo apt -y install python 3.7

2. Find where the binary python3.7 is by running this: sudo find / -name python3.7
Make a mental note of the result.

 » Read more..

How Do You Find the Largest and Smallest Numbers in a Python List in O(N^2) Time?

Problem scenario
You want to write a Python program that will accept a list of integers interactively. You want the program to find and print out the largest number and the smallest number of the list entered. You do not want to use the max() or min() functions (which work in Python 3 without importing any library).

What do you do?

Solution

def extremefinder(mainlist):
a = len(mainlist) – 1
b = len(mainlist)
maxnum = 0
minnum = 0
for x in mainlist:
low = 0
high = 0
for y in mainlist:
if (x y):
low = low + 1
if (low == a): # This never evaluates if x == y.  » Read more..

How Do You Install pip on a RHEL Server in AWS?

Problem scenario
You want to install pip on a RedHat Enterprise Linux server in AWS.  What do you do?

Solution
Prerequisites
This assumes that Python has been installed. If it has not, run this command:
sudo yum -y install python3


Procedures

Run these two commands:
curl “https://bootstrap.pypa.io/get-pip.py” -o “get-pip.py”
sudo python3 get-pip.py # use python is python3 is not found
sudo ln -s /usr/local/bin/pip /usr/bin/pip

 » Read more..

How Do You Troubleshoot the boto Message “NoRegionError”?

Problem scenarios (one or both of the following are happening)
#1 You are getting “botocore.exceptions.NoRegionError: You must specify a region” when you run a Python program (that involves boto and AWS).

#2 You are trying to create a session with a Python/boto program. You are printing out the Session information in your Python program using boto but you see this:

Session(region_name=None)

What should you do?

 » Read more..

Why Does Your Python Program Return “function at” with a Hexadecimal?

Problem scenario
You run a Python program. A value of a variable is printing “” with a hexadecimal. How can the variable be the intended value of the function and not this meta data of the Python interpreter or just-in-time compiler?

Solution
Use “()” at the end of the function invocation. This syntax is necessary. Here is an example that will cause the problem:

def cool():
a = “foobar”
return a

q = cool
print(q)

Here is an example that will not cause the problem:

def cool():
a = “foobar”
return a

q = cool()
print(q)  » Read more..

When Performing Equivalence Operations on Each Item in a List or in a Tuple, which Process Takes Less Time in Python?

Problem scenario
You are trying to refactor Python code. There is an equivalence operation that happens ten million times. You are concerned that a tuple may be a better data structure than a list. Which would operate more quickly and how do you find out for sure?

Solution
The tuple will be faster.

Operations in a tuple are slightly faster. This program generates a list and an equivalent tuple of 10,000,000 random numbers numbers.

 » Read more..

How Should You Automate Linux Tasks with Python 2 vs Python 3?

Problem scenario
You have been given a task to automate tasks in Python. You were advised to never use “import os” in a previous position. You were told that this time speed is very important. What should you do?

Solution
This solution examines the situation wherein you make thousands of calls to shell (the Linux command prompt). In reality you may be automating a few big shell tasks that are time consuming.

 » Read more..