Augmented Assignment variable value [duplicate] - python

This question already has answers here:
Python integer incrementing with ++ [duplicate]
(7 answers)
Closed 6 years ago.
Sorry for a question that might sound dumb, but in Python, is there a way to easily add 1 to a variable value, rather than doing
var_one = 1
var_one = var_one + 1
all the time?

Try this one on for size:
var_one += 1

Related

How to make the memory address of two variables equal in Python? [duplicate]

This question already has answers here:
Pass reference by reference in python
(3 answers)
Pass-by-reference differences in C++ vs Python
(3 answers)
Closed 26 days ago.
I need to equal the memory address of two variables...
I mean If the value of the first variable changes, the second variable will also take the same value.
For example :
a = 10
b = 10
# now addresses are same
a = 5
print(b) # must print 5
I want to do something like this:
id(a) = id(b)

Random number as file name [duplicate]

This question already has answers here:
How to insert a variable value in a string in python
(3 answers)
Closed 1 year ago.
So, I want to put a random number as my filename in f=open
I generated the number with d=random.randint(1,10)
and I want to put that as my filename in f=open.
f=open("test.txt", "x")
How can I do that?
I don't think you tried very hard to solve this yourself.
d = random.randint(1,10)
f = open(f"test{d}.txt",'w')

Curious about the function of ~in the following code [duplicate]

This question already has answers here:
The tilde operator in Python
(9 answers)
Closed 1 year ago.
pd_selftest = pd_selftest[pd_selftest['SICCD'] != 0]
pd_selftest = pd_selftest[~pd_selftest['SICCD'].isnull()]
I'd like to know what the function of the ~ is in the above code.
That's the bit-wise invert or not operator. So, it returns only those lines where the SICCID column is not null. I would probably use the word not in this case.

How to find the absolute value of a complex function? [duplicate]

This question already has answers here:
Complex numbers in python
(3 answers)
Closed 4 years ago.
I want to find the absolute value of something like 'i' but when I type 'abs(1)' it says 'i' is not defined. What do I do?
In python to find the absolute value of a complex function you use j instead of i.
abs(a+bj) # General Format
abs(0+1j)
>> 1
Or you could define i as the square root of -1 and use it instead
i = (-1) ** 0.5
abs(i)
>> 1

Python 3 - convert mathematical action to integer [duplicate]

This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Closed 6 years ago.
I have a string with a formula 5 - 3, and I need to get the result in integer. How could I do that?
use eval function:
eval("5 - 3") # 2
test = "5-3"
print(eval(test))
Gives 2

Categories

Resources