CBSE 10th Class Unit 2 - Errors-Flow of control-Conditional statements-simple programs-Iterative Statement-Strings

Errors-Flow of control-Conditional statements-simple programs-Iterative Statement-Strings

  • Syntax Errors:
    • Violations of the rules of the programming language (e.g., missing parentheses, incorrect indentation).
    • Detected by the interpreter during the code compilation.
  • Logical Errors:
    • Errors in the logic of the program that cause it to produce incorrect results.
    • Difficult to find as the program runs without crashing.
  • Runtime Errors:
    • Errors that occur during the execution of the program.
    • Example: ZeroDivisionError, IndexError, TypeError.

Flow of Control

  • Sequential Flow:
    • Statements are executed in the order they appear in the code.
  • Conditional Flow:
    • The flow of execution is determined by conditions.
    • if, elif, else statements are used to make decisions.

 

                                                     ifelifelse flow chart

  • Iterative Flow:
    • Statements are executed repeatedly.
    • for loop: Iterates over a sequence (e.g., list, string).
    • while loop: Executes as long as a condition is True.


                                                    while loop flow chart

Indentation

  • Python uses indentation to define code blocks.
  • Indentation is crucial for the correct execution of conditional and iterative statements.

Certainly, let's delve into Conditional Statements, Iterative Statements, Strings, and Lists in Python with illustrative examples and images.

1. Conditional Statements

  • if: Executes a block of code only if a condition is True.

Python

if condition:

    # code to be executed if condition is True

  • if-else: Executes one block of code if a condition is True, and another block if it's False.

Python

if condition:

    # code to be executed if condition is True

else:

    # code to be executed if condition is False


if-elif-else: Allows for multiple conditions to be checked sequentially.


ifelifelse flowchart

Python

if condition1:

    # code to be executed if condition1 is True

elif condition2:

    # code to be executed if condition2 is True

else:

    # code to be executed if none of the conditions are True

Example: Absolute Value

Python

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

 

if num >= 0:

    absolute_value = num

else:

    absolute_value = -num

 

print("Absolute value:", absolute_value)

Example: Sorting 3 Numbers

Python

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

num3 = int(input("Enter the third number: "))

 

if num1 >= num2 and num1 >= num3:

    largest = num1

    if num2 >= num3:

        middle = num2

        smallest = num3

    else:

        middle = num3

        smallest = num2

elif num2 >= num1 and num2 >= num3:

    largest = num2

    if num1 >= num3:

        middle = num1

        smallest = num3

    else:

        middle = num3

        smallest = num1

else:

    largest = num3

    if num1 >= num2:

        middle = num1

        smallest = num2

    else:

        middle = num2

        smallest = num1

 

print("Sorted order:", smallest, middle, largest)

Example: Divisibility

Python

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

divisor = int(input("Enter the divisor: "))

 

if num % divisor == 0:

    print(num, "is divisible by", divisor)

else:

    print(num, "is not divisible by", divisor)

2. Iterative Statements

  • for loop: Iterates over a sequence (e.g., list, string, range()).

Python

for item in sequence:

    # code to be executed for each item

  • range() function: Generates a sequence of numbers.
    • range(start, stop, step): Generates numbers from start (inclusive) to stop (exclusive) with a specified step.
  • while loop: Executes as long as a condition is True.

Python

while condition:

    # code to be executed as long as condition is True

  • break statement: Exits the current loop prematurely.
  • continue statement: Skips the current iteration and moves to the next.


 

                                                            while loop flowchart

  • Example: Generating Pattern

Python

for i in range(1, 6):

    print("*" * i)

  • Example: Summation of Series

Python

n = int(input("Enter the number of terms: "))

sum = 0

 

for i in range(1, n+1):

    sum += i

 

print("Sum of first", n, "natural numbers:", sum)

Example: Factorial of a Number

Python

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

factorial = 1

 

if num < 0:

    print("Factorial is not defined for negative numbers.")

elif num == 0:

    print("Factorial of 0 is 1")

else:

    for i in range(1, num+1):

        factorial *= i

 

print("Factorial of", num, "is", factorial)

3. Strings

  • Introduction: A sequence of characters enclosed in single quotes (') or double quotes (").
  • String Operations:
    • Concatenation: Combining two or more strings using the + operator.
    • Repetition: Repeating a string multiple times using the * operator.
    • Membership: Checking if a substring exists within a string using in and not in.
    • Slicing: Extracting a portion of a string using indexing and slicing.
  • Built-in Functions/Methods:
    • len(): Returns the length of a string.
    • capitalize(): Converts the first character to uppercase.
    • title(): Converts the first character of each word to uppercase.
    • lower(): Converts all characters to lowercase.
    • upper(): Converts all characters to uppercase.
    • count(): Counts the number of occurrences of a substring.
    • find(): Returns the index of the first occurrence of a substring.
    • index(): Same as find(), but raises an exception if the substring is not found.
    • endswith(): Checks if the string ends with a specified suffix.
    • startswith(): Checks if the string starts with a specified prefix.
    • isalnum(): Checks if the string consists of alphanumeric characters only.
    • isalpha(): Checks if the string consists of only alphabetic characters.
    • isdigit(): Checks if the string consists of only digits.
    • islower(): Checks if all characters are lowercase.
    • isupper(): Checks if all characters are uppercase.
    • isspace(): Checks if the string consists of only whitespace characters.
    • lstrip(): Removes leading whitespace characters.
    • rstrip(): Removes trailing whitespace characters.
    • strip(): Removes both leading and trailing whitespace characters.
    • replace(): Replaces occurrences of one substring with another.
    • join(): Joins elements of a sequence with a string as a separator.
    • partition(): Splits the string at the first occurrence of a specified separator.
split(): Splits the string into a list of substrings based on a separator.




No comments:

Post a Comment