Posts

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 ; }

requests module

#requests module allows user to send http requests using python import requests #this method is used to get data from the url #re=requests.get("https://nharishcoding.blogspot.com/2021/09/os-module") re1=requests.get( "https://financialmodelingprep.com/api/company/price/AAPL" ) print (re1.text) print (re1.status_code) #this function is used to post data to the server to the given url dic1={ "key1" : "value1" , "key2" : "value2" } url= "https://nharishcoding.blogspot.com/" re2=requests.post( url =url , data =dic1) print (re2.status_code)

different methods of calling a function

//different methods of calling a function //1. call by value //2. call by reference /*#include <stdio.h> int add(int x,int y)// function {     return x+y; } int main() {     int r, a=2,b=3;     r=add(a,b); //function called by value     printf("%d",r);     return 0; }*/ //called by reference-------------------------- #include <stdio.h> int func1 ( int  * address ) // function {     * address = 200 ;     } int main () {     int a = 2 ;     printf ( " %d \n " , a );     func1 (& a ); //function called by reference..in this address of value will be given as the argument     printf ( " %d " , a );     return 0 ; }

actual and formal parameters

#include <stdio.h> int add ( int x , int y ) // function // in this x & y are formal parameters {     return x + y ; } int main () {     int r , a = 2 , b = 3 ;     r = add ( a , b ); // calling the function //in this a & b are actual parameters     printf ( " %d " , r );     return 0 ; }