This question already has answers here:
How do I create a constant in Python?
(44 answers)
Closed 7 years ago.
Sometime there is a need to define constant numeric values to be used across our application.
For instance in C language I used to do thing like:
#define PI = 3.14
#define MIN = 0
#define MAX = 256
What is the equivalent in Python?
[Edit:] To the down voters. The question is not related to syntax. I'm interested in understanding conventions and doing things right in the Python philosophy.
There is no really equivalent in Python: just convention: uppercase!
If you write:
CST = 10
Everyone know that you don't have to change it…
Related
This question already has answers here:
How to evaluate a user input of a string as a math expression without using eval()?
(2 answers)
Closed 12 months ago.
I want my program to do this:
Input formula:
(2*(3+4)-5)/6
Output answer of formula:
1.5
Is there anyway to do it? I can't find anything from the Internet. Asking here is my last attempt.
you can use eval
>>> a = eval('(2*(3+4)-5)/6')
>>> print(a)
1.5
This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 7 years ago.
I was playing around with Python. I had a doubt about the power operation in Python. So, I tried this:
0726**13 = 54609997061205831773270000000000000L
726**13 = 15565965698792536237226936270158258176L
Why is there a difference between these two? I know it might be trivial. But, I could not figure it out. Could someone please explain? Thanks.
It's because an integer constant beginning with 0 is taken to be an octal value. In this case, 0726 is interpreted as 470:
>>> 0726
470
>>> 470**13
54609997061205831773270000000000000L
>>>
Numbers starting with 0 in Python are represented in Base 8 (octal numbers). That's why you're getting different results.
This question already has answers here:
Python objects confusion: a=b, modify b and a changes! [duplicate]
(3 answers)
Closed 8 years ago.
I'm new to python (using 2.7.6) and I'm sure this has been asked before, but I can't find an answer anywhere. I've looked at the python scoping rules and I don't understand what is happening in the following code (which converts three hex number strings to integers)
ls=['a','b','c']
d=ls
for i in range(0,len(d)):
d[i]=int(d[i],64)
print str(ls)
Why does the value of ls change along with the value of d?
I couldn't repeat this behavior with simple assignments. Thanks!
d is ls.
Not the value assigned to d equals the value assigned to ls. The two are one and the same. This is typical Python behavior, not just for lists.
A simple way to do what you want is
d = ls[:]
This question already has answers here:
What does `<>` mean in Python?
(5 answers)
Closed 9 years ago.
So, I'm making a python cheat sheet for myself, and when I started covering comparison operators, I noticed these two:
a = 1
b = 2
if a != b:
print("Dunno")
if a <> b:
print("Dunno")
I'm using python 2.7 and was curious if there's a difference between the two operators?
As described in the documentation, they are the same. <> is deprecated and was removed in Python 3, so you should use !=.
<> is deprecated. Other than that, no.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the # symbol do in Python
I thought it would be a good idea to as this question here since google outright ignores the # character in a query.
What does the # before an earlier declared function name do when used in the top level of a document, multiple times, just before def´s?
Example
As a follow up, is it still present in Python 3?
Those are decorators
... and yes they're present in Python 3, see ... Python 3 primer, Part 2: Advanced topics