In [1]:
print("Hello")
Hello

heading

heading 2

heading 3

  • Esc+M
  • Shift + Enter

History of Python

  • Python was invented by Guido Van Rossum
  • 1st version 0.9.0 in Feb 1991
  • PSF
  • Monte Python Circus
  • 2008 --> Python 3
  • 2018 --> 3.7

Features of Python

    1. Simple, readable and powerful
    1. Open source
    1. Interpreted
    1. Object-oriented
    1. Interactive
    1. Dynamic typing
    1. Implicitly defined
In [4]:
def add(a, b):
    print(a+b)
In [5]:
add(1,2)
3
In [6]:
add(2345234543.5, 3453453456534)
3455798691077.5
In [7]:
add([23,5,2,345], [34534,534,5,34])
[23, 5, 2, 345, 34534, 534, 5, 34]
In [8]:
add("Ram ", "Kumar")
Ram Kumar
In [9]:
class A:
    print("Hello")
x = A()
Hello
In [11]:
def greeting():
    print("Hello")
x = greeting()
Hello
In [12]:
print("Hello")
Hello
C                       Java                               Python
int i;                  int i = 0;                         i = 0
i = 0;
In [19]:
print("hello world")
hello world

Python Syntax

  • Python is case-sensitive
  • Single are double quotes in python can be used interchangeablity.
  • # is used for comment; ctrl + /
  • ; exits in python with the same meaning of line termination or seperatation.
  • Mutiple assignment
  • type(obj) : tells the type of object
  • dir(obj) : lists the attributes of a given object
  • help(obj) : explains the given object
  • Nomenclature
    i) Contains only [A-Za-z0-9_]
    ii) Start with only [A-Za-z_]
    iii) Not be a keyword
  • Indentation
In [23]:
'a' == 'A'
Out[23]:
False
In [24]:
'hello' == 'Hello'
Out[24]:
False
In [25]:
'a' == "a"
Out[25]:
True
In [26]:
'hello' == "hello"
Out[26]:
True
In [27]:
x = "I don't like red ink"
In [29]:
x = 'I don\'t like red ink'
In [30]:
# comment
In [31]:
x = 1
y = 2
In [34]:
x = 1; y = 2
In [36]:
a, b = 90, "hello"
In [37]:
a
Out[37]:
90
In [38]:
b
Out[38]:
'hello'
In [39]:
a, b = b, a
In [40]:
a
Out[40]:
'hello'
In [41]:
b
Out[41]:
90
In [42]:
a = b = c = 1
In [43]:
a
Out[43]:
1
In [44]:
b
Out[44]:
1
In [45]:
c
Out[45]:
1
In [46]:
a = 1
In [47]:
type(a)
Out[47]:
int
In [48]:
a = [231,54,235,2345,23,45234]
In [49]:
type(a)
Out[49]:
list
In [50]:
dir([231,54,235,2345,23,45234])
Out[50]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']
In [51]:
a
Out[51]:
[231, 54, 235, 2345, 23, 45234]
In [52]:
a.sort()
In [53]:
print(a)
[23, 54, 231, 235, 2345, 45234]
In [54]:
a.reverse()
In [55]:
print(a)
[45234, 2345, 235, 231, 54, 23]
In [56]:
help(list.sort)
Help on method_descriptor:

sort(...)
    L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*

In [57]:
l = [3, 1, 2]
In [58]:
l.sort()
In [59]:
print(l)
[1, 2, 3]
In [61]:
"".join(sorted("apple"))
Out[61]:
'aelpp'
In [62]:
"apple" == "apple"[::-1]
Out[62]:
False
In [63]:
"level" == "level"[::-1]
Out[63]:
True
In [64]:
#    0    1    2
l = ['a', 'b', 'c']
In [65]:
l[0]
Out[65]:
'a'
In [66]:
l[1:]
Out[66]:
['b', 'c']
In [67]:
l[-1]
Out[67]:
'c'
In [71]:
namefdhety_346534653456_23456346 = "Ram"
In [72]:
3M = 100
  File "<ipython-input-72-29cf997581f6>", line 1
    3M = 100
     ^
SyntaxError: invalid syntax
In [73]:
M3 = 100
In [74]:
if = 900
  File "<ipython-input-74-1919a25df829>", line 1
    if = 900
       ^
SyntaxError: invalid syntax
In [75]:
256**(26+26+10+1)
Out[75]:
52374249726338269920211035149241586435466272736689036631732661889538140742474792878132321477214466514414186946040961136147476104734166288853256441430016
In [76]:
import keyword
keyword.kwlist
Out[76]:
['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'break',
 'class',
 'continue',
 'def',
 'del',
 'elif',
 'else',
 'except',
 'finally',
 'for',
 'from',
 'global',
 'if',
 'import',
 'in',
 'is',
 'lambda',
 'nonlocal',
 'not',
 'or',
 'pass',
 'raise',
 'return',
 'try',
 'while',
 'with',
 'yield']
In [77]:
len(keyword.kwlist)
Out[77]:
33
In [78]:
print "hello"
  File "<ipython-input-78-2a0eaa89f43f>", line 1
    print "hello"
                ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?
In [81]:
def greeting():
    print("Hello")
In [82]:
greeting()
Hello
In [83]:
def greeting():
print("Hello")
  File "<ipython-input-83-e32ca84033dd>", line 2
    print("Hello")
        ^
IndentationError: expected an indented block
In [84]:
a = 1
    b = 2
c = 3
  File "<ipython-input-84-a751119bc620>", line 2
    b = 2
    ^
IndentationError: unexpected indent
class A
def function()
for i in "hello"
if i in "aeiou"
print(i)
A.function()
In [96]:
class A:
    def function():
        for i in "hello":
            if i in "aeiou":
                print(i)
A.function()
e
o
In [ ]:
class A:
    def function():
        for i in "hello":
            if i in "aeiou":
                print(i)
                # line 1
            else:
                # line 2
            # line 3
        # line 4
    # line 5
    # line 6
# line 7
A.function()
In [100]:
sum = 0
for i in [1, 2, 3, 4]:
    sum += i
print(sum)
10

Scope of variable

  • extent of variable defintion marked by functions as local and global
In [101]:
# https://goo.gl/Mv9zo7
x = 1 # gloabl 
X = 2
print(x)
def fun():
    a = 190 # local
    print(a)
    print(x)
fun()
print(x)
# print(a) --> NameError
1
190
1
1
In [103]:
a = [3, 1, 2, 5, 4]
b = a.sort()
print(b)
None
In [104]:
a
Out[104]:
[1, 2, 3, 4, 5]
In [105]:
a = [3, 1, 2, 5, 4]
b = sorted(a)
print(b)
[1, 2, 3, 4, 5]
In [106]:
a
Out[106]:
[3, 1, 2, 5, 4]
In [108]:
import keyword
keyword.kwlist
Out[108]:
['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'break',
 'class',
 'continue',
 'def',
 'del',
 'elif',
 'else',
 'except',
 'finally',
 'for',
 'from',
 'global',
 'if',
 'import',
 'in',
 'is',
 'lambda',
 'nonlocal',
 'not',
 'or',
 'pass',
 'raise',
 'return',
 'try',
 'while',
 'with',
 'yield']
In [109]:
dir(tuple)
Out[109]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'count',
 'index']
In [110]:
help(tuple.count)
Help on method_descriptor:

count(...)
    T.count(value) -> integer -- return number of occurrences of value

In [ ]: