Labs_solved

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 102

DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY

Name: Mohd Adnan Zohaib


Roll. No. : 2K22CSUN01149
Course: B.Tech(CSE)
Subject: Programming for Problem Solving using Python(CSW208B)

Lab -2: Programming constructs in python -hands-on practice

Blooms Taxonomy Level: BT1, BT2, BT3

1. Write a program to demonstrate basic data type in python.


Code :
2. Check whether a number is even or odd
Code :

Output:

3. Check whether an entered year is leap year or not.


Code :
Output:

4. Write a program to check whether a character is vowel or


consonants. Code:
Output:

5. Write a program to find the smallest of two numbers.


Code :
Output:

6. Find the Factorial of a Number


Code:

Output:
7. Write a program to print this patterns
*
* *
* * *
* * * *

Output:

8. Write a program to print this series


1 1 2 3 5 8 13
Code:
Output:

9. Check whether a number is prime or not


Code:
Output:

10. Make a Simple Calculator.


Code:
Output:
Name: Mohd Adnan Zohaib
Roll. No. : 2K22CSUN01149
Course: B.Tech(CSE)
Subject: Programming for Problem Solving using Python(CSW208B)

Lab -3: Programming constructs in python -hands-on practice

Blooms Taxonomy Level: BT1, BT2, BT3

1. Write a python program to swap two numbers using a third variable


Code:
Output:

2. Write a python program to swap two numbers without using third variable
Code:
Output:

3. Write a python program to read two numbers and find the sum of their cubes
Code:
Output:

4. Write a python program to read three numbers and if any two variables are
equal, print that number
Code:
Output:
5. Write a python program to read three numbers and find the smallest among
them
Code:

Output:
6. Write a python program to read three numbers and print them in ascending
order (without using sort function)
Code:

Output:
7. Write a python program to read radius of a circle and print the area
Code :

Output:

8. Write a python program to read a number ,if it is an even number , print the
square of that number and if it is odd number print cube of that number.
Code:

Output:
Name: Mohd Adnan Zohaib
Roll. No,: 2K22CSUN01149
Subject Name: Programming for Problem Solving using Python
Subject Code: (CSW208B)

Lab 4

Blooms Taxonomy Level: BT2, BT3


1. WAP to demonstrate Slicing Operations in Strings
Code :

Output:
2. WAP to demonstrate built in functions of Strings(rtrip, lstrip,lower, split)

Code:
Output:

3. WAP to check weather a given string is palindrome or not.


Code:

\
Output:
4. WAP to capitalize the first and last character of each word in a string.
Code:
Output:

5. WAP to accept two strings from the user and display the common
words.
Code:
Output:
6. WAP to accept a string and count the frequency of each vowel. Code:

Output:
7. WAP to display the smallest word from the string.
Code:

Output:
Name : Mohd Adnan Zohaib

Roll. No.: 2K22CSUN01149

Course: B.Tech(CSE)
Subject: Programming for Problem Solving using Python(CSW208B)

Lab-6: Strings - hands-on practice

Blooms Taxonomy Level: BT1, BT2, BT3

1. WAP to demonstrate while loop with else statement.


Code :

Output:
2. Print 1st 5 even numbers (use break statement).
Code:
Output:

3. Print 1st 4 even numbers (use continue statement).


Code:

Output:
4. WAP to demonstrate Pass statements.
Code:

Output:
5. Write a Python program to calculate the length of a string.
Code:

Ouput:

6. Write a Python program to count the number of characters


(character frequency) in a string

Code:
Output:

7. Write a Python program to get a string made of the first 2 and the
last 2 chars from a given a string. If the string length is less than 2,
return instead of the empty string.
Code:
Output:

8. Write a Python program to get a string from a given string where


all occurrences of its first char have been changed to '$', except the
first char itself
Code:
Output:

9. Write a Python program to get a single string from two given


strings, separated by a space and swap the first two characters of
each string.
Code:
Output:

10.Write a Python program to add 'ing' at the end of a given string (length
should be at least 3). If the given string already ends with 'ing' then add 'ly'
instead. If the string length of the given string is less than 3, leave it
unchanged.
Code:

Output:
Name: Mohd Adnan Zohaib

Roll no.: 2K22CSUN01149

Course: B.Tech(CSE) Semester:I V


Subject: Programming for Problem Solving using Python(CSW208B)

Lab 7
Blooms Taxonomy Level: BT1, BT2, BT3
Tuple
1. Write a Python program to create a tuple.
Code:

Output:

2. Write a Python program to create a tuple with different data types.


Code:

Output

3. Write a Python program to create a tuple with numbers and print one
item.
Code:

Output:

4. Write a Python program to unpack a tuple in several variables.

Code:
Output:

5. Write a Python program to add an item in a tuple.


Code:
Output:

6. Write a Python program to convert a tuple to a string.


Code:
Output:

7. Write a Python program to get the 4th element and 4th element from last
of a tuple
Code:

Output:

8. Write a Python program to create the colon of a tuple.


Code:
Output:

9. Write a Python program to find the repeated items of a tuple.


Code:
Output:

10. Write a Python program to check whether an element exists within a


tuple.
Code:
Output:

11. Write a Python program to convert a list to a tuple.


Code:
Output:

12. Write a Python program to remove an item from a tuple.


Code:
Output:

13. Write a Python program to slice a tuple


Code:
Output:

14. Write a Python program to find the index of an item of a tuple.


Code:
Output:

15. Write a Python program to find the length of a tuple.


Code:

Output:
List
1. Write a Python program to sum all the items in a list.
Code:

Output:

2. Write a Python program to multiplies all the items in a list.


Code:
Output:

3. Write a Python program to get the largest number from a list


Code:

Output:
4. Write a Python program to get the smallest number from a list.
Code:

Output:

5. Write a Python program to count the number of strings where the string
length is 2 or more and the first and last character are same from a given list
of strings
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result : 2
Code:
Output:

6. Write a Python program to get a list, sorted in increasing order by the last
element in each tuple from a given list of non-empty tuples.
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Code:

Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
Output:
7. Write a Python program to remove duplicates from a list.
Code:

Output:

8 Write a Python program to check a list is empty or not.


Code:
Output:

9. Write a Python program to clone or copy a list.


Code:

Output:
10. Write a Python program to find the list of words that are longer
than n from a given list of words.
Code:

Output:

11. Write a Python function that takes two lists and returns True if
they have at least one common member.
Code:
Output:

12. Write a Python program to print a specified list after removing


the 0th, 4th and 5th elements.
Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', 'Black']
Code:
Output:
Name : Mohd Adnan Zohaib
Roll no.: 2K22CSUN01149
Course: B.Tech(CSE)
Subject: Programming for Problem Solving using Python(CSW208B)

Lab-9: Object-Oriented Programming

Blooms Taxonomy Level: BT1, BT2, BT3,BT4


1. Write a Python program that create a class tringle and define
two methods, create_triangle() and print_sides().
Code:

Output:

2. Write a Python program to create a class with two methods


get_String() and print_String().
Code:

Output:

3. Write a Python program to create a class Rectangle that takes


the parameter length and width. The class should also contain a
method for computing its perimeter.
Code:
Output:

4. Write a Python program to create a class Circle that takes the


parameter radius. The class should also contain two methods for
computing its area & perimeter respectively. Use constructor to
implement initialization of parameters
Code:
Output:

5.Create a Cricle class and intialize it with radius. Make two


methods getArea and getCircumference inside this class.
Code:

Output:

6. Create a Temprature class. Make two methods :


1. convertFahrenheit - It will take celsius and will print it into
Fahrenheit.
2. convertCelsius - It will take Fahrenheit and will convert it into
Celsius.
Code:
Output:

7. Create a Student class and initialize it with name and roll


number. Make methods to :
1. Display - It should display all informations of the student.
2. setAge - It should assign age to student
3. setMarks - It should assign marks to the student.
Code:

Output:

8. Create a Time class and initialize it with hours and minutes.


1. Make a method addTime which should take two time object and
add them. E.g.- (2 hour and 50 min)+(1 hr and 20 min) is (4 hr and
10 min)
2. Make a method displayTime which should print the time.
3. Make a method DisplayMinute which should display the total
minutes in the Time. E.g.- (1 hr 2 min) should display 62 minute.
Code:

Output:
9. Write a Python class to reverse a string word by word.

Code:

Output:
Name: Mohd Adnan Zohaib

Roll no.: 2K22CSUN01149

Course: B.Tech(CSE)
Subject: Programming for Problem Solving using Python(CSW208B)

Lab-9.2: Object-Oriented Programming - Class Inheritance & Method


Overriding.
Blooms Taxonomy Level: BT1, BT2, BT3,BT4
1. Write a Python program that has a class Animal with a method
legs(). Create two subclasses Tiger and Dog, access the method leg
explicitly with class Dog and implicitly with the class Tiger.
Code:

Output:
2. Write a Python program to create a class Employee. Define two
subclasses: Engineer and Manager. Every class should have
method named printDesignation() that prints Engineer for Engineer
class and Manager for Manager Class.
Code:
Output:

3. Write a Python program to demonstrate classes and their attributes.


Code:
Output:

4. Write a Python program to demonstrate Inheritance and method


overriding.
Code:
Output:
5. Write a Python program to demonstrate multiple Inheritance.
Code:

Output:
Name : Mohd Adnan Zohaib

Roll no.: 2K22CSUN01149

Course: B.Tech(CSE)
Subject: Programming for Problem Solving using Python(CSW208B)

Lab-11: File handling: hands-on practice


Blooms Taxonomy Level: BT1, BT2, BT3, BT4

1. Write a Python program to read an entire text file.

Code:

:
Output:

2. Write a Python program to read a file line by line and store


it into a list.

Code:
Output:

3. Write a Python program to read a file line by line store it


into a variable.

Code:
Output:
4. Write a Python program to read a file line by line store it
into an array.

Code:

Output:

5. Write a python program to find the longest words.

Code:
Output:

6. Write a Python program to count the number of lines in a


text file.

Code:
Output:

7. Write a Python program to count the frequency of words in


a file.

Code:
Output:

8. Write a Python program to get the file size of a plain file.

Code:
Output:

9. Write a Python program to write a list to a file.

Code:
Output:

10. Write a Python program to copy the contents of a file to


another file.

Code:
Output:

11. Write a Python program to combine each line from


first file with the corresponding line in second file.

Code:
Output:

12. Write a Python program to read a random line from a


file.

Code:
Output:

13. Write a Python program to assess if a file is closed or


not.

Code:
Output:

14. Write a Python program to remove newline characters


from a file.

Code:
Output:

15. Write a Python program that takes a text file as input


and returns the number of words of a given text file.
Note: Some words can be separated by a comma
with no space.

Code:
Output:
Name : Mohd Adnan Zohaib

Roll no.: 2K22CSUN01149

Course: B.Tech(CSE)
Subject: Programming for Problem Solving using Python(CSW208B)

Lab-12: Series and DataFrames: hands-on practice

Blooms Taxonomy Level: BT1, BT2, BT3,BT4


6. Create the following:
a. empty series
b. series from an array
c. series from dictionary
d. scalar series
Code:
Output:
7. Create indexed series for above b-d points
Code:
Output:
8. Retrieve elements from the series using
a. Index names
b. Index numbers
c. Slicing
Code:

Output:
9. Create a DataFrame using the following:
a. Empty DataFrame
b. DataFrame using list
c. DataFrame using dictionary
d. DataFrame using series
Code:
Output:
10. Create 3-4 four colums in a DataFrame for Q4 b-d points
11. Add index to all the rows in Q5
12. Display all the values from the DataFrame mentioned in Q6
using index and index name for :
a. Second row values
b. Second column values
13. Perform slicing by displaying 3rd and 4th row on the
DataFrame
14. Delete a row
15. Delete a column

You might also like