0% found this document useful (0 votes)
26 views

6 - 10 Python

This document provides 10 Python tips related to functions like map(), range(), accessing private properties, checking memory usage, variable argument functions, inheritance, operator overloading and more. Each tip is demonstrated with a short code example.

Uploaded by

Vasudevan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

6 - 10 Python

This document provides 10 Python tips related to functions like map(), range(), accessing private properties, checking memory usage, variable argument functions, inheritance, operator overloading and more. Each tip is demonstrated with a short code example.

Uploaded by

Vasudevan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

10 Days of Python Tips : Day-6

Inspired by the article by Fatos Morina.

Link: https://towardsdatascience.com/100-helpful-python-tips-you-can-learn-before-finishing-your-morning-coffee-eb9c39e68958

Do you want to learn Python, SQL, Django, Machine Learning, Deep Learning, and Statistics in one-on-one
classes 🤔
Drop me a message on LinkedIn to discuss your requirements

Follow me on LinkedIn for more: linkedin.com/in/arjun-panwar/

1. map() returns a new object


In [3]: my_list = [1, 2, 3, 4]

squared = map(lambda x: x ** 2, my_list)

print(list(squared)) # [1, 4, 9, 16]


print(my_list) # [1, 2, 3, 4]

[1, 4, 9, 16]
[1, 2, 3, 4]

2. range() includes a step parameter that may not be known that much
In [4]: for number in range(1, 10, 3):
print(number, end=" ")
# 1 4 7

1 4 7

3. range() starts by default at 0


So you don’t need to include it at all.
In [5]: def range_with_zero(number):
for i in range(0, number):
print(i, end=' ')

def range_with_no_zero(number):
for i in range(number):
print(i, end=' ')

range_with_zero(3) # 0 1 2
range_with_no_zero(3) # 0 1 2

0 1 2 0 1 2

4. You don’t need to compare the length with 0


If the length is greater than 0, then it is by default True, so you don’t really need to compare it with 0:

In [7]: def get_element_with_comparison(my_list):


if len(my_list) > 0:
return my_list[0]

def get_first_element(my_list):
if len(my_list):
return my_list[0]

elements = [1, 2, 3, 4]
first_result = get_element_with_comparison(elements)
second_result = get_element_with_comparison(elements)

print(first_result == second_result) # True

True

5. You can define the same method multiple times inside the same scope
However, only the last one is called, since it overrides previous ones.##

In [8]: def get_address():


return "First address"
def get_address():
return "Second address"

def get_address():
return "Third address"

print(get_address()) # Third address

Third address

6. You can access private properties even outside their intended scope
In [9]: class Engineer:
def __init__(self, name):
self.name = name
self.__starting_salary = 62000

dain = Engineer('Dain')
print(dain._Engineer__starting_salary) # 62000

62000

7. Check the memory usage of an object


In [10]: import sys

print(sys.getsizeof("bitcoin")) # 56

56

8. You can define a method that can be called with as many parameters as you
want
In [11]: def get_sum(*arguments):
result = 0
for i in arguments:
result += i
return result
print(get_sum(1, 2, 3)) # 6
print(get_sum(1, 2, 3, 4, 5)) # 15
print(get_sum(1, 2, 3, 4, 5, 6, 7)) # 28

6
15
28

9. You can call the parent class’s initializer using super() or parent class’s name
Calling the parent’s class initializer using super():

In [12]: class Parent:


def __init__(self, city, address):
self.city = city
self.address = address

class Child(Parent):
def __init__(self, city, address, university):
super().__init__(city, address)
self.university = university

child = Child('Zürich', 'Rämistrasse 101', 'ETH Zürich')


print(child.university) # ETH Zürich

ETH Zürich

Calling the parent’s class using the parent class’s name:

In [13]: class Parent:


def __init__(self, city, address):
self.city = city
self.address = address

class Child(Parent):
def __init__(self, city, address, university):
Parent.__init__(self, city, address)
self.university = university
child = Child('Zürich', 'Rämistrasse 101', 'ETH Zürich')
print(child.university) # ETH Zürich

ETH Zürich

Note that calls to parent initializers using init() and super() can only be used inside the child class’s initializer.

10. You can redefine the “+” operator inside your own classes
Whenever you use the + operator between two int data types, then you are going to find their sum.

However, when you use it between two string data types, you are going to merge them:

In [15]: print(10 + 1) # Adding two integers using '+'


print('first' + 'second') # Merging two strings '+'

11
firstsecond

This represents the operator overloading.

You can also use it with your own classes as well:

In [17]: class Expenses:


def __init__(self, rent, groceries):
self.rent = rent
self.groceries = groceries

def __add__(self, other):


return Expenses(self.rent + other.rent,
self.groceries + other.groceries)

april_expenses = Expenses(1000, 200)


may_expenses = Expenses(1000, 300)

total_expenses = april_expenses + may_expenses


print(total_expenses.rent) # 2000
print(total_expenses.groceries) # 500

2000
500

In [ ]:

You might also like