In [6]:
print('Hello World!')
In [7]:
_ = """
language classification
- types
- interpreted
- python used?
- youtube, dropbox, linux os utilities
- https://www.paypal-engineering.com/2014/12/10/10-myths-of-enterprise-python/
- largest library
- repl
- python
- ipython
"""
In [8]:
# this is a comment
In [9]:
# variables
x = 7
print(x)
x = 'apple'
print(x)
del x
print(x)
In [ ]:
# multiple assignment
x, y = 3,4
print(x,y)
In [ ]:
# swapping
y, x = x, y
print(x,y)
In [ ]:
x, y = None, None
print(x, y)
In [ ]:
# equality - 'is' is not ==
# is compares references; checking for same object
# == calls the __eq__ function, checks for same value
# != calls the __ne__ function, checks for same value
In [ ]:
# Is it true?
t, f = True, False
print(2 == 2)
print(2 is 2)
In [ ]:
x = 100000
y = 100000
i, j = 1, 1
print(i == j)
print(i is j)
print(x == y)
print(x is y)
In [ ]:
# conditions
if 2 is 2:
print('but obviously')
if 'abc' is 'abc':
print('abc')
if 'abc' is not 2:
print("this will print")
elif 'abc' is 'abc':
print('elif prints')
else:
print('not this though')
if 2:
print('truthy baby')
if 0:
print("won't print")
else:
print("falsy baby")
In [ ]:
# lists
empty_list = []
x = [1,2,3,4,5]
x.append(6)
y = list()
y.append(1)
print(x,y)
if x:
print('truthy baby')
else:
print ("won't print")
if not empty_list:
print('finally found an empty list, I can retire now')
In [ ]:
# index access for lists
print(x[0])
print(x[1])
print(x[-1]) #negative indices
print(x[2:4])
print(x[1:])
print(x[-1:])
print(x[-3:-1]) #splicing
In [ ]:
# hetrogenous lists
hlist = ['apple', True, 42]
print(hlist)
In [ ]:
# list comprehension
numbers = range(0,100)
even = [i for i in numbers if i % 2 is 0]
odd = [i for i in numbers if i % 2 is not 0]
variant = [i + 1000 for i in numbers if i % 2 is 0]
print(even, odd, variant, sep='\n\n')
In [ ]:
# dictionary
d = {}
d = {'a':'apple', 'b':'ball'} #json if you use double quotes
d = {"a":"apple", "b":"ball"}
print(d)
In [ ]:
d['c'] = 'cat'
print(d)
In [ ]:
d['x']
In [ ]:
print(d.get('b'))
print(d.get('x'))
In [ ]:
print(d.get('e', '**'))
In [ ]:
for each in range(0,10):
print(each)
In [ ]:
x = 2
while x < 4:
print('ha')
x += 1
In [ ]:
p = (4,'apple')
print(p[0])
p[0] = 3
In [ ]:
def will_boss_give_me_a_raise(x, luck=True, hard_work=10000):
print(hard_work)
return True
print(will_boss_give_me_a_raise(3, hard_work=None))
print(will_boss_give_me_a_raise)
In [ ]:
def printer(self):
return 'Kashif works really hard, really!'
class Work:
pass
Work.__str__ = printer
class HardWork(Work):
pass
h = HardWork()
print(will_boss_give_me_a_raise(h))
In [ ]:
_ = """
#TODO
- dictionaries
- iteration
- default values
- loops
- for each
- ranges
- tuples
- printing
- functions
- optional params
- argument unpacking
- classes
- dunder methods
- exceptions
- import this
- advanced
- reflection
- decorators
- generators
- context manager - with
- annotations
- coroutines
"""
In [ ]: