0% found this document useful (0 votes)
4 views3 pages

Python Part 4

The document provides an overview of Python loops, specifically while loops and for loops, including their syntax and usage. It explains how to use the else statement with both types of loops and introduces the range function for generating sequences of numbers. Additionally, it includes programming exercises to practice computing sums and averages of integers and multiples.

Uploaded by

tbhncgj6mj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Part 4

The document provides an overview of Python loops, specifically while loops and for loops, including their syntax and usage. It explains how to use the else statement with both types of loops and introduces the range function for generating sequences of numbers. Additionally, it includes programming exercises to practice computing sums and averages of integers and multiples.

Uploaded by

tbhncgj6mj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Computing Fundamentals Cs1150 Lab NO.

10 Python

Python Loops

Python has two primitive loop commands:

• while loops
• for loops

1-While Loops

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

We can run a series of statements while a condition is true by using the while loop.

• The Else Statement

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

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

2- For Loops

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

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)

Page | 1
Done by Eng. Ahmad Al Matar
Computing Fundamentals Cs1150 Lab NO.10 Python

• Looping Through a String

Characters are arranged in a string, which is also an iterable object:

• The Range Function

The range () function defaults to 0 as a starting value, however it is possible to specify the
starting value by adding a parameter: range (5, 10), which means values from 5 to 10 (but not
including 10) and you can add a third parameter to specify the increment value:

for x in range (5, 10):


print(x)

for x in range(2, 30, 3):


print(x)

• Else in For Loop

The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

for x in range(6):
print(x)
else:
print("Finally finished!")

Page | 2
Done by Eng. Ahmad Al Matar
Computing Fundamentals Cs1150 Lab NO.10 Python

Using for loop or while loop:

1. Write a Python program to compute and print the sum of all integers between 5 and 20,
and then compute the average of these numbers.

2. Write a program that prints the multiples of 3, between 1 and 999.

3. Write a Python program to compute and print the sum and average of all even integers
between 0 and 100.

Page | 3
Done by Eng. Ahmad Al Matar

You might also like