15,665,444 members
See more:
Ques 1.
Python
```x=+1
print(x)```

Ques 2.
Python
```x=12
def f1(a,b=x):
print(a,b)
x=15
f1(4)
output:4,12```

What I have tried:

I don understand the output in first question isn't it arise a name error and in 2nd question why is a =4 and b =12.
Posted
Updated 15-Feb-23 18:25pm
v2

## Solution 1

1) Because "+" is a "dual operator" - it can work with one or two parameters.
With two, it is an addition operator:
Python
`x = 3 + 3`
But with one, it is a sign operator:
Python
`x = +2`
In this, it is matched by "-":
Python
`x = 3 - 4`
Python
`x = -2`

Unary Operators in Python | Examples of Different Operators in Python[^]
2) Because `f1` is a function: it takes two parameters you pass when you call it named `a` and `b` but if you omit the second parameter it defaults to the value of `x`
Python Functions[^]

Sandeep Mewara 16-Feb-23 3:21am
Just to add for second case, x value is not the current value that gets defaulted when called through one parameter only.

## Solution 3

Quote:
Ques 1.
x=+1
This will always be 1 as it's an assignment not an increment.
If you do x+=1 without initializing, it will throw an error.

Quote:
Ques 2.
output:4,12

This happened as during the compiler run, value of b was already defined as 12 in sequence of code. (It is not a reference that will be updated if x changes later). For more clarity, have updated your code to show how execution happened and values of variables were being used:
Python
```x=12
print('W',x)
def f1(a,b=x):
print('X',x)
print(a,b)
x=15
print('Y',x)
f1(4)
print('Z',x)```
Output for it is:
```W 12
Y 15
X 15
4 12
Z 15```

You can see the value of x did change to 15 before the function call (even x value is 15 inside), but the value of b was set to 12 as it was the case when the function definition was setup in execution.

## Solution 2

A1: `x` initial value is `0`, so `0 + 1 = 1`
A2: Because `x` is defined before the function, therefore predefined as the default value, and `x = 15` is not passed to the function.

Sandeep Mewara 16-Feb-23 3:18am
@Graeme - I am not entriely sure that would be the case. There is no default value of variables in python. If you try:
x = x+1 without defining x, or you do print(x) without defining, it will throw an error.
Richard Deeming 16-Feb-23 4:29am
I suspect you're confusing `=+` with `+=` - the OP's code is assigning `+1` to the variable, not adding `1` to the existing value of the variable. :)