Shoaib Akhtar
ASSIGNMENT OP-
ERATORS PRACTICE
Assignment Operators Homework
Due: Friday 11:55 PM PKT
Instructions:
• Ò Write your code in Google Colab.
• After writing the code for all questions, you will download the .ipynb file from
Google Colab and then submit it.
• Use assignment operators (=, +=, -=, *=, /=,
• º Show the value after each operation
• Late submissions will be penalized with zero marks
Questions:
Question 1: Basic Assignment Operations
Problem:
Create a variable ‘x‘ with initial value 10. Then perform these operations and print the
value after each step:
• Add 5 to x using +=
• Subtract 3 from x using -=
• Multiply x by 2 using *=
• Divide x by 4 using /=
Explanation:
Use different assignment operators to modify the variable and print after each operation.
1
Sample Output:
Initial x: 10
After adding 5: 15
After subtracting 3: 12
After multiplying by 2: 24
After dividing by 4: 6.0
Question 2: Bank Account Balance
Problem:
Create a variable ‘balance‘ with initial value 1000. Then perform these banking opera-
tions:
• Deposit 500 using +=
• Withdraw 300 using -=
• Apply 10
• Pay 50 fee using -=
Explanation:
Use assignment operators to update the bank balance after each transaction.
Sample Output:
Initial balance: 1000
After deposit: 1500
After withdrawal: 1200
After interest: 1320.0
After fee: 1270.0
2
Question 3: Shopping Cart Total
Problem:
Create a variable ‘total‘ with initial value 0. Then add these items to your cart:
• Shirt: 1200 (use +=)
• Shoes: 2500 (use +=)
• Apply 15
• Add 200 shipping charge using +=
Explanation:
Use assignment operators to calculate the final total after all operations.
Sample Output:
Initial total: 0
After shirt: 1200
After shoes: 3700
After discount: 3145.0
After shipping: 3345.0
Final total: 3345.0
Question 4: Student Score Calculator
Problem:
A student has these scores in different subjects:
• Math: 85 (use =)
• Science: 92 (use +=)
• English: 78 (use +=)
• Calculate average using /=
• Add 5 bonus marks using +=
Explanation:
Use assignment operators to calculate total and average scores.
Sample Output:
Math score: 85
Total after Science: 177
Total after English: 255
Average: 85.0
After bonus: 90.0
Question 5: Modulo Assignment Operator
3
Problem:
Create a variable ‘number‘ with value 27. Then perform these operations:
• Find remainder when divided by 5 using %=
• Find remainder when divided by 4 using %=
• Find remainder when divided by 7 using %=
Explanation:
Use modulo assignment operator to find remainders.
Sample Output:
Initial number: 27
After % 5: 2
After % 4: 2
After % 7: 2