What does '%' do? [duplicate] - python

This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 1 year ago.
I ran this code
x = 2
y = 6
print(x%y)
Output
2
What did '%' exactly do?

It's the modulo operator, it gives the rest from a division that doesn't give an integer( or 0 if the division gives an integer)
So 10 modulo 4 is 2 since 10-4*2=2

Related

5*2=55 not 10! Why? [duplicate]

This question already has answers here:
multiplication of two arguments in python [duplicate]
(6 answers)
Getting issue while trying to multiply string and integer [duplicate]
(2 answers)
Closed 3 months ago.
I want to output 5 * 2 = 10 but python output is 55!
How do I resolve this problem?
a = 0
b = 2
a = input("a? :") #(get 5 as input)
c = a * b
print (c)
This is my code.
when I input a number it repeat same number I entered two times insterd of showing multipiy it. What do I have to do to solve this?
a is a string,
so it will be
'5'*2='55'
if you want 10, you need to cast a to int.
a=int(input())
here is the link to document
https://docs.python.org/3/library/functions.html#input

how to do division in python without decimal points [duplicate]

This question already has an answer here:
Python 3 integer division [duplicate]
(1 answer)
Closed 2 years ago.
if I use integer type cast conversion technique then it doesn't work for large numbers like 12630717197566440063
I got wrong answer in some cases like below in python 3
a =12630717197566440063;
print(a)
temp = a/10
print(int(temp))
Then I am getting 1263071719756644096 as a answer instead of 1263071719756644006
You can use the // (floor division) operator:
temp = a//10
print(temp)

4 > + 4 I would like to know what the plus stands for? [duplicate]

This question already has answers here:
Why does 4 > +4 evaluate to false? [closed]
(3 answers)
Closed 5 years ago.
Ok, I get that four is not greater than four but what is confusing me is the + what does that mean? Why doesn't it cause an error? I know its false but whats up with the +?
There is NO ERROR in syntax.
+4 signifies that 4 is positive. If you put -4 it is negative 4 and you will get True.

Splitting a string into a list of numbers [duplicate]

This question already has answers here:
String of numbers to a list of integers?
(4 answers)
Closed 5 years ago.
The following block of code was tested on python 2.
A="1 2 10"
A=raw_input.split()
print A
This prints a list with 4 numbers(splitting the 10 into 1 and 0),
Why does this happen?
Just use normal split
B = A.split()

Why does 1/len(list) return 0? [duplicate]

This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 5 years ago.
lets say I have a list a and a variable b = 1/len(a) but when I display the value of b it gives me 0
Because of integer divided by integer.
Try b=1.0/len(a)

Categories