0% found this document useful (0 votes)
21 views2 pages

Programming Exercises for Beginners

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

Programming Exercises for Beginners

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

1. Write a program to demonstrate the usage of various arithmetic operators.

2. Write a program that makes use of trigonometric functions available in math module.
3. Write a program that will convert various temperatures.
a. Fahrenheit to Centigrade
b. Centigrade to Fahrenheit
4. Write a program that will find the roots of a quadratic equation: ax² + bx + c = 0
5. Write a program that demonstrate the usage of various String functions.
6. Write a program that will ask you to enter your name, through keyboard, and perform
following operations
a. Find the middle name
b. Find the last name (using string slicing)
c. Re-write the name with surname first.
7. Write a program to find out whether the integer entered by the user, through the
keyboard, is even or odd number.
8. Find out the youngest among Shyam, Dugu and Ishan whose ages are entered by the
user through keyboard.
9. Given three points (x1, y1), (x2, y2), (x3, y3), write a program to check all the three
points fall on one straight line.
10. Write a program that will print the odd numbers from n1 to n2 where the values of n1
and n2 are entered by the user.
11. Write a program to find the factorial value of a number entered by the user.
12. Write a program to print all prime numbers between n1 to n2 where the values of n1
and n2 are entered by the user.
13. Write a program to demonstrate basic operations on the list.
14. Write a program to demonstrate stack and queue operations using a list of numbers.
15. Write a program to ask the data of five students that contain name, roll number, age.
Sort the list based on roll number of the student. [Note: Use list of lists].
16. Write a program that will add two square matrices. The dimension and elements of
the matrices will be entered by the user.
17. Write a program to demonstrate basic operations on the tuple.
18. Store the data about the shares held by the user as tuples containing the following
information about shares: share name, cost price, number of shares, selling price.
Write a program to determine:
a. total cost of the portfolio
b. total amount gained or lost
19. Write a program to demonstrate basic operations on the set.
20. Create an empty set. Write a program that adds five student names to this set,
modifies one existing name, and deletes two names existing in it. [ask the user which
name to modify/delete].
21. Write a program to demonstrate basic operations on the dictionary.
22. Create a dictionary to store data (name, roll number) of N students. The key will be
the roll number of the student and the value contains the data of the student (in a list).
Write a program that asks the user to enter a name of a Student, search it in the
dictionary and print the data of the Student if it is available otherwise display an
appropriate message.
23. Write a program to demonstrate basic comprehensions on list, set and dictionary.
[Link] a program that will find x^n (x to the power of n) using a function. The function
receives the value of x, n and should return the value of x^n. [don’t use any mathematical
function].

Common questions

Powered by AI

Using dictionary data structures to store student information is beneficial because they allow fast lookups, insertions, and deletions, with student data accessed via unique keys such as roll numbers. This structure provides clarity, as data is structured in a key-value format, allowing quick retrieval and updates. However, downsides include increased memory usage compared to simple lists, especially with large datasets, and potential complications in handling hash collisions. Additionally, maintaining unique keys can be complex if multiple entries require management, requiring vigilant key management .

List comprehensions provide a concise way to create lists. They optimize code by reducing the number of lines and thus improving readability and performance. Instead of using a loop to append elements to a list, a list comprehension can perform the same operation in a single line, which is particularly useful for filtering and applying transformations to a list. This method can lead to execution that is both faster and memory efficient due to Python’s internal optimizations. Additionally, list comprehensions can be more expressive and intuitive, making it easier to understand and maintain the code .

To determine if three points lie on a straight line, the program can calculate the slope between each pair of points. If the slopes are equal, the points lie on the same line. The steps involve obtaining the coordinates (x1, y1), (x2, y2), (x3, y3) from the user, then calculating the slope between (x1, y1) and (x2, y2), and between (x2, y2) and (x3, y3) using the formula slope = (y2 - y1) / (x2 - x1). If both calculated slopes are equal, then the points are collinear. The program must account for division by zero and parallel lines when x-coordinates are equal to avoid errors .

The use of trigonometric functions enhances a program's functionality by allowing it to perform calculations related to angles, waves, and periodic phenomena, which are common in fields such as physics, engineering, and computer graphics. For example, trigonometric functions are essential for simulating motion, generating realistic graphics, and calculating trajectories. Incorporating these functions into programs extends their ability to solve real-world problems involving geometric and periodic behavior .

When designing a program for temperature conversion, considerations include understanding the mathematical formulas: °C = (°F - 32) × 5/9 for Fahrenheit to Centigrade, and °F = °C × 9/5 + 32 for Centigrade to Fahrenheit. The program must accurately handle user input, being robust to different input types and potential errors. It should also provide clear and precise feedback to the user. Furthermore, optimizing for efficiency, especially when handling batch conversions or integrating with other systems, and ensuring that the program is maintainable for future modifications are important considerations .

A program can determine the roots of a quadratic equation using the quadratic formula: roots = (-b ± √(b² - 4ac)) / (2a). Implementation involves calculating the discriminant (b² - 4ac), ensuring it’s non-negative for real roots. The program must handle different scenarios: two distinct real roots when the discriminant is positive, one real root when it is zero, and complex roots when it's negative. Challenges include handling numerical precision errors, especially for very small or large coefficients, and ensuring the program doesn’t attempt division by zero when 'a' is zero, which would not form a quadratic equation .

Understanding the structure and behavior of stacks and queues aids in solving programming problems because they offer specific ways to manage data. Stacks follow the Last In, First Out (LIFO) principle, making them useful for problems involving backtracking, reversing, or undo operations. Queues follow the First In, First Out (FIFO) principle, ideal for managing processes in order, such as scheduling tasks or operations that require sequential processing. These structures help organize data efficiently and are foundational for various algorithms, providing efficient ways to implement complex tasks .

To determine the youngest person among Shyam, Dugu, and Ishan, the program should compare the ages provided by the user. This involves storing each name and corresponding age in a data structure, such as a dictionary or list, and then using a loop or built-in functions to find the entry with the minimal value for age. The program must handle cases of equal ages, possibly by returning all names that share the minimal age. Proper error handling for potential incorrect user input is also necessary .

String slicing can effectively manipulate and analyze user input by allowing precise control over substrings, which facilitates operations like extracting specific parts of input, such as a middle name, or re-ordering textual data. This can be crucial in applications where user input needs to be reshaped or formatted. For instance, string slicing can be used to extract last names from full names, reverse strings, or modify input for personalized responses in applications. Effective use of slicing improves performance and readability by eliminating the need for complex manipulations with multiple string operations .

Proper input validation is crucial when adding two square matrices to ensure that the matrices have the same dimensions, which is necessary for element-wise addition. Without input validation, the program may try to add matrices of different sizes, leading to runtime errors or incorrect results. Ensuring that the user inputs numeric values and that the dimensions of both matrices are identical is essential for accurate computation. Additionally, validation prevents potential security vulnerabilities, such as injection attacks, when improper input is handled inadvertently .

You might also like