DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Day 15 - for loop and Indexing

Find a Fibonacci series:

Generate the Fibonacci sequence up to a given number.
Example: Input: 10 → Output: 0, 1, 1, 2, 3, 5, 8.

f, s = -1, 1
t = 0
while t<=13:
    t= f + s
    print(t,end= ' ')
    f,s = s, t
Enter fullscreen mode Exit fullscreen mode
0 1 1 2 3 5 8 13 21 
Enter fullscreen mode Exit fullscreen mode

Find a Fibonacci series without using third variable:

f, s = -1, 1 
while f+s<=13: 
    print(f + s,end= ' ')  
    f,s = s, f + s
Enter fullscreen mode Exit fullscreen mode
0 1 1 2 3 5 8 13 
Enter fullscreen mode Exit fullscreen mode

for loop:

A for loop is a control flow statement used in programming to repeat a block of code a specific number of times or to iterate over a sequence.

Syntax:

for variable in iterable:
Enter fullscreen mode Exit fullscreen mode

Step operator:

A step operator refers to the ability to specify an increment (or step) for iteration in loops. In Python, this is often used with the range() function, which allows specifying a step to control how the loop variable changes after each iteration.

Syntax:

range(start, stop, step)
Enter fullscreen mode Exit fullscreen mode

start: The starting value of the sequence (inclusive).
stop: The stopping value of the sequence (exclusive).
step: The amount by which the sequence increases (or decreases, if negative) in each iteration.

print("First Output")
for no in range(10):
    print(no, end=' ')

print("\nSecond Output")
for no in range(1,10):
    print(no, end=' ')

print("\nThird Output")
for no in range(5,10):
    print(no, end=' ')

print("\nFourth Output")
for no in range(1,10,2):
    print(no, end=' ')

print("\nFifth Output")
for no in range(3,15,3):
    print(no, end=' ')

print("\nSixth Output")
for no in range(10,1):
    print(no, end=' ')

print("\nSeventh Output")
for no in range(10,1,-1):
    print(no, end=' ')

print("\nEighth Output")
for no in range(20,3,-1):
    print(no, end=' ')

print("\nNineth Output")
for no in range(20,2,-2):
    print(no, end=' ')
Enter fullscreen mode Exit fullscreen mode
First Output
0 1 2 3 4 5 6 7 8 9 
Second Output
1 2 3 4 5 6 7 8 9 
Third Output
5 6 7 8 9 
Fourth Output
1 3 5 7 9 
Fifth Output
3 6 9 12 
Sixth Output

Seventh Output
10 9 8 7 6 5 4 3 2 
Eighth Output
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 
Nineth Output
20 18 16 14 12 10 8 6 4
Enter fullscreen mode Exit fullscreen mode

Indexing:

Indexing refers to accessing elements in a sequence (like a list, tuple, or string) using their position, or index.

Types of Indexing:

1.Positive Indexing:
Starts from 0 for the first element.

2.Negative indexing:
Starts from -1 for the last element.

name = 'ABCDEFGHI'
print("First output")
for letter in name[0:5]:  
    print(letter, end=' ')
print("\nSecond output")
for letter in name[0:6:2]:
    print(letter, end=' ')
print("\nThird output")
for letter in name[8:0:-1]:
    print(letter, end=' ')
print("\nFourth output")
for letter in name[8:2:-1]:
    print(letter, end=' ')
print("\nFifth output")
for letter in name[8:-1:-1]:
    print(letter, end=' ')
print("\nSixth output")
for letter in name[8:3:-2]:
    print(letter, end=' ')
print("\nSeventh output")
for letter in name[8::-1]:
    print(letter, end=' ')
print("\nNinth output")
for letter in name[::]:
    print(letter, end=' ')
print("\nTenth output")
for letter in name[6::]:
    print(letter, end=' ')
print("\nEleventh output")
for letter in name[2::2]:
    print(letter, end=' ')

Enter fullscreen mode Exit fullscreen mode
First output
A B C D E 
Second output
A C E 
Third output
I H G F E D C B 
Fourth output
I H G F E D 
Fifth output

Sixth output
I G E 
Seventh output
I H G F E D C B A 
Ninth output
A B C D E F G H I 
Tenth output
G H I 
Eleventh output
C E G I

Enter fullscreen mode Exit fullscreen mode
name = 'ABCDEFGHI'
print(name[0])
print(name[-1])
print(name[-2])
print(name[-3])
print(name[-1::-1])

Enter fullscreen mode Exit fullscreen mode
A
I
H
G
IHGFEDCBA
Enter fullscreen mode Exit fullscreen mode

Write a program to check the given string is palindrome or not

name = input("Enter word: ")
if name[::] == name[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")
Enter fullscreen mode Exit fullscreen mode
Enter word: amma
Palindrome
Enter word: ggfhyjdr
Not Palindrome
Enter fullscreen mode Exit fullscreen mode
name = 'abcd'
print(name * 3)
Enter fullscreen mode Exit fullscreen mode
abcdabcdabcd

Enter fullscreen mode Exit fullscreen mode
name = 'abcd'
print(name + 3)
Enter fullscreen mode Exit fullscreen mode
TypeError: can only concatenate str (not "int") to str
Enter fullscreen mode Exit fullscreen mode

This error occurs because you're trying to concatenate a string (name) with an integer (3) using the + operator. In Python, the + operator for strings is used for concatenation, but both operands must be strings.

for num in range(5):
    print("* " * num)

Enter fullscreen mode Exit fullscreen mode

* 
* * 
* * * 
* * * * 
Enter fullscreen mode Exit fullscreen mode
for num in range(1,6):
    print("* " * num)
Enter fullscreen mode Exit fullscreen mode
* 
* * 
* * * 
* * * * 
* * * * * 
Enter fullscreen mode Exit fullscreen mode
for num in range(5,0,-1):
    print("* " * num)

Enter fullscreen mode Exit fullscreen mode
* * * * * 
* * * * 
* * * 
* * 
* 
Enter fullscreen mode Exit fullscreen mode
digit = "1"
for num in range(5,0,-1): 
    print(digit * num)
    digit = str(int(digit)+1) 

Enter fullscreen mode Exit fullscreen mode
11111
2222
333
44
5

Enter fullscreen mode Exit fullscreen mode

Task:

ABCDEFGHI
XYZ
ZYXWV
ACEGI
IGECA
ZXVTRPNLJHFDB

word = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print("First Output")
for letter in word[0:9]:
    print(letter , end=" ")
print("\nSecond Output")
for letter in word[23::]:
    print(letter , end=" ")
print("\nThird Output")
for letter in word[-1:-6:-1]:
    print(letter , end=" ")
print("\nFouth Output")
for letter in word[0:9:2]:
    print(letter , end=" ")
print("\nFifth Output")
for letter in word[8::-2]:
    print(letter , end=" ")
print("\nSixth Output")
for letter in word[-1::-2]:
    print(letter , end=" ")
Enter fullscreen mode Exit fullscreen mode
First Output
A B C D E F G H I 
Second Output
X Y Z 
Third Output
Z Y X W V 
Fouth Output
A C E G I 
Fifth Output
I G E C A 
Sixth Output
Z X V T R P N L J H F D B
Enter fullscreen mode Exit fullscreen mode

Top comments (0)