Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Python looping Statements and file Handling

Rating
-
Sold
-
Pages
27
Uploaded on
26-10-2021
Written in
2021/2022

This is an overall study of python loops and file handling in python and I have given you some of my code attached inside the file where you can practice and enjoy

Institution
Course

Content preview

Python Loops
Python has two primitive loop commands:

 while loops
 for loops




The while Loop
With the while loop we can execute a set of statements as long as a condition
is true.

Example
Print i as long as i is less than 6:

i = 1
while i < 6:
print(i)
i += 1

Note: remember to increment i, or else the loop will continue forever.

The while loop requires relevant variables to be ready, in this example we need
to define an indexing variable, i, which we set to 1.




The break Statement
With the break statement we can stop the loop even if the while condition is
true:

Example
Exit the loop when i is 3:

i = 1
while i < 6:

, print(i)
if i == 3:
break
i += 1




The continue Statement
With the continue statement we can stop the current iteration, and continue
with the next:

Example
Continue to the next iteration if i is 3:

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



The else Statement
With the else statement we can run a block of code once when the condition no
longer is true:

Example
Print a message once the condition is false:

i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")

Try it Yourself »

, Python For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated programming
languages.

With the for loop we can execute a set of statements, once for each item in a
list, tuple, set etc.

Example
Print each fruit in a fruit list:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

Try it Yourself »

The for loop does not require an indexing variable to set beforehand.




Looping Through a String
Even strings are iterable objects, they contain a sequence of characters:

Example
Loop through the letters in the word "banana":

for x in "banana":
print(x)

Try it Yourself »




The break Statement

Written for

Institution
Course

Document information

Uploaded on
October 26, 2021
Number of pages
27
Written in
2021/2022
Type
SUMMARY

Subjects

$4.98
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
kishoreragav179

Get to know the seller

Seller avatar
kishoreragav179 Asan Memorial College of Arts and Science
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
4 year
Number of followers
1
Documents
1
Last sold
4 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions