I have two expressions:
a = 3 * b + c , with c = 1
b = (a - d) / 5 , with d = 1
I must calculate a in 1 starting with b = 1, then (with a just calculated) I must calculate b in 2.
if the subtraction of b in 1 and b in 2 is > 0,25
the loop continues calculating a in 1 again with the b value in 2 and so on,
otherwise it stops.
It needs two iterations and I think I must use the while loop but I can't write it down.
Especially I don't know how giving the instruction to take the b value calculated in 2 as the first step
of the following iteration.
It sounds like you mean this. Some kind of iterative function closing in on a value?
#!/usr/bin/env python3
c = 1
d = 1
b = 1
while True:
a = 3 * b + c
new_b = (a - d) / 5
if b - new_b <= 0.25:
break
b = new_b
print(a, new_b)
Roughly this:
b, c, d = [1]*3
while True:
a=3*b + c
b= (a - d)/5
if condition:
break
You just need to fill out condition
Related
B = 1
A = 3
C = 1
while C < 1000:
B = B + 1
C = A * B
print (C)
This is the code and i want to get the sum of the numbers that it prints
Here is a possibility,
B = 1
A = 3
C = 1
D = 0
while C < 1000:
B = B + 1
C = A * B
D += C
print (C)
# sum of all printed numbers
print(D)
B runs over all the integers from 2 to 334; you only need the sum of all integers from 2 to 334 (which is well known: average * number of elements) and then multiply it by A:
A = 3
B_max = 334 # ~ (1000 // A) + ...
res = A * (B_max + 2) * (B_max - 1) // 2
# 167832
you just need to make sure you get B_max right...
there is no reason for a loop at all if that is all you need to do.
define a list outside while:
dataList = []
then add C in that list:
while C < 1000:
B = B + 1
C = A * B
dataList.append(C)
then find sum:
print(sum(dataList))
for me, your goal is not clear. can you explain it more for me to be able to help you?
PS. your B = B + 1 can be simplified to:
B += 1
You should declare SUM first:
SUM=0
And at the end of while loop after print message, add
SUM = SUM + C
And thats all, simplest possible way.
B = 1
A = 3
C = 1
total = 0
while C < 1000:
B = B + 1
C = A * B
print(C)
total+=C
print("Sum is : ",total)
This question already has an answer here:
how generators work in python
(1 answer)
Closed 3 years ago.
Can someone explain how the increment of a or value of a occurs within the for loop to generate the Fib sequence?
I have an understanding of (a, b = b, a + b). However, I am unable to figure how the increment occurs in the for loop when next() is called.
def fib(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
x = fib(4)
print(x.__next__())
print(x.__next__())
print(x.__next__())
print(x.__next__())
0
1
1
2
To begin with, you can go to the next element of the generator by next(x).
Just using a print statement in your code will help you understand as well.
def fib(n):
a, b = 0, 1
for _ in range(n):
print(a, b)
yield a
a, b = b, a + b
x = fib(4)
print(next(x))
print(next(x))
print(next(x))
print(next(x))
0 1
0
1 1
1
1 2
1
2 3
2
Here the next function lazily evaluates and prints out the value of a, until you call next again.
So in the first next, it prints out 0.
Then when you call next again, a = 1 and b = 1, and you get a = 1.
Then when you call next again, a = 1 and b = 2, and you get a = 1.
Then when you call next again, a = 2 and b = 3, and you get a = 2.
After that, since you are done with your for loop, you cannot call next anymore
This is my first question and I started to learn Python.
Is there a difference between:
a, b = b, a + b
and
a = b
b = a + b
When you write it in below example it shows different results.
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
print()
fib(1000)
and
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a = b
b = a + b
print()
fib(1000)
In a, b = b, a + b, the expressions on the right hand side are evaluated before being assigned to the left hand side. So it is equivalent to:
c = a + b
a = b
b = c
In the second example, the value of a has already been changed by the time b = a + b is run. Hence, the result is different.
The line:
a, b = b, a + b
is closer to:
temp_a = a
a = b
b = temp_a + b
where b is using the old value of a before a was reassigned to the value of b.
Python first evaluates the right-hand expression and stores the results on the stack, then takes those two values and assigns them to a and b. That means that a + b is calculated before a is changed.
See How does swapping of members in the python tuples (a,b)=(b,a) work internally? for the low-down on how this all works, at the bytecode level.
Let's say we start with a and b like this:
a = 2
b = 3
So, when you do:
a, b = b, a + b
what happens is you create the tuple (b, a + b) or (3, 5) and then unpack it into a and b so a becomes 3 and b becomes 5.
In your second example:
a = b
# a is now 3
b = a + b
# b is 3 + 3, or 6.
Let's grok it.
a, b = b, a + b
It's a tuple assignment, means (a, b) = (b, a + b), just like (a, b) = (b, a)
Start from a quick example:
a, b = 0, 1
#equivalent to
(a, b) = (0, 1)
#implement as
a = 0
b = 1
When comes to (a, b) = (b, a + b)
EAFP, have a try directly
a, b = 0, 1
a = b #a=b=1
b = a + b #b=1+1
#output
In [87]: a
Out[87]: 1
In [88]: b
Out[88]: 2
However,
In [93]: a, b = b, a+b
In [94]: a
Out[94]: 3
In [95]: b
Out[95]: 5
The result is different from the first try.
Tha's because Python firstly evaluates the right-hand a+b
So it equivalent to:
old_a = a
old_b = b
c = old_a + old_b
a = old_b
b = c
In summary, a, b = b, a+b means,
a exchanges to get old_value of b,
b exchanges to get the sum of old value a and old value b,
a, b = b, a + b is similar to a, b = 0, 1 assigning values to both variables a, b at same time. First assign a = b and then b = a + b.
I hope that you haven't been influenced by C language, which the priority of assignment operator = is higher than that of Comma operator ,. Do not think it's (a), (b = b), (a + b). It's a tuple assignment, meaning it's (a, b) = (b, a + b).
There are differences between a,b = b,a+b and a=b b=a+b
let's have a look at the following two examples:
eg1:
a,b = 0,1
while a<10:
print(a)
a,b = b,a+b
#output:
0
1
1
2
3
5
8
eg2:
a,b = 0,1
while a<10:
print(a)
a=b
b=a+b
#output:
0
1
2
4
8
This is because the interpreter always calculates the figures in the right side of the Equals sign first. The calculation results will be assigned to the variables which on the left hand side only if all the calculation has been done on the right hand side.
I have started learning python and my first program on fibonacci started giving me some weird answer, I know I am missing conceptually something so need guide from some expert on this. My program looks like this
#! usr/bin/python
a,b = 0, 1
while (b < 50):
print(b)
a = b
b = a + b
output
1
2
4
8
16
32
But When i wrote like this I got correct result
#! usr/bin/python
a,b = 0, 1
while (b < 50):
print(b)
a,b = b, a + b
output:
1
1
2
3
5
8
13
21
34
Guide me pls
a,b = 0,1
a = b # a <- 1
b = a + b # b <- a + b (1 + 1 = 2)
That's two separate operations where the a in the final line has already been modified before use.
On the other hand:
a,b = b, a + b
is an atomic operation where everything on the right side of = is the original value.
Hence it's equivalent to:
a,b = 0,1
t = a # t <- 0
a = b # a <- 1
b = t + b # b <- t + b (0 + 1 = 1)
This question already has answers here:
Multiple assignment and evaluation order in Python
(11 answers)
Closed 6 years ago.
I am new to python. Can someone explain why the following two python example does not output the same thing?
Example 1
a, b = 0, 1
while b < 50:
print(b)
a, b = b, a + b
Example 2
a, b = 0, 1
while b < 50:
print(b)
a = b
b = a + b
Thanks
In the first version, when you write
a, b = b, a + b
the expression
b, a + b
is evaluated first. After it has been evaluated, it is subsequently assigned to a, b with tuple unpacking. The key point is that the entire right hand side of an assignment statement is evaluated in its entirety before performing the binding of the left-hand side names.
In the second version,
a = b
# a is now re-bound and potentially has changed value
is performed first, and then
b = a + b
# the expression a + b uses the modified value of a
happens after. By which point, a has been re-bound.
So, to illustrate with some values. The first time round the loop, after a, b = 0, 1 we have
# a == 0
# b == 1
a, b = b, a + b
Now, b, a + b is 1, 1. And so we have
a, b = 1, 1
But the alternative goes like this:
a = b
# same as a = 1
b = a + b
# same as b = 1 + 1
You ask in a comment:
How would Example 1 be rewritten into 2 statements?
The safest way is to introduce temporary variables. Like this:
a1 = b
b1 = a + b
# now we can assign to a and b
a = a1
b = b1
In this case you don't actually need two temporary variables, one will suffice, but this is the most general form.
This tuple unpacking is how you swap two values idiomatically in Python. In other languages you write:
temp = a
a = b
b = temp
In Python you write:
a, b = b, a