Posts

Operators in C

Image
  //operators in C programming language //Operators: a symbol used to perform operations in given programming language //Types of operators in C language     /*1. Arithmetic Operators       2. Relational Operators       3. Logical Operators       4. Bitwise Operators       5. Logical Operators     */ //--------------------------------------- //Arithmetic Operators     /*(+): addition       (-): subtraction       (*): multiplication       (/): division       (%): modulus - gives remainder       */ //--------------------------------------- //Relational Operators     /*(==): is equal to       (!=): is not equal to       (>): greater than       (<): less than       (>=): greater than or equal to       (<=): less  than or eq...

Variables and Data Types in C

//variables in C //variable: A name given to a memory location is called variable //declaring variable: type variable_name; //rules for defining a variable in C     /*1. can contain alphabets, digits and underscore(_)       2. a variable name can start with an alphabet and underscore only       3. can't start with a digit       4. whie space and reserved words are not allowed to give name of a variable       */ //Data types in C     /*1. basic data type: int, char, float, double       2. derived data type: this type of data types are created or derived by using basic data types                             array, pointers, structure, union       3. enumeration data type: emum       4. void data type: void     */ #include <stdio.h> int main () {     printf ( " %lu " , s...

Tokens in C

//basic syntax of a C program // a C program is made up of the collection of tokens // Token: smallest unit used in a C program is called token #include <stdio.h> int main () {     int     a // token (identifier)     ;     a     = // token (operator)     100 ;     printf     (           "softwareharaish" //token (string literal)     )     ; //token (symbol)     return //token (keyword)     0 //token (constant)     ; } // in the above c program I breaks the program into individual tokens // types of tokens i C program     /*  1. keywords: reserved words which has a special meaning in c program ()         2. identifiers: name of functions, variables ets to identifie                         (panctuators are not allowed to make id...

Steps taken by a compiler to execute a C program

//steps taken by a compiler to execute a C program /*  1. pre processing: i) comments removal                        ii) macros expansion                        iii) #files inclusion                        iv) conditional compilation     2. compilation: converts to assembly level instructions     3. assembling: converts to machine level instructions     4. linking: linker combine all machine level instructions                 to create an executable file that is .exe file*/ //to understand better how compiler works follow the below step /* A) execute "gcc -Wall -save-temps file_name.c" on vs code terminal     u will get 4 extra files in explorer this files will show u how compiler works         1. file_name...

Command line utility

#there are two types of command line arguments are there #1. positional arguments #- we can give value arguments directly in WPS (windows powershell) #- cant skip any value argument #- expected command in WPS for command line argument program execution ( 4 6 add [note:cant change position]) #2. optional arguments #- we have to give value arguments in WPS with (--argument_name argument_value) #- we can skip any value argument #- expected command in WPS for program execution (--number1 4 --number2 6 --operation add [note:can change position]) import argparse #python built in library import sys if __name__== '__main__' : parser=argparse.ArgumentParser() #parser object # 1. positional arguments # using add_argument function or attribute adding arguments '''parser.add_argument("num1", help="first number") parser.add_argument("num2", help="second number") parser.add_argument(...

'is' vs '==' in python

# '==' : value equality-two objects have the same value # is : reference equality- two references refer to the same object a=[ 1 , 2 , 3 ] b=a #two references refer to the same object(b is not the copy of a) print (b==a) print (b is a) c=a[:] #two references refer to the two different object(now c is the copy of a) print (c==a) print (c is a)

Raise keyword in python

#raise is a built-in keyword used to raise exception # visit all built-in Exceptions and understand name= input ( "enter u r name:" ) if name.isnumeric(): raise Exception ( "numbers r not allowed" ) #here we used raise keyword

Typedef keyword in c

// typedef is a keyword which is used to give name to the existing data type // syntax- typedef <previous_name> <alias_name>; #include <stdio.h> int main () {     typedef int nt ; // here i given a new name to the data type int to 'nt'     nt a ;           // now i can use 'nt' in place of int     // it is not neccesary to use alias name or new name i can use previous name also like u can see below     int b ;     //--------------------     // using typedef with structure     typedef struct library     {         char name [ 20 ];         int pages ;         char author [ 20 ];     } lib ;             // here i given a new name to the data type library(structure) to 'lib'     lib book1 , book2 ; // here now i can use 'lib' instead of 'struct libra...

structure in c

//structures are user defined data types in c //in structures we can store any type of data types like int floate char etc //declaring a structure #include <stdio.h> #include <string.h> struct library //here strut is a keyword for creating a structure, here library also called structure tag {     char name [ 20 ]; //members or elements of library tag or structure name     int pages ;     char author [ 20 ]; //character array or string of size 20, we have to use string for a char value //there are two methods to initialize a variable from structure }; //book1; //method 1 int main () {     struct library book1 , book2 ; //method 2     //there are two ways to assign values to the variables     //struct library book1={'h',2,'s'}; //way 1,but this is an error, like this we can only assign int values not char     strcpy ( book1 . name , "softwareharish" ); //way 2     book1 . pages = 100 ; ...

python speak

#from pywin32 module we can make read the sentences by the computer from win32com.client import Dispatch speak=Dispatch( "sapi.spvoice" ) speak.speak( "harish is a champion in coding " ) #in function def speak (str): from win32com.client import Dispatch speak = Dispatch( "sapi.spvoice" ) speak.speak(str) if __name__== '__main__' : speak( "jai ntr" )

string functions

#include <stdio.h> #include <string.h> //string functions library int main () {     char s1 [] = "harish" ; //string1     char s2 [] = "shiva" ; //string2     char s3 [ 10 ];     //puts(strcat(s1,s2)); //concatenation     printf ( "length of s1: %d \n " , strlen ( s2 )); //give length of string     printf ( "the reversed string of s1: " );     puts ( strrev ( s1 )); //reverse the string     //s3=strcat(s1,s2); //you canot do like this     strcpy ( s3 , strcat ( s1 , s2 )); //copy value in another variable     printf ( "s3 copied from s1 and s2:" );     puts ( s3 );     return 0 ; }

triangular star

#include <stdio.h> char printstar ( int rows ); int main () {     int n , row , j ;     char star = '*' ;     printf ( "enter 1 for triangle star \n 0 for reverse triangle star \n " );     printf ( "enter:" );     scanf ( " %d " ,& n );     if ( n == 0 )     {         printf ( "enter no of rows u want:" );         scanf ( " %d " ,& row );         for ( j = row ; j >= 1 ; j --)         {             printstar ( j );         }     }     else if ( n == 1 )     {         printf ( "enter no of rows u want:" );         scanf ( " %d " ,& row );         for ( j = 1 ; j <= row ; j ++)         {             printstar ( j ); ...

Fibbonachi series

#include <stdio.h> int recursive ( int n ); int iterative ( int n ); int main () {     int n , fib1 , fib2 ;     printf ( "enter no of which u want fibbonachi:" );     scanf ( " %d " ,& n );     fib1 = recursive ( n );     fib2 = iterative ( n );     printf ( "recursive fibonachi is %d \n " , fib1 );     printf ( "iterative fibonachi is %d " , fib2 );     return 0 ; } int recursive ( int n ) {     if ( n == 1 || n == 2 )     {         return n - 1 ;     }     else     {         return recursive ( n - 1 )+ recursive ( n - 2 );     } } int iterative ( int n ) {     int a = 0 , b = 1 ;     for ( int i = 0 ; i < n - 1 ; i ++)     {         b = a + b ;         a = b - a ;     }     return a ; }

some conversions

// 1kg = 2.205 pounds // 1km = o.621 mile // 1 foot = 12 inch // 1 meter = 3.280 foot // 1 inch = 2.54 cm #include <stdio.h> int main () {     char input ;     float quantity , result ;     float kgtopound = 2.205 ;     float kmtomile = 0.621 ;     float foottoinch = 12 ;     float mtofoot = 3.280 ;     float inchtocm = 2.54 ;     while ( 1 )         {             printf ( "enter q to quiet \n    1.kg to pound \n    2.km to mile \n    3.foot to inch \n    4.m to foot \n    5.inch to cm \n " );             scanf ( " %c " ,& input );             switch ( input )             {                 case 'q' :                 p...

array reversing

#include <stdio.h> void arr_rev ( int arr [] ) //reversing array user built function {     for ( int i = 0 ; i < 5 / 2 ; i ++)     {         //swaping method         int temp ;         temp = arr [ i ];         arr [ i ]= arr [ 4 - i ];         arr [ 4 - i ]= temp ;     } } int main () {     int arr [] ={ 1 , 2 , 3 , 4 , 5 };     for ( int j = 0 ; j < 5 ; j ++)     {         printf ( "element of %d is %d \n " , j , arr [ j ]);     }     printf ( "reversed array \n " );     arr_rev ( arr );     for ( int j = 0 ; j < 5 ; j ++)     {         printf ( "element of %d is %d \n " , j , arr [ j ]);     }     return 0 ; }

Dictionary

d1={ } #dictionary print ( type (d1)) d2={ "name" : "n harish" , "aim" : "softwareharish" , "gf" : "single king" , "salary" : 10000 } print (d2) print (d2[ "name" ]) d2[ "nick name" ]= "shiva" #adding another key value pair print (d2) del d2[ "gf" ] #delete key value pair print (d2) #some functions of dictionary d3=d2.copy() #copy the dictionary print (d3) print (d2.get( "aim" )) # access key value pair d2.update({ "propery" : "laptop" }) #update dictionary print (d2) print (d2.keys()) #give all keys of dictionary print (d2.items()) #give all items of dictionary

RE (regular expression) module

Image
  Above everything will be used in re module that is  1. metacharacters 2. Special sequences 3. Sets

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