Posts

Showing posts from March, 2022

pickle module

import pickle '''#pickling a python object friends=["vijay","shankar","raju"] file="friends.pkl" #define a pickle file fileobj=open(file,"wb") #pickle file object in write binary(wb) mode pickle.dump(friends,fileobj) #dump method is used to add content to the pickle file fileobj.close() #we cannot read simply pickle file in text format because it is of binary format''' #reading a pickle file fileobj= open ( "friends.pkl" , "rb" ) myfriends=pickle.load(fileobj) #load method is used to read a pickle file print (myfriends) fileobj.close()

json module

#json (java script object notation) is a standardized format commonly used to transfer data as text #from web application to server and vice versa. #json is a built in module in python import json #loads method used to convert json format to python jsonstr= '{"name":"harish","goal":"engineer","channel":"softwareharish"}' #json string parse=json.loads(jsonstr) #convert json string to python print (parse[ "goal" ]) print (parse) #dumps method used to convert python to json format dic1={ "name" : "harish" , "goal" : "engineer" , "channel" : "softwareharish" , "subscribers" : 1000 , "watchtime" : 2000 , "israj" : False } x=json.dumps(dic1) #convert python to json string print ( "json string \n " , x)

strings 2

//different methods to take a string from user #include <stdio.h> int main () {     char str1 [ 10 ];     printf ( "enter a string:" );     gets ( str1 ); //method 1: using gets function     printf ( " %s \n " , str1 );     char str2 [ 10 ];     printf ( "enter a string:" );     scanf ( " %s " ,& str2 ); //method 2:using scanf, but with this we cannot use white spaces in string     printf ( " %s \n " , str2 );     printf ( "using puts displaying string output \n " );     puts ( str2 ); //puts function is used to display string output     return 0 ; }

strings 1

//strings are not a data type in c language //string- array of characters terminated by null character('\0') //we can create a character array in the following ways: //1. char name[]="harish" here compiler will automaticaly take null character //2. char name[]={'h','a','r','i','s','h','/0'} here we have to put null character('\0') #include <stdio.h> void printstr ( char str [] ) //function to print string {     int i = 0 ;     while ( str [ i ]!= ' \0 ' )     {         printf ( " %c " , str [ i ]);         i ++;     }     printf ( " \n " ); } int main () {     char str1 [] ={ 'h' , 'a' , 'r' , 'i' , 's' , 'h' , ' \0 ' }; // 1st way to giving value a string     printstr ( str1 );     char str2 [] = "shiva" ; // 2nd way to giving value a string     printstr ( str2 );     return 0 ; }