Python Pioneers: A Beginner's Guide ๐Ÿ‡ช๐Ÿ‡น
8.82K subscribers
346 photos
40 videos
24 files
64 links
Welcome to Python Pioneers: Empowering Ethiopian students to embark on their coding journey.

๐Ÿ“ข Channel: @python_pioneers
๐Ÿ‘ฅ Group: @python_pioneers_chat
Download Telegram
FLOW CONTROL : WHILE LOOP

แ‹›แˆฌ แˆ›แ‰ณ แŠจแ‰ฃแˆˆแˆแ‹ แ‰ตแˆแˆ…แˆญแ‰ต แ‹จแ‰€แŒ แˆˆ แ‰ Python แˆตแˆˆ While loop แŠฅแŠ•แˆ›แˆซแˆˆแŠ•แข

Stay tuned!

โฑ๏ธ 8 : 00 PM EAT

@python_pioneers
@python_pioneers
๐Ÿ‘1
What is a While Loop?

แ‹จแ‰ฐแ‹ˆแˆฐแА แˆแŠ”แ‰ณ แŠฅแ‹แАแ‰ต แŠฅแˆตแŠจแˆ†แА แ‹ตแˆจแˆต แ‰ แ“แ‹ญแ‹˜แŠ• แ‹แˆตแŒฅ แˆˆแ‰ฐแ‹ˆแˆฐแА แŒŠแ‹œ loop แ‰ แ‰ฐแ‹ฐแŒ‹แŒ‹แˆš แ‹จแŠฎแ‹ต แŠฅแŒˆแ‹ณแŠ• แ‹ญแˆแŒฝแˆ›แˆแข แŒฅแ‰…แˆ แˆ‹แ‹ญ แ‹จแˆšแ‹แˆˆแ‹ แ‹จแ‹ตแŒแŒแˆžแˆฝ แ‰ฅแ‹›แ‰ต แŠ แˆตแ‰€แ‹ตแˆž แ‰ แˆ›แ‹ญแ‰ณแ‹ˆแ‰…แ‰ แ‰ต แŒŠแ‹œ แŠฅแŠ“ แŠ แŠ•แ‹ณแŠ•แ‹ต แˆแŠ”แ‰ณแ‹Žแ‰ฝ แ‰ แˆšแˆŸแˆ‰แ‰ แ‰ต แŒŠแ‹œ แАแ‹.

While loop แ‰ Python แŠ แŠ•แ‹ตแŠ• block of code specify แ‹จแ‰ฐแ‹ฐแˆจแŒˆแ‹ condition แŠฅแ‹แАแ‰ต แŠฅแˆตแŠจแˆ†แА แ‹ตแˆจแˆต repeatedly(แ‰ แ‰ฐแ‹ฐแŒ‹แŒ‹แˆš) execute แ‹จแˆšแ‹ซแˆจแŒ แ‹จ Python flow control แŠ แ‹ญแАแ‰ต แАแ‹แข While loopแŠ• แ‹จแˆแŠ•แŒ แ‰€แˆ˜แ‹ number of iteration แˆˆแˆ›แ‹ญแ‰ณแ‹ˆแ‰…แ‰ฃแ‰ธแ‹ แ‹ˆแ‹ญแˆ executionแŠ แ‰ธแ‹ on some condition แˆ‹แ‹ญ depend แˆˆแˆšแˆ†แŠ•แ‰ฃแ‰ธแ‹ blocks of code แАแ‹แข

Syntax of While Loop

แ‹จ While loop basic syntax แ‹ญแˆ„แŠ•แŠ• แ‹ญแˆ˜แˆตแˆ‹แˆ:

while condition:
# Code block to be executed

- condition: แŠ แŠ•แ‹ต condition แŠฅแ‹แАแ‰ต แ‹ˆแ‹ญแˆ แˆ€แˆฐแ‰ต แŠฅแŠ•แ‹ฐแˆ†แА evaluate แ‹จแˆšแ‹ซแˆจแŒ expression แАแ‹แข

- Code block: แ‹ญแˆ„ แ‹ฐแŒแˆž specified แ‹จแˆ†แАแ‹ condition True แ‰ แˆ†แА แ‰แŒฅแˆญ execute แ‹จแˆšแ‹ฐแˆจแŒ แ‹จstatements แˆตแ‰ฅแˆตแ‰ฅ แАแ‹แข

Flowchart of a While Loop

1. Evaluate the condition.
2. If the condition is true, execute the code block.
3. Re-evaluate the condition.
4. If the condition is still true, repeat the code block.
5. Continue until the condition becomes false.


to be cont'd...

#WhileLoop

@python_pioneers
@python_pioneers
โšก1
...cont'd

แˆแˆณแˆŒ:
แ‰ แ‹šแˆ… แˆแˆณแˆŒ แ‹จแˆแŠ“แ‹จแ‹ program แŠจ 1 แŠฅแˆตแŠจ 5 แ‹ซแˆ‰ แ‰แŒฅแˆฎแ‰ฝแŠ• print แ‹จแˆšแ‹ซแˆจแŒ แАแ‹:

i = 1
while i <= 5:
print(i)
i += 1 # Increment the value of i by 1

Detailed Explanation

1. Initialization: i is initialized to 1 แˆ›แˆˆแ‰ตแˆ i แ‹จแˆšแ‰ฃแˆ variable initial valueแ‹แŠ• แŠ แŠ•แ‹ต แŠ แˆญแŒˆแŠ“แˆแข

2. Condition Check: i <= 5 check แ‹ญแ‹ฐแˆจแŒ‹แˆ: conditionแŠก แŠฅแ‹แАแ‰ต แŠจแˆ†แА loopแŠก แ‹ญแ‰€แŒฅแˆ‹แˆ otherwise loopแŠก terminate แ‹ซแˆจแŒ‹แˆแข

3. Body of Loop: print(i) แ‹จ variable 'i' แŠ• current value print แ‹ซแˆจแŒ‹แˆแข

4. Increment: i is incremented by 1 using i += 1.
Increment แˆ›แˆˆแ‰ต แ‹จแŠ แŠ•แ‹ตแŠ• variable value แ‰ แˆ†แА แ‹ซแˆ…แˆ แŠ แˆ›แ‹แŠ•แ‰ต แˆ˜แŒจแˆ˜แˆญ แˆ›แˆˆแ‰ต แАแ‹แฃ แ‰ แˆ‹แ‹ญแŠ›แ‹ case each time the variable 'i' is being incremented by 1.

5. Re-evaluation: The condition i <= 5 is checked again with the new value of i.

If you have understood about while loop from the above two posts, answer these questions given below.

#WhileLoop

@python_pioneers
@python_pioneers
โค1๐Ÿ‘1
5. How is the condition in a while loop written in Python?
Anonymous Quiz
20%
A. if condition:
68%
B. while condition:
4%
C. until condition:
8%
D. for condition in range:
Comment your answers from now on...

6. What is the output of the following code?

i = 0
while i < 3:
print(i)
i += 1

A) 0 1 2

B) 0 1 2 3

C) 1 2 3

D) 0 1
7. How many times will "Hello, World!" be printed?

count = 5
while count > 0:
print("Hello, World!")
count -= 1

A) 6

B) 5

C) 4

D) Infinite
8. What will be the value of sum after the execution of the following code?

sum = 0
i = 1
while i <= 5:
sum += i
i += 1

A) 10

B) 15

C) 20

D) 5
9. What is the purpose of the following while loop?

n = 10
while n > 0:
n -= 1

A) To count up from 1 to 10

B) To count down from 10 to 1

C) To count down from 10 to 0

D) To count up from 0 to 10
10. What will be the output of the following code?

x = 0
while x < 4:
x += 1
print(x)

A) 0 1 2 3

B) 1 2 3 4

C) 0 1 2 3 4

D) 1 2 3
๐Ÿ‘1
แˆแˆ‹แ‰ฝแˆแŠ•แˆ แˆตแˆˆแ‰ฐแˆณแ‰ตแแ‰ฝแˆ แ‰ แŒฃแˆ แŠฅแŠ“แˆ˜แˆฐแŒแŠ“แˆˆแŠ•!

แ‰ แ‰ดแŠญแŠ–แˆŽแŒ‚แ‹ แ‹˜แˆญแ แ‰ฅแ‰ แ‹จแˆ†แА แ‰ตแ‹แˆแ‹ต แˆ›แแˆซแ‰ต แАแŒŒ แˆ‹แ‹ญ แŠ แ‹ตแŒ‹ แˆแŠ“แ‹ซแ‰ต แ‹จแˆแŠ•แˆแˆแŒ‹แ‰ตแŠ• แŠขแ‰ตแ‹ฎแŒตแ‹ซ แˆˆแˆ˜แŒˆแŠ•แ‰ฃแ‰ต แ‹‹แАแŠ› แŠ แˆ›แˆซแŒญ แАแ‹แข

แˆ˜แˆแŠซแˆ แˆแˆฝแ‰ต!

@python_pioneers
@python_pioneers
โค2๐Ÿ‘1๐Ÿ™1
https://t.me/tapswap_mirror_1_bot?start=r_6163528558 ๐ŸŽ +2.5k Shares as a first-time gift
๐Ÿ‘2
Infinite Loop

While loopแŠ• แˆตแŠ•แŒ แ‰€แˆ common แ‹จแˆ†แА แ‰ฝแŒแˆญ แ‰ขแŠ–แˆญ แ‹จinfinite loop แˆ˜แˆแŒ แˆญ แАแ‹แข แ‹ญแˆ…แˆ infinite loop conditionแŠก False แˆ˜แ‰ผแˆ แ‰ แˆ›แ‹ญแˆ†แŠ•แ‰ แ‰ต แŒŠแ‹œ แ‹ญแˆแŒ แˆซแˆแข


Example of an infinite loop:

while True:
print("This will print forever")

แŠจแˆ‹แ‹ญ แ‹ซแˆˆแ‹ แ•แˆฎแŒแˆซแˆ "This will print forever" แ‹จแˆšแˆˆแ‹แŠ• แ‹/แАแŒˆแˆญ แ‹ซแˆˆ แŒˆแ‹ฐแ‰ฅ print แ‹ซแˆจแŒ‹แˆแข


Breaking Out of a Loop

Infinite Loop avoid แˆˆแˆ›แˆจแŒ "break" statement แˆ˜แŒ แ‰€แˆ แŠฅแŠ•แ‰ฝแˆ‹แˆˆแŠ•แฃ แ‹ญแˆ…แˆ แ‹จแˆ†แА condition set แŠ แˆญแŒˆแŠ• แŠฅแ‹แАแ‰ต แ‰ แˆšแˆ†แŠ•แ‰ แ‰ต แŒŠแ‹œ loopแŠกแŠ• exit แŠฅแŠ•แ‹ตแŠ“แˆจแŒ แ‹ญแˆจแ‹ณแŠ“แˆแข



i = 1
while i <= 10:
print(i)
if i == 5:
break # Exit the loop when i is 5
i += 1

แŠจแˆ‹แ‹ญ แ‹ซแˆˆแ‹ แ•แˆฎแŒแˆซแˆ แˆ˜แŒ€แˆ˜แˆชแ‹ซ แ‹จแ‰ฐแƒแˆแ‹ แŠจ 1 แŠฅแˆตแŠจ 10 แ‹ซแˆ‰ แ‰แŒฅแˆฎแ‰ฝแŠ• print แŠฅแŠ•แ‹ฒแ‹ซแˆจแŒ แ‰ขแˆ†แŠ•แˆ แ‹จ'i' value 5 แˆฒแˆ†แŠ• แŠฅแŠ•แ‹ฒแ‹ซแ‰†แˆ แ‰…แ‹ตแˆ˜-แˆแŠ”แ‰ณ แˆตแˆ‹แˆตแ‰€แˆ˜แŒฅแŠ• แˆแŠญ แ‹จ'i' value 5 แˆฒแˆ†แŠ• loopแŠก แ‹ญแ‰†แˆ›แˆแข


Using Else with While Loop

Python 'else' clause แŠจ while loop แŒ‹แˆญ แŠฅแŠ•แ‹ตแŠ•แŒ แ‰€แˆ แ‹ญแˆแ‰…แ‹ณแˆแข แ‹จ'else' block executed แ‹จแˆšแˆ†แАแ‹ แ‹จ while loop condition แˆ€แˆฐแ‰ต แˆฒแˆ†แŠ• แАแ‹แข (i.e., the loop terminates naturally).

แˆแˆณแˆŒ

i = 1
while i <= 5:
print(i)
i += 1
else:
print("Loop has ended")

...แ‹ญแ‰€แŒฅแˆ‹แˆ

#WhileLoop

@python_pioneers
@python_pioneers
๐Ÿ‘4โค1
๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

-> @lewtayew

#Fun

@python_pioneers
๐Ÿ˜4๐Ÿ‘1
Common Use Cases

1. Waiting for User Input: แ‹จแˆแˆˆแŒแАแ‹แŠ• data แŠฅแˆตแŠจแˆแŠ“แŒˆแŠ User input แ‰ แ‰ฐแ‹ฐแŒ‹แŒ‹แˆš แŠฅแŠ•แ‹ตแŠ•แ‰€แ‰ แˆ แ‹ญแˆจแ‹ณแŠ“แˆแข


2. Processing Data Streams: แˆ˜แŒจแˆจแˆป แˆ‹แ‹ญ แŠฅแˆตแŠจแˆแŠ•แ‹ฐแˆญแˆต data แŠจfile แ‹ˆแ‹ญแˆ แŠจ network stream continuously แŠฅแŠ•แ‹ตแŠ“แАแ‰ฅ แ‹ญแˆจแ‹ณแŠ“แˆแข

3. Polling: แ‹จแŠ แŠ•แ‹ตแŠ• resource status periodically check แŠฅแŠ•แ‹ตแŠ“แˆจแŒ แ‹ญแˆจแ‹ณแŠ“แˆแข

แˆแˆณแˆŒ: User Input

Here's an example where we keep asking the user for a positive number:

number = -1
while number < 0:
number = int(input("Enter a positive number: "))
print("You entered: ", number)

แŠจแˆ‹แ‹ญ แ‹ซแˆˆแ‹ แ•แˆฎแŒแˆซแˆ userแŠก positive แ‰แŒฅแˆญ แŠฅแŠ•แ‹ฒแ‹ซแˆตแŒˆแ‰ฃ repeatedly แ‹ญแŒ แ‹ญแ‰€แ‹‹แˆแฃ แ‰แŒฅแˆฉแŠ• แ‹ฐแŒแˆž print แ‹ซแˆจแŒ‹แˆแฃ if the user enters a positive number the loop ends, because the condition becomes false.

...to be continued

#WhileLoop

@python_pioneers
@python_pioneers
๐Ÿ‘2
๐Ÿ‘‹ Let me share with you some beautiful and powerful quotes that I gathered from different sources.๐Ÿ‘‡


People were created to be loved. Things were created to be used. The reason why the world is in chaos, is because things are being loved and people are being used.

- John Green

You get a strange feeling when you're about to leave a place. Like you'll not only miss the people you love but you'll miss the person you are now at this time and this place, because you'll never be this way ever again.

- Azar Nafisi

People ask if you have graduated, if you have married, if you have kids, as if life is like a checklist. No one asks you if you're happy

- Criolo

If you get tired, learn to rest, not to quit.

- Banksy

The sun does not forget a village just because it is small.

- African Proverb

A bird sitting on a tree is never afraid of the branch breaking, because its trust is not on the branch but on its own wings. Always believe in yourself

- Anonymous

Raise your words, not your voice. It is rain that grows flowers, not thunder.

- Rumi

If you think you are too small to make a difference, try to sleep with a mosquito in a room.

- Dalai Lama

You can't keep blaming yourself. Just blame yourself once and move on.

- Homer Simpson

Have a nice day!

#Quotes

@python_pioneers
@python_pioneers
โค7๐Ÿ‘2๐Ÿ”ฅ1๐Ÿ™1๐Ÿ˜Ž1
Fun_damentals_of_Python_Book_by_Bemnet_Girma_1715444510.pdf
10.6 MB
For Python beginners, this book is a must-have.

-Someone

#Books

@python_pioneers
๐Ÿ‘4โค1