This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 7 years ago.
I'm new to Python and I have a question about the string operations. Is there an over-arching reason that I should understand as to why the lower operation is written as 'variable.lower()' while another one, say length, would be written as 'len(variable)'?
lower is a string method, that is, a function built in to the string object itself. It only applies to string objects.
len is a built in function, that is, a function available in the top namespace. It can be called on many different objects (strings, lists, dicts) and isn't unique to strings.
Related
This question already has answers here:
Is there a simple way of having a python list containing a given type of object only
(2 answers)
Python Typed Array of a Certain Size
(2 answers)
Closed 27 days ago.
I am trying to initialize a list object in python that accepts only one data type, let's say that I want to make a list of integers in c++, I would write int list[] = {1,2,3};, what is the python equivalent of this?
This question already has answers here:
Specify length of Sequence or List with Python typing module
(4 answers)
How to limit function parameter as array of fixed-size?
(3 answers)
Closed last month.
I am writing a function that needs to take in an ordered pair coordinate (represented in a tuple). The problem is that I don't know a way to require it to have exactly two elements in the parameters of the function. Is there a better way to handle this kind of situation (like a better data type to use)? I can't think of anything.
def function(coordinate: tuple):
pass
A tuple needs to be of a given length for some non-specific function to be executed properly.
We can check the number of elements in the tuple element by using the len() function on it that comes with python directly. Wrapped in a short if condition we can only execute the said function when we have guaranteed that the given tuple has 2 elements in it. Otherwise we may print a short error message for the user.
if len(your_tuple) == 2:
function(your_tuple)
else:
print("Please enter only 2D coordinates")
This question already has answers here:
Why are Python strings immutable? Best practices for using them
(7 answers)
What's the difference between lists and tuples?
(22 answers)
"Why" python data types are immutable
(1 answer)
Immutable vs Mutable types
(18 answers)
Closed 2 years ago.
I'm a beginner of python 3. Recently, I found that the variable types of python3 can be divided into changeable variable and unchangeable variable. Number, string and tuple belong to immutable variable, and set, dict and list belong to variable variable variable. I'm curious why there is such a difference? In particular, string and tuple are similar to list. Why can't string and tuple change one element but the whole, list not?
This question already has answers here:
Why is the order in dictionaries and sets arbitrary?
(5 answers)
Closed 6 years ago.
I am a beginner in python and I am using python 2.4.3.
I have a question regarding to the order resulted from the set()function.
I understand set() will remove the the duplicate elements from a string and
[class set([iterable])
Return a new set object, optionally with elements taken from iterable.]1
But for example, when I do the following
a='abcdabcd'
set(a)
it returned a result of
set(['a','c','b','d'])
in stead of
set(['a','b','c','d'])
which I would actually expect.
Why is that? I am not able to understand how the output was generated.
Many thanks in advance.
A set is defined as an "unordered collection of unique elements" (see here). The set object in Python make no guarantees about ordering, and you should not expect nor rely on the order to stay the same.
This question already has answers here:
How to expand a list to function arguments in Python [duplicate]
(4 answers)
Closed 10 months ago.
I have a python list:
x = ['aa', 'bb', 'cc']
The len() (or list length, which is 3 in this case) of this list can be any number, as it is basically coming from database. My question is: how can I assign each string member of this list into a separate variable automatically? The point over here is: I do not know the number of elements in the list, since they are always different.
Once this is resolved, I am trying to put it into a Python Google Charting (GChartWrapper's pie3d chart) function like this:
G.label(aa,bb,cc)
However, if I simply put the list in like:
G.label(x)
then it is naming only one section of the pie chart as the complete list.
You're doing it wrong.
G.label(*x)