PythonLogo
from IPython.display import HTML
HTML('<iframe src=http://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/SymPy.ipynb width=700 height=350></iframe>')
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/display.py:689: UserWarning: Consider using IPython.display.IFrame instead warnings.warn("Consider using IPython.display.IFrame instead")
1 + 3
4
5 * 7
35
5 / 2
2.5
5 // 2
2
0.1 + 0.1 + 0.1 - 0.3
# What's going on ?
5.551115123125783e-17
True and True
True
True and False
False
True or False
True
( 2 > 3 ) and ( 1 < 2)
False
( 1 < 2 < 5 )
True
mystring = 'qwerty'
print(mystring)
qwerty
otherstring = "a nice string"
print(otherstring)
a nice string
empty = ''
len(empty)
0
len(otherstring)
13
len(None)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-18-96b07938784c> in <module> ----> 1 len(None) TypeError: object of type 'NoneType' has no len()
S1 = "Γειάσου "
S2 = "Κόσμε !"
print(S1+S2)
Γειάσου Κόσμε !
We can't do anything:
S1*S2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-20-e4eb3fd43725> in <module> ----> 1 S1*S2 TypeError: can't multiply sequence by non-int of type 'str'
But still
S1 * 3
'Γειάσου Γειάσου Γειάσου '
# or
print('*'*60)
************************************************************
S = 'azertyuiop'
print(S[1])
print(S[1:5])
z zert
and what about this ?
print(S[-1])
print(S[-2])
S[::-1]
p o
'poiuytreza'
S = "Hello, the weather is rainy today, isn't it ? "
S.split(',')
['Hello', ' the weather is rainy today', " isn't it ? "]
S.find('rainy')
# Returns the index of 'rainy' in the string S
22
S.replace('rainy','nice')
"Hello, the weather is nice today, isn't it ? "
S = "ahoouutch "
S.rstrip()
# cleaning spaces
'ahoouutch'
# Let's import networkx to deal with graphs
import networkx as nx
# Let's create a digraph G
G = nx.DiGraph()
# and add 3 nodes to G
G.add_nodes_from('ABC')
# add an edge from A to B
G.add_edge('A','B')
# and an other from A to C
G.add_edge('A','C')
print(G)
# We can give some pos
pos = { 'A' : (0,0), 'B' : (1,1), 'C' : (-1,1) }
# and draw the graph
nx.draw(G, pos, edge_color='b',node_color='y')
import pandas
%%bash
mkdir data
%%file data/data.csv
Langage,Strengths, Weaknesses
Java, Structured, Verbose
JavaScript,'Ubiquitous','Weard'
PHP, Simple,'Not very concise'
Python,Powerful,'2to3'
Overwriting data/data.csv
# Read again file
df = pandas.read_csv('data/data.csv')
df
Langage | Strengths | Weaknesses | |
---|---|---|---|
0 | Java | Structured | Verbose |
1 | JavaScript | 'Ubiquitous' | 'Weard' |
2 | PHP | Simple | 'Not very concise' |
3 | Python | Powerful | '2to3' |
import nltk
phrase="today, the weather is mild and I would like to go around the lake"
tokens=nltk.word_tokenize(phrase)
tokens
# Try completions nltk. ...
['today', ',', 'the', 'weather', 'is', 'mild', 'and', 'I', 'would', 'like', 'to', 'go', 'around', 'the', 'lake']
We can use bigger texts
from urllib import request
url='http://www.gutenberg.org/cache/epub/1497/pg1497.txt'
response = request.urlopen(url)
republic = response.read().decode('utf8')
type(republic)
# to go through a proxy:
# proxies = {'http': 'http://www.myproxy.com:3128'}
# request.ProxyHandler(proxies)
str
len(republic)
1239079
republic[231900:232071]
'And the steps of the ladder leading up to this highest or\r\nuniversal existence are the mathematical sciences, which also contain\r\nin themselves an element of the universal'