#21 Python Tutorial for Beginners | For Loop in Python

#21 Python Tutorial for Beginners | For Loop in Python

Introduction to For Loops

In this video, Ivan Vetti introduces the concept of for loops in Python. He explains how for loops differ from while loops and how they work with sequences such as lists, tuples, and strings.

For Loop Basics

  • While loops have a set number of iterations based on a condition, but for loops work with sequences.
  • To print values from a list one by one using a for loop, use the syntax for i in x: where i represents each element of the list.
  • Use proper indentation when writing code within a for loop.
  • Unlike while loops, for loops do not require variables or conditions to be specified.

Using For Loops with Different Sequences

  • For loops can be used with different types of sequences such as strings and tuples.
  • When using a string in a for loop, each character is treated as an element of the sequence.
  • Instead of using a separate variable to store the list values, you can specify the list directly within the for loop syntax.
  • The range() function can be used within a for loop to print values from 1 to 10.

Range and For Loop

In this section, the speaker explains how to use range and for loop in Python.

Using Range Function

  • To generate a sequence of numbers, we can use the range() function.
  • The range() function takes three arguments: starting point, ending point, and iteration.
  • We can specify the gap between two numbers by setting the iteration value. If not specified, it defaults to 1.
  • To print values in reverse order, we need to set the iteration value as -1.

Using For Loop

  • We can use a for loop to iterate over a sequence of values generated by the range() function.
  • We can add an if condition inside the for loop to print only certain values that meet specific criteria.
  • To skip printing values that are divisible by 5, we can add an if condition with modulo operator %.

Conclusion

In this section, the speaker concludes this video and invites viewers to leave comments and like the video.

Conclusion

  • The speaker thanks viewers for watching and invites them to leave comments in the comment section below.
  • Viewers are also encouraged to hit the like button if they enjoyed watching this video.
Video description

Check out our courses: AI Powered DevOps with AWS - Live Course :- https://go.telusko.com/AIDevOps-AWS Coupon: TELUSKO10 (10% Discount) Complete Java Developer Course Batch-4: https://go.telusko.com/Complete4 Coupon: TELUSKO10 (10% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) Udemy Courses: Spring: https://go.telusko.com/udemyteluskospring Java:- https://go.telusko.com/udemyteluskojava Java Spring:- https://go.telusko.com/Udemyjavaspring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker For More Queries WhatsApp or Call on : +919008963671 website : https://courses.telusko.com/ Instagram : https://www.instagram.com/navinreddyofficial/ Linkedin : https://in.linkedin.com/in/navinreddy20 TELUSKO Android App : https://bit.ly/TeluskoApp In this lecture we are discussing about: #1 break #2 continue #3 pass In Python, break, continue, and pass are control flow statements that are used to alter the normal flow of execution in a loop or conditional statement. #1 break: The break statement is used to terminate a loop prematurely when a certain condition is met. Once the break statement is encountered inside a loop, the loop is immediately terminated and the program continues with the next statement after the loop. for i in range(1, 6): if i == 3: break print(i) output: 1 2 #2 continue: The continue statement is used to skip the current iteration of a loop and move on to the next iteration, without executing the remaining code in the loop for the current iteration. for i in range(1, 6): if i == 3: continue print(i) output: 1 2 4 5 #3 pass: The pass statement is a placeholder statement that is used to indicate that no action should be taken. It is often used as a placeholder when writing code that will be filled in later. for i in range(1, 6): if i == 3: pass else: print(i) output: 1 2 4 5 Github :- https://github.com/navinreddy20/Python- Python for Beginners :- http://bit.ly/3JOLQhl Editing Monitors : https://amzn.to/2RfKWgL https://amzn.to/2Q665JW https://amzn.to/2OUP21a. Java:- https://bit.ly/JavaUdemyTelusko Spring:- https://bit.ly/SpringUdemyTelusko More Learning : Java :- https://bit.ly/3x6rr0N Python :- https://bit.ly/3GRc7JX Django :- https://bit.ly/3MmoJK6 JavaScript :- https://bit.ly/3tiAlHo Node JS :- https://bit.ly/3GT4liq Rest Api :-https://bit.ly/3MjhZwt Servlet :- https://bit.ly/3Q7eA7k Spring Framework :- https://bit.ly/3xi7buh Design Patterns in Java :- https://bit.ly/3MocXiq Docker :- https://bit.ly/3xjWzLA Blockchain Tutorial :- https://bit.ly/3NSbOkc Corda Tutorial:- https://bit.ly/3thbUKa Hyperledger Fabric :- https://bit.ly/38RZCRB NoSQL Tutorial :- https://bit.ly/3aJpRuc Mysql Tutorial :- https://bit.ly/3thpr4L Data Structures using Java :- https://bit.ly/3MuJa7S Git Tutorial :- https://bit.ly/3NXyCPu Donation: PayPal Id : navinreddy20 https://www.telusko.com