Python Projects & Resources
56.5K subscribers
769 photos
342 files
327 links
Perfect channel to learn Python Programming ๐Ÿ‡ฎ๐Ÿ‡ณ
Download Free Books & Courses to master Python Programming
- โœ… Free Courses
- โœ… Projects
- โœ… Pdfs
- โœ… Bootcamps
- โœ… Notes

Admin: @Coderfun
Download Telegram
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.
๐Ÿ‘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
๐Ÿ‘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
๐Ÿ‘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:

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:

from functools import reduce
def add_num(a, b):
return a+b
a = [1, 2, 3, 10]
print(reduce(add_num, a))

Output: 16

You can also format a list of strings using the reduce() function:

from functools import reduce
def add_str(a,b):
return a+' '+b
a = ['MUO', 'is', 'a', 'media', 'website']
print(reduce(add_str, a))

Output: MUO is a media website
โค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:

words = "column1 column2 column3"
words = words.split(" ")
print(words)

Output: ['column1', 'column2', 'column3']
๐Ÿ‘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:

fruits = ["grape", "apple", "mango"]
for i, j in enumerate(fruits):
print(i, j)

Output:
0 grape
1 apple
2 mango

Whereas, you might've wasted valuable time using the following method to achieve this:

fruits = ["grape", "apple", "mango"]
for i in range(len(fruits)):
print(i, fruits[i])

In addition to being faster, enumerating the list lets you customize how your numbered items come through.

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):
print(i, j)

Output:
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:

g = "(4 * 5)/4"
d = eval(g)
print(d)

Output: 5.0
๐Ÿ‘3
5. round()

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)
rounded_average=round(raw_average, 2)
print("The raw average is:", raw_average)
print("The rounded average is:", rounded_average)

Output:
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:

b = {1:"grape", 2:"apple", 3:"applesss", 4:"zebra", 5:"mango"}
print(max(b.values()))

Output: zebra

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:

a = [1, 65, 7, 9]
print(max(a))

Output: 65
๐Ÿ‘3
7. min()

The min() function does the opposite of what max() does:

fruits = ["grape", "apple", "applesss", "zebra", "mango"]
b = {1:"grape", 2:"apple", 3:"applesss", 4:"zebra", 5:"mango"}
a = [1, 65, 7, 9]
print(min(a))
print(min(b.values()))

Output:
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:

b = [1, 3, 4, 6]
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)

Output: 96
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:

class ty:
def init(self, number, name):
self.number = number
self.name = name
a = ty(5*8, "Idowu")
b = getattr(a, 'name')
print(b)

Output:Idowu
โค1