Learning Python Basics


In [26]:
# This is a comment

Types


In [34]:
type(2)


Out[34]:
int

In [35]:
type(2.0)


Out[35]:
float

In [39]:
type("Double quoted also string")


Out[39]:
str

In [36]:
type('string')


Out[36]:
str

In [40]:
type(False)


Out[40]:
bool

In [41]:
a = 'sample'
print(a)
type(a)


sample
Out[41]:
str

In [42]:
a = ['1','2','3']
type(a)


Out[42]:
list

In [44]:
b = ('a','2','c')
type(b)


Out[44]:
tuple

In [45]:
c = {'a':1,'b':2,'c':3}
type(c)


Out[45]:
dict

Numbers

Addition


In [2]:
2 + 2


Out[2]:
4

In [4]:
2.0 + 2


Out[4]:
4.0

In [5]:
3 + 2.0


Out[5]:
5.0

Subtraction


In [6]:
4 - 2


Out[6]:
2

In [7]:
5 - 3.0


Out[7]:
2.0

Multiplication


In [9]:
2 * 3


Out[9]:
6

In [10]:
2 * 3.0


Out[10]:
6.0

In [11]:
3.0 * 6.0


Out[11]:
18.0

In [12]:
5 * 0.0


Out[12]:
0.0

In [13]:
2 + 3 * 5 + 3


Out[13]:
20

In [14]:
(2 + 3) * (5 + 3)


Out[14]:
40

Division


In [15]:
5 / 2


Out[15]:
2.5

In [16]:
5 // 2


Out[16]:
2

In [17]:
5 // 2.0


Out[17]:
2.0

In [18]:
5 / 2.0


Out[18]:
2.5

Exponentiation


In [20]:
5 ** 2


Out[20]:
25

In [21]:
452 ** 0.5


Out[21]:
21.2602916254693

In [22]:
4 ** 0.5


Out[22]:
2.0

In [23]:
164 ** 0.5


Out[23]:
12.806248474865697

In [24]:
64 ** 0.5


Out[24]:
8.0

In [25]:
10 + 4e2


Out[25]:
410.0

Strings


In [28]:
print('Hello World')


Hello World

In [29]:
# You can use single quote or double quotes
print("Hello World")


Hello World

In [31]:
print("Hello " + "World")


Hello World

In [32]:
print("Hello"[:3])


Hel

In [33]:
print("Hello"[3:])


lo

Variables


In [46]:
a = 10
print(a)


10

In [47]:
a = 10 + 2
print(a)


12

In [49]:
a = a + 2
print(a)


16

In [50]:
type(a)


Out[50]:
int

In [51]:
a = b = 10
print(a)
print(a+b)


10
20

In [52]:
a = b = c = 10.5
print(a,b,c)


10.5 10.5 10.5

In [53]:
one, two, three = 1,"two",30.00
print(one)
print(two)
print(three)


1
two
30.0

In [56]:
number = 1
print(type(number))
str = 'string'
print(type(str))
number = str
print(number)
print(type(number))


<class 'int'>
<class 'str'>
string
<class 'str'>

In [ ]: