This question already has answers here:
"num - 1" vs "num -= 1"
(9 answers)
Closed 8 years ago.
I have trying to translate code from C++ to python and on one line(in for loop), I have:
x -= (t = u/1.0+ MIN(c, EPS))
I want to know what the '=' sign after a decrement indicates? how can I translate this line in python
thank u
In c assigments are functions at there own, in Python assignments are expressions.
In python this means
t = u/1.0 + min(c, EPS)
x -= t # same as x = x - t
Related
This question already has an answer here:
Python script run through IDLE has no output
(1 answer)
Closed 2 years ago.
In python code, I've written:
x = 1
y = 2
def add_nums(first, second):
sum = first + second
return sum
And when I do:
add_nums(x, y)
It returns nothing what is wrong with the code?
The function returns its local sum variable. Perhaps you meant to print it
print(add_nums(x, y))
This question already has answers here:
What do the * (star) and ** (double star) operators mean in a function call?
(4 answers)
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 2 years ago.
The code below represents one method of writing the program using list comprehension and generators.
comp = (num for num in range(2000, 3201) if num % 7 == 0 and num % 5 != 0)
print(comp)
for item in comp:
print(item, end =", ")
This is another way of doing it, using again both list comprehension and generators, however this time, the code makes use of the * symbol inside of the print function.
print(* (i for i in range(2000, 3201) if i % 7 == 0 and i % 5 != 0), sep=", ")
What is the reasoning behind * being used here, given that without it, iterating over the generator would be impossible.
This question already has answers here:
What does the ^ (XOR) operator do? [duplicate]
(6 answers)
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed 3 years ago.
In this piece of code the iterations should stop at one position earlier than they do.
k = 0
while 2^k < 5:
k += 1
print(k)
I expect it to print '3' but it prints out '4'.
^ is the bitwise XOR operator. You probably meant ** for exponentiation.
In Python the ^ operator is bitwise exclusive-or - not exponentiation. Use ** for exponentiation.
This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 5 years ago.
Suppose that I have the following Python Code:
def fun5(a, b, c):
return a <= b if b <= c else False
fun5(2, 4, 6)
The output of fun5 is True. How is this code being evaluated by Python?
I was expecting a SyntaxError because of the lack of indentations and Python requires indentation.
What you're looking at is called a conditional expression/ternary operator, and it is perfectly valid syntax.
It's equivalent to:
if b <= c:
return a<=b
else:
return False
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 5 years ago.
# Example for Algorithm Case Study
def naïve(a, b):
x = a
y = b
z = 0
while x > 0:
z = z + y
x = x - 1
return z
print naïve(4,5)
The output should be 20. Because of syntax error in print statement, I am not getting the answer.
print in Python 3 is a function, meaning you need to call it with parenthesis:
print(naïve(4,5))