This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def maxi(a,b): | |
| return max(a,b) | |
| print(maxi(1,2)) | |
| print(maxi(1,a)) #>> NameError: name 'a' is not defined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| users_name3 =[user['name'] for user in Users if user['name'][0]=="A"] | |
| print(users_name3) #>>> ['Ahmed', 'Aziz'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def recursive_factorial(n): | |
| return n*recursive_factorial(n-1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def recursive_factorial(n): | |
| if n <= 1: | |
| return 1 | |
| else: | |
| return n*recursive_factorial(n-1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def iterative_factorial(n): | |
| f = 1 | |
| for i in range(2, n+1): | |
| f *= i | |
| return f |