DEV Community

Cover image for Learning Python-Basic course: Day 7, Exercises and coding challenges⚔️
Aatmaj
Aatmaj

Posted on • Updated on

Learning Python-Basic course: Day 7, Exercises and coding challenges⚔️

Welcome 🖐️ Today's agenda is solving more interesting questions! ❤️ intricate patterns and mind-blowing sequences ✨


Let us now today solve some more questions 😀 on while loops and for loops. 😁 We will look at 2-3 sample questions followed by exercises and a coding challenge ⚔️.

Sample questions-

1) Write a program to print Fibonacci numbers.

a=0
b=1
print('0,1,',end="")
for i in range(0,10):
   a,b=b,b+a
   print(b,end=",")
Enter fullscreen mode Exit fullscreen mode

Simultaneous assignment of values
Note the second last line. This is the Python syntax for simultaneous assignment. This is equivalent to using a temp variable like-

temp=a
a=b
b=b+temp
Enter fullscreen mode Exit fullscreen mode

This python shortcut comes very very handy while swapping two numbers to

2) Write a program to display times table from 1-10

for i in range(1,11):
    for j in range(1,11):
        print(i,"*",j,"=",j*i)
    print()
Enter fullscreen mode Exit fullscreen mode

OUTPUT-

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
.
.
.
. for numbers upto 10
Enter fullscreen mode Exit fullscreen mode

What such a small amount of code can do in seconds took us years to learn...🤩

Exercises-

1)- Modify the factorial program we did in day 4 to error check for zero and negative numbers.(using if-else) Answer

2)Write a program to give the following output. Answer

1
121
12321
1234321
Enter fullscreen mode Exit fullscreen mode

3) Write a program to display perfect numbers from a range entered by a user (include 0).

Coding challenges-⚔️
1) Write a program to display terms of this infinite pattern until 50.

1, 2,2,3,3, 4,4,4,5,5,5,6,6,6, 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,.....
Enter fullscreen mode Exit fullscreen mode

Comment your answers below. Let's see who can solve tis one. 🗡️🛡️ Beware, it is harder than it seems....😉

Answer to this question will be given in tomorrow's session. 🤞 So stay tuned by following me for updates 👍. Please like and comment below 😊

Top comments (0)