Order of precedence equation [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Evening all, I am more or less familiar with the order of precedence but this one confuses me and I have an exam tomorrow so yer :d....
Ok so, 6-2/2+5
Is it:
2/2 = 1
1+5 = 6
6-2 = 4
then 4+6 = 10
The part that confused me is once you have in step 3 the value 4 then you just add what ever is left in the equation to the total ?

(2/2) = 1 division is performed first
(6-(1)) = 5 + and - have equal precedence, so we go left to right, hence 6 - (2/2) is done first
(5)+5 = 10 finish it by doing addition

No, it is:
6-2/2+5
= 6 - 2/2 + 5
= (6 - (2/2)) + 5
= (6 - 1 ) + 5
= 5 + 5
= 10
/ and * before + and -. In case of equal precedence it evaluates from left to right.
For more details you might want to consider the documentation (just scroll down to the end).

Related

Making a number bigger when its small, and smaller when big [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
i have been coding in python for some time now, and am trying to make a robotic hand in combination with Arduino. I have 2 servo's facing the opposite way (small electric motors) which means that when i rotate them by the same angle, the second one will rotate the opposite way. As the servo can't handle negative integers, i have to come with a solution that basically "reverses" the integer. What i mean by that is that if the number is greater than the middle point (45 in this case) i want it to be smaller, so lets say we have 46 it should be 44 and 47 -> 43 so on. How would i go about creating this? Thanks for reading.
This is just a tricky algorithm:
edge = 45
number = 44
result = edge + (edge - number)
result will be 46
edge = 45
number = 47
result = edge + (edge - number)
result will be 43

python math, How do I get 2 full box and 9 partial on my apples problems [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
lets say I have 29 apples and put them in boxes where I can fit 10 apples in each box.
when I do print statement I want to see
print(apple)
1 (Full)
2 (Full)
9 (EA)
have any idea how my code should be?
I tried using
full = 10
qty = 29
fullboxes = math.ceil(qty/full)
partials = qty-fullboxes
You can use divmod to get the number of full boxes and the remainder. The rest is just printing.
apples = 29
boxsize = 10
a,b = divmod(apples, boxsize)
for i in range(a):
print(f'{i+1} (Full)', end=' ')
if b:
print(f'{b} (EA)')
output: 1 (Full) 2 (Full) 9 (EA)
output for 30 apples: 1 (Full) 2 (Full) 3 (Full)
Not exactly sure what you are trying to achieve here but
the following code will do what you have described.
d = 29
j = divmod(d, 10)
for i in range(j[0]):
print(f'({i + 1}(full)', end=' ')
else:
print(f'{j[1]}(EA)')
The function divmod(x, y) takes two arguments x and y and gives a tuple of their quotient and remainder.
You can read more about it here

Using the (x%y) in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Why in python the x%y result is x when x is less than y. should the result be zero?
for example when I try 4%5, python give me 4 as a result of and I believe it should be zero
% -> refers to the modulo operator
So you would get the remainder if you use this operator
4%5 will give 4 as 4 is the remainder when you divide 5 by 4
If you wish to get the quotient then you can use // which is used for absolute division
so 4%5 gives 4
and 4//5 gives 0
The % symbol in Python is called the Modulo Operator.
It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.
Example:
5%6
# results in 5
See the long division below
_____0__
6 | 5
| 0
|_____
| 5
Performing 5 / 6 results in reminder of 5, thus 5%6 is 5

shortest code for printing backwards [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
so I'm trying to create as short as possible code for printing an input backwards, and I want to go below 60B. My code takes 79B, and have no idea if it is actually possible to shorten it even more.
tab=[i for i in map(int,input().split())]
print(" ".join(map(str, tab[::-1])))
So when I input:
1 2 3 4 5
I get in output:
5 4 3 2 1
Anybody got idea if it can be even shorter?
print(*input().split()[::-1])
Splits the list by spaces, then reverses and sends to print as a bunch of arguments.
print supplies the separating space automatically.
One liner 41 Bytes
>>> print(''.join(i for i in input()[::-1]))
1 2 3 4 5
5 4 3 2 1
Altough, you could just reverse the input() output w/ subscripting, and print that.
>>> print(input()[::-1])
1 2 3 4 5
5 4 3 2 1
Maybe I don't understand what you're asking, but if it's just to reverse the string as such, then print(input()[::-1]) clocks in at 21B.

How do i figure out the multiple of a number python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
ok, I am studying python in my computing course and i have been challenged with designing a code which validates a GTIN-8 code, I have came to a hurdle that i can not jump. I have looked on how to find the equal or higher multiple of a 10 and i have had no success so far, hope you guys can help me!
Here is a small piece of code, i need to find the equal or higher multiple of 10;
NewNumber = (NewGtin_1 + Gtin_2 + NewGtin_3 + Gtin_4 + NewGtin_5 + Gtin_6 + NewGtin_7)
print (NewNumber)
The easiest way, involving no functions and modules, could be using floor division operator //.
def neareast_higher_multiple_10(number):
return ((number // 10) + 1) * 10
Examples of usage:
>>> neareast_higher_multiple_10(15)
20
>>> neareast_higher_multiple_10(21)
30
>>> neareast_higher_multiple_10(20.1)
30.0
>>> neareast_higher_multiple_10(20)
30
We can also make a generalized version:
def neareast_higher_multiple(number, mult_of):
return ((number // mult_of) + 1) * mult_of
If you need nearest lower multiple, just remove + 1:
def neareast_lower_multiple(number, mult_of):
return (number // mult_of) * mult_of
To find the nearest multiple, you can call both these functions and use that one with a lower difference from the original number.

Categories

Resources