0% found this document useful (0 votes)
39 views3 pages

Python Practice Programs Collection

The document contains a collection of Python programs for practice, including finding Armstrong numbers, the largest among three numbers, factors of a number, square roots, multiplication tables, checking positive/negative/zero, printing prime numbers, checking leap years, and swapping variables. Each program is accompanied by code snippets demonstrating its functionality. The examples are straightforward and aimed at helping learners practice basic Python programming concepts.

Uploaded by

trix r8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Python Practice Programs Collection

The document contains a collection of Python programs for practice, including finding Armstrong numbers, the largest among three numbers, factors of a number, square roots, multiplication tables, checking positive/negative/zero, printing prime numbers, checking leap years, and swapping variables. Each program is accompanied by code snippets demonstrating its functionality. The examples are straightforward and aimed at helping learners practice basic Python programming concepts.

Uploaded by

trix r8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programs for Practice

1. Python Program to Find Armstrong Number in an Interval


lwr = 100

uppr = 2000

for numb in range(lwr, uppe + 1):

# order of number

odr = len(str(numb))

# initialize sum

sum = 0

tem = numb

while tem > 0:

digit = tem % 10

sum += digit ** odr

tem //= 10

if numb == sum:

print("The Armstrong numbers are: ",numb)

2. Python Program to Find the Largest Among Three Numbers:

def maximum(x, y, z):


if (x >= y) and (x >= z):
largest = x
elif (y >= x) and (y >= z):
largest = y
else:
largest = z
return largest
3. Python Program to Find the Factors of a Number
numb=int(input("enter any number"))

facts=[]

for a in range(1,numb+1):

if numb%a==0:

[Link](a)

print ("Factors of {} = {}".format(numb,facts))


4. Python Program to Find the Factors of a Number
numb=int(input("enter any number"))

facts=[]

for a in range(1,numb+1):

if numb%a==0:

[Link](a)

print ("Factors of {} = {}".format(numb,facts))

5. Python Program to Find the Square Root


numb = float(input('Enter a number: '))

numsqrt = numb ** 0.5

print('square root of %0.3f is %0.3f'%(numb ,numsqrt))

6. Python Program to Display the Multiplication Table


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

# using the for loop to generate the multiplication tables

print("Table of: ")

for a in range(1,11):

print(num,'x',a,'=',num*a)

7. Python Program to Check if a Number is Positive, Negative, or 0


n = float (input (“Enter the number: “))

if n > 0:

print (“The number provided is positive”)

elif n == 0:

print (“The number input is zero”)

else:

print (“The number provided is negative”)

8. Python Program to Print all Prime Numbers in an Interval


lwr = 900

upr = 1000

print("Prime numbers between", lwr, "and", upr, "are:")

for numb in range(lwr, upr + 1):

# all prime numbers are greater than 1

if numb > 1:
for a in range(2, numb):

if (numb % a) == 0:

break

else:

print(numb)

9. Python Program to Check Leap Year


year = int (input (“Enter any year that is to be checked for leap year: “))

if (year % 4) == 0:

if (year % 100) == 0:

if (year % 400) == 0:

print (“The given year is a leap year”)

else:

print (“It is not a leap year”)

else:

print (“It is not a leap year”)

else:

print (“It is not a leap year”)

10. Python Program to Swap Two Variables


var1 = input (“Enter the first variable”)

var2 = input (“Enter the second variable”)

temp = var1

var1 = var2

var2 = temp

print (“The value of the first variable after swapping is:”, var1)

print (“The value of the second variable after swapping is:”, var2)

Common questions

Powered by AI

The order of operations is crucial in the Python program that detects Armstrong numbers because it dictates how computations are carried out, specifically in raising each digit to the power of the number of digits. If calculated out of order, the comparison between the sum of powered digits and the original number could yield incorrect results, failing to identify true Armstrong numbers. Consistent application of operator precedence ensures accurate determination of these special numbers within the specified range .

The Python program displays a multiplication table for a given number using a for loop that iterates from 1 to 10. In each iteration, it multiplies the given number by the current iteration variable and prints the result. This method is effective because it utilizes Python's looping capabilities to automate repetitive tasks, ensuring each step of the table is accurately and clearly displayed .

The Python program identifies the factors of a number by iterating through all integers from 1 to the number itself and checking divisibility using the modulus operator. If the number is divisible by an integer, it is a factor and added to a list. This brute-force approach has a time complexity of O(n), as it must check each integer up to the number, which can be inefficient for large numbers compared to more optimized methods like only checking up to the square root of the number .

An Armstrong number is an integer such that the sum of its own digits each raised to the power of the number of digits is equal to the number itself. The Python program in the document determines Armstrong numbers within an interval by first calculating the number of digits in each number (order), then summing the digits each raised to this power, and finally checking if the resulting sum equals the original number. If they are equal, it prints the number as an Armstrong number .

The Python program checks whether a number is positive, negative, or zero using conditional statements. It first checks if the number is greater than zero, printing that it is positive if true. It then checks if the number is zero. If neither condition is met, the number must be negative, and it prints accordingly. This structure straightforwardly classifies the number based on its sign .

The leap year check method employed in the Python program efficiently uses nested conditional statements to evaluate the criteria: divisible by 4, but not by 100 unless also by 400. This logical sequence ensures that computational checks occur in the minimal necessary steps, optimizing performance by short-circuiting unnecessary evaluations as soon as a condition fails. This results in a highly efficient solution given the constraints of the problem .

The Python program identifies prime numbers within an interval by iterating through each number in the interval and checking if it is greater than 1. For each number, it attempts to divide it by every integer starting from 2 to the number minus one. If a division without remainder occurs, the number is not prime. If no such division is found, the number is confirmed as prime and printed. The method is practical for short intervals but not optimal for large numbers due to its higher computational complexity .

The swap operation in the Python program is executed by using a temporary variable to hold the value of one of the variables, allowing their values to be exchanged. Specifically, it stores the value of the first variable, assigns the second variable's value to the first, and finally sets the temporary value to the second variable. This illustrates the use of temporary storage to safely exchange values without loss, a common technique in programming .

The Python program determines the largest of three numbers by using conditional statements. It checks if the first number is greater than or equal to both the second and third numbers, assigning it as the largest if true. If not, it checks if the second number is greater than or equal to both the first and third numbers, and assigns it as the largest. Otherwise, it assigns the third number as the largest. This logic ensures each number is compared effectively to find the maximum .

The Python program calculates the square root of a number by using the exponentiation operator '**' with a power of 0.5. The mathematical principle relies on the fact that raising a number to the power of 0.5 is equivalent to taking its square root. This method provides a straightforward and efficient way to compute square roots in Python .

You might also like