Skip to content

Commit 3f1d2e2

Browse files
Merge pull request geekcomputers#693 from Msubboti/master
List comprehension is used for solving tasks.
2 parents 3ceadb1 + 6a40871 commit 3f1d2e2

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

Print_List_of_Even_Numbers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ def print_error_messages(): # function to print error message if user enters a
55

66

77
n = int(input()) # user input
8-
start = 0
8+
99

1010
if n < 0:
1111
print_error_messages()
1212

13-
for i in range(n): # loop till the number n entered by the user
14-
print(start) # prints 0 first as it is an even number
15-
start += 2 # increases the value of start by 2 to get to the next even number
16-
# even numbers are printed starting from 0
13+
14+
result = ''.join(str(i)+"\n" for i in range(0,n+1,2)) # creating string with number "i"
15+
print(result) # in range from 0 till "n" with step 2;

Print_List_of_Odd_Numbers.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ def print_error_messages():
1414
if n < 0:
1515
print_error_messages()
1616

17-
for i in range(n):
18-
if n == 0:
19-
start += 2 # increases the value of start by 2 to get to the next even number
20-
print(start) # prints 0 first as it is an even number
21-
start += 2 # increases the value of start by 2 to get to the next even number
22-
elif n % 2 == 0:
23-
print(start)
17+
result = "0\n" # Number 0 is added to string
18+
if n == 0:
19+
print(result) # If number is equal to zero, output will be just "0"
20+
elif n > 0:
21+
result += ''.join(str(i)+"\n" for i in range(1, n+1, 2)) # creating string with number "i"
22+
print(result) # in range from 1 till "n+1" with step 2;

0 commit comments

Comments
 (0)