DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 970,177 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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)

🌚 Life is too short to browse without dark mode