Professional Documents
Culture Documents
Syntax
range(start, stop, step)
# 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)
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)
# 1,4,7,
for i in range(1, 10, 3):
print(i)
Python loops
1. while loop
2. for loop
To count factors:
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
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Printing pattern using for loop:
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
"""
We can get the elements of it by using iterative statement called for loop
r1 = range(11)
print("Iterable(range) object elements")
for ele in r1:
print(ele)
print(l1[::])
print(l1[::-1])
name = "madhu"
print(name)
print(name[::-1])
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.
n = int(input("enter n:"))
#1,3,5,7,9,
for i in range(1, n+1, 2):
print(i)
for loop.
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.
# 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)
"""
"""
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:
List[]:
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)