How is 10%-3 -2 in python? Please explain [duplicate] - python

This question already has answers here:
How does the modulo (%) operator work on negative numbers in Python?
(12 answers)
Closed 8 days ago.
I am not able to understand how is 10%-3 -2 in python? I am a beginner who just started learning python. Please explain

It is a question of math, the evaluation is like this, (10) % (-3), and if you input this into any calculator, it will give you -2. Try typing 10 modulo -3 on Google and see the result

Related

Calculate by user input [duplicate]

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

how can I write fractional powers in python code like x^(2/3)? [duplicate]

This question already has answers here:
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed 4 years ago.
how can I write x^(2/3) in python code?
I want to include 1/3 and 2/3 in my calculation but unable to do so. please help
As you would do it with integer powers:
x ** (2 / 3)
Try this code here:
Also, you can initialize x
x=3
x**(2/3)

Python - Is there a way to use a greater than operator that is not a comparison operator? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 months ago.
I am trying to make a program that only prints positions in a string that is greater than this position in the string, first i tried the > but it comes up with "Invalid syntax" and highlights it in red. I tried the comparison greater than but that does the same thing (Because i am not comparing)
What i have tried:
sentence = ("Mark")
print (sentence[> 1])
What i want it to print:
rk
If you have any solutions or alternatives to a greater than operator in Python please let me know and i will give it a go. :)
All you need to do is print (sentence[2:])
For future reference and research, it is called slice.

Power operation in Python [duplicate]

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.

Large integers addition error in python 2.7.10 [duplicate]

This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 7 years ago.
I am trying to solve a problem using python. In which I have to deal with large integers (upto 500 digits). According to my current stage of understanding, python can handle any numbers in same traditional way. But I have problem in simple addition like this:
>>> p= 1001101111101011011100101100100110111011111011000100111100111110111101011011011100111001100011111010
>>> q= 0011111011111010111101111110101101111001111111100011111101101100100011010011111011111110110011111000
>>> p+q
1001101111105557844987142979708366943425581971579987152809865568761000527613931421735161949470823522L
Can anyone please explain why i got such an error.
Var q starts with a zero, making it an octal number, rather than decimal

Categories