write a python program to print the square of all even numbers from 1 to 20
Answers
"""
Project Type: Brainly Answer
Date Created: 20-02-2021
Date Edited Last Time: ---NIL---
Question Link: https://brainly.in/question/35468648
Question: Write a python program to print the
square of all even numbers from 1 to 20.
Program Created By: atrs7391
Programming Language: Python
Language version (When program created or last edited): Python 3.9.1
"""
for i in range(1, 21):
if i % 2 == 0:
print("Square of", i, "is", (i*i))
Answer :
Here's a Python program to print the square of all even numbers from 1 to 20:
for i in range(2, 21, 2):
print(i**2)
Explanation:
This program uses a for loop to iterate over the range of numbers from 2 to 20 with a step size of 2. This ensures that only even numbers are considered. The ** operator is used to calculate the square of each even number, which is then printed to the console.
Alternatively, we can use a list comprehension as follows:
squares = [i**2 for i in range(2, 21, 2)]
print(squares)
This program creates a list of squares of all even numbers from 1 to 20 using list comprehension and then prints it.
Both programs produce the same output:
4
16
36
64
100
144
196
256
324
400
Note that we start the range from 2 instead of 1 because we want only even numbers.
Learn more about Python Programming Language :
https://brainly.in/question/29133558
#SPJ3