0% found this document useful (0 votes)
16 views

Python Loops

The document contains examples of Python programs that use loops and conditional statements to perform tasks like finding the average of numbers, checking if a number is divisible by another number, printing patterns, and more.

Uploaded by

shreyaxchauhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Loops

The document contains examples of Python programs that use loops and conditional statements to perform tasks like finding the average of numbers, checking if a number is divisible by another number, printing patterns, and more.

Uploaded by

shreyaxchauhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Loop excercises

Python program to find out the average of a set of


integers

count = int(input("Enter the count of numbers: "))


i=0
sum = 0
for i in range(count):
x = int(input("Enter an integer: "))
sum = sum + x
avg = sum/count
print(" The average is: ", avg)
Python program to check whether the given integer
is a multiple of 5

number = int(input("Enter an integer: "))


if(number%5==0):
print(number, "is a multile of 5")
else:
print(number, "is not a multiple of 5")
to display all the multiples of 3 within the range 10
to 50

for i in range(10,50):
if (i%3==0):
print(i)
to find the product of a set of real numbers

i=0
product = 1
count = int(input("Enter the number of real numbers: "))
for i in range(count):
x = float(input("Enter a real number: "))
product = product * x
print("The product of the numbers is: ", product)
Python program to insert a number to any position in
a list

numbers = [3,4,1,9,6,2,8]
print(numbers)
x = int(input("Enter the number to be inserted:
"))
y = int(input("Enter the position: "))
numbers.insert(y,x)
print(numbers)
Python program to delete an element from a list by
index

numbers = [3,4,1,9,6,2,8]
print(numbers)
x = int(input("Enter the position of the element to
be deleted: "))
numbers.pop(x)
print(numbers)
Python Program to Iterate Through Two Lists in
Parallel

list_1 = [1, 2, 3, 4]
list_2 = ['a', 'b', 'c']

for i, j in zip(list_1, list_2):


print(i, j)
to print Half pyramid pattern with stars(*)

rows = 5
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
# new line after each row
print("\r")
*
**
***
****
*****
Python program to print the simple number pattern

rows = 6
# outer loop
for i in range(rows):
# nested loop
for j in range(i):
print(i, end=' ')
# new line after each row 1
print('') 22
333
4444
55555
To check the leap year

year = int(input("Enter a year: "))


if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print(year, " is a leap
year")
else:
print(year, " is not a leap
year")
else:
print(year, " is a leap year")
else:
print(year, " is not a leap
year")
To do task

Create a variable storing your prn.

Create a string of your name.


Find leangth of the string
Reverse the string
Access each character of the string seperately
Assignment
>>> x = 4
>>> x * x
16

>>> a, b = 1, 2
>>> a
1
>>> b
2
>>> a + b
3
Check output of the following program.
x=4
y=x+1
x=2
print x, y

a, b = 2, 3
c, b = a, c + 1
print a, b, c
> x = "hello"
>>> y = 'world'
>>> print x, y
hello world

min(2, 3)
2
>>> max(3, 4)
istrcmp('python', 'Python')
True
>>> istrcmp('LaTeX', 'Latex')
True
>>> istrcmp('a', 'b')
False

istrcmp to compare two strings, ignoring the case.


>>> x = 5
>>> 2 < x < 10
True
>>> 2 < 3 < 4 < 5 < 6
True

>>> reverse([1, 2, 3, 4])


[4, 3, 2, 1]
>>> reverse(reverse([1, 2, 3, 4]))
[1, 2, 3, 4]
.
>>> a = [2, 10, 4, 3, 7]
>>> a.sort()
>>> a
[2, 3, 4, 7 10]

x the list from right.


>>> x = [1, 2, 3, 4]
>>> x[-1]
4
>>> x [-2]
3
> x = [1, 2, 3, 4]
>>> x[0:2]
[1, 2]
>>> x[1:4]
[2, 3, 4]
>>> a[:2]
[1, 2]
>>> a[2:]
[3, 4]
>>> a[:]
[1, 2, 3, 4]
>>> x = [1, 2, 3, 4]
>>> x[1] = 5
>>> x
[1, 5, 3, 4]

You might also like