Posts

Unit-5 programs

  Program-1: Aim: Python program to check whether a JSON string contains complex object or not. Program: import json def has_complex_object(json_str):     try:         data=json.loads(json_str)         for value in data.values():             if isinstance(value,(list,dict)):                 return True             return False     except json.JSONDecodeError:         return False json_str1='{"name":"ravi","age":30,"city":"vijayawada"}' json_str2='{"name":"kumar","age":25,"contact":{"email":"ravi@gmail.com","phone":"8273672682"}}' print(has_complex_object(json_str1)) print(has_complex_object(json_str2)) program-2: Aim: Python program ...

python unit-4

 Unit-4 programs Program-1: Aim: Write a python program to sort words in a file and put them in another file. The output file should have only lowercase words, so any uppercase words from source must be lowered. Program: def sort_words(input_file,output_file):     with open(input_file,'r') as file:         words=file.read().split()     words=sorted([word.lower() for word in words])     with open(output_file,'w') as file:         file.write("\n".join(words)) input_file='input.txt' output_file='output.txt' sort_words(input_file,output_file) print(f"words sorted and saved to'{output_file}'")   output: Program-2: Aim: Python program to print each line of a file in reverse order. Program: def reverse_lines(input_file):     with open(input_file,'r')as file:         lines=file.readlines() ...

python unit-3 programs

  Program-1: Aim: Write a program to create tuples (name, age, address, college) for at least two members and concatenate the tuples and print the concatenated tuples. Program: t1=('revanth',19,'vijayawada','LBRCE college') t2=('kumar',20,'mylavaram','CMR college') con_tuple=t1+t2 print(con_tuple) output:       program-2: Aim : Write a program to count the number of vowels in a string (No control flow allowed). Program: string="pythonprogramming" vowel_count=len([char for char in string if char in 'aeiouAEIOU']) print(vowel_count) output: program-3: Aim : Write a program to check if a given key exists in a dictionary or not. Program: dict={'a':1,'b':2,'c':3} check='b' exists=check in dict print(exists) output: program-4: Aim: Write a program to add a new key-value pair to an existing dictionary Program: dict={'a':1,'b...

pyhton unit-2 programs

  Program-1: Aim: Write a python program to define a function with multiple return values. Program: def operations(x,y):     sum=x+y     sub=x-y     mul=x*y     return sum,sub,mul sum,sub,mul=operations(25,15) print("sum:",sum) print("sub:",sub) print("mul:",mul) Output: (Or) def operations(x,y):     sum=x+y     sub=x-y     mul=x*y     return sum,sub,mul sum,sub,mul=operations(25,15) print("sum:",sum) print("sub:",sub) print("mul:",mul) program-2: Aim: Write a python program to define a function using default arguments. Program: def defaultArgs(a=10,b=15,c=30):     print(a,b,c)     return defaultArgs() Output: (or) def area_rect(height=1,length=1):     area=height*length     print(area) area_rect(10,10) output: Program-3: Aim:...

python unit-1 programs

Program 1: Aim: Write a python program to find the largest element among three Numbers Program: a=int(input("Enter a value :")) b=int(input("Enter b value :")) c=int(input("Enter c value :")) if((a>b)&(a>c)):     print("a is big",a) elif(b>c):     print("b is big",b) else:     print("c is big",c)   Output:                       Program 2: Aim: Write a python program for all prime numbers with in range. Program: min=int(input("Enter min number")) max=int(input("Enter max number")) for i in range(min,max):     count=0     for j in range(1,i+1):         if(i%j==0):             count+=1     if(count==2):         print(i) Output: ...