invalid keyword argument for print() - python

When I wrote this code:
b = 5
a = b
c = a
print(a)
print(z=c*a)
print(z+b)
I got this error:
TypeError: 'z' is an invalid keyword argument for print()
So I decided to make a variable z and changed my code to this:
b = 5
a = b
c = a
print(a)
z
print(z=c*a)
print(z+b)
I still got the same error:
TypeError: 'z' is an invalid keyword argument for print()
It worked when I wrote:
b = 5
a = b
c = a
print(a)
z=c*a
print(z)
print(z+b)
Why was I getting an error on the first two attempts and what does 'invalid keyword argument for print()' mean?

If you use python 3.8 it is possible by := as known the walrus operator, check definitions here
try this:
b = 5
a = b
c = a
print(a)
print(z:=c*a)
print(z+b)
Output:
5
25
30

The built-in print() function simply prints whatever the stuff in the brackets 'returns'.
z = c * a Doesn't 'return' anything, it just defines what z is.
When you write z = c * a and then print(z), stating the variable z returns its value and so the print statement has a result to print.

In python, print is a function, which means you can't declare or assign values to variables within it. In some functions, you can specify parameters such as encoding="" in the open function where encoding is the keyword. There is no 'z' keyword for the print statement. The last time, it worked because you assigned the variable before the print statement and then printed it.
Try to do some basic research before you ask here, as this is not too complicated

You can't assign a value to z in the print function, that is related to the 2nd print line, if you print the addition by itself or print z after assigning the value to it in a separate line.

Related

why python can't access global variable value with function parameter name change?

in these two pieces of code, why the second one gives error about local variable assignment? two codes are similar just function parameters is different, in the second one it's able to read the global variable why not in the first one what changes with parameter name change about symbol table?
first one:
def a(z):
z+=1
z=3
a(z)
second one:
def a(z):
b += 1
b = 5
a(b)
There aren't any global variables in use here.
In the first example, z is a parameter to the function, not a global. Note that when you increment z, it does not change in the calling scope, because the z inside the function is a copy of the z you passed in from outside the function.
In the second example, there is no b inside the function (the parameter is z), which is why you get an error inside the function when you try to modify it.
To do what you're trying to do, you should make the parameter a mutable object that contains the value you're trying to mutate; that way you can modify the value inside the function and the caller will have access to the new value. You could define a class for this purpose, or you could simply make it a single-element list:
def a(z):
z[0] += 1
b = [5]
a(b) # b == [6]
Or, if possible, a better approach IMO is not to depend on mutability, and to just return the new value, requiring the caller to explicitly re-assign it within its scope:
def a(z)
return z + 1
b = 5
b = a(b) # b == 6
You should concider def blocks as stand alone.
In the first snippet:
def a(z):
z+=1
What is z ? It's the first parameter of a
In the second snippet:
def a(z):
b += 1
What is b ? It is unknown. That's why this code fails.
You should also notice that in your first snippet, z inside the function is not the same than z=3:
>>> def a(z):
... z+=1
...
>>> z=3
>>> a(z)
>>>
>>> z
3
In the second code, the parameter is "z", and you tried to access that parameter with "b"
def a(z):
b += 1

How to pass tuple to a Matlab function from Python

I have a Matlab function that I'm calling from a python script:
import matlab.engine
eng = matlab.engine.start_matlab()
t = (1,2,3)
z = eng.tstFnc(t)
print z
The function tstFnc is as follows:
function [ z ] = tstFnc( a, b, c )
z = a + b + c
This does not work, however, as Matlab receives one input instead of three. Could this be made to work?
Note: this is a simplified case of what I want to do. In the actual problem I have a variable number of lists that I pass into a Matlab function, which is interpreted in the Matlab function using varargin.
As notes in the comments, the arguments need to be applied instead of passed as a tuple of length 1.
z = eng.tstFnc(*t)
This causes a call to tstFnc with len(t) arguments instead of a single tuple argument. Similarly you could just pass each argument individually.
z = eng.tstFnc(1, 2, 3)

Return and print in functions

I would like to ask the difference between return and print.
def x(n):
a=1
print a,n,
return a
print x(2)
If I type x(2) it will output on the console: 1 2 right?
Is the variable a printed a second time only when I type print x(2)?
When you write
print X(2)
then the function X gets called with 2 as a parameter, let's go inside it:
a=1
print a,2,
return a
it'll print 1 and 2, then it returns a which has the value 1.
The returned value (1) gets printed since you wrote print X(2).
If you have wrote X(2) (without print), you would get 1 and 2 printed, and the returned value will be unused.
def x(n):
print n+1
Now, when x(5) is called, it will print 6 to the console. However, if you did y=x(5), the value of y would be None.
Now:
def x(n):
return x+1
If you called x(5), it would still print 6 to the console. But, if you did y=x(5), the value of y would be 6, not None
The two appear to do similar things, but are quite different.
Most of the time, you'll be using return as an output for a function. Using print implies just that: printing something as a string (perhaps to a file or interpretor, etc).
Also, you can't do anything with a value printed by a function. Returning a value provides you more in this regard since it isn't "garbage collected" like a printed value.
Return also allows you to break out of a function.
>>> def x(y):
... squared = y ** 2
... return squared
...
>>> x(2)
4
>>> z = x(2)
>>> z
4
>>> def a(b):
... squared = b ** 2
... print(squared)
...
>>> a(2)
4
>>> c = a(2)
4
>>> c
>>>
In this example, I have two functions: x and a. Both take one positional argument and either return or print that valued squared.
Notice that if I assign the function with an argument to a variable, I can return that value by calling the variable with the function returning a value but not with a function printing the value.

I keep on getting NameError: name 'a' is not defined?

def main():
a == 3
b = a + 1
c = b + 1
print(a)
if (a<0):
print(a<0)
print(c)
else:
print('a is not less than 0')
print(a)
I watched the khan academy video #1 on Python programming and tried to duplicate it but it kept on giving the error above.
Thanks for your help
I am a first time python user
You are not assigning to a; you are instead testing for equality with a double ==:
a == 3
Since you didn't assign anything to a yet to compare with 3, that results in a NameError.
Remove one = sign to assign instead:
a = 3
This all assumes that the rest of your code is indented correctly to match the rest of your function:
def main():
a = 3
b = a + 1
c = b + 1
print(a)
if (a<0):
print(a<0)
print(c)
else:
print('a is not less than 0')
print(a)
== is used for comparison tests. You need to use = for variable assignment:
a = 3
Also, as your code currently stands, the stuff outside of main will not be able to access a because it is local to the function. Hence, you need to indent it one level:
def main():
a = 3
b = a + 1
c = b + 1
print(a)
if (a<0):
print(a<0)
print(c)
else:
print('a is not less than 0')
print(a)
main()
I think you want the following code:
def main():
a = 3
b = a + 1
c = b + 1
print(a)
if (a<0):
print(a<0)
print(c)
else:
print('a is not less than 0')
print(a)
main()
You want the if statements to be inside the function that you're making, in this case main(). Otherwise, 'a' will not be defined because it is inside the function main(). Welcome to python, and to stack overflow!

Why is b=(a+=1) an invalid syntax in python?

If I write the following in python, I get a syntax error, why so?
a = 1
b = (a+=1)
I am using python version 2.7
what I get when I run it, the following:
>>> a = 1
>>> b = (a +=1)
File "<stdin>", line 1
b = (a +=1)
^
SyntaxError: invalid syntax
>>>
Unlike in some other languages, assignment (including augmented assignment, like +=) in Python is not an expression. This also affects things like this:
(a=1) > 2
which is legal in C, and several other languages.
The reason generally given for this is because it helps to prevent a class of bugs like this:
if a = 1: # instead of ==
pass
else:
pass
since assignment isn't an expression, this is a SyntaxError in Python. In the equivalent C code, it is a subtle bug where the variable will be modified rather than checked, the check will always be true (in C, like in Python, a non-zero integer is always truthy), and the else block can never fire.
You can still do chained assignment in Python, so this works:
>>> a = 1
>>> a = b = a+1
>>> a
2
>>> b
2
a +=1 is a statement in Python and you can't assign a statement to a variable. Though it is a valid syntax in languages like C, PHP, etc but not Python.
b = (a+=1)
An equivalent version will be:
>>> a = 1
>>> a += 1
>>> b = a
As #Ashwini stated, a+=1 is an assigment, not a value. You can't assign it to b, or any variable. What you probably want is:
b = a+1
All the answers provided here are good, I just want to add that you can achieve what you want in a one-line expression, but written in a different manner:
b, a = a+1, a+1
Here you're doing almost the same thing: incrementing a by 1, and assigning the value of a+1 to b - I'm telling 'almost' because here we have two summations instead of one.

Categories

Resources