Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views14 pages

For Loop Answers

The document contains a series of Python code snippets along with their outputs and syntax explanations. It covers various programming concepts such as loops, conditionals, and basic arithmetic operations. Each example illustrates how to use different constructs in Python to achieve specific tasks.

Uploaded by

rifathsalam123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

For Loop Answers

The document contains a series of Python code snippets along with their outputs and syntax explanations. It covers various programming concepts such as loops, conditionals, and basic arithmetic operations. Each example illustrates how to use different constructs in Python to achieve specific tasks.

Uploaded by

rifathsalam123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1) write the output and syntax of the following code:

for in range(5):
print(i)

OUTPUT:
0
1
2
3
4

2) for i in range(1,2,3):
print(i)

OUTPUT:
1

3) for i in range(2,3,4):
print(i)

OUTPUT:
2

4) for i in (4, 3, 2, 1, 0):


print(i, end="")

OUTPUT:
43210

5) for i in range(10):
if (i % 2 != 0):
print("hello", i)

Syntax:
for variable in iterable:
if condition:
# Code to execute if the condition is true
OUTPUT:
hello 1
hello 3
hello 5
hello 7
hello 9
6) for i in range(10, 2, -2):
print(i, "hello")

Syntax:
for variable in range(start, stop, step):
# Code to execute on each iteration
OUTPUT:
10 hello
8 hello
6 hello
4 hello

7) str = "python output based questions"


word = str.split()
for i in word:
print(i)

Syntax:
# Initial string
str = "some string"

# Split the string into a list of words


word = str.split()

# Loop over each word in the list


for i in word:
print(i) # Print each word

OUTPUT:
python
output
based
questions

8) for i in range(7, 10):


print("python output based questions")
print("python output based questions")

Syntax:
# For loop starts here
for variable in range(start, stop):
# Code to execute for each iteration
print("something")

# Code after the loop executes once


print("something else")
OUTPUT:
python output based questions
python output based questions
python output based questions
python output based questions

9) for i in range(7, -2, -9):


for j in range(i):
print(j)

Syntax:
for i in range(start, stop, step):
for j in range(i):
# Code to execute for each iteration
print(j)
Output:
0
1
2
3
4
5
6

10) i = "9"
for k in i:
print(k)

SYNTAX:

string_variable = "some string"


for variable in string_variable:
# Code to execute for each character in the string
print(variable)

OUTPUT:
9
11) for i in range(1, 8):
print(i)
i += 2

Syntax:

for variable in range(start, stop):


print(variable)
variable += increment_value # Modifying the variable (optional)

OUTPUT:
1
2
3
4
5
6
7

12) for i in range(4, 7):


i=i+3
print("Hello")

SYNTAX:

for variable in range(start, stop):


variable = variable + increment_value
print("Hello")

OUTPUT:
Hello
Hello
Hello

13) for i in range(4,7):


i=i+3
print("Hello",i)

SYNTAX:
for variable in range(start, stop):
variable = variable + increment_value # Modify the loop variable
print("message", variable) # Print message and modified variable

OUTPUT:
Hello 7
Hello 8
Hello 9
14) i=4
while(i< 10):
i=i+3
print(i)

SYNTAX:

variable = initial_value
while condition:
# Code to execute as long as the condition is True
variable = variable + increment_value # Modify the loop variable
print(variable) # Print the updated value of the variable

OUTPUT:

7
10

15) for i in range(20):


if i // 4 == 0:
print(i)

SYNTAX:

for variable in range(start, stop):


if condition:
# Code to execute if the condition is True
print(variable)

OUTPUT:

0
1
2
3

16) x = 1234
while x % 10:
x = x // 10
print(x)

SYNTAX:
variable = initial_value
while condition:
# Code to execute as long as the condition is True
variable = variable // divisor # Modify the loop variable
print(variable) # Print the modified value of the variable
OUTPUT:

123
12
1

17) for i in 1, 2, 3:
print(i * i)

SYNTAX:
for variable in (value1, value2, value3):
# Code to execute for each value in the sequence
print(variable * variable)

OUTPUT:

1
4
9

18) for i in 2, 4, 6:
print("H" * i)

SYNTAX:

for variable in (value1, value2, value3):


# Code to execute for each value in the sequence
print("H" * variable)

OUTPUT:
HH
HHHH
HHHHHH

19) p = 10
q = 20
p = p * q // 4
q = p + q ** 3
print(p, q)

SYNTAX:
variable1 = value1
variable2 = value2
variable1 = variable1 * variable2 // divisor
variable2 = variable1 + variable2 ** power
print(variable1, variable2)
OUTPUT:

50 800020

20) x = 2
y=6
x = x + y / 2 + y // 4
print(x)

SYNTAX:

variable1 = value1
variable2 = value2
variable1 = variable1 + (variable2 / divisor1) + (variable2 // divisor2)
print(variable1)

OUTPUT:

5.0

21) h = 11
n=h

for i in range(2, n // 2):


if n % i != 0:
print("Python Output based Questions")
break
else:
print("Bye")

SYNTAX:

variable1 = value1 # Initialize a variable with a value


variable2 = variable1 # Assign another variable to the first variable's value

for variable in range(start, stop): # Loop over a range of values


if condition: # Check a condition
# Code to execute if the condition is true
print(message) # Print a message and break
break
else:
# Code to execute if the condition is false
print(another_message)

OUTPUT:
Bye
22)n = 20

# First loop:
for i in range(2, n // 4):
if n % i == 0:
print("Python Output based Questions")
else:
print("Bye")

# Second loop:
for i in 123:
print(i)

SYNTAX:

variable = value
# First loop
for variable in range(start, stop):

if condition: # Check the condition


print(message) # Print a message if the condition is true

else:
print(another_message) # Print a different message if the condition is false

# Second loop
for variable in iterable: # Loop over each element in an iterable (e.g., string, list)
print(variable) # Print each element of the iterable

OUTPUT:

Bye
Bye
Python Output based Questions
1
2
3

for i in [10, 20, 30]:


print("Hello", i)

SYNTAX:

for variable in [value1, value2, value3]: # Loop over a list of values


# Code to execute for each value in the list
print(message, variable) # Print a message with the current value
OUTPUT:

Hello 10
Hello 20
Hello 30

x=2
for i in range(x**2, x, -1):
print(x)

SYNTAX:

variable1 = value1 # Initialize a variable with a value


for variable in range(start, stop, step): # Loop over a range from start to stop, with
a given step
print(variable2) # Print variable2 (in this case, x) in each iteration

OUTPUT:

2
2

x = 10
for i in range(x):
if x == 5:
break
print("H")
print(x)

SYNTAX:

variable = value
for variable in range(start, stop): # Loop over a range of values
if condition: # Check if a certain condition is true
break # Exit the loop if the condition is met
print(message) # Print a message in each iteration

print(variable) # Print the value of the variable after the loop

OUTPUT:
H
H
H
H
H
H
H
H
H
10

s=0
for i in range(5):
s=s+i
print(s)

SYNTAX:

variable = 0 # Initialize the sum variable


for variable in range(start, stop): # Loop over a range of values
variable = variable + loop_var # Add the current value to the sum variable

print(variable) # Print the final sum value

OUTPUT:

10

for i in range(1, 11):


print("%d" % i)

SYNTAX:

for variable in range(start, stop): # Loop over a range of values


print("formatted_output" % variable) # Print formatted output for each variable

OUTPUT:

1
2
3
4
5
6
7
8
9
10
print((3 > 1) and (9 < 1))

SYNTAX:

print(expression1 and expression2)


· expression1 and expression2 are Boolean expressions (like 3 > 1 and 9 < 1).
· The and operator evaluates the logical AND of the two expressions.

OUTPUT:

False

Write a program to print first 10 natural number.

for i in range(1, 11):


print(i)

OUTPUT:
1
2
3
4
5
6
7
8
9
10

Write a program to print first 10 even numbers.

for i in range(2, 21, 2):


print(i)

OUTPUT:
2
4
6
8
10
12
14
16
18
20
Write a program to print first 10 odd numbers.

for i in range(1, 20, 2):


print(i)

OUTPUT:

1
3
5
7
9
11
13
15
17
19

Write a program to print first 10 even numbers in reverse order

for i in range(20, 1, -2):


print(i)

OUTPUT:

20
18
16
14
12
10
8
6
4
2

Write a program to print table of a number accepted from user

num = int(input("Enter a number to print its table: "))

for i in range(1, 11):


print(f"{num} x {i} = {num * i}")
OUTPUT:

Enter a number to print its table: 5


5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Write a program to display product of the digits of a number accepted from the
user.

num = int(input("Enter a number: "))


product = 1
while num > 0:
digit = num % 10
product *= digit
num //= 10
print("The product of the digits is:", product)

OUTPUT:
Enter a number: 123
The product of the digits is: 6

Enter a number: 456


The product of the digits is: 120

Write a program to find the factorial of a number.

num = int(input("Enter a number to find its factorial: "))


factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers.")
else:
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is: {factorial}")
OUTPUT:

Enter a number to find its factorial: 5


The factorial of 5 is: 120

Enter a number to find its factorial: 7


The factorial of 7 is: 5040

Enter a number to find its factorial: -3


Factorial does not exist for negative numbers.

You might also like