Fibonnaci sequence:
1) Using 3 variables:
f, s = -1, 1
t = 0
while t<=13:
t= f + s
print(t,end= ' ')
f,s = s, t
Output:
0 1 1 2 3 5 8 13 21
2) Using 2 variables:
f, s = -1, 1
while f+s<=13:
print(f + s,end= ' ')
f,s = s, f + s
Output:
0 1 1 2 3 5 8 13
Range function:
range() function is used to generate a sequence of numbers. It is commonly used in loops for iterating a specific number of times.
Example:
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=' ')
Output:
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
sixth output:
it doesn't generate any output because it requires a step operator to print it in reverse.so it doesnot print any output.
Example:
name = 'ABCDEFGHI'
for letter in name[0:5]:
print(letter, end=' ')
print()
for letter in name[0:6:2]:
print(letter, end=' ')
print()
for letter in name[8:0:-1]:
print(letter, end=' ')
print()
for letter in name[8:2:-1]:
print(letter, end=' ')
print()
for letter in name[8:-1:-1]:
print(letter, end=' ')
print()
for letter in name[8:3:-2]:
print(letter, end=' ')
print()
for letter in name[8::-1]:
print(letter, end=' ')
print()
for letter in name[::]:
print(letter, end=' ')
print()
for letter in name[6::]:
print(letter, end=' ')
print()
for letter in name[2::2]:
print(letter, end=' ')
Output:
A B C D E
A C E
I H G F E D C B
I H G F E D
I G E
I H G F E D C B A
A B C D E F G H I
G H I
C E G I
Find palindrome or not for given string:
name = input("Enter word: ")
if name[::] == name[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Output:
Enter word: amma
Palindrome
Pattern formation:
Example:1
for num in range(1,6):
print("* " * num)
Output:
*
* *
* * *
* * * *
* * * * *
Example:2
for num in range(5,0,-1):
print("* " * num)
Output:
* * * * *
* * * *
* * *
* *
*
Note: * works between 2 strings but + will not work between 2 strings.(for eg- a*2-->aa,a+2-->a2)
Example:3
digit = "1"
for num in range(5,0,-1):
print(digit * num)
digit = str(int(digit)+1)
print()
Output:
11111
2222
333
44
5
Tasks:
word = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1)ABCDEFGHI
2)XYZ
3)ZYXWV
4)ACEGI
5)IGECA
6)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("\nFourth 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=" ")
Output:
First Output
A B C D E F G H I
Second Output
X Y Z
Third Output
Z Y X W V
Fourth 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
Top comments (0)