Learn programming in Python 100 seconds at a time in this tutorial for absolute beginners. This is video number 7: while loops
I know im going fast in this video, please use timestamps below to navigate the video.
Timestamps:
00:00 - Start
00:09 - while statement
00:33 - flag
00:50 - break statement
01:02 - continue statement
01:31 - loop combined with remove function
—————————————————————
WHILE STATEMENT
—————————————————————
The while loop is like the for loop
But works slightly different
The for loop executes a piece of code IF the condition is valid
#once for each item in a group of items, like a list, tuple or dictionary
The while loop continues the loop as long as a condition is valid
#Here’s an example
This code will continue to print the variable number
Adding 1 to the value in every iteration
Until the value 5 is reached
Number = 1
While Number =5:
print(number)
number = number + 1
VS studio
In this example there is a limitation set with the operator on the second line
A while loop needs limitations
Or else you might create an infinite loop
And the code won’t stop running
Another form of a limitation is a FLAG
A flag is a type of variable
As long as it’s labeled TRUE the while loop keeps executing the code
If the user enters quite
The variable is changed to false
And the loop stops
Active = True
While active:
message = input(“I will print what you type in here. \n type ‘quit’ to stop the program”
if message == ‘quit’:
active = False
else:
print(message)
Keynote drop
The break statement can be used to stop the code immediately
No other code will be executed after the break statement
VS STUDIO WITH ARROW
Active = True
While active:
message = input(“I will print what you type in here. \n type ‘quit’ to stop the program”
if message == ‘quit’:
break
else:
print(message)
Break works for any Python loop
So it can also be used with the for loop
The continue statement is used to ignore a certain condition by skipping it and continueing with the remaining code
Here the loop continues to add 1 to the variable number
However inside the loop
The ‘if’ statement declares, that if the variable number contains the value ‘2’
That it should not print this value
And CONTINUE WITH THE NEXT VALUE
Number = 0
While number 6:
number +=1 EXPLAIN HOW THIS IS THE SAME AS NUMBER = NUMBER +1
if number == 2:
continue
print(number)
KEYNOTE
Loops can run infinite if you don’t put any limitations
So this piece of code will run, until the program is stopped manually
Number = 1
While number =5:
print(number)
VSSTUDIO
Loops can be combined with any function within python
Here is an example with the remove function we used earlier in a list
The remove function removes an items from a list
But let’s say the item occurs multiple times in a list and you want to remove all items
The while statement continues to loop through the list
Until all the specified values are removed
VS STUDIO
Colors = [‘red’,’red’,’blue’,’purple’,’red’]
print(colors)
While ‘red’ in colors:
colors.remove(‘colors’)
print(colors)
————————————————————————
🦁 Who are you?
My name is Raza. I am 30. I am a manager within IT for a year, but I’ve been an accountant for the majority of my career.
I’m in love with #Python :)
You can find me here:
📷 Instagram: / razacodes
📈 Twitter: / razacodes
—————————————————————————————
Goals for 2021
Training Python hours: 35/1000
Python/Django Projects: 1/30
Subscribers: 1395/10,000
—————————————————————————————-
#100SecondsOfCode
#pythonforbeginners #programming
Nesta página do site você pode assistir ao vídeo on-line Python Tutorial for Absolute Beginners - #7 While Loops duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Raza Zaidi 21 Fevereiro 2021, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 3,500 vezes e gostou 68 espectadores. Boa visualização!