Why does Pythons append function gives None as a result? [duplicate] - python

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 6 years ago.
Im working on coding bat questions. trying to rotate the lift one to the left. My code always return None as a result. what can i do?
**
def rotate_left3(nums):
a = nums.pop(0)
return nums.append(a)
**

Because append is a method without a return value.
Any Python function without a return value will return None
Split the lines to return the appended list.
Or return nums + [a]

Related

My Code is answering None. Why is not appending 4 twice? [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed last month.
I am expecting it append 4 twice but it is displaying None.
b = [2,3]
b.append(4)
print(b.append(4))
append returns None
append adds the element but returns none
you can print b later to verify
b = [2,3]
b.append(4)
print(b.append(4))
print(b)

How come my function didn't print a output? [Python] [duplicate]

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))

Python Function Won't Return Value [duplicate]

This question already has an answer here:
Why Python recursive function returns None [duplicate]
(1 answer)
Closed 2 years ago.
I'm trying to create a recursive function in Python (as part of an online course) which takes two numbers and returns the highest common denominator between them, however, the function won't return the value.
My code is:
def gcdRecur(a, b):
x = min(a,b)
y = max(a, b)
if x == 0:
return y
else:
gcdRecur(x, y%x)
When I test the function with
gcdRecur(2,12)
or
gcdRecur(6,12)
nothing is returned. I know the function works because if I print y before I return it then the correct denominator is printed to the console, I'm just not sure why the function doesn't output y.
It's probably something obvious but as I'm new to Python I'm not sure what I'm doing wrong.
Any help would be much appreciated.
You need to return something in both branches:
else:
return gcdRecur(x, y%x)

Is returning a list a good programming practice? [duplicate]

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()

python list: append vs += [duplicate]

This question already has answers here:
What is the difference between Python's list methods append and extend?
(20 answers)
Why does += behave unexpectedly on lists?
(9 answers)
Closed 4 years ago.
For Python list, is append() the same as +=?
I know that + will lead to the creation of a new list, while append() just append new stuff to the old list.
But will += be optimized to be more similar to append()? since they do the same thing.
It's an __iadd__ operator. Docs.
Importantly, this means that it only tries to append. "For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . Otherwise, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y."
This thread specifically deals with lists and their iadd behavior

Categories

Resources