This content originally appeared on DEV Community and was authored by Aatmaj
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=",")
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
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()
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
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
1
121
12321
1234321
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,.....
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 ?
This content originally appeared on DEV Community and was authored by Aatmaj
Aatmaj | Sciencx (2021-06-30T12:02:33+00:00) Learning Python-Basic course: Day 7, Exercises and coding challenges⚔️. Retrieved from https://www.scien.cx/2021/06/30/learning-python-basic-course-day-7-exercises-and-coding-challenges%e2%9a%94%ef%b8%8f/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.