This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
Closed 3 years ago.
Given this code:
nums = [1,2,3]
def test1(nums):
for i in nums:
if i == 1:
return True
return False
test1(nums)
The result is True even though after the for loop there is a return False. What am I missing?
Related
This question already has answers here:
How to remove even numbers from a list in Python? [duplicate]
(5 answers)
How to remove items from a list while iterating?
(25 answers)
Python: remove odd number from a list
(9 answers)
Strange result when removing item from a list while iterating over it
(8 answers)
Closed last year.
I am having some trouble while removing even integers from a list in Python. This is what I am trying to do but I can't figure out what am I doing wrong. Is the array skipping elements due to items being removed? I would really appreciate some help.
def removeEven(l):
for e in l:
if e % 2 == 0:
l.remove(e)
print(l)
Try filter:
def removeEvent(l):
return list(filter(lambda x: x % 2 == 0, l))
print(removeEvent(l))
Or even better:
print(list(filter(lambda x: x % 2 == 0, l)))
This question already has answers here:
What does enumerate() mean?
(7 answers)
Closed 1 year ago.
def capital_indexes(string):
indexes = []
for i, s in enumerate(string):
if s.isupper():
indexes.append(1)
return indexes
What does the s in the for loop mean?
i is the index in the string, while s is a string of length 1 representing the letter at that index.
See documentation for enumerate
This question already has an answer here:
Python function returns None, unclear why
(1 answer)
Closed 2 years ago.
I created a function to square some numbers. I was wondering why my code didn't print.
def Square(A):
for i in range(len(A)):
A[i] = A[i]**2
A = [2,0,-3]
print(Square(A))
Add a return value.
def Square(A):
for i in range(len(A)):
A[i] = A[i]**2
return A
print(Square(A))
This question already has answers here:
What's the best way to return multiple values from a function? [duplicate]
(6 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 2 years ago.
So i have a function that has to return 3 values, i haven't found a better way to do this other than returning a list. Is this code a good programming practice? And if not how to fix it.
Example function:
def func():
#code
return [a,b,c]
Main code:
#code
list = func()
k = list[0]
l = list[1]
m = list[2]
You can pack/unpack directly in python:
def func():
a = 1
b = 2
c = 3
return a, b, c
k, l, m = func()
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 5 years ago.
I've been banging my head with the following code...
code = (404, 200)
if code[0] is 404:
print "yes"
if code[1] is 200:
print "yes"
should return true both right? but just the condition of the number 200 and true why?