Python Programming Practical Exercises
Python Programming Practical Exercises
The Python program swaps list elements specifically at even and odd indices by iterating through the list with a step of 2, starting from index 0. This ensures that each pair consists of even-odd index combinations. This design is significant because it maintains logical pairing and swapping across even positioned elements and their succeeding elements without affecting list integrity or missing an index inadvertently .
The Python program classifies a number as Prime or Composite by counting its divisors. It loops from 1 to the number itself, checking divisibility. If the number has exactly two divisors, 1 and the number itself, it is classified as Prime; otherwise, it is Composite. This approach is effective because it directly tests the fundamental definition of a prime number, ensuring an accurate classification through exhaustive divisor checking .
The Python program determines whether a number is a Special number by checking three characteristics: if it's a Perfect, Armstrong, or Palindrome number. A Palindrome number reads the same backward as forward, verified by reversing the digits and comparing to the original number. An Armstrong number is equal to the sum of its digits each raised to the power of three. A Perfect number equals the sum of its proper divisors. For a given number, all these checks are performed, and the program prints the respective type if the condition is met .
The algorithm used in the Python program to calculate the Greatest Common Divisor (GCD) is Euclid's Algorithm. This algorithm works by repeatedly replacing the larger number by its remainder when divided by the smaller number until one of the numbers becomes zero. The other number at this point is the GCD. This method correctly finds the GCD because it relies on the principle that the GCD of two numbers also divides their difference .
The Python program computes the Fibonacci sequence using iterative addition. It initializes the first two terms as 0 and 1. For each subsequent term up to the specified number, it prints the current term and updates the next term as the sum of the previous two terms. This computation continues until all desired terms in the sequence are generated, relying on simple addition to build the sequence iteratively .
The program detects if a string is a palindrome by comparing it to its reverse using slicing `s[::-1]`. For case conversion, it uses the `upper()` and `lower()` methods to display the string in uppercase and lowercase respectively. This dual functional approach makes it efficient for both palindrome checking and providing different case representations .
The Python program manages student records using a dictionary to store names and marks. It employs a simple loop to collect data for each student, then iterates over the stored records to print those scoring above a certain threshold (75 marks). This strategy allows efficient data retrieval based on conditional checks, demonstrating a straightforward yet effective method for record management and querying based on specific criteria .
The Python program implements a search in a list using the `in` keyword, which checks for the presence of a specified element within the list. It reads the target element from the user input and iterates over the list elements to see if the element is matched. If found, it prints 'Element found'; otherwise, it prints 'Element not found'. This logic is efficient for membership testing as it leverages Python's built-in list operations .
The Python program counts different character types in a string using a loop that iterates over each character. It distinguishes between vowels and consonants by checking if the character is in 'aeiou'. It also differentiates between uppercase and lowercase characters using `.isupper()` and `.islower()` methods. Counts for vowels, consonants, uppercase, and lowercase are maintained separately for accurate results .
The pattern printing algorithm uses nested loops to print a triangular pattern of asterisks. The outer loop determines the number of rows, while the inner loop prints an increasing number of asterisks per row. This construct effectively scales with the number of rows, producing output efficiently with time complexity proportional to the square of n, ensuring consistent performance irrespective of input size .