0% found this document useful (0 votes)
7 views6 pages

AI 417 Python Prac

The document contains a series of Python programs for practical exercises, including printing personal information, calculating simple interest, and generating patterns. It covers various topics such as area calculations, checking number properties, and generating sequences like Fibonacci. Each program is presented with code snippets and brief descriptions of their functionality.

Uploaded by

sharvimulay3
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)
7 views6 pages

AI 417 Python Prac

The document contains a series of Python programs for practical exercises, including printing personal information, calculating simple interest, and generating patterns. It covers various topics such as area calculations, checking number properties, and generating sequences like Fibonacci. Each program is presented with code snippets and brief descriptions of their functionality.

Uploaded by

sharvimulay3
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 Programmes for Practical File

1. To print personal information like Name, Father’s Name, Class,


School Name.

name = input("Enter Name: ")


father_name = input("Enter Father's Name: ")
student_class = input("Enter Class: ")
school_name = input("Enter School Name: ")
print("Name:”, name)
print("Father :", father_name)
print("Class:",student_class)
print("School Name:", school_name)

2. To print the following patterns using multiple print commands

rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print("*", end="")
print()

3. To convert given length in kilometers into meters.

km = float(input("Enter length in kilometers: "))


m = km*1000
print("{km} kilometers is equal to {m} meters")

4. To calculate Simple Interest if the principle_amount = 2000


rate_of_interest = 4.5 time = 10

principle = 2000
rate = 4.5
time = 10
interest = (principle * rate * time) / 100
print("Principle Amount:” ,principle)
print("Rate of Interest: “,rate”%")
print("Time Period (years):”, time)
print("Simple Interest:”, interest)
5. To calculate Area of a triangle with Base and Height

base = 10
height = 10
area = 0.5 * base * height
print("The base of the triangle is:”, base)
print("The height of the triangle is:”, height)
print("The area of the triangle is:”, area)

6. To calculate Surface Area and Volume of a Cuboid

length = 4
breadth = 5
height = 6

area_cuboid = 2 * ((length * breadth) + (breadth * height) + (height *


length))
volume = length * breadth * height
print("Surface area of the cuboid having length ", length, " breadth ",
breadth, " and height ", height, " is --> ", area_cuboid)
print("Volume of cuboid having length", length, " breath", breadth, " and
height ", height, " is --> ", volume)

7. Input a number and check if the number is positive, negative or


zero and display an appropriate message

number = float(input("Enter a number: "))


if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")

8. Create a list of first 10 even numbers, add 1 to each list item and
print the final list.

even_numbers = [i * 2 for i in range(10)]


print("Original list of the first 10 even numbers:”, even_numbers)
modified_numbers = [num + 1 for num in even_numbers]
print(“List after adding 1 to each item:”, modified_numbers)
[Link] print odd numbers from 1 to n

def print_odd_numbers(n):
if n < 1:
print("Please enter a positive integer for n.")
return

print("Printing odd numbers from 1 to {n}:")

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


print(i)
limit = 15
# Call the function
print_odd_numbers(limit)

[Link] print sum of first 10 natural numbers

# Sum of natural numbers up to num

num = 16

if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
[Link] print factorial of a number.(Using list)

[Link] find the greatest of 4 number.


[Link] print pattern
A

AB

ABC

14. To print Fibonacci series

nterms = int(input("How many terms? "))


# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
15. WAP TO REVERSE AN INTEGER

16. WAP TO PRINT THE INTEGER IS PALINDROME OR NOT


PALINDROME

Common questions

Powered by AI

To alter a list of odd numbers from 1 to n to exclude those divisible by a specific integer, you filter with a condition inside a list comprehension. For example, if 'd' is the integer and 'n' is the limit, a list comprehension would look like: [i for i in range(1, n+1, 2) if i % d != 0]. This checks each odd number generated in the range loop, including it in the list only if it is not divisible by 'd'. This method efficiently applies complex filters without additional loops.

The program ensures that a user only inputs a positive number by using an if-else statement to check the user's input. If the number is negative, an error message is printed: 'Enter a positive number'. Only when the condition 'num >= 0' is true does the program proceed to calculate the sum using the while loop. This pre-validation step ensures that calculations are performed only on valid input.

List comprehensions in Python provide a concise way to create and modify lists. They offer several advantages: syntax transparency, reduced need for boilerplate code, and improved readability by combining iteration and list manipulation in a single line. For example, creating a list of even numbers or modifying list elements with operations (like adding 1) can be carried out within a single expression, making the code cleaner and more efficient. Additionally, comprehensions can lead to performance benefits due to optimized list construction in Python.

When designing a program to calculate simple interest with varying parameters, you must ensure the program accepts dynamic input values for the principal amount, interest rate, and time. The formula for simple interest is (principal_amount * rate_of_interest * time) / 100. The program should validate the inputs to handle negative or zero values inappropriate for these financial calculations. Additionally, user interface considerations can improve usability by making sure the input prompts are clear and the output is presented in an easily understandable format.

Initializing variables before loops is crucial in list-based factorial calculations as it sets up the starting values that subsequently drive the loop's operations. For factorials, you must start at an initial value of 1 (since factorial is a product operation), before iterating over a computed list of integers. By initializing, you ensure the loop can accurately perform calculations, updating and multiplying the value correctly at each step. Improper or missing initialization could lead to incorrect outcomes or errors.

The provided method for generating Fibonacci sequence up to 'n' terms is a linear algorithm with O(n) complexity, as it uses a simple while loop to iterate up to 'n' terms. Each iteration involves a constant-time operation of adding the previous two numbers. While efficient, if multiple Fibonacci numbers are required, a more memory-efficient approach can be an iterative solution without storing the whole sequence, or a dynamic programming approach using memoization. For large values of 'n', these alternatives can be optimized, reducing space or repeated calculations respectively.

To expand the logic for identifying the greatest of four numbers to a list of varying length, you would iterate over the list using a loop. Initialize a variable, say 'max_number', with the first element of the list. Then iterate through each number in the list, updating 'max_number' whenever a larger number is encountered (if number > max_number: max_number = number). This allows you to find the greatest number regardless of list length.

The principle behind converting kilometers to meters lies in the metric system, where 1 kilometer equals 1000 meters. To implement this conversion in a program, multiply the value in kilometers by 1000. For example, in Python, if km is the input variable, the conversion to meters is done by m = km * 1000. This simple multiplication expresses the conversion ratio between kilometers and meters.

To modify a program that reverses an integer to handle a string, you would change the data type handling and logic. Instead of manipulating each digit, you would manipulate each character in the string. You can use Python's slicing feature to reverse the string: for instance, reversed_string = input_string[::-1]. This method efficiently reverses strings by leveraging step indexing in slices.

Using nested loops is a suitable method for printing patterns like A, AB, ABC, because loops efficiently handle repeated operations and conditions. The outer loop controls the number of lines, and the inner loop prints characters accordingly. However, an alternative method could use join operations on a range of characters. For instance, you could construct each line by joining the first several characters of the alphabet sequence up to the desired index for each line, which can be more concise and potentially more readable in some cases.

You might also like