What does python mean in python
What does python mean in python
I’ve recently noticed something interesting when looking at Python 3.3 grammar specification:
The optional ‘arrow’ block was absent in Python 2 and I couldn’t find any information regarding its meaning in Python 3. It turns out this is correct Python and it’s accepted by the interpreter:
I thought that this might be some kind of a precondition syntax, but:
Could anyone familiar with this syntax style explain it?
10 Answers 10
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
In more detail, Python 2.x has docstrings, which allow you to attach a metadata string to various types of object. This is amazingly handy, so Python 3 extends the feature by allowing you to attach metadata to functions describing their parameters and return values.
There’s no preconceived use case, but the PEP suggests several. One very handy one is to allow you to annotate parameters with their expected types; it would then be easy to write a decorator that verifies the annotations or coerces the arguments to the right type. Another is to allow parameter-specific documentation instead of encoding it into the docstring.
Annotations are dictionaries, so you can do this:
You can also have a python data structure rather than just a string:
Or, you can use function attributes to validate called values:
In the following code:
Python also supports parameter annotations:
The fastest conceivable scheme would introduce silent deprecation of non-type-hint annotations in 3.6, full deprecation in 3.7, and declare type hints as the only allowed use of annotations in Python 3.8.
This hasn’t been actually implemented as of 3.6 as far as I can tell so it might get bumped to future versions.
According to this, the example you’ve supplied:
will be forbidden in the future (and in current versions will be confusing), it would need to be changed to:
The annotations are not used in any way by Python itself, it pretty much populates and ignores them. It’s up to 3rd party libraries to work with them.
What does & mean in python [duplicate]
I came across the following code
what exactly is going on here? I’m not sure what the «&» is doing.
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Answer
The & symbol is a bitwise AND operator. Used with 1, it basically masks the value to extract the lowest bit, or in other words will tell you if the value is even or odd.
More Info on Python’s & operator
Why it Works to check Odd vs. Even
EDIT: Adding this section since this answer is getting some love
The reason why ANDing a value with 1 tells if the value is odd or even may not be obvious at first.
There is only one way to represent any number in this way. E.g. the number 13 (base 10) can be written in binary as «1101» (or hexadecimal as 0xD, but that’s beside the point). See here:
Notice that aside from the rightmost binary digit, all other 1 digits will add an even number (i.e. a multiple of 2) to the sum. So the only way to get an odd final sum is to add that odd 1 from the rightmost digit. So if we’re curious if a number is odd or even, we can look at its binary representation and ignore everything except for the rightmost digit.
To do this, we use the bitwise AND operator. The value 1 in binary is expressed as 1 :
ANDing a value with 1 like this will result in 1 if the value’s rightmost bit is set, and 0 if it is not.
And because 0 is generally considered «false» in most languages, and non-zero values considered «true», we can simply say as a shortcut:
What does += mean in Python?
I see code like this for example in Python:
What does the += mean?
6 Answers 6
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If this is the first time you encounter the += operator, you may wonder why it matters that it may modify the object in-place instead of building a new one. Here is an example:
is in this case the same as
In this case cnt += 1 means that cnt is increased by one.
Note that the code you pasted will loop indefinitely if cnt > 0 and len(aStr) > 1.
Edit: quote Carl Meyer: «[..] the answer is misleadingly mostly correct. There is a subtle but very significant difference between + and +=, see Bastien’s answer.».
Search for += once the page loads up for a more detailed answer.
FYI: it looks like you might have an infinite loop in your example.
The net result is that cnt will always be greater than 0 and the loop will never exit.
The operator is often used in a similar fashion to the ++ operator in C-ish languages, to increment a variable by one in a loop ( i += 1 )
There are similar operator for subtraction/multiplication/division/power and others:
The += operator also works on strings, for example:
People tend to recommend against doing this for performance reason, but for the most scripts this really isn’t an issue. To quote from the «Sequence Types» docs:
The str.join() method refers to doing the following:
..instead of the more obvious:
The problem with the later is (aside from the leading-space), depending on the Python implementation, the Python interpreter will have to make a new copy of the string in memory every time you append (because strings are immutable), which will get progressively slower the longer the string to append is.. Whereas appending to a list then joining it together into a string is a consistent speed (regardless of implementation)
If you’re doing basic string manipulation, don’t worry about it. If you see a loop which is basically just appending to a string, consider constructing an array, then «».join() ‘ing it.
What does `<>` mean in Python?
I’m trying to use in Python 3.3 an old library (dating from 2003!). When I import it, Python throws me an error because there are <> signs in the source file, e.g.:
I guess it’s a now-abandoned sign in the language.
What exactly does it mean, and which (more recent) sign should I replace it with?
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
It means not equal to. It was taken from ABC (python’s predecessor) see here:
Order tests ( <> means ‘not equals’)
I believe ABC took it from Pascal, a language Guido began programming with.
It’s worth knowing that you can use Python itself to find documentation, even for punctuation mark operators that Google can’t cope with.
Comparisons
Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a have the interpretation that is conventional in mathematics:
What do the symbol «=» and «==» mean in python?
When should i use the symbol ‘==’ and when only the symbol ‘=’ is enough? What does the symbol ‘==’ mean in python?
I started python coding 2 weeks ago and these two symbols confused me sometimes. Several times when i used ‘=’, i would get an error message. After i changed it to ‘==’, no error message any more. It seems that ‘==’ can be used in any situation when ‘=’ works. Is it true? Can any one of you explain the principle of using these two symbols?
Greatly appreciate your help!
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
== is a comparison operator while = will assign a value to said variable.
You can use == to see whether any two items as long they are the same type are equivalent:
Now here’s the thing. If you are comparing any two items like these, an error will pop up:
String with integer
Integer with string
String and float
Float and string
You can use = to assign values to variables. Using == will either do nothing or throw an error (if the variable is undefined). For example, you wanted the variable hi to have the value of 2. Then use the = :
So in conclusion, they are different as:
== is a comparison operator: returns True is the two items are equal, returns False if not, throws error if used to assign variable before definition and if the two items are not compatible
= is an assignment operator: will assign values like strings or numbers to variables. Can be used in forms like += when variable’s value is a number and is already defined.
The only way they can be used the same time is that they can be used in strings: