Advertisements
Advertisements
Question
What are the assignment operators that can be used in Python?
Advertisements
Solution
Assignment operators:
In Python, = is a simple assignment operator to assign values to variables. Let a = 5 and b = 10 assign the value 5 to a and 10 to b this two assignment statements can also be given as a, b = 5, 10 that assigns the value 5 and 10 on the right to the variables a and b respectively. There are various compound operators in Python like + =, – =,* =, / = % =,** = and //= are also available.
| Operator | Description | Example |
| Assume x=10 | ||
| = | Assigns right side operands to left variable | >>> x=10 >>> b= "Computer" |
| += | Added and assign back the result to left operand i.e. x=30 | >>> x+=20 # x=x+20 |
| -= | Subtracted and assign back the result to left operand i.e. x=25 | >>> x-=5 # x=x-5 |
| * = | Multiplied and assign back the result to left operand i.e. x=125 | >>> x*=5 # x=x*5 |
| /= | Divided and assign back the result to left operand i.e. x=62.5 | >>> x/=2 # x=x/2 |
| %= | Taken modulus(Remainder) using two operands and assign the result to left operand i.e. x=2.5 | >>> x%=3#x=x%3 |
| **= | Performed exponential (power) calculation on operators and assign value to the left operand i.e. x=6.25 | >>> x**=2#x=x**2 |
| //= | Performed floor division on operators and assign value to the left operand i.e. x=2.0 | >>> x//=3 |
APPEARS IN
RELATED QUESTIONS
Write short notes on Tokens.
What is a literal?
Write short notes on the Arithmetic operator with examples.
Explain the Ternary operator with examples.
Discuss in detail Tokens in Python.
Which of the following is a valid java keyword?
The default value of a boolean variable is ______.
A student executes the following code to increase the value of a variable 'x' by 2.
He has written the following statement, which in incorrect.
x=+2;
What will be the correct statement?
- x+=2;
- x=2
- x=x+2;
Consider the following program segment in which the statements are jumbled, choose the correct order of statements to swap two variables using the third variable.
void swap(int a, int b)
{ a=b; -> (1)
b=t; -> (2)
int t=0; -> (3)
t=a; -> (4)
}
Define a class with the following specifications:
Class name: Bank
Member variables:
double p - stores the principal amount
double n - stores the time period in years
double r - stores the rate of interest
double a - stores the amount
member methods:
void accept () - input values for p and n using Scanner class methods only.
void calculate () - calculate the amount based on the following conditions:
| Time in (Year) | Rate % |
| Upto `1/2` | 9 |
| >`1/2` to 1 year | 10 |
| > 1 to 3 year | 11 |
| > 3year | 12 |
`a = p (1+r/100)^n`
void display () - display the details in the given format.
Write the main method to create an object and call the above methods.
