Range Function That Prints Even Numbers From 100 To 200 Inclusive [duplicate] - python

This question already has answers here:
how to make a range() only even numbers in python3.3?
(2 answers)
Closed 9 months ago.
I have to create a range function so that the code prints the even numbers between 100 and 200 inclusive.
The solution I tried is
num = 100
for x in range(num % 2, 200):
print(x)

range(start,end,step)
list(range(100,200,2))

Related

How do you stop Python from converting a number to scientific notation? [duplicate]

This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 10 months ago.
This is my code:
m = int(input(""))
p= 3.00*10*10*10*10*10*10*10*10
c = p*p
E = m * c
print(E)
The answer is 19e+16.
But I don't want the scientific notation: I want the number.
Actually it is not from VSCode, Python prints that way for better readability. You can print the long form with the help of string formatting. When you apply precision it will expand the printed value:
E = 3.00 * 100 * 100 * 100 * 100 * 100 * 100 * 100 * 100
print(E)
print(f"{E:.1f}")
print(f"{E:.10f}")
output:
3e+16
30000000000000000.0
30000000000000000.0000000000
Possible duplicate of:
How to suppress scientific notation when printing float values?
TL;DR
print('{:f}'.format(E))

Python: C code for-loop that multiplies the loop variable [duplicate]

This question already has answers here:
Is there any method like divide by or multiply by in python range()?
(4 answers)
Closed 2 years ago.
The C code here:
for(i = 1; i <= 1000; i *= 3){
//task
}
How can I write this in python with a for loop?
So far I can only think of a solution using while loops:
i = 1
while i <= 1000:
#task
i *= 3
Is there a way I can write this with a for loop in python?
try this
step = 3
_i=1
for x in range(0,1000):
if x%_i!=0:
continue
_i*=step
# task
print(x)

Modulo 10 for large Numbers | Checking if a number is in powers of 10 [duplicate]

This question already has answers here:
Mod function fails in python for large numbers
(2 answers)
Closed 4 years ago.
I want to check in Python if a (large integer) value is in powers of 10. Normally I would simply check the modulo 10 of the value. This works fine for values of up to 1.E+22, after that, it returns weird values:
lowInteger = 1.E12
highInteger = 1.E23
print(lowInteger % 10) #as expected returns 0
>>> 0.0
print(highInteger % 10) #returns 2.0 instead of 0.0, 1.E23 returns 3.0, etc
>>> 2.0
The floating point number 1.E23 is not a power of 10. It is the nearest representable number to the power of 10 you wanted. Its actual value is
>>> int(1e23)
99999999999999991611392
If you want exact integers, use ints, not floats. You want 10**23.
>>> 10**23
100000000000000000000000
Also, if you want to check whether a number is a power of 10, x % 10 == 0 is the wrong check anyway, because that's checking whether the number is a multiple of 10.

Write Numeric Manipulation in Python in 1 line [duplicate]

This question already has answers here:
How can I clamp (clip, restrict) a number to some range?
(9 answers)
Closed 4 years ago.
i have a simple problem in Python where i need to return a value that is equal to floor if it is smaller than floor, and return ceiling if the value is larger than ceiling. I'm not sure if there is a simpler way to do so. What i will do is:
def floor_ceil(x):
floor = 0
ceil = 100
if x < floor:
return floor
elif x > ceil:
return ceil
return x
Are there simpler ways to do so? The code looks clunky
You could reuse the built-in max and min:
def floor_ceil(x):
return max(0, min(100, x))

Python finding n as 101 instead of 100 [duplicate]

This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 6 years ago.
Python loops are finding n as 101 instead of 100, I am getting the average of 5050 as 50 instead of 50.50, What could be the problem? How should I go through it? Here is my function.
def sum_and_average(n):
total = 0
for i in range(1, n+1):
total += i
average = float(total/n)
print "the sum is %d and the average is %f" %(total, average)
sum_and_average(100)
It returns:
the sum is 5050 and the average is 50.000000
Do float(total) / n.
In Python 2 when one of the arguments is float, the calculation will be carried out in float.
But doing float(total/n) won't work, since total/n has already been calculated in integers, and floating the result is already too late.
To get the average you want this:
average = float(total)/n
Some examples:
>>> float(101/2)
50.0
>>> 101/2
50
>>> float(101)/2
50.5

Categories

Resources