Python Learning Journey:Operator Precedence,Scenario-Based Questions and While Loops
Recently i explored operator precedence,scenario-based operator programming questions while loops and micro to macro thinking.
1.What is Operator precedence in python?
It determines the order in which python evaluates operators in an expression.
Example:
result = 10 + 5 *2
print(result)
Output:
20
Here multiplication has higher precedence than addition.
We should follow the order:
- Parenthesis
- Exponentiation
- Multiplication
- Division
- Addition/Subtraction
2.Python Scenario Based Questions:
Scenario1:Five students standing in a line
A teacher asks five students to stand in a line.Each student wears a badge with the number 1.Display the badge numbers from left to right.
`no=1
while count <=5:
print(no,end="")
count = count+1
Output:
11111
`
*Scenario2:Assigning roll numbers
Five students enter the classroom one after another.
The first student gets roll number1,second gets roll number2 and so on. Display the assigned roll numbers.
*
`number = 1
while number <= 5:
print(number, end=" ")
number = number + 1
Output:
12345`
3.Difference between for loop and while loop
Python provides two commonly used loops. for and while
Both used to repeat a block of code but they work differently
*For loop * *
- Iterates over a sequence or Iterable.
- Useful when iterating through know range or collection.
- Automatically moves to the next item.
While Loop
- Repeats a while condition is true .
- Useful when repetition depends on a condition.
- Usually requires manual variable updates.
4.Scenario3:Even-numbered positions
A coach asks students to stand only at even-numbered positions in a queue.Display first five positions.
`position = 2
while position<=10:
print(position,end="")
position = position + 2
Output:
2 4 6 8 10`
**5.Scenario 4:Lift stops every third floor
A newly constructed building has a special lift that does not stop o every floor For safety reasons it stops only at every third floor
A person enters the lift at the ground floor and presses the Up button five times.**
`floor = 0
count = 0
while count<5:
floor = floor + 3
print(floor,ends="")
count = count + 1
Output:
3 6 9 12 15`
** 6.Scenario 5:Rocket countdown
A rocket launches with a countdown using only even numbers starting from 10.Display the countdown**
number = 10
while number>=0:
print(number,end="")
number = number- 2
7.Micro to Macro Thinking
It is an approach where we start by understanding smaller part of a problem and gradually combine them to solve a bigger problem.
1.Understand the individual variables.
2.Understand the conditions.
3.Understand the loop.
4.Understand how the variable changes.
5.Combine these elements to solve the complete problem.
This approach helps me break bigger programming problems into small manageable steps.
I am continued to improve my python problem solving skills one at a time.