am i using too many or statements in one line? [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 2 days ago.
Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper "def fix_teen(n):"that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper below and at the same indent level as the main no_teen_sum().
def no_teen_sum(a, b, c):
return fix_teen(a) + fix_teen(b) + fix_teen(c)
def fix_teen(n):
if n == 15:
return 15
if n == 16:
return 16
if n == (13 or 14 or 17 or 18 or 19):
return 0
return n

Related

How to loop some amount of number in python [duplicate]

This question already has answers here:
How to calculate a mod b in Python?
(7 answers)
Closed 1 year ago.
I want to make the number begin from zero if it is more than twenty-five. For example,
x = 24 + 10 must be 8. How can I do that?
You can use the modulus operator, or %. a % b finds the remainder when you divide a by b. If you want to keep the number at 25 or lower, you should take the mod with 26.
def add_and_wrap(n1, n2, wrap_at):
return (n1 + n2) % (wrap_at + 1)

How does multiple assignment in Fibonacci code used in Python website work? [duplicate]

This question already has answers here:
How does swapping of members in tuples (a,b)=(b,a) work internally?
(1 answer)
Is there a standardized method to swap two variables in Python?
(8 answers)
Closed 2 years ago.
I'm a newbie to Python and just can't understand this:
Code#1:
a, b = 0, 1
while a < 10:
print(a)
a, b = b, a+b
Code#2
a, b = 0, 1
while a < 10:
print(a)
a = b
b = a+b
Why Code#1 produce Fibonacci sequence while Code#2 doesn't?

Python: What does 'and not' mean in this code [duplicate]

This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 3 years ago.
This is the entire code. What does 'and not' mean in the code. I understand it to mean that only a number that will equal to 0 when the number modulus 2 is carried out.
That is if 10 is entered by the user, 2,4,6,8 will be sum to get 20.
the_max = int(input("Enter the upper limit:"))
the_sum = 0
extra = 0
for number in range(1,the_max):
if number%2 and not number%3:
the_sum = the_sum + number
else:
extra = extra + 1 # Line 1
print(the_sum) # Line 2
print(extra) # Line 3
It means that the number is not a multiple of 2 but a multiple of 3;
the if statement has two conditions:
number % 2
since the % returns the remainder of a a number divided by 2, it will return 0 on a multiple of 2 and will result in rejection of if condition.
and not number % 3
This means that we need both condition to be good. But with this one the not operand reverses it.
So this time any number % 3 in which number is a multiple of 3 will result in 0 and will be reversed to 1;
You're parsing it wrong. The correct interpretation is
if (number % 2 != 0) and (number % 3 == 0):
This code is taking shortcuts by omitting the explicit comparison to zero, since 0 evaluates to False in a boolean expression like this one, whereas any other integer value evaluates to True.
The second clause, thus, is given a not to flip its polarity. not (number % 3) is equivalent to (number % 3 == 0).

How to sum up values of a for range function in python [duplicate]

This question already has answers here:
Python:How to make the sum of the values of a while loop store into a variable?
(4 answers)
Closed 3 months ago.
Basically i want to sum up the result of the expression k=5x+17 but with different x, like (k=53+17) + (k=5*4+17) and so on... so far my code looks like the following.The result needs to be Σ which goes from the range (3,9).
for x in range(3,9):
k=5*x+17
k+=k
print(k)
you're overwriting k at each iteration, so in the end the result is just (5*8+17)*2
To perform such a sum, with x varying between 3 and 8 (9 is not included) so it in a generator comprehension and pass the result to sum, you'll avoid the nasty side-effects like you just created.
result = sum(5*x+17 for x in range(3,9))
(of course if you want to include 9 you have to range from 3 to 10), so depending on the upper boundary, you get 267 or 329
You can also do that without using sum at all using the n*(n+1)//2 formula for sum of integers from 1 to n and adapting it a la project euler to reduce complexity:
start = 3
end = 9 # inclusive
result = ((end*(end+1))//2 - ((start-1)*(start))//2)*5 + (end-start+1)*17
Remembering that the sum of integers between 1 and n is n*(n+1)/2 and using some basic summation equalities, you can calculate the result directly:
>>> (3 + 9 - 1) * (9 - 3) // 2 * 5 + 17 * (9 - 3)
267
For a range from i to j and an expression a*x+b, you can do:
a*(j-i)*(i+j-1)//2 + b*(j-i)
Because what you want is:
Σax+b = aΣx + Σb
#Use this simple code
l=[]
for x in range(3,9):
y = lambda x :5*x+17
l.append(y(x))
l =sum(l)
print l

How can I test if a number is a square number in Python? [duplicate]

This question already has answers here:
Check if a number is a perfect square
(25 answers)
Closed 8 days ago.
My code is
if graph == square_grid and type(math.sqrt(nodes)) is not int:
print "Your netork can't have that number of nodes"
Of course this doesn't work because math.sqrt always returns a float. How can I do this?
One way is
int(math.sqrt(x)) ** 2 == x
Because math.sqrt always returns a float, you can use the built in is_integer method
def is_square(x):
answer = math.sqrt(x)
return answer.is_integer()
this will return True if x is a square and False if it's not
>>> is_square(25)
True
>>> is_square(14)
False
try:
math.sqrt(nodes) == int(math.sqrt(nodes))
Using pure maths (no libraries) a square number can be determined in Python as follows:
not n**0.5 % 1
As the square root of a square integer is an integer, this test uses modulo 1 equal to zero to test if the square root is an integer.
For example:
The following will print the first five square numbers:
for i in range(1, 26):
if not i**0.5 % 1:
print(i)
Output:
1
4
9
16
25

Categories