Assignment

 

Programming in Python CST 362 

Assignment -1

Date of submission:3-Feb-2025  before 1:30pm

Note: Create a pdf file with program and output. Submit the printout

control statements (if , for , while )

1. Print the sin series x-x^3/3!+x^5/5!....x^n/n! ( read n.)

2. In the above program read the value x and find the sum of the series.(Use factorial function from math)

3.Print the following patterns

  *

  *  *

  *   *    *

  *   *    *   *

   1

   1     2


 1        2  3

   1    2  3  4

The pyramid is given for n=4 do this for any n

4. Find the position of the largest digit in a number.( consider unique digits)

Eg: i/p : 546  largest digit=6 position =3

5. Positive integer is called an Armstrong number of order n if 

abcd….= a^n + b^n + c^n + d^n +  where n is the length of the number

Eg: 153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong Number.

Eg:1634= 1**4+6**4+3**4+4**4=1634 //1634 is an Armstrong Number.

6. Find the square root of a number using Newton’s method ( refer the text book/blog for reference)

7. Write a program that computes an investment report.

Analysis

The inputs to this program are the following:

An initial amount to be invested (a floating-point number)

A period of years (an integer)

An interest rate (a percentage expressed as an integer)


8. Check whether the given number is a Krishnamurti number (.Use factorial () function from math)

For example: 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145 is a Krishnamurthy Number

9. Print all prime numbers less than 1000.

10. Input a number and print it in words ( i/p:345 o/p: Three Four Five)

 

Functions

 1.What are lambda functions

Let

f(x)=x^2+3x+5

g(x,y)=x^2y+5xy+6

Find f(2)+g(3,2) using lambda functions

2.Create a List L consist of (rno,name) tuples.( include your name and rollnumber)

L=[(1,’aswith’),(3,’charles’),(2,’ ’bibit’)]

Sort and print the list in the order of rno

Sort and print the list in the order of name

Use lambda functions

3. Use list comprehension to create a list of odd numbers less than 50.

Use lambda function and map function to find the cube of numbers stored in the list

Use lambda function and filter function to find the numbers divisible by 3

4. Write a function prime (n) which will return True if the number is prime or else False

Use the above function to print numbers less than 100.

5. Write a recursive function to find the factorial of a given number

Use the above function to compute nCr

6. Write a recursive function to find the n’th Fibonacci number

Use the above function to print the Fibonacci series.

7. Use a recursive function to find the sum of the digits of a number.

8.Write a function which will take n numbers as argument and return the largest element.

9. Find the binary equivalent of each digit of a number(ie;BCD each digit is represented as 4 bit binary). Use a separate function to find binary.

10. Find the sum of the cosine series 1-x^2/2!+x^4/4!-x^6/6!......n terms.Use a function fact(n) to find the factorial of the number and also pow(x,n) to find x**n.

 

Strings

Outcome:Learn String  Indexing and slicing, programming with strings

Consider the string str="Python Programming by Yourname" ( Replace Yourname with your first name)
Write statements in python to implement the following and also print the output of each statement.
a) To display the last four characters.
b) To display the substring starting from index 4 and ending at index 8.
c) Find the length of the string,min and max(characters)
d) To trim the last four characters from the string.
e) To trim the first four characters from the string.
f) To display the starting index of the substring 'gr'.
g) To change the case of the given string.( small letter to capital and capital to small)
h) To check if the string is in title case.
i) To replace all the occurrences of letter 'm' in the string with '*'

j)reverse the string

k)count the occurrence of the character ‘m’

l)characters in even positions 0,2,4,…..

m) characters in even positions 0,2,4,…..in reverse order

n)check whether the substring ‘on’ is present in the string or not

o)Find the first occurrence of character ‘t’

p)convert the string into upper case

 

2. Write a program to check whether the given string is palindrome or not.(modify the program to print all palindrome words in a sentence Hint: use split function to separate words)

3. Count the vowels, digits, consonents, spaces in a string.

4. Read a binary number as a string and find its corresponding decimal ( multiply with powers of 2)

5. Read a decimal number and find its binary.( Hint: divide by 2 and append the reminder to a string)

6. Read a string and print all 4 letter words.

7. Find the average word length.( find word length of each word and then find the average)

8. Read a string and swap the case ( small letters to capital letters and capital letters to small letters)

9. Encrypt a string using the shift cipher( key=3 Ceaser cipher).Encrypt your name.

 

10. Write a Python program to check the validity of a password given by the user.

The Password should satisfy the following criteria:

1. Contains at least one letter between a and z

2. Contains at least one number between 0 and 9

3. Contains at least one letter between A and Z

4. Contains at least one special character from $, #, @

5.Minimum length of password: 8

 

Lists and Tuples

1. Write commands for the following ( use your personal details)

a. Create an empty list (stud)

b. Add your roll number to the list (use + operator)

c. Append your name (use append)

d. Extend the list with your place and pin and mobile number (extend)

e. Insert your KTU-ID at stud [0]

f. Print your KTU-ID and name

g. Print the number of characters in your name.

h. Print last 5 digit of the phone number

I.Reverse the stud list

j.Find the index of your name.

 

2. Read list of numbers and store it in a list. Create two new lists from the list created which contains prime and composite numbers. Print all three lists.

 

3. Read list of students names and do the following

a) Sort the name in alphabetical order

b) Find the name with largest length

c) print the names starting with letter ‘A’ ( assume first letter capital)

d) Print the names in reverse alphabetical order with all names converted to capital letters.

e) Print the names in the order of length

4. Find the statistical measures of the marks of 25 students stored in a list

a) mean b)median c)mode d) standard deviation e)range f)inter quartile range(Q1-Q3)

5. Use list comprehension to create lists

a)powers of 3 upto 20

b) Numbers less than 100 which are divisible by 3

6. Read 10 numbers and stores it in a tuple. Find the sum, average of these elements also find the largest and smallest.

Consider a list consisting of integers, floating point numbers and strings. Separate them into different lists depending on the data types.

7. Check if the items in the list are sorted in ascending or descending order and print suitable messages accordingly. Otherwise, print “Items in list are not sorted”

8. Remove all duplicate elements from a list i/p:10 20 20 30 40  o/p:10 30 40

 

 

Sets

Write examples for the following set operations

union(), intersection(), difference(), issubset(),discard(), update()

Create two sets A and B and do the operations and print the results.

 


 

Files

1.Copy one file to another

2.Copyone file to another after removing blank lines

3.Print the words and length of words in sorted order of words from a file.

4. Read numbers stored in one file and store the sorted numbers in another file after deleting duplicates.

5.List of numbers are stored in a file(num.dat). Create two file Prime ( containing prime numbers) and composite( containing composite numbers) from the numbers stored in num.dat

6.Find the words having largest frequency from the given file.

7.Create a data file in the following format(rno,name.m1,m2,m3 marks out of 50 )

1|aswith|30|40|50

Write scripts for the following

a)Print the rno,name and Tot marks in the descending order of  total marks ( Rank list)

b)Print the list of passed students ( marks >=25 for all three subjects)

c)Print the pass percentage in each subject

d)List of students having 80% or more in all the three subjects

 

8. Write a Python program to create a text file. Read the contents of the file, encrypt every character in the file with a distance of 3 and write it to a new file.#Eg:yak is encrypted as bdn

9.A text file contains the keyword mec replace all occurrence of the word mec with MEC

10.Copy an image file ( jpeg) into another file.

11.Read last and print last 5 characters and first 10 characters from a file ( use fseek and read)

 

Dictionary

1. Write commands for the following

a. Create an empty dictionary stud

b. Add details of 5 students ( rno,namewith rno as key) from your class students list starting from your roll number.( consider circular list)

c. Sort and Print the list in the order of rno (Convert dictionary stud into a list)

d.Print the student list in the order of name( use lambda function)

e. Create another dictionary stud1 with rno,name,mark(rno as key)

f. Print the details of the students(rno,name,mark) having highest mark.

g. Read a rno and delete that students details from the dictionary.

h. Print the list of passed students ( mark >=50 assume marks out of 100)

2. Use dictionary comprehension to create a dictionary with numbers (key) and its squares(values).

3. Read a string and find the frequency of occurrence of each character in it. Use dictionary.

4. Create a dictionary with binary equivalent of all hexa decimal digits. Use this dictionary to convert the given hexadecimal number into binary.

5.Find the frequency of occurrence of words in a sentence. Also print the words with highest freq

6.Find the mod of list of numbers stored in a List ( use dictionary)

 

 

 

 

Comments

Popular posts from this blog

Programming in Python CST 362 KTU CS Sixth Semester Elective Notes

Image Processing

Turtle Graphics