Whats the difference between a module and a library in Python?

| |

I have background in Java and I am new to Python. I want to make sure I understand correctly Python terminology before I go ahead.

My understanding of a module is: a script which can be imported by many scripts, to make reading easier. Just like in java you have a class, and that class can be imported by many other classes.

My understanding of a library is: A library contains many modules which are separated by its use.

My question is: Are libraries like packages, where you have a package e.g. called food, then:

  • chocolate.py
  • sweets.py
  • biscuts.py

are contained in the food package?

Or do libraries use packages, so if we had another package drink:

  • milk.py
  • juice.py

contained in the package. The library contains two packages?

Also, an application programming interface (API) usually contains a set of libraries is this at the top of the hierarchy:

  1. API
  2. Library
  3. Package
  4. Module
  5. Script

So an API will consist off all from 2-5?

Whats the difference between a module and a library in Python? log: Questions


Python"s equivalent of && (logical-and) in an if-statement

5 answers

delete By delete

Here"s my code:

def front_back(a, b):
  # +++your code here+++
  if len(a) % 2 == 0 && len(b) % 2 == 0:
    return a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):] 
    #todo! Not yet done. :P

I"m getting an error in the IF conditional.
What am I doing wrong?


Answer #1

You would want and instead of &&.


Answer #2

Python uses and and or conditionals.


if foo == "abc" and bar == "bac" or zoo == "123":
  # do something


How do you get the logical xor of two variables in Python?

5 answers

Zach Hirsch By Zach Hirsch

How do you get the logical xor of two variables in Python?

For example, I have two variables that I expect to be strings. I want to test that only one of them contains a True value (is not None or the empty string):

str1 = raw_input("Enter string one:")
str2 = raw_input("Enter string two:")
if logical_xor(str1, str2):
    print "ok"
    print "bad"

The ^ operator seems to be bitwise, and not defined on all objects:

>>> 1 ^ 1
>>> 2 ^ 1
>>> "abc" ^ ""
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ^: "str" and "str"

Answer #1

If you"re already normalizing the inputs to booleans, then != is xor.

bool(a) != bool(b)

Whats the difference between a module and a library in Python? sep: Questions


How to print number with commas as thousands separators?

5 answers

I am trying to print an integer in Python 2.6.1 with commas as thousands separators. For example, I want to show the number 1234567 as 1,234,567. How would I go about doing this? I have seen many examples on Google, but I am looking for the simplest practical way.

It does not need to be locale-specific to decide between periods and commas. I would prefer something as simple as reasonably possible.


Answer #1

Locale unaware

"{:,}".format(value)  # For Python ‚â•2.7
f"{value:,}"  # For Python ‚â•3.6

Locale aware

import locale
locale.setlocale(locale.LC_ALL, "")  # Use "" for auto, or force e.g. to "en_US.UTF-8"

"{:n}".format(value)  # For Python ‚â•2.7
f"{value:n}"  # For Python ‚â•3.6


Per Format Specification Mini-Language,

The "," option signals the use of a comma for a thousands separator. For a locale aware separator, use the "n" integer presentation type instead.


Answer #2

I got this to work:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "en_US")
>>> locale.format("%d", 1255000, grouping=True)

Sure, you don"t need internationalization support, but it"s clear, concise, and uses a built-in library.

P.S. That "%d" is the usual %-style formatter. You can have only one formatter, but it can be whatever you need in terms of field width and precision settings.

P.P.S. If you can"t get locale to work, I"d suggest a modified version of Mark"s answer:

def intWithCommas(x):
    if type(x) not in [type(0), type(0L)]:
        raise TypeError("Parameter must be an integer.")
    if x < 0:
        return "-" + intWithCommas(-x)
    result = ""
    while x >= 1000:
        x, r = divmod(x, 1000)
        result = ",%03d%s" % (r, result)
    return "%d%s" % (x, result)

Recursion is useful for the negative case, but one recursion per comma seems a bit excessive to me.


How would you make a comma-separated string from a list of strings?

5 answers

mweerden By mweerden

What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance, ["a", "b", "c"] to "a,b,c"? (The cases ["s"] and [] should be mapped to "s" and "", respectively.)

I usually end up using something like "".join(map(lambda x: x+",",l))[:-1], but also feeling somewhat unsatisfied.


Answer #1

my_list = ["a", "b", "c", "d"]
my_string = ",".join(my_list)

This won"t work if the list contains integers

And if the list contains non-string types (such as integers, floats, bools, None) then do:

my_string = ",".join(map(str, my_list)) 


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method