Python Programs for Number Operations
Python Programs for Number Operations
The program demonstrates slicing, concatenation, and repetition. Slicing extracts a substring, shown by str6 which is str1's first five elements. Concatenation combines multiple strings into one, producing str7 by joining str2, str3, str4, and str5. Repetition replicates str7 thrice to form str8 .
The diamond pattern is generated using nested loops, where spaces and asterisks print symmetrically. Initially, spaces are set to 5 and decrease while the number of asterisks (sp_ch) increases, forming the upper half. Then, sp_ch is decremented and spaces incremented to complete the lower half, creating a symmetrical diamond shape .
The program illustrates appending, inserting, and extending lists. append adds 'finish' to the end of L1. insert places L2 at the start of L1, nesting it within L1. extend incorporates all elements of L3 into L1. Each operation alters the length and structure of the list, affecting iteration and element access .
The program using a library function leverages Python's 'math' module, specifically math.factorial(num), to directly compute the factorial of a number, making it more efficient. In contrast, the manual method iteratively multiplies numbers from 1 to num, which is more prone to errors and less optimal for larger numbers .
The program initializes a flag to 0, then iterates through numbers from 2 to one less than the given number, checking for divisibility. If the input number is divisible by any of these numbers, the flag is set to 1, indicating non-prime. If the flag remains 0 throughout, the number is prime .
The program compares the three numbers using conditional statements. It first checks if the first number (num1) is greater than or equal to both the second (num2) and third number (num3). If true, it prints num1 as the largest. If not, it checks if num2 is greater than or equal to num1 and num3; if true, it prints num2 as the largest. Otherwise, num3 is the largest and is printed .
The program initializes the first two numbers of the Fibonacci series as num1=0 and num2=1. Using a loop that runs up to n times, it prints num1, then computes the next Fibonacci number as the sum of num1 and num2. It updates num1 and num2 to continue the series .
The program calculates the number of digits in the given number and stores it in num_L. It then initializes arm to 0 and uses a loop to extract each digit of the number. It raises each digit to the power of num_L, adds these to arm, and finally compares arm with the original number (n). If they are equal, the number is an Armstrong number .
The linear search function iterates through the elements of a list. It compares each element to the target value n. If a match is found, it returns True; otherwise, it proceeds until all elements are checked, ultimately returning False. It's used to find if the word "looping" is present in a tuple L1 .
The program reverses the input number by repeatedly extracting the last digit and building a reversed number. It compares this reversed number with the original. If they match, the number is a palindrome .