Untitled Document-10
Untitled Document-10
# Defining a 1D array
arr = [10, 20, 30, 40, 50]
# Accessing elements
print(arr[0]) # Output: 10
print(arr[2]) # Output: 30
Conditions:
A conditions system in programming refers to the use of conditional statements to
control the flow of a program based on specified conditions. It allows the program to
make decisions and execute specific code blocks depending on whether a condition is
true or false.
2. if-else Statement: Executes one block of code if the condition is true, and another
block if it is false.
4. Switch Case (in some languages): Alternative for multiple conditions (not available
directly in Python).
# if statement
x = 10
if x > 5:
print("x is greater than 5") # Output: x is greater than 5
# if-else statement
y=3
if y > 5:
print("y is greater than 5")
else:
print("y is less than or equal to 5") # Output: y is less than or equal to 5
# if-elif-else statement
z=7
if z < 5:
print("z is less than 5")
elif z == 7:
print("z is equal to 7") # Output: z is equal to 7
else:
print("z is greater than 5 but not equal to 7")
If you're facing issues with a conditions system, here are a few common problems and
solutions you can look into:
Problem: The condition might not be evaluating as expected, leading to incorrect output.
Example:
x = 10
if x = 10: # Incorrect: assignment instead of comparison
print("x is 10")
if x == 10:
print("x is 10")
Example:
x=5
if x > 0:
if x < 10:
print("x is between 0 and 10") # Indentation error
Solution: Ensure the code inside the nested condition is properly indented.
if x > 0:
if x < 10:
print("x is between 0 and 10")
3. Multiple Conditions
Example:
x=5
if x > 0 or x < 10: # This always evaluates to True
print("x is positive or less than 10")
Solution: Double-check the logic. In the example above, x > 0 or x < 10 will always be
true because all numbers are either positive or less than 10. Consider restructuring your
condition:
Example:
x = 15
if x < 10:
print("x is less than 10")
# No else or elif to handle other conditions
if x < 10:
print("x is less than 10")
else:
print("x is 10 or greater")
if x == 10:
print("x is 10")
6. Boolean Variables
Problem: Confusion when using Boolean values (True or False) directly in conditions.
Example:
is_raining = True
if is_raining:
print("Take an umbrella")
Solution: If the variable is already a Boolean, you can directly use it in the condition
without comparison. Just check the value.
Dry Run:
A dry run is a manual simulation of a program's execution to track the flow and values of
variables at each step. Let's do a dry run of a simple 1D array operation in Python to
help you understand how it works.
Problem:
We have a 1D array, and we want to find the sum of all the elements in the array.
Code:
Dry Run:
1. Initialization:
arr = [1, 2, 3, 4, 5]
sum = 0
7. End of Loop: The loop has finished iterating through all the elements in the array.
8. Final Output:
print("Sum of elements:", sum) will output:
Sum of elements: 15
Explanation:
In each iteration of the loop, the current element of the array (num) is added to the sum.
After all iterations, the sum holds the total of all elements in the array, which is 15.
This dry run shows how the loop processes each element and how the sum variable is
updated at each step.