Back Python Loops Tutorial | Python For Loop | While Loop Python | Python Training | Edureka

40-50
Created By: edureka!
Published At: 2017-03-30
Search Query: Python For Loops,Python While Loops
Tags: yt:cc=on python loops tutorial python for loop while loop python python loop python loops python loops explained python loop syntax python loop example python tutorial python training python programming python programming language python certification python classes python edureka edureka
🔥 Python Certification Training (Use Code "𝐘𝐎𝐔𝐓𝐔𝐁𝐄𝟐𝟎"): https://www.edureka.co/data-science-python-certification-course
This Edureka "Python Loops" tutorial (Python Tutorial Blog: https://goo.gl/wd28Zr) will help you in understanding different types of loops used in Python. You will be learning how to implement all the loops in python practically. Check out our playlist for more videos: https://goo.gl/Na1p9G

Below are the topics covered in this tutorial:

1) Why to use loops?
2) What are loops
3) Types of loops in Python: While, For, Nested
4) Demo on each Python loop

Subscribe to our channel to get video updates. Hit the subscribe button above.

#Python #Pythontutorial #Pythononlinetraining #Pythonforbeginners #Pythonloops

-------------------------------------------------------------------------------------------

How it Works?
1. This is a 5 Week Instructor led Online Course,40 hours of assignment and 20 hours of project work
2. We have a 24x7 One-on-One LIVE Technical Support to help you with any problems you might face or any clarifications you may require during the course.
3. At the end of the training you will be working on a real time project for which we will provide you a Grade and a Verifiable Certificate!

- - - - - - - - - - - - - - - - -

About the Course

Edureka's Python Online Certification Training will make you an expert in Python programming. It will also help you learn Python the Big data way with integration of Machine learning, Pig, Hive and Web Scraping through beautiful soup. During our Python Certification training, our instructors will help you:

1. Master the Basic and Advanced Concepts of Python
2. Understand Python Scripts on UNIX/Windows, Python Editors and IDEs
3. Master the Concepts of Sequences and File operations
4. Learn how to use and create functions, sorting different elements, Lambda function, error handling techniques and Regular expressions ans using modules in Python
5. Gain expertise in machine learning using Python and build a Real Life Machine Learning application
6. Understand the supervised and unsupervised learning and concepts of Scikit-Learn
7. Master the concepts of MapReduce in Hadoop
8. Learn to write Complex MapReduce programs
9. Understand what is PIG and HIVE, Streaming feature in Hadoop, MapReduce job running with Python
10. Implementing a PIG UDF in Python, Writing a HIVE UDF in Python, Pydoop and/Or MRjob Basics
11. Master the concepts of Web scraping in Python
12. Work on a Real Life Project on Big Data Analytics using Python and gain Hands on Project Experience
- - - - - - - - - - - - - - - - - - -

Why learn Python?

Programmers love Python because of how fast and easy it is to use. Python cuts development time in half with its simple to read syntax and easy compilation feature. Debugging your programs is a breeze in Python with its built in debugger. Using Python makes Programmers more productive and their programs ultimately better. Python continues to be a favorite option for data scientists who use it for building and using Machine learning applications and other scientific computations.
Python runs on Windows, Linux/Unix, Mac OS and has been ported to Java and .NET virtual machines. Python is free to use, even for the commercial products, because of its OSI-approved open source license.
Python has evolved as the most preferred Language for Data Analytics and the increasing search trends on python also indicates that Python is the next "Big Thing" and a must for Professionals in the Data Analytics domain.

Customer Review

Sairaam Varadarajan, Data Evangelist at Medtronic, Tempe, Arizona: "I took Big Data and Hadoop / Python course and I am planning to take Apache Mahout thus becoming the "customer of Edureka!". Instructors are knowledge... able and interactive in teaching. The sessions are well structured with a proper content in helping us to dive into Big Data / Python. Most of the online courses are free, edureka charges a minimal amount. Its acceptable for their hard-work in tailoring - All new advanced courses and its specific usage in industry. I am confident that, no other website which have tailored the courses like Edureka. It will help for an immediate take-off in Data Science and Hadoop working."
For more information, Please write back to us at sales@edureka.co or call us at IND: 9606058406 / US: 18338555775 (toll free).

Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@SaimDiscoveries
2018-04-27

I do not understand that what is the purpose of this line
"to_be_guessed =int(n*random.random())+1"

@edurekaIN

Hey, sorry for the delay.


"""to_be_guessed =int(n*random.random())+1"" is used to keep the range of the number between 1 and 20.
random.random() gives you a random number less than zero in floating point
Ex: print (random.random()) will give 0.95795482028667 as output, which is less than zero and not suitable for our code.
n*random.random() is done so that when another number is multiplied with the random number generated, the value becomes more than zero.
But suppose n=1, then n*random.random() will give the same number that is less than 1. To avoid this, we add 1 to n*random.random().
Finally, (n*random.random())+1 will result in a number greater than 0 and less than or equal to 20.
And then this number is converted to int type from float.
Hence, we use the line ""to_be_guessed =int(n*random.random())+1"""



Hope this helps!

VidTutorAssistant (GPT-4)

Hello @SaimDiscoveries, great question! This line of code is used to generate a random number within a specific range - in this case 0 to 20. Let's break it down:

1. `random.random()` generates a random floating point number in the range [0.0, 1.0). Essentially, it just gives you a random decimal number between 0 (inclusive) and 1 (exclusive).

2. `n*random.random()` will generate a random floating-point number in the range [0.0, n). What it does is it multiplies n (which we have assigned the value of 20) with that random decimal. This results in a random floating point number between 0 and n.

3. `int(n*random.random())` is used to convert our random floating-point number to a random integer. The `int()` function trims the decimal part and gives us a whole number.

4. Finally, `int(n*random.random())+1` ensures our number falls within the range of 1 to 20. This is because in Python, the upper value in a range function is exclusive and we want to include 20 in our possibilities. By adding 1, we shift our range of possible values up by one.

Hence, `to_be_guessed =int(n*random.random())+1` will result in a random number between 1 and 20 (inclusive). I hope this clarifies your doubt, happy learning!


@5_alenrenny567
2022-07-31

It help me and i won first place in programming compitition

@edurekaIN

Good To know our videos are helping you learn better :) Stay connected with us and keep learning ! Do subscribe the channel for more updates : )

VidTutorAssistant (GPT-4)

Congratulations @5_alenrenny567! We are delighted to know that our content has been helpful to you. Achieving the first place in a programming competition is no small feat, and we're honored to have contributed to your success. Stay tuned for more informative videos on Python and other programming languages. Remember, "Happy Learning!" is not just a tag line for us, it's our mission. Keep exploring, coding, and winning!


@k.g.goletar6881
2023-02-12

I was really confused about the loops before seeing the video now I completely understood it

@edurekaIN

Hi : ) We really are glad to hear this ! Truly feels good that our team is delivering and making your learning easier :) Keep learning with us .Stay connected with our channel and team :) . Do subscribe the channel for more updates : ) Hit the bell icon to never miss an update from our channel : )

VidTutorAssistant (GPT-4)

We're thrilled to hear that our video helped to clarify your understanding of loops within Python, @k.g.goletar6881! That's exactly why we're here: to make complex programming concepts a little easier to grasp. Remember that practice is key when it comes to mastering these ideas. You might want to try devising your own practical applications for each loop type, as it's a great way to practice your new skills. Feel free to reach out with any further questions you have. Happy coding!


@mahalaxmihiremath1423
2018-11-19

In the nested loop 1st example what will happen if i use for loop instead while loop

@edurekaIN

Hey Mahalaxmi, "A for loop runs for a pre-determined number of times. So, it can iterate through an array from start to finish, say, but it will only ever loop for that specific number.

A while loop will carry on until a pre-determined scenario takes place. That may happen immediately, or it may require a hundred iterations. So, the number of loops is governed by an outcome, not a number."

Hope this helps!

VidTutorAssistant (GPT-4)

Thank you for your question @mahalaxmihiremath1423! Good observation, you can definitely use a for loop instead of a while loop in the nested loop example. The effect of the operation will remain similar, but the condition checking and incrementing method will be different.

If you use a 'for' loop, you'd initiate a sequence or range within the loop declaration itself, stating where to begin and end the loop. You also wouldn't have to manually handle the increment or decrements (like we do in while loop with the 'chances' variable, decrementing on each incorrect PIN attempt).

Just remember to make consequential changes to the rest of the code (like user prompts and decision handling) to have it align with the way a for loop processes its data. Happy coding! We're here to help if you have more questions. Happy learning!


@mangeshraj8913
2022-11-08

loved this video

@edurekaIN

Thank you 😊 Glad it was helpful!!

VidTutorAssistant (GPT-4)

Hi @mangeshraj8913, we're delighted to hear that you loved the video. At edureka, our goal is to deliver informative and engaging content. If you have any queries or doubts concerning Python loops or any other topics, feel free to ask. Remember to check out our other videos for more learning. Happy coding!


@vyshnavipolepalle5562
2019-05-23

what is random?.....is that any module, built in function?...could tell more about it and u called random() function too...what does random.random() means and how these two random's are different?

@edurekaIN

Yes, there is a module called as random which is used to generate random numbers. But to use them, we would need to call the random file. That is the reason we import random. Once the random file/module is imported, we need to call the random function, that's the reason we have random.random() where the first random is the built-in file and the second random() is the function to generate the random number.

VidTutorAssistant (GPT-4)

Hello @vyshnavipolepalle5562, great question! In Python, 'random' is a built-in module that can be used to generate random numbers. That means it comes with Python, no need for extra installations.

When we say 'import random', we are importing this module to our code so we can use the functions it provides.

As for 'random.random()', it's a built-in function in the random module. Calling 'random.random()' generates a random float number between 0.0 and 1.0.

The initial 'random' refers to the random module, and the second 'random()' is the function we're calling from that module. Thus, 'random.random()' means we're calling the 'random()' function from the 'random' module.

I hope this clears up your confusion. If you have any more queries, feel free to ask. Happy learning!


@suryabh2307
2021-07-01

You rocked it man!
Finally I'm with a clear explanation

@edurekaIN

Hi : ) We really are glad to hear this ! Truly feels good that our team is delivering and making your learning easier :) Keep learning with us .Stay connected with our channel and team :) . Do subscribe the channel for more updates : ) Hit the bell icon to never miss an update from our channel : )

VidTutorAssistant (GPT-4)

Thank you, @suryabh2307! We're thrilled that you found the explanation clear. If you have any more questions or topics you'd like us to cover about Python programming or if there's anything else you don't understand, feel free to ask! Keep learning and coding with us on edureka!. Happy Learning!


@suneetachauhan1984
2019-11-17

## Why the below command does not move the cursor to the end of the statement?
print('welcome to the bank')

count = 3

while count >= 0:

pin = int(input('enter the pin :'))

if pin == 1234:

print('the correct pin',end='\n')

break

elif pin!= ('1234'):

print('incorrect pin')

count -= 1

if count == 0:

print('\n please collect your card')

break

@edurekaIN

Looking at the code, the execution will go on until the count is 0, that means you will be able to give the pin 3 times. In the incorrect pin statement there is no break statement. so the loop continues to execute the next statements.

VidTutorAssistant (GPT-4)

Hello @suneetachauhan1984, thanks for your question! The key lies in understanding the purpose of the `end` argument in the `print()` function. By default, the `end` argument is set to '\n' which adds a newline at the end of the print statement. So, when you write `print('the correct pin', end='\n')`, the cursor does actually move to the end of the line or the start of a new line as expected.

However, if you meant that the cursor does not move onto the next line automatically after each print statement, it's because the '\n' character is skipped when you're using `input()`. In Python, `input()` strips whitespace characters, including '\n', from the beginning and end of the entered string.

Hope this clarifies your doubt! Please feel free to ask if you have any more questions. Happy coding!


@arihantsaxena4757
2019-02-03

Why have we used count=count+1 in while loop?

@edurekaIN

Hey Saxena! Its there to increase the counter variable by 1. So, in the while loop, since we want a set of actions to be executed till the condition is satisfied. we use this counter variable to iterate to each and every step.
Hope this helps!

VidTutorAssistant (GPT-4)

Great question, @arihantsaxena4757! The part `count = count + 1` in the while loop is used to increment the value of `count` by one in every loop iteration. This is important to ensure that the loop doesn't run indefinitely. If we didn't increment `count`, its value would always stay at 0 and the condition `count < 9` would always be true, causing an infinite loop. By incrementing `count`, we ensure that at some point `count` will be equal to 9, our loop will stop, and Python will move onto the next lines of code. Always remember, controlling the condition is crucial in loops to avoid infinite iterations. Hope this clarifies your doubt! Happy coding!


@denniskamonde6836
2017-04-22

great video

@edurekaIN

Hey Dennis, thanks for checking out our tutorial! We're glad you found it useful.
Here's another video that we thought you might like: https://www.youtube.com/watch?v=98YeQpmQeH8.
Do subscribe to our channel to stay posted on upcoming tutorials. Cheers!

VidTutorAssistant (GPT-4)

Thank you for your kind words, @denniskamonde6836! We're glad you found the video helpful. If you have any questions or doubts about Python loops, feel free to ask. Don't forget to check out our other videos for more Python learning resources. Happy Learning!