1669772356
In this tutorial, we learn about While Loops in Python.
Python while
loop is used to run a specific code until a certain condition is met.
Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed.
Code in GitHub: https://github.com/AlexTheAnalyst/PythonYouTubeSeries/blob/main/Python%20Basics%20101%20-%20While%20Loops.ipynb
0:00 Intro
0:35 Syntax of While Loop
1:57 Break Statement in While Loop
2:44 Else Statement in While Loop
3:50 Continue Statement in While Loop
5:18 Outro
Python while
loop is used to run a specific code until a certain condition is met.
The syntax of while
loop is:
while condition:
# body of while loop
Here,
while
loop evaluates the condition
condition
evaluates to True
, the code inside the while
loop is executed.condition
is evaluated again.False
.condition
evaluates to False
, the loop stops.Flowchart for while Loop in Python
# program to display numbers from 1 to 5
# initialize the variable
i = 1
n = 5
# while loop from i = 1 to 5
while i <= n:
print(i)
i = i + 1
Output
1
2
3
4
5
Here's how the program works:
Variable | Condition: i <= n | Action |
---|---|---|
i = 1 n = 5 | True | 1 is printed. i is increased to 2. |
i = 2 n = 5 | True | 2 is printed. i is increased to 3. |
i = 3 n = 5 | True | 3 is printed. i is increased to 4. |
i = 4 n = 5 | True | 4 is printed. i is increased to 5. |
i = 5 n = 5 | True | 5 is printed. i is increased to 6. |
i = 6 n = 5 | False | The loop is terminated. |
current_level = 0
final_level = 5
game_completed = True
while current_level <= final_level:
if game_completed:
print('You have passed level', current_level)
current_level += 1
print('Level Ends')
Output
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
In the above example, we have used the while
loop to check the current level and display it on the console.
If the condition
of a loop is always True
, the loop runs for infinite times (until the memory is full). For example,
# infinite while loop
while True:
# body of the loop
In the above example, the condition
is always True
. Hence, the loop body will run for infinite times.
A while
loop can have an optional else
block as well.
The else
part is executed after the condition
in the while loop evaluates to False
. For example,
counter = 0
while counter < 3:
print('Inside loop')
counter = counter + 1
else:
print('Inside else')
Output
Inside loop
Inside loop
Inside loop
Inside else
Here, we have used the counter variable to print the 'Inside Loop'
string three times.
On the fourth iteration, the condition in while
becomes False
. Hence, the else
part is executed.
The for
loop is usually used when the number of iterations is known. For example,
# this loop is iterated 4 times (0 to 3)
for i in range(4):
print(i)
And while loop is usually used when the number of iterations are unknown. For example,
while condition:
# body of loop
#python
1599099600
When we’re programming in R (or any other language, for that matter), we often want to control when and how particular parts of our code are executed. We can do that using control structures like if-else statements, for loops, and while loops.
Control structures are blocks of code that determine how other sections of code are executed based on specified parameters. You can think of these as a bit like the instructions a parent might give a child before leaving the house:
“If I’m not home by 8pm, make yourself dinner.”
Control structures set a condition and tell R what to do when that condition is met or not met. And unlike some kids, R will always do what we tell it to! You can learn more about control structures in the R documentation if you would like.
In this tutorial, we assume you’re familiar with basic data structures, and arithmetic operations in R.
Not quite there yet? Check out our Introductory R Programming course that’s part of our Data Analyst in R path. It’s free to start learning, there are no prerequisites, and there’s nothing to install — you can start learning in your browser right now.
install.packages(“Dataquest”)
Start learning R today with our Introduction to R course — no credit card required!
(This tutorial is based on our intermediate R programming course, so check that out as well! It’s interactive and will allow you to write and run code right in your browser.)
In order to use control structures, we need to create statements that will turn out to be either TRUE
or FALSE
. In the kids example above, the statement “It’s 8pm. Are my parents home yet?” yields TRUE
(“Yes”) or FALSE
(“No”). In R, the most fundamental way to evaluate something as TRUE
or FALSE
is through comparison operators.
Below are six essential comparison operators for working with control structures in R:
==
means equality. The statement x == a
framed as a question means “Does the value of x
equal the value of a
?”!=
means “not equal”. The statement x == b
means “Does the value of x
not equal the value of b
?”<
means “less than”. The statement x < c
means “Is the value of x
less than the value of c
?”<=
means “less than or equal”. The statement x <= d
means “Is the value of x
less or equal to the value of d
?”>
means “greater than”. The statement x >
e means “Is the value of x
greater than the value of e
?”>=
means “greater than or equal”. The statement x >= f
means “Is the value of x
greater than or equal to the value of f
?”#data science tutorials #beginner #for loop #for loops #if #if else #learn r #r #r tutorial #rstats #tutorial #tutorials #while loop #while loops
1625760540
Get started with Python!
In this Python tutorial, we will keep learning about repetition structures in Python.
You will understand how and when While Loops should be used. We will also cover the concept of infinite loop.
The break and continue statements will also be explained, they are used to control the execution flow of any repetition structure in Python.
For Loop Video: https://www.youtube.com/watch?v=XTisXBSH7kQ&feature=youtu.be
Playlist: Beginner Python Tutorials | Video #9
Access the code here: https://github.com/rscorrea1/youtube.git
Timestamp:
0:00 - Video summary
0:20 - While loop
6:54 - Infinite loop
8:29 - Break statement
9:57 - Continue statement
Thumbnail:
Photo by Mario Ho on Unsplash
#while loops #python #beginner python tutorial #while loops in python
1624291780
This course will give you a full introduction into all of the core concepts in python. Follow along with the videos and you’ll be a python programmer in no time!
⭐️ Contents ⭐
⌨️ (0:00) Introduction
⌨️ (1:45) Installing Python & PyCharm
⌨️ (6:40) Setup & Hello World
⌨️ (10:23) Drawing a Shape
⌨️ (15:06) Variables & Data Types
⌨️ (27:03) Working With Strings
⌨️ (38:18) Working With Numbers
⌨️ (48:26) Getting Input From Users
⌨️ (52:37) Building a Basic Calculator
⌨️ (58:27) Mad Libs Game
⌨️ (1:03:10) Lists
⌨️ (1:10:44) List Functions
⌨️ (1:18:57) Tuples
⌨️ (1:24:15) Functions
⌨️ (1:34:11) Return Statement
⌨️ (1:40:06) If Statements
⌨️ (1:54:07) If Statements & Comparisons
⌨️ (2:00:37) Building a better Calculator
⌨️ (2:07:17) Dictionaries
⌨️ (2:14:13) While Loop
⌨️ (2:20:21) Building a Guessing Game
⌨️ (2:32:44) For Loops
⌨️ (2:41:20) Exponent Function
⌨️ (2:47:13) 2D Lists & Nested Loops
⌨️ (2:52:41) Building a Translator
⌨️ (3:00:18) Comments
⌨️ (3:04:17) Try / Except
⌨️ (3:12:41) Reading Files
⌨️ (3:21:26) Writing to Files
⌨️ (3:28:13) Modules & Pip
⌨️ (3:43:56) Classes & Objects
⌨️ (3:57:37) Building a Multiple Choice Quiz
⌨️ (4:08:28) Object Functions
⌨️ (4:12:37) Inheritance
⌨️ (4:20:43) Python Interpreter
📺 The video in this post was made by freeCodeCamp.org
The origin of the article: https://www.youtube.com/watch?v=rfscVS0vtbw&list=PLWKjhJtqVAblfum5WiQblKPwIbqYXkDoC&index=3
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!
#python #learn python #learn python for beginners #learn python - full course for beginners [tutorial] #python programmer #concepts in python
1619510796
Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.
Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is
Syntax: x = lambda arguments : expression
Now i will show you some python lambda function examples:
#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map
1625756820
Get started with Python!
In this Python tutorial, we will start learning about repetition structures in Python.
You will understand how and when For Loops should be used.
The break and continue statements will also be explained, they are used to control the execution flow of any repetition structure in Python.
Playlist: Beginner Python Tutorials | Video #8
Access the code here: https://github.com/rscorrea1/youtube.git
Timestamp:
00:00 - Video summary
00:26 - For Loops
04:40 - Break statement
06:37 - Continue statement
07:56 - Nested loops
09:33 - Exercise 1: nested loop + break
11:03 - Exercise 2: nested loop + continue statement
12:04 - Wrapping up
Thumbnail:
Photo by Mario Ho on Unsplash
#for loops #python #beginner python tutorial #loops