Characters and Booleans¶

you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=QtbosKrL2Mk&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=9

In [1]:
import ROOT
In [2]:
import fortranmagic
%load_ext fortranmagic
import os
import sys

import numpy as np

if sys.platform.startswith("win"):
        # Depends of system, python builds, and compilers compatibility.
        # See below.
    f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
        # For Unix, compilers are usually more compatible.
    f_config = ""

    # Disable only deprecated NumPy API warning without disable any APIs.
f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"

%fortran_config {f_config}
%load_ext rpy2.ipython
New default arguments for %fortran:
	 --extra '-DNPY_NO_DEPRECATED_API=0'
Error importing in API mode: ImportError('/home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages/_rinterface_cffi_api.abi3.so: undefined symbol: R_ClosureEnv')
Trying to import in ABI mode.

Characters¶

C++¶

In [3]:
%%cpp
char c='x'; //attention ' here is important otherwise it is a string in C++ and C
cout << "Ich bin "<<c<<"."<<endl;
Ich bin x.

C¶

In [4]:
%%cpp
char c='x'; //attention ' here is important otherwise it is a string in C++ and C
printf("Ich bin %c.\n",c);
Ich bin x.
In [5]:
%%cpp
printf("Ich bin %c.\n"); //c is dangerous if you forget the paramter you get a stack underflow crash
Ich bin p.
input_line_60:2:19: warning: more '%' conversions than data arguments [-Wformat-insufficient-args]
 printf("Ich bin %c.\n"); //c is dangerous if you forget the paramter you get a stack underflow crash
                 ~^

Javascript¶

No character type exist, they are considered strings with length 1.

In [6]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}
let c="x"
console.log("Ich bin "+c+".\n");

Python¶

No character type exist, they are considered strings with length 1.

In [7]:
c="x"
print("Ich bin "+c+".\n")
Ich bin x.

R¶

No character type exists, they are considered strings with length 1.

In [8]:
%%R
c<-"x"
print(paste("Ich bin",c))
[1] "Ich bin x"

Fortran¶

In [9]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none
    character :: c="X" ! this is a character
    print *,"Hello Mr. ",c
! end program
end subroutine main
In [10]:
main()
 Hello Mr. X

Boolean¶

C++¶

In [11]:
%%cpp
bool old=true,young=false;
cout <<"I am "<<old<<" and "<<young<<"."<<endl;
I am 1 and 0.

C¶

C does not have boolean type. We just use int.

In [12]:
%%cpp
int old=1,young=0;
cout <<"I am "<<old<<" and "<<young<<"."<<endl;
I am 1 and 0.

Javascript¶

In [13]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let old=true,young=false;
console.log("I am "+old+" and "+young+".\n")

Python¶

In [14]:
old=True;young=False;
print("I am",old,"and",young,".\n")
I am True and False .

R¶

In [15]:
%%R
old<-TRUE;young<-FALSE
print(paste("I am",old,"and",young))
[1] "I am TRUE and FALSE"

Fortran¶

In [16]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none
    logical :: old=.true. 
    logical :: young=.false.
    print *,"I am",old,"and",young
! end program
end subroutine main
In [17]:
main()
 I am T and F