this was a markdown cell and how a heading

  • sage is using python 3
  • good thing is that you can check line by line
In [1]:
2 + 2
Out[1]:
4
In [3]:
type(234)
Out[3]:
float
In [4]:
type(234.)
Out[4]:
float
In [5]:
type(234.0)
Out[5]:
float
In [6]:
type("hello world")
Out[6]:
str
In [7]:
type('hello world')
Out[7]:
str
In [8]:
101 - 2
Out[8]:
99
In [10]:
9 / 3
  File "<ipython-input-10-00b7104e9bf2>", line 1
    9 / 3 // double slash for python 3
                        ^
SyntaxError: invalid syntax
In [12]:
51 //3
Out[12]:
17
In [13]:
11*11
Out[13]:
121
In [14]:
11**2
Out[14]:
121
In [15]:
3^8
Out[15]:
11
In [16]:
2^7
Out[16]:
5
In [17]:
5%2
Out[17]:
1

↑modulus

In [18]:
2. + 2.
Out[18]:
4.0
In [19]:
2. + 2
Out[19]:
4.0

float (any math) int = float

In [20]:
52. / 3
Out[20]:
17.333333333333332
In [21]:
52. //3 
Out[21]:
17.0

"//" for integer, "/" for float

In [22]:
"hello" + "world"
Out[22]:
'helloworld'

add string, just join them

In [23]:
"hello"*"world"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-1fea36b8cd73> in <module>()
----> 1 "hello"*"world"

TypeError: can't multiply sequence by non-int of type 'str'
In [24]:
"hello"/"world"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-fc98848b3340> in <module>()
----> 1 "hello"/"world"

TypeError: unsupported operand type(s) for /: 'str' and 'str'
In [25]:
"hello"**"world"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-df869e9c8fb4> in <module>()
----> 1 "hello"**"world"

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'str'
In [26]:
"hello"%"world"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-5c5c2000917c> in <module>()
----> 1 "hello"%"world"

TypeError: not all arguments converted during string formatting
In [27]:
2 + "hello"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-282a54e01199> in <module>()
----> 1 2 + "hello"

TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [31]:
2*"hello"
Out[31]:
'hellohello'

because it's the same as "hello" + "hello"

In [29]:
2.1*"hello"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-559775491960> in <module>()
----> 1 2.1*"hello"

TypeError: can't multiply sequence by non-int of type 'float'

SO FAR JUST ALL PYTHON, NOT ANY SAGE(mathematically stronger) YET

In [32]:
3*"hello"+"world"
Out[32]:
'hellohellohelloworld'
In [33]:
"hello" + "" + "world"
Out[33]:
'hellospace'
In [34]:
"hello" + " " + "world"
Out[34]:
'hello world'
In [36]:
a = 3
In [38]:
b = 4
In [39]:
a = b
In [40]:
a+b
Out[40]:
8
In [41]:
2 = 2
  File "<ipython-input-41-112056e27a6a>", line 1
    2 = 2
SyntaxError: can't assign to literal
In [42]:
2 == 2
Out[42]:
True
In [43]:
type(a)
Out[43]:
int
In [44]:
a = 0.2
In [45]:
type(a)
Out[45]:
float

dynamically changing

In [48]:
a = "hello"
In [49]:
b = "world"
In [50]:
a+b
Out[50]:
'helloworld'
In [51]:
type(2==3)
Out[51]:
bool
In [52]:
2==3
Out[52]:
False
In [53]:
2< 3
Out[53]:
True
In [54]:
3 >= 2
Out[54]:
True
In [55]:
"hello" != "hello "
Out[55]:
True
In [56]:
4 != 5
Out[56]:
True
In []:
 
In [57]:
for n in range(5):
    print(n)
  File "<ipython-input-57-a436cd77f76e>", line 1
    for n in range(5)
                     ^
SyntaxError: invalid syntax
In []: