Best python github Repositories very helpful for beginners -
1. scikit-learn : https://github.com/scikit-learn
2. Flask : https://github.com/pallets/flask
3. Keras : https://github.com/keras-team/keras
4. Sentry : https://github.com/getsentry/sentry
5. Django : https://github.com/django/django
6. Ansible : https://github.com/ansible/ansible
7. Tornado : https://github.com/tornadoweb/tornado
1. scikit-learn : https://github.com/scikit-learn
2. Flask : https://github.com/pallets/flask
3. Keras : https://github.com/keras-team/keras
4. Sentry : https://github.com/getsentry/sentry
5. Django : https://github.com/django/django
6. Ansible : https://github.com/ansible/ansible
7. Tornado : https://github.com/tornadoweb/tornado
๐6
What are python namespaces?
๐A Python namespace ensures that object names in a program are unique and can be used without any conflict. Python implements these namespaces as dictionaries with โname as keyโ mapped to its respective โobject as valueโ.
Letโs explore some examples of namespaces:
๐Local Namespace consists of local names inside a function. It is temporarily created for a function call and gets cleared once the function returns.
๐Global Namespace consists of names from various imported modules/packages that are being used in the ongoing project. It is created once the package is imported into the script and survives till the execution of the script.
๐Built-in Namespace consists of built-in functions of core Python and dedicated built-in names for various types of exceptions.
๐A Python namespace ensures that object names in a program are unique and can be used without any conflict. Python implements these namespaces as dictionaries with โname as keyโ mapped to its respective โobject as valueโ.
Letโs explore some examples of namespaces:
๐Local Namespace consists of local names inside a function. It is temporarily created for a function call and gets cleared once the function returns.
๐Global Namespace consists of names from various imported modules/packages that are being used in the ongoing project. It is created once the package is imported into the script and survives till the execution of the script.
๐Built-in Namespace consists of built-in functions of core Python and dedicated built-in names for various types of exceptions.
๐8
Inheritance in Python with an example?
๐As Python follows an object-oriented programming paradigm, classes in Python have the ability to inherit the properties of another class. This process is known as inheritance. Inheritance provides the code reusability feature. The class that is being inherited is called a superclass or the parent class, and the class that inherits the superclass is called a derived or child class. The following types of inheritance are supported in Python:
๐Single inheritance: When a class inherits only one superclass
๐Multiple inheritance: When a class inherits multiple superclasses
๐Multilevel inheritance: When a class inherits a superclass, and then another class inherits this derived class forming a โparent, child, and grandchildโ class structure
๐Hierarchical inheritance: When one superclass is inherited by multiple derived classes
๐As Python follows an object-oriented programming paradigm, classes in Python have the ability to inherit the properties of another class. This process is known as inheritance. Inheritance provides the code reusability feature. The class that is being inherited is called a superclass or the parent class, and the class that inherits the superclass is called a derived or child class. The following types of inheritance are supported in Python:
๐Single inheritance: When a class inherits only one superclass
๐Multiple inheritance: When a class inherits multiple superclasses
๐Multilevel inheritance: When a class inherits a superclass, and then another class inherits this derived class forming a โparent, child, and grandchildโ class structure
๐Hierarchical inheritance: When one superclass is inherited by multiple derived classes
๐10
What are the common built-in data types in Python?
Python supports the below-mentioned built-in data types:
Immutable data types:
๐Number
๐String
๐Tuple
Mutable data types:
๐List
๐Dictionary
๐set
Python supports the below-mentioned built-in data types:
Immutable data types:
๐Number
๐String
๐Tuple
Mutable data types:
๐List
๐Dictionary
๐set
๐10๐ฅ7โค1
What is the lambda function in Python?
A lambda function is an anonymous function (a function that does not have a name) in Python. To define anonymous functions, we use the โlambdaโ keyword instead of the โdefโ keyword, hence the name โlambda functionโ. Lambda functions can have any number of arguments but only one statement.
Example:
A lambda function is an anonymous function (a function that does not have a name) in Python. To define anonymous functions, we use the โlambdaโ keyword instead of the โdefโ keyword, hence the name โlambda functionโ. Lambda functions can have any number of arguments but only one statement.
Example:
l = lambda x,y : x*y
print(a(5, 6))
Output:30๐19โค2
Here's a list of 20 valuable built-in Python functions and methods that shorten your code and improve its efficiency.
โค10๐1
1. reduce()
Python's reduce() function iterates over each item in a list, or any other iterable data type, and returns a single value. It's one of the methods of the built-in functools class of Python.
Here's an example of how to use reduce:
You can also format a list of strings using the reduce() function:
Python's reduce() function iterates over each item in a list, or any other iterable data type, and returns a single value. It's one of the methods of the built-in functools class of Python.
Here's an example of how to use reduce:
from functools import reduceOutput: 16
def add_num(a, b):
return a+b
a = [1, 2, 3, 10]
print(reduce(add_num, a))
You can also format a list of strings using the reduce() function:
from functools import reduceOutput: MUO is a media website
def add_str(a,b):
return a+' '+b
a = ['MUO', 'is', 'a', 'media', 'website']
print(reduce(add_str, a))
โค6๐2
2. split()
The split() function breaks a string based on set criteria. You can use it to split a string value from a web form. Or you can even use it to count the number of words in a piece of text.
The example code below splits a list wherever there's a space:
The split() function breaks a string based on set criteria. You can use it to split a string value from a web form. Or you can even use it to count the number of words in a piece of text.
The example code below splits a list wherever there's a space:
words = "column1 column2 column3"Output: ['column1', 'column2', 'column3']
words = words.split(" ")
print(words)
๐2
3. enumerate()
The enumerate() function returns the length of an iterable and loops through its items simultaneously. Thus, while printing each item in an iterable data type, it simultaneously outputs its index.
Assume that you want a user to see the list of items available in your database. You can pass them into a list and use the enumerate() function to return this as a numbered list.
Here's how you can achieve this using the enumerate() method:
0 grape
1 apple
2 mango
Whereas, you might've wasted valuable time using the following method to achieve this:
In essence, you can decide to start numbering from one instead of zero, by including a start parameter:
1 grape
2 apple
3 mango
The enumerate() function returns the length of an iterable and loops through its items simultaneously. Thus, while printing each item in an iterable data type, it simultaneously outputs its index.
Assume that you want a user to see the list of items available in your database. You can pass them into a list and use the enumerate() function to return this as a numbered list.
Here's how you can achieve this using the enumerate() method:
fruits = ["grape", "apple", "mango"]Output:
for i, j in enumerate(fruits):
print(i, j)
0 grape
1 apple
2 mango
Whereas, you might've wasted valuable time using the following method to achieve this:
fruits = ["grape", "apple", "mango"]In addition to being faster, enumerating the list lets you customize how your numbered items come through.
for i in range(len(fruits)):
print(i, fruits[i])
In essence, you can decide to start numbering from one instead of zero, by including a start parameter:
for i, j in enumerate(fruits, start=1):Output:
print(i, j)
1 grape
2 apple
3 mango
๐8
4. eval()
Python's eval() function lets you perform mathematical operations on integers or floats, even in their string forms. It's often helpful if a mathematical calculation is in a string format.
Here's how it works:
Python's eval() function lets you perform mathematical operations on integers or floats, even in their string forms. It's often helpful if a mathematical calculation is in a string format.
Here's how it works:
g = "(4 * 5)/4"Output: 5.0
d = eval(g)
print(d)
๐3
5. round()
You can round up the result of a mathematical operation to a specific number of significant figures using round():
The raw average is: 11.333333333333334
The rounded average is: 11.33
You can round up the result of a mathematical operation to a specific number of significant figures using round():
raw_average = (4+5+7/3)Output:
rounded_average=round(raw_average, 2)
print("The raw average is:", raw_average)
print("The rounded average is:", rounded_average)
The raw average is: 11.333333333333334
The rounded average is: 11.33
๐1
6. max()
The max() function returns the highest ranked item in an iterable. Be careful not to confuse this with the most frequently occurring value, though.
Let's print the highest ranked value in the dictionary below using the max() function:
The code above ranks the items in the dictionary alphabetically and prints the last one.
Now use the max() function to see the largest integer in a list:
The max() function returns the highest ranked item in an iterable. Be careful not to confuse this with the most frequently occurring value, though.
Let's print the highest ranked value in the dictionary below using the max() function:
b = {1:"grape", 2:"apple", 3:"applesss", 4:"zebra", 5:"mango"}
print(max(b.values()))
Output: zebraThe code above ranks the items in the dictionary alphabetically and prints the last one.
Now use the max() function to see the largest integer in a list:
a = [1, 65, 7, 9]Output: 65
print(max(a))
๐3
7. min()
The min() function does the opposite of what max() does:
1
apple
The min() function does the opposite of what max() does:
fruits = ["grape", "apple", "applesss", "zebra", "mango"]Output:
b = {1:"grape", 2:"apple", 3:"applesss", 4:"zebra", 5:"mango"}
a = [1, 65, 7, 9]
print(min(a))
print(min(b.values()))
1
apple
๐1
8. map()
Like reduce(), the map() function lets you iterate over each item in an iterable. However, instead of producing a single result, map() operates on each item independently.
Ultimately, you can perform mathematical operations on two or more lists using the map() function. You can even use it to manipulate an array containing any data type.
Here's how to find the combined sum of two lists containing integers using the map() function:
Like reduce(), the map() function lets you iterate over each item in an iterable. However, instead of producing a single result, map() operates on each item independently.
Ultimately, you can perform mathematical operations on two or more lists using the map() function. You can even use it to manipulate an array containing any data type.
Here's how to find the combined sum of two lists containing integers using the map() function:
b = [1, 3, 4, 6]Output: 96
a = [1, 65, 7, 9]
# Declare a separate function to handle the addition:
def add(a, b):
return a+b
# Pass the function and the two lists into the built-in map() function:
a = sum(map(add, b, a))
print(a)
9. getattr()
Python's getattr() returns the attribute of an object. It accepts two parameters: the class and the target attribute name.
Here's an example:
Python's getattr() returns the attribute of an object. It accepts two parameters: the class and the target attribute name.
Here's an example:
class ty:Output:Idowu
def init(self, number, name):
self.number = number
self.name = name
a = ty(5*8, "Idowu")
b = getattr(a, 'name')
print(b)
โค1