Skip to content

Instantly share code, notes, and snippets.

View ahmedazizkhelifi's full-sized avatar
🍉

KHELIFI Ahmed Aziz ahmedazizkhelifi

🍉
View GitHub Profile
def maxi(a,b):
return max(a,b)
print(maxi(1,2))
print(maxi(1,a)) #>> NameError: name 'a' is not defined
def add_def(x,y):
return x+y
add_lambda = lambda x,y : x+y
add_def(5,3) # >>> 8
add_lambda(5,3) # >>> 8
L= [0,1,2,3,4,5,6,7,8,9]
def condition(x):
return x>4
L2 = list(filter(condition,L))
# >>> [5, 6, 7, 8, 9]
L3 = list(filter(lambda x : x > 4 , L ))
# >>> [5, 6, 7, 8, 9]
import numpy as np
Users= [
{'id' : 0, 'name' : 'Ahmed', 'salary' : 1200},
{'id' : 1, 'name' : 'Aziz', 'salary' : 1800},
{'id' : 2, 'name' : 'Khelifi', 'salary' : 820}
]
#conventional syntax:
users_name=[]
for user in Users:
users_name.append(user['name'])
users_name3 =[user['name'] for user in Users if user['name'][0]=="A"]
print(users_name3) #>>> ['Ahmed', 'Aziz']
# 1) conventional syntax:
dict1={}
for user in Users:
dict1.update({user['name']:user['salary']})
print (dict1) #>>> {'Ahmed': 1200, 'Aziz': 1800, 'Khelifi': 820}
# 2) Dict comprehesion:
dict2 = {user['name'] : user['salary'] for user in Users }
print(dict2) #>>> {'Ahmed': 1200, 'Aziz': 1800, 'Khelifi': 820}
# 1) conventional syntax:
dict1={}
for user in Users:
dict1.update({user['name']:user['salary']})
print (dict1) #>>> {'Ahmed': 1200, 'Aziz': 1800, 'Khelifi': 820}
# 2) Dict comprehesion:
dict2 = {user['name'] : user['salary'] for user in Users }
print(dict2) #>>> {'Ahmed': 1200, 'Aziz': 1800, 'Khelifi': 820}
def recursive_factorial(n):
return n*recursive_factorial(n-1)
def recursive_factorial(n):
if n <= 1:
return 1
else:
return n*recursive_factorial(n-1)
def iterative_factorial(n):
f = 1
for i in range(2, n+1):
f *= i
return f