Computer Science, asked by Ipsitsingharoy, 6 months ago

WRITE a program to print the square of 4 and 9.
NOTE :- DON'T COPY FROM ANY SITE OR FROM GOOGLE.
IF U CAN THEN ONLY ANSWER OTHERWISE GET LOST​

Answers

Answered by anagha1232
1

Explanation:

A for loop allows us to execute a block of code multiple times with some parameters updated each time through the loop. A for loop begins with the for statement:

iterable = [1,2,3]

for item in iterable:

# code block indented 4 spaces

print(item)

1

2

3

The main points to observe are:

for and in keywords

iterable is a sequence object such as a list, tuple or range

item is a variable which takes each value in iterable

end for statement with a colon :

code block indented 4 spaces which executes once for each value in iterable

For example, let's print for from 0 to 5:

for n in [0,1,2,3,4,5]:

square = n**2

print(n,'squared is',square)

print('The for loop is complete!')

0 squared is 0

1 squared is 1

2 squared is 4

3 squared is 9

4 squared is 16

5 squared is 25

The for loop is complete!

Copy and paste this code and any of the examples below into the Python visualizer to see each step in a for loop!

while Loops

What if we want to execute a block of code multiple times but we don't know exactly how many times? We can't write a for loop because this requires us to set the length of the loop in advance. This is a situation when a while loop is useful.

The following example illustrates a while loop:

n = 5

while n > 0:

print(n)

n = n - 1

5

4

3

2

1

The main points to observe are:

while keyword

a logical expression followed by a colon :

loop executes its code block if the logical expression evaluates to True

update the variable in the logical expression each time through the loop

BEWARE! If the logical expression always evaluates to True, then you get an infinite loop!

We prefer for loops over while loops because of the last point. A for loop will never result in an infinite loop. If a loop can be constructed with for or while, we'll always choose for.

Constructing Sequences

There are several ways to construct a sequence of values and to save them as a Python list. We have already seen Python's list comprehension syntax. There is also the append list method described below.

Sequences by a Formula

If a sequence is given by a formula then we can use a list comprehension to construct it. For example, the sequence of squares from 1 to 100 can be constructed using a list comprehension:

squares = [d**2 for d in range(1,11)]

print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

However, we can achieve the same result with a for loop and the append method for lists:

# Intialize an empty list

squares = []

for d in range(1,11):

# Append the next square to the list

squares.append(d**2)

print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

In fact, the two examples above are equivalent. The purpose of list comprehensions is to simplify and compress the syntax into a one-line construction.

Recursive Sequences

We can only use a list comprehension to construct a sequence when the sequence values are defined by a formula. But what if we want to construct a sequence where the next value depends on previous values? This is called a recursive sequence.

For example, consider the Fibonacci sequence:

where

We can't use a list comprehension to build the list of Fibonacci numbers, and so we must use a for loop with the append method instead. For example, the first 15 Fibonacci numbers are:

fibonacci_numbers = [1,1]

for n in range(2,15):

fibonacci_n = fibonacci_numbers[n-1] + fibonacci_numbers[n-2]

fibonacci_numbers.append(fibonacci_n)

print(fibonacci_numbers)

Answered by InnocentPahadi
6

Answer:

ye lo Khush Ho abbb.....

Attachments:
Similar questions