Home >Backend Development >Python Tutorial >Day - for loop and Indexing

Day - for loop and Indexing

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-08 06:03:09561browse

Day - 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
0 1 1 2 3 5 8 13 21 

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
0 1 1 2 3 5 8 13 

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:

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)

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=' ')
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

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=' ')

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

name = 'ABCDEFGHI'
print(name[0])
print(name[-1])
print(name[-2])
print(name[-3])
print(name[-1::-1])

A
I
H
G
IHGFEDCBA

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 word: amma
Palindrome
Enter word: ggfhyjdr
Not Palindrome
name = 'abcd'
print(name * 3)
abcdabcdabcd

name = 'abcd'
print(name + 3)
TypeError: can only concatenate str (not "int") to str
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)

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

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

11111
2222
333
44
5

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=" ")
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

The above is the detailed content of Day - for loop and Indexing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn