How Do You Generate 10,000 Random Numbers in a Tuple in Python?

Problem scenario
You want to generate 10,000 random numbers in a tuple. That is, you want a tuple with 10,000 numbers. The numbers can be chosen at random. How do you do this?

Solution
See this posting first and follow it: How Do You Generate 10,000 Random Numbers in a list in Python?
See this posting second and follow it: How Do You,

Why Does a Python Variable Keep Getting Assigned to 0 When the Output of the Bash Command Is a Different Number?

Problem scenario
In a Python script a variable that receives the output of a Bash command. When you run the Bash command on Linux or in a script, it works fine. But in the Python script it keeps being assigned to 0. What is wrong?

Solution

subprocess.call(“bash command”, shell=True) is NOT the same as subprocess.check_output(“bash command”, shell=True).

The subprocess.call will return a 0 if the Bash command was successful.

Using Python How Do You Write a Palindrome Tester?

Problem scenario
You want to write a Python program that tests if a string is a palindrome. You want the calling function to have five lines of code or fewer. How do you do this?

Solution
Here is a program that interactively prompts you for a string and will tell you if it is palindromic or not.

# You may want to see this documentation page: https://docs.python.org/2.3/whatsnew/section-slices.html

stra = input(“Enter a string: “)

def goodone(stra):
if(str(stra) == str(stra)[::-1]):
print(“It is palindromic.”)
else:
print(“The entered string was not a palindrome.”)

goodone(stra) …

How Do You Use Google’s Cloud Pub/Sub with Python?

Problem scenario
You want to use a Data Analytics or a Big Data tool that publishes messages and subscribes to listening to messages being published. You know GCP has a Pub/Sub tool. You know it supports synchronous and asynchronous messaging. How do you use it with Python?

Solution

  1. Log into GCP via the web UI.
  2. Go here: https://console.cloud.google.com/cloudpubsub/
  3. Click “Create Topic”.

How Is Input Passed into the Python Program in HackerRank.com?

Problem scenario
You wrote a Python program on a Linux machine, and it works by interactively prompting a user for input. You want it to work for HackerRank, but you are not sure how standard input works in their platform. You use this line of code in your program: ‘input(“Enter your input here: “)’ to ask the user for input. This does not work in HackerRank.

You tried rewriting your program to accept parameters when you run it like this:

python foobar.py blue

In foobar.py you have “sys.argv[1]” to accept the “blue” argument.

How Do You Use a Bound Method in Python?

Problem scenario
You want to write a Python program with a bound method.  What should you do?

Solution
This program uses a bound method.  The method funtest() is bound to objects that are members of the class “widgetfactory”.  You cannot use the method without it being bound to this class.  (If you uncomment out the last line, you’ll see it is a bound method and not an unbound method;

How Do You, in Python, Make a Tuple from a List?

Problem scenario
You are writing code in Python.  You have a list that you want to have a copy of in the form of a tuple.  What do you do to convert the content to a different data type?

Solution
Assuming you have a list called “contintlist”, this line would create a tuple with the content of “contintlist”:

cooltuple = tuple(contintlist)

Here is a Python program that generates a list,