Académique Documents
Professionnel Documents
Culture Documents
¶
why python???¶
BYEEEEEEEEEEEEEEEEE ¶
Very easy to use :)¶
Out[1]:
In [2]:
# Subtraction
1 - 3
Out[2]:
-2
In [3]:
# Multiplication
4 * 6
Out[3]:
24
In [4]:
Out[4]:
1.0
In [5]:
Out[5]:
6.0
In [6]:
# Exponential operation
2 ** 6
Out[6]:
64
In [2]:
a=5
print(a)
¶
Variable assignment in Python¶
In [7]:
var = 1
In [8]:
var
Out[8]:
In [9]:
x=
4
y= 9
x+ y
Out[9]:
13
In [10]:
x = x ** x
x
Out[10]:
256
In [3]:
12var = 5
Strings¶
In [2]:
'Hello'
Out[2]:
'Hello'
In [3]:
Out[3]:
In [4]:
Out[4]:
'hello'
In [5]:
print('str')
str
In [2]:
xyz=("jdhvdscvgsdchjbshvhsbchsasbch")
print(xyz.find("d"))
In [1]:
p=("i like to play guitar and i am huge fan of sanju and he play very good guitar")
q=(p.find("p"))
print(p[q:])
play guitar and i am huge fan of sanju and he play very good guitar
In [6]:
p=("i like to play guitar and i am huge fan of sanju and he play very good guitar")
q=(p.find("p"))
#q
print(p[:q])
i like to
In [6]:
p=("i like to play guitar and i am huge fan of sanju and he play very good guitar")
q=(p.find("guitar"))
print(q)
15
In [7]:
p=("i like to play guitar and i am huge fan of sanju and he play very good guitar")
q=(p.find("play"))
r=(p.find("play",q+1))
print(r)
56
requesting input¶
In [2]:
Out[2]:
'kggfhjfhv'
In [9]:
In [4]:
def sum(a,b):
return a+b
print (sum(5,10))
15
String formatting¶
In [5]:
#name = 'John'
#num = 12
print('My Name is {1} and my age is {0}'.format('qwe','ewr'))
My Name is ewr and my age is qwe
In [2]:
In [3]:
String Indexing¶
In [9]:
Name[0]
Out[10]:
'R'
In [11]:
Name[3]
Out[11]:
'e'
Slice method¶
In [12]:
Name[:]
Out[12]:
'Robert'
In [13]:
Name[1:]
Out[13]:
'obert'
In [14]:
Name[3:5]
# Grab everything from index 3 till 4 (Don't include 5th one)
Out[14]:
'er'
List¶
In [15]:
[1,2,3]
Out[15]:
[1, 2, 3]
In [8]:
In [9]:
In [18]:
Out[18]:
[1, 2, 3, 4, 5, 6]
In [19]:
Out[19]:
In [20]:
lis1.append(lis2)
lis1
# Nested lists
Out[20]:
In [21]:
lis1[3][2]
# Accessing the inner list at position 4 and element 6 which is at position 2 of the inner list
Out[21]:
¶
Dictionary¶
In [22]:
d = {1:'One',2:'Two'}
In [23]:
d[1]
Out[23]:
'One'
In [24]:
d.update({3:'Three'})
In [25]:
Out[25]:
In [26]:
d.update({4:[1,2,3,4,5]})
In [27]:
d
Out[27]:
In [28]:
d[4][3]
Out[28]:
¶
Tuples and Sets¶
In [1]:
In [2]:
tup[0]
Out[2]:
In [31]:
tup[0] = 3
# We cannot the change the items in the tuple since its immutable
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-31-126f4e82e227> in <module>()
----> 1 tup[0] = 3
2 # We cannot the change the items in the tuple since its immutable
In [35]:
s = {1,2,3,4}
s
# Sets
Out[35]:
{1, 2, 3, 4}
In [34]:
{1,2,3,4,4,5,5,6,9,3,3,3}
# Grabs only unique elements
Out[34]:
{1, 2, 3, 4, 5, 6, 9}
In [33]:
set([1,2,2,2,3,3,3,5,4,4])
Out[33]:
{1, 2, 3, 4, 5}
In [36]:
set(d[3])
# From the value at key 3 in the above defined dictionary
Out[36]:
In [38]:
s.add(6)
s
Out[38]:
{1, 2, 3, 4, 6}
¶
If elif and for loop¶
In [4]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-f8770effdb35> in <module>()
2 print('Hello')
3 elif 3 == 3:
----> 4 for i in s:
5 print(i)
6 else:
In [40]:
i = 1
In [10]:
0
1
2
3
4
5
6
7
8
9
In [ ]:
¶
List Comprehension¶
In [44]:
x = list(range(0,10))
out = []
out
Out[44]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [47]:
Out[47]:
¶
Functions¶
In [49]:
def func(p1):
print(p1)
func(1)
In [1]:
def func1(p1,p2):
print('Area of square is {}'.format(p1*p2))
func1(2,4)
Area of square is 8
In [55]:
def circleArea(r,pi=3.14):
"""
This is Docstring
Returns area of circle
input the radius and preferrd pi value
"""
print('Area is {}'.format(pi*r**2))
In [56]:
circleArea(7)
Area is 153.86
¶
Map & Filter Function and Lambda expression¶
In [58]:
[1, 2, 3, 4, 5]
In [59]:
def square(s):
return s**2
In [61]:
list(map(square,seq))
# The map function takes as an input the function, and the parameters which can be in the form of list and applies
each parameters to the function
Out[61]:
In [64]:
list(map(Square,seq))
Out[65]:
In [66]:
Square(2)
Out[66]:
In [69]:
Out[69]:
[2, 4]
In [71]:
Out[71]:
'hello'
In [72]:
str.upper()
Out[72]:
'HELLO'
In [74]:
Out[74]:
['Today', 'I', 'went', 'to', 'beach', '#Sunkissed', '#Fun']
In [81]:
tweet.split('#')[2]
Out[81]:
'Fun'
In [89]:
d.keys()
Out[89]:
dict_keys([1, 2, 3, 4])
In [90]:
Out[90]:
True
In [91]:
ls = [(1,2),(3,5),(7,8)]
In [92]:
ls[0]
Out[92]:
(1, 2)
In [94]:
# tuple unpacking
for item in ls:
print(item)
(1, 2)
(3, 5)
(7, 8)
In [95]:
1
3
7
In [100]:
12
35
78