Repository navigation
Expand file tree
/
Copy path099-Assignment-Operators.py
More file actions
142 lines (118 loc) · 5.11 KB
/
Copy path099-Assignment-Operators.py
File metadata and controls
142 lines (118 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
===============================================================================
File Name : 99-Assignment-Operators.py
Description : Assignment Operators in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
Assignment operators are used to assign values to variables. They can also
perform arithmetic operations and update the variable in a single statement.
Assignment Operators:
1. = (Assignment)
2. += (Add and Assign)
3. -= (Subtract and Assign)
4. *= (Multiply and Assign)
5. /= (Divide and Assign)
6. //= (Floor Divide and Assign)
7. %= (Modulus and Assign)
8. **= (Exponentiation and Assign)
Syntax:
variable operator= value
Example:
number += 5
"""
# -----------------------------------------------------------------------------
# Assigning an initial value to the variable.
# -----------------------------------------------------------------------------
number = 20
# -----------------------------------------------------------------------------
# Displaying the initial value.
# -----------------------------------------------------------------------------
print("Initial Value :", number)
# Output:
# Initial Value : 20
print()
# -----------------------------------------------------------------------------
# Using the assignment (=) operator.
# -----------------------------------------------------------------------------
number = 30
# -----------------------------------------------------------------------------
# Displaying the updated value.
# -----------------------------------------------------------------------------
print("After '=' :", number)
# Output:
# After '=' : 30
# -----------------------------------------------------------------------------
# Using the add and assign (+=) operator.
# -----------------------------------------------------------------------------
number += 10
# -----------------------------------------------------------------------------
# Displaying the updated value.
# -----------------------------------------------------------------------------
print("After '+=' :", number)
# Output:
# After '+=' : 40
# -----------------------------------------------------------------------------
# Using the subtract and assign (-=) operator.
# -----------------------------------------------------------------------------
number -= 5
# -----------------------------------------------------------------------------
# Displaying the updated value.
# -----------------------------------------------------------------------------
print("After '-=' :", number)
# Output:
# After '-=' : 35
# -----------------------------------------------------------------------------
# Using the multiply and assign (*=) operator.
# -----------------------------------------------------------------------------
number *= 2
# -----------------------------------------------------------------------------
# Displaying the updated value.
# -----------------------------------------------------------------------------
print("After '*=' :", number)
# Output:
# After '*=' : 70
# -----------------------------------------------------------------------------
# Using the divide and assign (/=) operator.
# -----------------------------------------------------------------------------
number /= 7
# -----------------------------------------------------------------------------
# Displaying the updated value.
# -----------------------------------------------------------------------------
print("After '/=' :", number)
# Output:
# After '/=' : 10.0
# -----------------------------------------------------------------------------
# Using the floor divide and assign (//=) operator.
# -----------------------------------------------------------------------------
number //= 3
# -----------------------------------------------------------------------------
# Displaying the updated value.
# -----------------------------------------------------------------------------
print("After '//=' :", number)
# Output:
# After '//=' : 3.0
# -----------------------------------------------------------------------------
# Using the modulus and assign (%=) operator.
# -----------------------------------------------------------------------------
number %= 2
# -----------------------------------------------------------------------------
# Displaying the updated value.
# -----------------------------------------------------------------------------
print("After '%=' :", number)
# Output:
# After '%=' : 1.0
# -----------------------------------------------------------------------------
# Assigning a new value for exponentiation.
# -----------------------------------------------------------------------------
number = 5
# -----------------------------------------------------------------------------
# Using the exponentiation and assign (**=) operator.
# -----------------------------------------------------------------------------
number **= 3
# -----------------------------------------------------------------------------
# Displaying the updated value.
# -----------------------------------------------------------------------------
print("After '**=' :", number)
# Output:
# After '**=' : 125