Python daily exercise 1

Today, I just continue to do exercise on PYnative

Question

The question is to generate a list of all even number between 4 to 30.
The expected output is:

[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

Solution

At firs…


This content originally appeared on DEV Community and was authored by HHMathewChan

Today, I just continue to do exercise on PYnative

Question

The question is to generate a list of all even number between 4 to 30.
The expected output is:

[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

Solution

At first, my solution is using a for loop to produce the list as follow:

list1 = []  
for number in range(4,30):  
    if not number % 2:  
        list1.append(number)  

print(list1)

But the recommended is really simple, just use the list(), which is:

print(list(range(4, 30, 2)))

My reflection

Now I learn a new function, should handle the same problem easily next time.


This content originally appeared on DEV Community and was authored by HHMathewChan


Print Share Comment Cite Upload Translate Updates
APA

HHMathewChan | Sciencx (2022-06-01T21:49:38+00:00) Python daily exercise 1. Retrieved from https://www.scien.cx/2022/06/01/python-daily-exercise-1/

MLA
" » Python daily exercise 1." HHMathewChan | Sciencx - Wednesday June 1, 2022, https://www.scien.cx/2022/06/01/python-daily-exercise-1/
HARVARD
HHMathewChan | Sciencx Wednesday June 1, 2022 » Python daily exercise 1., viewed ,<https://www.scien.cx/2022/06/01/python-daily-exercise-1/>
VANCOUVER
HHMathewChan | Sciencx - » Python daily exercise 1. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/01/python-daily-exercise-1/
CHICAGO
" » Python daily exercise 1." HHMathewChan | Sciencx - Accessed . https://www.scien.cx/2022/06/01/python-daily-exercise-1/
IEEE
" » Python daily exercise 1." HHMathewChan | Sciencx [Online]. Available: https://www.scien.cx/2022/06/01/python-daily-exercise-1/. [Accessed: ]
rf:citation
» Python daily exercise 1 | HHMathewChan | Sciencx | https://www.scien.cx/2022/06/01/python-daily-exercise-1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.