Write/Read a text file

# open the file hello.dat in write modewithopen("hello.dat","w")asfile:file.write("Hello!!")# write

In [ ]:

# list content of the local directory !ls
# give a quick look at the file !cat hello.dat

In [ ]:

# open the file hello.dat in read mode withopen("hello.dat","r")asfile:data=file.read()# read # data holds the content read from the fileprint(data)# show

In [ ]:

# remove the file !rm hello.dat

Convert Python data structures to/from a JSON file

We show how Python data structures can be easily dumped in/loaded from a JSON file.

Let's create a Python data structure.

In [ ]:

# a dictionary contains keys and valuesdata={}data["project"]="liquid_project"# it can be nested data["material"]={}data["material"]["name"]="water"data["material"]["formula"]="H2O"data["material"]["density"]=1# lists contain information which can be iterateddata["material"]["atomic_species"]=["O","H"]# show the data print(data)

importmatplotlib.pyplotasplt# plot fig,ax=plt.subplots(1,1)ax.plot(x,y1,'bo-',label="$y=x$")ax.plot(x,y2,'ro-',label="$y=x^2$")# set labels plt.xlabel('x')plt.ylabel('y')# set legendplt.legend()# set title plt.title("Functions")# show the plotplt.show()

Hi 5! You are no longer a toddler!

This website does not host notebooks, it only renders notebooks
available on other websites.