Robotics & Artificial Intelligence
Python allows repetitive execution of a block of code through loops. The two primary loops in Python are for and while. You may use the range() function with a for loop to iterate over a sequence of numbers.
Write Python code that calculates the sum of all even numbers between 1 and 100 using for loop.
Python Control Flow
3 Likes
Answer
sum_even = 0
for i in range(1, 101):
if i % 2 == 0:
sum_even = sum_even + i
print("Sum of all even numbers between 1 and 100 =", sum_even)Output
Sum of all even numbers between 1 and 100 = 2550
Answered By
2 Likes
Related Questions
Which of the following errors occur when there is a grammatical mistake in the code, such as a misspelling of a function name?
- Logical errors
- Linking errors
- Syntax errors
- Runtime errors
What is a dictionary in Python?
- A collection of ordered and immutable elements
- A collection of unordered, changeable, and indexed key-value pairs
- A collection of unique elements
- A collection of ordered values with no keys
A nested loop refers to a loop inside another loop. This is often used when you need to perform operations that involve multi-dimensional data or multiple conditions.
Write Python code to print the multiplication table for numbers from 1 to 5.
What is the purpose of the 'in' operator in Python?
- To check if a value exists in a sequence
- To start a loop
- To create a function
- To define a variable