You are on page 1of 13

range()

Definition and Usage


The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

Syntax
range(start, stop, step)

First example on range

# range()
# we can pass 3 arguments to range constructor
r = range(10) # Here 0 to 9 numbers will be generated
# default start value is 0
# default step value is 1
# r=0,1,2,3,4,5,6,7,8,9

for i in r:
print(i)

Second example using range

r = range(1, 11)
for i in r:
print(i)
print(f"Odd numbers from 1 to {r}")
r1 = range(1, 11, 2)
for i in r1:
print(i)
print(f"Even numbers from 1 to {r}")
r2 = range(2, 11, 2)
for i in r2:
print(i)

range object another example

# 1,4,7,
for i in range(1, 10, 3):
print(i)

Python loops

1. while loop
2. for loop

The while Loop


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

Note: ++ (increment) and –-(decrement) operators are not supported by python

program to print 1 to 10 using while loop

# Example on while loop


# 1 to 10
i = 1

while i <= 10:


print(i)
i = i+1 # i+=1

Program to print even numbers from 1 to 10 (while loop)

# To print Even numbers from 1 to 10


i = 1
while i <= 10:
if (i % 2 == 0):
print(i)
i = i+1

Same program with different logic

# To print Even numbers from 1 to 10


i = 2
# i=2
while i <= 10:
print(i) # 2
i = i+2

Program to print odd numbers(while loop):

# Odd numbers from 1 to 10


i = 1
while i <= 10:
print(i)
i = i+2

Program to print factors of a given number

# factors of a given number


n = int(input("Enter any number:\t"))
# n=6
# 1 2 3 6
i = 1
while i <= n:
if n % i == 0:
print(i)
i = i+1

To count factors:

# factors of a given number


n = int(input("Enter any number:\t"))
# n=6
# 1 2 3 6
i = 1
count = 0
while i <= n:
if n % i == 0:
count = count+1
print(i)
i = i+1
print("Count=\t", count)

To check given number is prime or not:

# factors of a given number


n = int(input("Enter any number:\t"))
# n=6
# 1 2 3 6
i = 1
count = 0
while i <= n:
if n % i == 0:
count = count+1
print(i)
i = i+1
print("Count=\t", count)
if count == 2:
print("Given number is prime number")
else:
print("Given number is not a prime number")
Pattern program:

i = 1
n = int(input("Enter any number:\t"))
while i <= n:
j = 1
while j <= i:
print(i, end=" ")
j = j+1
print()
i = i+1
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Simple way to print above pattern:

n = int(input("Enter any number:\t"))


while i <= n:
print(i*"*")
i = i+1

Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Printing pattern using for loop:

n = int(input("Enter any number:\t"))


for i in range(1, n+1):
print(i*"*")

List[]:

"""
Lists
1. collection or iterative object which contains
same or different types of elements(objects)
2. We can create a list by using pair of square brackets
or by using class name called 'list'
3. list maintains insertion order
4. list supports +ve and -ve indexing
5. list supports slicing
6. list is mutable object
7. list supports concatenation
8. list supports repetition
"""

Why it is called as collection object or collection literal?

1. because it is used to store collection of elements

Why it is called as iterable object?

We can get the elements of it by using iterative statement called for loop

Example on iterable objects

l1 = [10, 20, 30, 40, 50]


print("Iterable(list) object elements")
for ele in l1:
print(ele)

r1 = range(11)
print("Iterable(range) object elements")
for ele in r1:
print(ele)

t1 = (10, 20, 30, 40, 50)


print("Iterable(tuple) object elements")
for ele in t1:
print(ele)

s1 = {10, 20, 30, 40, 50}


print("Iterable(set) object elements")
for ele in s1:
print(ele)

d1 = {1: "madhu", 2: "priya"}


print("Iterable(set) object elements")
for key in d1:
print(key)

s1 = "madhu tech skills"


for ch in s1:
print(ch)

list +ve and –ve indexing


l1 = [10, 20, 30, 40, 50]
print(l1[2])
l2 = l1[1:4]
# 1,2,3
print(l2)
print(l1[-6:-3])
print(l1[-6:-3:1])

print(l1[::])
print(l1[::-1])

name = "madhu"
print(name)
print(name[::-1])

Input(): To input function we can pass either zero or 1 argument.

It prints the given argument and waits there in the console monitor to take the input from the keyboard.

Print(): To the print function we can pass either zero or n number of arguments.

Print() prints all the given arguments and moves the cursor to the new line.

range(): We use range class to generate sequence of numbers.

Program on range()[to print odd numbers]

n = int(input("enter n:"))
#1,3,5,7,9,
for i in range(1, n+1, 2):
print(i)

What is the used of for loop in python?

For loop is used to get the elements from an iterable objects.

What is an iterable object?

It is an object whose elements can be accessed by using an iterative statement called

for loop.

Example on Iterable objects:

s1 = "Madhu Tech Skills"


print("Type of s1:\t", type(s1))
for ch in s1:
print(ch)

l1 = [10, 20, 30, 40, 50]


s1 = {10, 20, 30, 40, 50}
t1 = (10, 20, 30, 40, 50)
d1 = {1: "Madhu", 2: "vinnu", 3: "Prannu"}

print("Elements in the list are:\t")


for ele in l1:
print(ele)

print("Element in set s1")


for i in s1:
print(i, end="\t")

print("\nElements in tuple t1")


for ele in t1:
print(ele, end="\t")

print("\nElements in dictionary d1")


for ele in d1:
print(ele)
Iterative statements: In python we have only two looping statements.

1. while loop
2. for loop

While loop: While loop is used to execute 1 or more statements repeatedly until the given condition
becomes false.

First example on while loop to print 1 to n natural numbers:

# Iterative statements
# while loop
# for loop:to get the elements from iterable
# 1,2,3,4,5,6,7,8,9,10
n = int(input("Enter n value:\t"))
i = 1
# i=1
# n=10
while i <= n:
print(i)
i = i+1 # i=2,3

"""
Three steps we have to follow
1. initialize the variable (ex: i=1)
2. test condition (i<=n)
3. update the variable (i=i+1)
"""

program to print 1 to n odd numbers

"""
Write a program to print odd numbers from 1 to n
"""
i = 1
n = 10
while i <= n:
print(i) # 1 3
i = i+2 # i=1,3,5
Program to find Factorial of given number:

n = int(input("Enter value in n:"))


i = 1
f = 1
while i <= n:
f = f*i
i = i+1
print("Factorial of given no is:\t", f)

List[]:

1. List is iterable object.


2. We can create List in two ways..

1.By using [] brackets

2. By constructor calling i.e list()

3. List object Is mutable

4. List accepts duplicate elements.

5. List supports concatenation and repetition

Example on creating a list without using square brackets

r1 = range(1, 11)
l1 = list(r1)
print(l1)
l2 = list("10,20,30")
print(l2)
t1 = (1, 2, 3, 4, 5)
l3 = list(t1)
print("values in l3(using tuple):")
print(l3)
d1 = {1: "Madhu", 2: "Vinnu", 3:
"Prannu"}
l4 = list(d1)
print(l4)

Another program on list(Slicing)

l1 = [10, 20, 30, 40, 50, 60, 70, 80]


print("size of l1:\t", len(l1))
print("size of l1:\t", l1)
print("size of l1:\t", l1[7])
print("size of l1:\t", l1[-1])
print(l1[0:5])
print(l1[3:7])
print(l1[-7:-2:1])
# -7,-6,-5,-4,-3
print(l1[-8:0])
print(l1[-6:5])

You might also like