0% found this document useful (0 votes)
31 views2 pages

Python Variable & Arithmetic Exercises

The document contains a list of 50 Python practice questions focused on variables and arithmetic operations. Each question requires writing code to perform specific calculations or manipulations with given variables. The questions cover a range of topics including basic arithmetic, average calculations, and simple interest, among others.

Uploaded by

giigdyerug
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Python Variable & Arithmetic Exercises

The document contains a list of 50 Python practice questions focused on variables and arithmetic operations. Each question requires writing code to perform specific calculations or manipulations with given variables. The questions cover a range of topics including basic arithmetic, average calculations, and simple interest, among others.

Uploaded by

giigdyerug
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Variable & Arithmetic Practice Questions

1. Write a program that initializes `a = 5`, `b = 10`, then increases `a` by 2 and prints `a + b`.

2. Set `x = 15`, `y = 7`. Decrease `y` by 3 and print `x - y`.

3. Assign `m = 4`, `n = 2`. Multiply both by 2 and print the product of `m` and `n`.

4. Write code to add 5 to a variable `a = 20` and subtract 3 from `b = 8`, then print `a - b`.

5. Start with `a = 100`, `b = 50`. Divide `a` by 2, then print `a // b`.

6. Take `x = 3`, `y = 4`. Raise `x` to the power of `y` and print the result.

7. Assign `a = 10`, then do `a += 10`, then `a -= 5`. Print the final value.

8. Given `p = 7`, `q = 3`, calculate and print the remainder of `p % q`.

9. Write a program to swap two numbers `a = 5` and `b = 10`, then print their values.

10. Set `x = 9`, `y = 6`, then increase both by 3. Print `x * y`.

11. Initialize `a = 1`. Do `a = a + 2`, `a = a * 3`, and print `a`.

12. Given `m = 25`, `n = 5`, print the value of `m / n` and `m // n`.

13. Assign `a = 14`, `b = 6`, then find the average of `a` and `b`.

14. Input any two numbers and print their sum.

15. Set `a = 50`, then perform `a %= 9` and print the result.

16. Assign `x = 3.5`, `y = 1.2`, add them and print the sum.

17. Calculate the square of a number `a = 6` using multiplication.

18. Write a program to increase `a = 2` five times using `a += 1` in each step and print `a`.

19. Given `x = 8`, decrement it by 1 and multiply the result by 3. Print it.

20. Assign `a = 100`, subtract 40 and then divide the result by 2. Show the result.

21. If `p = 3`, `q = 5`, perform and print `(p + q) * (p - q)`.

22. Set `a = 13`, `b = 4`. Print the result of `a**b`.

23. Assign `x = 12.5`, `y = 2.5`, and calculate `x/y`.

24. Create a variable `temp = 30`, convert it to Fahrenheit using `F = (temp * 9/5) + 32`.

25. Write a program to find the cube of a number using exponentiation.

Page 1
Python Variable & Arithmetic Practice Questions

26. If `a = 11`, `b = 3`, find the floor division and modulus.

27. Given `total = 500`, `count = 8`, find the average.

28. Set `a = 9`, increase by 10%, then print the new value.

29. Set `price = 100`, apply 18% GST, print final amount.

30. If `a = 25`, subtract 5, multiply the result by 4, and add 2. Print the final output.

31. Write a program to calculate `simple interest = (P*T*R)/100` where `P=1000, T=2, R=5`.

32. Given `a = 7`, `b = 4`, calculate `a^2 + b^2`.

33. Take two variables and print whether their sum is even or odd.

34. Assign `num = 153`, break it into digits and print their sum.

35. Write a program to reverse a 3-digit number.

36. Set `x = 20`, `y = 3`, perform all five basic operations and print results.

37. Given a number `a = 10`, increment it without using `+` operator.

38. Write a program that multiplies two numbers and subtracts a third number.

39. Create a variable `radius = 7`, find the area of a circle.

40. Assign `a = 20`, `b = 7`, `c = 3`, then print `a + b * c`.

41. If `length = 10`, `breadth = 5`, calculate the perimeter of the rectangle.

42. Assign `base = 6`, `height = 4`, calculate the area of triangle.

43. Write a program to find the average of three given numbers.

44. Given `a = 3`, `b = 5`, `c = 7`, compute `a * b + c`.

45. Assign `a = 8`, use only `*` operator to find the square.

46. Create a program to compute `volume = length * breadth * height` with inputs.

47. Write a program to calculate BMI using `BMI = weight/(height*height)` with dummy values.

48. Given `n = 37`, check if it's divisible by 3 and print result.

49. Calculate the percentage marks out of 500 marks.

50. If `a = 15`, `b = 4`, print both `(a/b)` and `(a//b)`.

Page 2

Common questions

Powered by AI

To check divisibility in Python, you use the modulus operator `%`. If `a % b == 0`, the number `a` is divisible by `b`. For example, to check if `n = 37` is divisible by `3`, compute `37 % 3`. Since this results in 1, `37` is not divisible by `3` .

In Python, a common approach to swap two variables without using a temporary variable is to exploit tuple unpacking. For instance, to swap `a = 5` and `b = 10`, you can use the syntax `a, b = b, a`. After executing this, the value of `a` becomes 10 and `b` becomes 5, which effectively swaps their values .

To calculate the average of two numbers in Python, you add them together and then divide by the number of values, which is 2. For example, given `a = 14` and `b = 6`, the average can be calculated as `(a + b) / 2`. Evaluating this expression, `(14 + 6) / 2` gives an average of 10.0 .

Python employs the `**` operator for exponentiation. For example, to calculate the power `a**b`, where `a = 13` and `b = 4`, you use `a**b`. This expression evaluates to `13**4`, which results in 28561 .

Calculating geometric properties like area, perimeter, and volume can influence programming solutions by providing essential data for tasks such as resource allocation, layout design, and material estimation in real-world applications. For example, in Python, calculating the perimeter of a rectangle, given `length = 10` and `breadth = 5`, is straightforward using `perimeter = 2 * (length + breadth)`, yielding a result of 30 .

To calculate the area of a triangle in Python, you use the formula `area = 0.5 * base * height`. For example, with `base = 6` and `height = 4`, the area is calculated as `0.5 * 6 * 4`, resulting in an area of 12.0 .

Simple interest in Python can be calculated using the formula `simple_interest = (P * T * R) / 100`, where `P` is the principal, `T` the time, and `R` the rate. For example, with `P=1000`, `T=2`, and `R=5`, the simple interest is calculated as `(1000 * 2 * 5) / 100`, resulting in 100.0 .

In Python, division and modulus can be calculated using `/` for division and `%` for modulus. Given `a = 11` and `b = 3`, division is `a / b` which evaluates to `11 / 3 = 3.6667`, and modulus is `a % b` which yields `11 % 3 = 2` .

The perimeter of a rectangle is found using the formula `perimeter = 2 * (length + breadth)`. For example, given `length = 10` and `breadth = 5`, the perimeter is calculated as `2 * (10 + 5)`, resulting in 30 .

In Python, to determine if the sum of two numbers is even or odd, you can use the modulus operator. First, calculate the sum `sum = a + b`, then check if `sum % 2 == 0`. If true, the sum is even; otherwise, it's odd. For instance, if `a = 3` and `b = 5`, `sum = 8`. Since `8 % 2 == 0`, the sum is even .

You might also like