assigning values in python 1

assigning values in python

Operator:  Example:  Equivalent:
  =         a = b     a = b
  +=        a += b    a = (a + b)
  -=        a -= b    a = (a - b)
  *=        a *= b    a = (a * b)
  /=        a /= b    a = (a / b)
  %=        a %= b    a = (a % b)
  //=       a //= b   a = (a // b)
  **=       a **= b   a = (a ** b)

Here is what the above code is Doing:
1. We are creating a variable called ‘a’ and assigning it the value of ‘5’.
2. We are creating a variable called ‘b’ and assigning it the value of ‘3’.
3. We are creating a variable called ‘c’ and assigning it the value of ‘a + b’.
4. We are creating a variable called ‘d’ and assigning it the value of ‘a – b’.
5. We are creating a variable called ‘e’ and assigning it the value of ‘a * b’.
6. We are creating a variable called ‘f’ and assigning it the value of ‘a / b’.
7. We are creating a variable called ‘g’ and assigning it the value of ‘a % b’.
8. We are creating a variable called ‘h’ and assigning it the value of ‘a // b’.
9. We are creating a variable called ‘i’ and assigning it the value of ‘a ** b’.

Now let’s see what happens when we run the code:

>>> print(a)
5
>>> print(b)
3
>>> print(c)
8
>>> print(d)
2
>>> print(e)
15
>>> print(f)
1.6666666666666667
>>> print(g)
2
>>> print(h)
1
>>> print(i)
125

As you can see, the values of the variables ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’ and ‘i’ have all been calculated and printed to the screen.

Now let’s try using the assignment operators:

>>> a = 5
>>> b = 3
>>> a += b
>>> print(a)
8
>>> a = 5
>>> b = 3
>>> a -= b
>>> print(a)
2
>>> a = 5
>>> b = 3
>>> a *= b
>>> print(a)
15
>>> a = 5
>>> b = 3
>>> a /= b
>>> print(a)
1.6666666666666667
>>> a = 5
>>> b = 3
>>> a %= b
>>> print(a)
2
>>> a = 5
>>> b = 3
>>> a //= b
>>> print(a)
1
>>> a = 5
>>> b = 3
>>> a **= b
>>> print(a)
125

As you can see, the assignment operators work in the same way as the arithmetic operators, but they also assign the result of the operation to the variable on the left hand side of the operator.

Now let’s try using the increment and decrement operators:

>>> a = 5
>>> a += 1
>>> print(a)
6
>>> a = 5
>>> a -= 1
>>> print(a)
4
>>> a = 5
>>> a *= 2
>>> print(a)
10
>>> a = 5
>>> a /= 2
>>> print(a)
2.5
>>> a = 5
>>> a %= 2
>>> print(a)
1
>>> a = 5
>>> a //= 2
>>> print(a)
2
>>> a = 5
>>> a **= 2
>>> print(a)
25

As you can see, the increment and decrement operators work in the same way as the assignment operators, but they increment or decrement the variable on the left hand side of the operator by 1.

Now let’s try using the comparison operators:

>>> a = 5
>>> b = 3
>>> a == b
False
>>> a != b
True
>>> a > b
True
>>> a < b False >>> a >= b
True
>>> a <= b False As you can see, the comparison operators compare the values of the variables on the left and right hand side of the operator and return a Boolean value (True or False). Now let's try using the logical operators: >>> a = True
>>> b = False
>>> a and b
False
>>> a or b
True
>>> not a
False
>>> not b
True

As you can see, the logical operators return a Boolean value (True or False) depending on the values of the variables on the left and right hand side of the operator.

Now let’s try using the bitwise operators:

>>> a = 0b1010
>>> b = 0b1100
>>> a & b
0b1000
>>> a | b
0b1110
>>> ~a
-0b1011
>>>

Similar Posts