0% found this document useful (0 votes)
40 views52 pages

Python Programming Lab Manual

Uploaded by

Amardeep
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)
40 views52 pages

Python Programming Lab Manual

Uploaded by

Amardeep
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

lOMoARcPSD|46886623

SRI SUKHMANI INSTITUTE OF


HOSPITALITY

&

MANAGEMENT
LAB MANUAL

Name: Amardeep
Course Code: UGCA1917
Course Name: Programming in
python laboratory Submitted to:
Miss Manpreet Kaur
lOMoARcPSD|46886623

[Link] Practical Name Date Signature

1. Compute sum, subtraction, multiplication, division and exponent of


given variables input by the user.

2. Compute area of following shapes: circle, rectangle, triangle, square,


trapezoid and parallelogram

3. Compute volume of following 3D shapes: cube, cylinder, cone and


sphere

4. Compute and print roots of quadratic equation ax2+bx+c=0, where


the values of a, b, and c are input by the user.

5. Print numbers up to N which are not divisible by 3, 6, 9,, e.g., 1, 2,


4, 5, 7,….

6. Write a program to determine whether a triangle is isosceles or not?

7. Print multiplication table of a number input by the user

8. Compute sum of natural numbers from one to n number

9. Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13…..n

10. Compute factorial of a given number

11. Count occurrence of a digit 5 in a given integer number input by the


user

12. Print Geometric and Harmonic means of a series input by the user

13. Evaluate the following expressions: a. x-x2/2!


+x3/3!- x4/4!+… xn/n!
b. x-x3/3!+x5/5!- x7/7!+… xn/n!

14. Print all possible combinations of 4, 5, and 6.

15. Determine prime numbers within a specific range

1
lOMoARcPSD|46886623

Practical-1

Compute sum, subtraction, multiplication, division and exponent of given variables input by

the user

2
lOMoARcPSD|46886623

Output:

3
lOMoARcPSD|46886623

Practical-2

Compute area of following shapes: circle, rectangle, triangle, square,


trapezoid and parallelogram

4
lOMoARcPSD|46886623

Output:

5
lOMoARcPSD|46886623

Practical-3

Compute volume of following 3D shapes: cube, cylinder, cone and sphere

6
lOMoARcPSD|46886623

Output:

7
lOMoARcPSD|46886623

Practical-4

Compute and print roots of quadratic equation ax2+bx+c=0, where


the values of a, b, and c are input by the user

8
lOMoARcPSD|46886623

Output:

9
lOMoARcPSD|46886623

Practical-5

Print numbers up to N which are not divisible by 3, 6, 9,, e.g., 1, 2, 4, 5, 7, ….

10
lOMoARcPSD|46886623

P
ractical-6

Write a program to determine whether a triangle is isosceles or not?

11
lOMoARcPSD|46886623

Practical – 7

Print multiplication table of a number input by the user

12
lOMoARcPSD|46886623

Output:

Practical – 8

Compute sum of natural numbers from one to n number

13
lOMoARcPSD|46886623

Output:

Practical-9

Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13…..n

14
lOMoARcPSD|46886623

Output:

15
lOMoARcPSD|

Practical—

10

Compute factorial of a given number

Output:

16
lOMoARcPSD|

Practical—

11

Count occurrence of a digit 5 in a given integer number input by the user

Output:

17
lOMoARcPSD|

Practical—

12

Print Geometric and Harmonic means of a series input by the user

Output:
lOMoARcPSD|

Practical—

16

13

Evaluate the following expressions:

a. x-x2/2!+x3/3!- x4/4!+… xn/n!

b. x-x3/3!+x5/5!- x7/7!+… xn/n!

Output:

19
lOMoARcPSD|

Practical—
lOMoARcPSD|

Practical—

b) x-x3/3!+x5/5!- x7/7!+… xn/n!

Output:

21
lOMoARcPSD|46886623

14

Print all possible combinations of 4, 5, and 6.

Output:

Practical –15

22
lOMoARcPSD|

Practical—

Determine prime numbers within a specific range

Output:

16

Count number of persons of age above 60 and below 90.

23
lOMoARcPSD|46886623

Output:

24
lOMoARcPSD|46886623

17

Compute transpose of a matrix

Output:

25
lOMoARcPSD|

Practical—

18

26
lOMoARcPSD|46886623

Perform following operations on two matrices.

1) Addition

2) Subtraction

3) Multiplication

Output:

Output:

27
lOMoARcPSD|

Practical—

Output:

19
Count occurrence of vowels.

28
lOMoARcPSD|46886623

Output:

29
lOMoARcPSD|

Practical—

20
Count total number of vowels in a word

Output:

21
Determine whether a string is palindrome or not

30
lOMoARcPSD|

Practical—

Output:

22
Perform following operations on a list of numbers:

1) Insert an element

2) delete an element

3) sort the list

4) delete

31
lOMoARcPSD|

Practical—

32
lOMoARcPSD|46886623

Output:

Practical –23
Display word after Sorting in alphabetical order

33
lOMoARcPSD|46886623

Output:

Practical –24
Perform sequential search on a list of given numbers

34
lOMoARcPSD|46886623

Output:

35
lOMoARcPSD|46886623

25

Perform sequential search on ordered list of given numbers

Output:

Practical –26
Maintain practical note book as per their serial numbers in library using Python dictionary

36
lOMoARcPSD|

Practical—

Output:

27
Perform following operations on dictionary

1) Insert

2) delete

37
lOMoARcPSD|46886623

3) change

Ouptut:

38
lOMoARcPSD|

Practical—

28
Check whether a number is in a given range using functions.

Output:

29
Write a Python function that accepts a string and calculates number of upper case letters and lower
case letters available in that string

39
lOMoARcPSD|

Practical—

Output:

30

To find the Max of three numbers using functions

40
lOMoARcPSD|

Practical—

Output:

31

Multiply all the numbers in a list using functions

41
lOMoARcPSD|

Practical—

Output:

32

Solve the Fibonacci sequence using recursion

42
lOMoARcPSD|

Practical—

Output:

33

Get the factorial of a non-negative integer using recursion

43
lOMoARcPSD|

Practical—

Output:

34

44
lOMoARcPSD|

Practical—

Write a program to create a module of factorial in Python

Output

35

45
lOMoARcPSD|

Practical—

Design a Python class named Rectangle, constructed by a length & width, also design a method

which will compute the area of a rectangle

Output

36

46
lOMoARcPSD|

Practical—

Design a Python class named Circle constructed by a radius and two methods which will compute

the area and the perimeter of a circle

Output

47
lOMoARcPSD|

Practical – 37

Design a Python class to reverse a string ‘word by word’

Output

48
lOMoARcPSD|

Practical –38

Write a Python program to read an entire text file.

Contents of [Link]

A pattern can be defined as anything that follows a trend and exhibits some kind of regularity. The

recognition of patterns can be done physically, mathematically or by the use of algorithms.

Output

Practical—39

49
lOMoARcPSD|

Design a Python program to read first n lines of a text file.

Contents of [Link]

Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.


A pattern can be defined as anything that follows a trend and exhibits some kind of regularity.
The recognition of patterns can be done physically, mathematically or by the use of algorithms.

Output

Practical—40

Construct a Python program to write and append text to a file and display the text.

50
lOMoARcPSD|

Output

51

Common questions

Powered by AI

To compute the area of a trapezoid in Python, you'll need the lengths of the two parallel sides (bases) and the height of the trapezoid. The formula for the area of a trapezoid is (1/2) * (base1 + base2) * height. You can implement this in Python by defining a function that takes the bases and height as inputs and returns the calculated area.

The advantage of using recursion to solve the Fibonacci sequence is that it provides a cleaner and more intuitive understanding of the problem, as it directly mirrors the mathematical definition of the Fibonacci sequence. However, this approach can be less efficient in terms of time and memory, as it may involve repeated calculations of the same values unless optimized with techniques like memoization. An iterative approach, on the other hand, is more efficient as it calculates the sequence iteratively and uses less memory.

The harmonic mean can be calculated in Python using the formula: n / (sum of reciprocals of the series), where n is the number of values. In a Python program, you could prompt the user to input a series of numbers, use a loop to calculate the reciprocals of these numbers, sum them up, and then divide the number of values by this sum to get the harmonic mean. This approach allows you to capture and compare different rates effectively, as harmonic mean gives higher weight to smaller values in the data set.

Python's list operations allow modification and organization of collections of numbers through methods such as append(), insert(), remove(), and sort(). For instance, you can insert an element at any position using list.insert(index, element) and use list.sort() to arrange elements in ascending or descending order. These operations are fundamental in managing and manipulating collections of data, making Python lists a powerful and versatile data structure for different types of data handling tasks.

To determine if a string is a palindrome in Python, you can compare the string with its reverse. This can be done by using slicing techniques (e.g., word[::-1]) and checking if the original string is equal to its reversed form. This functionality is useful in programming because it can help with data validation, ensuring that inputs meet specific criteria that might be ideal for problem-solving or UI interactions. Palindrome checking is also a common exercise in algorithmic training, enhancing skills in string manipulation.

Encapsulating geometry calculations in Python using classes, such as for a Rectangle and a Circle, is significant because it promotes the principles of object-oriented programming, including encapsulation and information hiding. It allows defining properties and behaviors (like area and perimeter calculations) that belong to these geometric shapes, making programs more modular, scalable, and easier to maintain. Classes provide a blueprint for creating objects, making it easier to implement changes in one place and enhancing code reuse.

Python can handle matrices through libraries like NumPy, which offer efficient implementations for matrix operations. You can perform addition, subtraction, and multiplication of matrices by leveraging NumPy's array operations, which are optimized for performance. This capability is important in computer science because matrices are foundational in numerous fields such as graphics, machine learning, and scientific computing, where they help represent data structures and enable complex calculations to be performed efficiently.

Python provides built-in functions to handle file operations, such as open(), read(), write(), and append(). To read from a file, the 'r' mode is used, and for appending, the 'a' mode is used where new content is added to the end of the file. These functionalities are crucial in real-world applications like logging (where new data is appended), data storage and retrieval, configuration management, and dynamically loading data into programs. They enable seamless interaction between applications and persistent data.

To evaluate the Taylor series using Python, you can use a loop to iterate over the terms while calculating each one using the general formula and summing them up. For example, x - x^2/2! + x^3/3! - x^4/4! + ..., you can write a Python function that takes x and n (number of terms) as inputs. Inside the loop, calculate each term using factorial calculation, possibly using Python's math module for efficiency, and alternate the signs as you sum them. This provides an approximation based on the number of terms you choose.

The challenges of using recursion for factorial computation include potential stack overflow errors for very large numbers and inefficiency due to repeated function calls and increased memory usage. However, the benefits include a straightforward and elegant implementation that clearly expresses the mathematical definition of factorial. This recursive approach enhances understanding and can lead to insights for solving more complex recursive problems. Optimization techniques like memoization can mitigate some performance issues.

You might also like