| Python 2 | Python 3 |
| input() may store as int, string raw_input() stores str always | input() function was fixed in Python 3 so that it always stores the user inputs as str |
| print "Hi" print("Hi") | print("Hi") |
| 3/2 ==> floor(1.5) => 1 (defaults to floor), return int | 3/2 ==> 1.5 |
| Strings default stores as Ascii | Strings default stores as unicode Unicode is a superset of ASCII and hence, can encode more characters including foreign ones. |
| sorted(employees.items(), key=lambda(x,y): y['age']) | sorted(employees.items(), key=lambda x: x[1]['age']) |
| AsyncIO | |
| Fstrings | |
| It is recommended to use __future__ imports it if you are planning Python 3.x support for your code | |
| xrange() - Lazy evaluation | range() - Lazy evaluation |
| except NameError, err: | except NameError as err: |
| my_generator = (letter for letter in 'abcdefg') next(my_generator) my_generator.next() | my_generator = (letter for letter in 'abcdefg') next(my_generator) |
| print 'Python', python_version() i = 1 print 'before: i =', i print 'comprehension: ', [i for i in range(5)] print 'after: i =', i Python 2.7.6 before: i = 1 comprehension: [0, 1, 2, 3, 4] after: i = 4 | Python 3.x for-loop variables don’t leak into the global namespace anymore! print ('Python', python_version()) i = 1 print 'before: i =', i print 'comprehension: ', [i for i in range(5)] print 'after: i =', i Python 3.4.1 before: i = 1 comprehension: [0, 1, 2, 3, 4] after: i = 1 |
| print range(3) print type(range(3)) [0, 1, 2] <type 'list'> | print range(3) print type(range(3)) print(list(range(3))) range(0, 3) <class 'range'> [0, 1, 2] |
| round(15.5) # 16.0 round(16.5) # 17.0 | Bankers rounding round(15.5) # 16 round(16.5) # 16 |
PySpark, BigData, SQL, Hive, AWS, Python, Unix/Linux, Shortcuts, Examples, Scripts, Perl
Showing posts with label python_basics. Show all posts
Showing posts with label python_basics. Show all posts
May 19, 2020
Python 2 Vs 3
Labels:
ascii,
fstrings,
input,
python,
python_2_vs_3,
python_advanced,
python_asyncIO,
python_basics,
python_interview_questions,
python_lambda,
python_sorted,
Python3,
range,
raw_input,
unicode,
xrange
Python List Vs Array
# Arrays Vs Lists
- Arrays need to be declared. Lists don’t
- Arrays can store data very compactly
- Arrays are great for numerical operations
import array
# Array (stores single data type)
array.array('i', [1, 22, 30, 44, 51]) # integer
array.array('d', [2.5, 3.2, 3.3]) # float
array.array('u', ['a', 'b', 'c']) # unicode
#List
ll = ['abc', 10, ['a', 'b', 'c'], (1,2,3)] # List can store anything
import numpy as np
# Numpy Array (it can store various data types)
array_2 = np.array(["numbers", 3, 6, 9, 12])
print (array_2)
print(type(array_2))
Labels:
array,
List,
Numpy,
Numpy_Array,
python_basics,
python_interview_questions
Python random
import random
random.random() ---> 0 to 1 float
random.randint() * 100 ---> 0 to 100 integer
random.randint() * 100 - 50 ---> -50 to 50 integer
random.randint(1, 40) ---> b/w 1 to 40 integer
random.uniform(1,50) ---> b/w 1 to 50 float
May 15, 2020
Python Super Tutorial 2
super()
- will allow us not to call explicitly
- Enable multiple inheritance
class Computer():
def __init__(self, computer, ram, storage):
self.computer = computer
self.ram = ram
self.storage = storage
# Class Mobile inherits Computer
class Mobile(Computer):
def __init__(self, computer, ram, storage, model):
super().__init__(computer, ram, storage)
self.model = model
Apple = Mobile('Apple', 2, 64, 'iPhone X')
print('The mobile is:', Apple.computer)
print('The RAM is:', Apple.ram)
print('The storage is:', Apple.storage)
print('The model is:', Apple.model)
May 13, 2020
Python Unittest
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
Apr 19, 2020
Python collections namedtuple
import collections
fields = ['OBJECTID', 'Identifier', 'Occurrence_Date', 'Day_of_Week', 'Occurrence_Month', 'Occurrence_Day', 'Occurrence_Year', 'Occurrence_Hour', 'CompStat_Month', 'CompStat_Day', 'CompStat_Year', 'Offense', 'Offense_Classification', 'Sector', 'Precinct', 'Borough', 'Jurisdiction', 'XCoordinate', 'YCoordinate', 'Location_1']
Crime = collections.namedtuple('Crime', fields)
fields = ['OBJECTID', 'Identifier', 'Occurrence_Date', 'Day_of_Week', 'Occurrence_Month', 'Occurrence_Day', 'Occurrence_Year', 'Occurrence_Hour', 'CompStat_Month', 'CompStat_Day', 'CompStat_Year', 'Offense', 'Offense_Classification', 'Sector', 'Precinct', 'Borough', 'Jurisdiction', 'XCoordinate', 'YCoordinate', 'Location_1']
Crime = collections.namedtuple('Crime', fields)
row1_value = ['1', 'f070032d', '09/06/1940 07:30:00 PM', 'Friday', 'Sep', '6', '1940', '19', '9', '7', '2010', 'BURGLARY', 'FELONY', 'D', '66', 'BROOKLYN', 'N.Y. POLICE DEPT', '987478', '166141', '(40.6227027620001, -73.9883732929999)']
row1_obj = Crime(*row1_value)
print(row1_obj)
Output:
Crime(OBJECTID='1', Identifier='f070032d', Occurrence_Date='09/06/1940 07:30:0
0 PM', Day_of_Week='Friday', Occurrence_Month='Sep', Occurrence_Day='6', Occur
rence_Year='1940', Occurrence_Hour='19', CompStat_Month='9', CompStat_Day='7',
CompStat_Year='2010', Offense='BURGLARY', Offense_Classification='FELONY', Se
ctor='D', Precinct='66', Borough='BROOKLYN', Jurisdiction='N.Y. POLICE DEPT',
XCoordinate='987478', YCoordinate='166141', Location_1='(40.6227027620001, -73
.9883732929999)')
Labels:
collections,
namedtuple,
python,
python_advanced,
python_basics,
python_interview_questions,
python_tuple,
tuple
Mar 29, 2020
Python Puzzle Remove even numbers
Python Puzzle Remove even numbers
# Wrong approach (incorrect - using For loop)
def removeEven(List):
print(id(List)) # 139909029905664
for each in List:
i f each % 2 == 0:
List.remove(each)
myList = [152, 168, 154, 32, -55, 81, 146, -34, -124, -9, 4, -31, -131, -86, -190, -38]
print(id(myList)) # 139909029905664
print(myList) # [1, 2, 4, 5, 10, 6, 3]
removeEven(myList)
print(myList) # [168, 32, -55, 81, -34, -9, -31, -131, -190]
# Wrong as when element gets deleted, index goes down
print('-' * 60)
# Correct approach (using While loop)
def removeEvenNew(List):
print(id(List))
i = 0
while i < len(List):
if List[i] % 2 == 0:
List.remove(List[i])
else:
i += 1
myList = [152, 168, 154, 32, -55, 81, 146, -34, -124, -9, 4, -31, -131, -86, -190, -38]
print(id(myList))
print(myList) #
removeEvenNew(myList)
print(myList) # [-55, 81, -9, -31, -131]
Output:
140407115793920
[152, 168, 154, 32, -55, 81, 146, -34, -124, -9, 4, -31, -131, -86, -190, -38]
140407115793920
[168, 32, -55, 81, -34, -9, -31, -131, -190]
------------------------------------------------------------
140407114861888
[152, 168, 154, 32, -55, 81, 146, -34, -124, -9, 4, -31, -131, -86, -190, -38]
140407114861888
[-55, 81, -9, -31, -131]
Labels:
for loop,
python,
python_advanced,
python_basics,
python_interview_questions,
python_loop,
python_puzzles,
while loop
Python Lists Advanced
ll = [10, 20, 30, 40, 50]
# insert, remove, pop
ll.remove(20) #[10, 30, 40, 50]
ll.pop() # #[10, 30, 40]
ll = [1, 3, 5, 'seven']
ll.insert(0, 2)
print(ll) # [2, 1, 3, 5, 'seven']
ll.pop(2) # pops 2nd index element
print(ll) # [2, 1, 5, 'seven']
ll.pop() # takes out last item
print(ll) # [2, 1, 5]
# Slice
gg = [1, 3, 5, 'seven', 'eight', 'nine', [10, 20,]]
print(gg[1:4]) # [3, 5, 'seven']
print(gg[3:]) # ['seven', 'eight', 'nine', [10, 20]]
print(gg[:3]) # [1, 3, 5]
print(gg[:]) # [1, 3, 5, 'seven', 'eight', 'nine', [10, 20]]
print(gg[-1:]) # [[10, 20]]
print(gg[:-1]) # [1, 3, 5, 'seven', 'eight', 'nine']
print(gg[-3:-1]) # ['eight', 'nine']
# list[start:stop:step]
print(gg[0:7:2]) # [1, 5, 'eight', [10, 20]]
ff = [1, 3, 5, 'seven', 'eight', 'nine']
print(ff) #[1, 3, 5, 'seven', 'eight', 'nine']
ff[2:2] = ['test']
print(ff) # [1, 3, 'test', 5, 'seven', 'eight', 'nine']
ff[1:3] = []
print(ff) # [1, 5, 'seven', 'eight', 'nine']
del ff[::2] # Delete even numbred indeces
print(ff) # [5, 'eight']
# Concatenate
kk = [1, 2, 3, 4] # [1, 2, 3, 4]
kk += 'ab' # since string, it takes as two elements
print(kk) # [1, 2, 3, 4, 'a', 'b']
kk += ['c', 'd']
print(kk) # [1, 2, 3, 4, 'a', 'b', 'c', 'd']
kk.extend(['e', 'f'])
print(kk) # [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f']
# List Vs Array
# Array has homogenous elements
# Python arrays are just wrappers for C language
import array
# type: 'd' (float), initializer list: [1, 2, 3]
newArray = array.array('i', [1, 2, 3])
print(newArray) # array('i', [1, 2, 3])
# insert, remove, pop
ll.remove(20) #[10, 30, 40, 50]
ll.pop() # #[10, 30, 40]
ll = [1, 3, 5, 'seven']
ll.insert(0, 2)
print(ll) # [2, 1, 3, 5, 'seven']
ll.pop(2) # pops 2nd index element
print(ll) # [2, 1, 5, 'seven']
ll.pop() # takes out last item
print(ll) # [2, 1, 5]
# Slice
gg = [1, 3, 5, 'seven', 'eight', 'nine', [10, 20,]]
print(gg[1:4]) # [3, 5, 'seven']
print(gg[3:]) # ['seven', 'eight', 'nine', [10, 20]]
print(gg[:3]) # [1, 3, 5]
print(gg[:]) # [1, 3, 5, 'seven', 'eight', 'nine', [10, 20]]
print(gg[-1:]) # [[10, 20]]
print(gg[:-1]) # [1, 3, 5, 'seven', 'eight', 'nine']
print(gg[-3:-1]) # ['eight', 'nine']
# list[start:stop:step]
print(gg[0:7:2]) # [1, 5, 'eight', [10, 20]]
ff = [1, 3, 5, 'seven', 'eight', 'nine']
print(ff) #[1, 3, 5, 'seven', 'eight', 'nine']
ff[2:2] = ['test']
print(ff) # [1, 3, 'test', 5, 'seven', 'eight', 'nine']
ff[1:3] = []
print(ff) # [1, 5, 'seven', 'eight', 'nine']
del ff[::2] # Delete even numbred indeces
print(ff) # [5, 'eight']
# Concatenate
kk = [1, 2, 3, 4] # [1, 2, 3, 4]
kk += 'ab' # since string, it takes as two elements
print(kk) # [1, 2, 3, 4, 'a', 'b']
kk += ['c', 'd']
print(kk) # [1, 2, 3, 4, 'a', 'b', 'c', 'd']
kk.extend(['e', 'f'])
print(kk) # [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f']
# List Vs Array
# Array has homogenous elements
# Python arrays are just wrappers for C language
import array
# type: 'd' (float), initializer list: [1, 2, 3]
newArray = array.array('i', [1, 2, 3])
print(newArray) # array('i', [1, 2, 3])
Mar 24, 2020
Python Palindrome
Using Recursion
def isPalindrome(testVariable):
print(testVariable)
if len(testVariable) <= 1:
return True
length = len(testVariable)
if testVariable[0] == testVariable[length-1]:
return isPalindrome(testVariable[1:length-1])
return False
a = isPalindrome('MADAM')
print(a) ## True
Using normal way
def isPalindrome(testVariable):
if testVariable == testVariable[::-1]:
return True
return False
a = isPalindrome('MADAM')
print(a) ## True
def isPalindrome(testVariable):
print(testVariable)
if len(testVariable) <= 1:
return True
length = len(testVariable)
if testVariable[0] == testVariable[length-1]:
return isPalindrome(testVariable[1:length-1])
return False
a = isPalindrome('MADAM')
print(a) ## True
Using normal way
def isPalindrome(testVariable):
if testVariable == testVariable[::-1]:
return True
return False
a = isPalindrome('MADAM')
print(a) ## True
Labels:
interview,
interview questions,
palindrome,
python,
python_basics,
python_interview_questions,
recursion
Python recursion
def factorial(num):
if num == 1:
return num
else:
return num * factorial(num-1)
target = factorial(5)
print(target) ## 120
def square(num):
if num == 1:
return num
else:
return square(num-1) + 2*num - 1
target = square(6)
print(target) ## 36
if num == 1:
return num
else:
return num * factorial(num-1)
target = factorial(5)
print(target) ## 120
def square(num):
if num == 1:
return num
else:
return square(num-1) + 2*num - 1
target = square(6)
print(target) ## 36
python reverse string
def reverse_fuc(input_str):
reverse = ''
length = len(input_str) - 1
while length >= 0:
reverse = reverse + input_str[length]
length = length - 1
return reverse
target = reverse_fuc('prabhath')
print(target) #htahbarp
reverse = ''
length = len(input_str) - 1
while length >= 0:
reverse = reverse + input_str[length]
length = length - 1
return reverse
target = reverse_fuc('prabhath')
print(target) #htahbarp
Nov 11, 2019
Python Disadvantages
Python Disadvantages
- Its an interpreted language, not as fast as a compiled language
- Slower than C & C++. Since Python is a high level language unlike C, C++, its not close to hardware
- Not good for Gaming, Mobile dev, Desktop UI applications
- Not good for memory intensive, due to flexibility of data types, python memory consumption is high
- Python's database access layer is found to be bit underdeveloped and primitive (JDBC/ODBC)
- GIL
- Global interpreter lock: can’t run more than one thread using in one interpreter
- Creating, managing and tearing down processes (multi-processing, processes are heavier than threads) is more expensive than doing the same for threads. Furthermore, inter-process communication is relatively slower than inter-thread communication.
- Both these drawbacks may not make Python a practical technology choice for super-critical or time-sensitive use-cases.
- Python community are working to remove the GIL from CPython. One such attempt is known as the Gilectomy.
- GIL exists only in the original Python implementation that is CPython.
- Python has multiple interpreter implementations. CPython, Jython, IronPython and PyPy, written in C, Java, C# and Python respectively, are the most popular ones.
Labels:
cpython,
disadvantages,
GIL,
IronPython,
python,
python_basics,
python_interview_questions
Python fstrings
elem = 10
elem_index = 5
def to_lowercase(st):
return st.lower()
#Formatted string literals (f-strings)
#The idea behind f-strings is to make string interpolation simpler.
#f-strings are expressions evaluated at runtime rather than constant values
print(f'Index of element {elem} is {elem_index}')
#call functions from f-strings
print(f'String to lowercase: {to_lowercase("TEST_STRING")}')
print(f'String to lowercase: {"TEST_STRING".lower()}')
#Format - used in previous versions of python
print('Index of element {} is {}'.format(elem, elem_index))
elapsed = 23.5678
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
Output:
Index of element 10 is 5
String to lowercase: test_string
String to lowercase: test_string
Index of element 10 is 5
main.py executed in 23.57 seconds.
main.py executed in 23.57 seconds.
Labels:
fstrings,
python,
python_basics,
python_interview_questions
Sep 25, 2019
Python Prime number
import math
def is_prime(n):
if n<2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(2, sqrt_n + 1):
if n % i == 0:
return False
return True
foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print("All list :", list(foo))
print("Prime list :", list(filter(is_prime, foo)))
Output:
All list : [2, 18, 9, 22, 17, 24, 8, 12, 27]
Prime list : [2, 17]
def is_prime(n):
if n<2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(2, sqrt_n + 1):
if n % i == 0:
return False
return True
foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print("All list :", list(foo))
print("Prime list :", list(filter(is_prime, foo)))
Output:
All list : [2, 18, 9, 22, 17, 24, 8, 12, 27]
Prime list : [2, 17]
Labels:
python,
python_advanced,
python_algorithm,
python_basics,
python_interview_questions,
python_prime_number
Sep 24, 2019
Python sort by val
myd = {
"Peter": 40,
"John": 2,
"Bob": 1,
"Danny": 3,
}
#sort by val
s = sorted(myd.items(), key= lambda x:x[1])
print(s)
Output:
[('Bob', 1), ('John', 2), ('Danny', 3), ('Peter', 40)]
Labels:
python,
python_basics,
python_sort_by_val,
python_sorted
Python Sorted 2 Vs 3 versions
employees = {1000: {'name': 'Sahasra','country': 'India', 'age': 25}, \
1001: {'name': 'Peter','country': 'US', 'age': 21}, \
1002: {'name': 'John','country': 'US', 'age': 36}, \
1003: {'name': 'Sarayu','country': 'India', 'age': 30},\
1004: {'name': 'Akio','country': 'Japan', 'age': 60}, \
1005: {'name': 'Anand','country': 'India', 'age': 50}, \
1006: {'name': 'Vidya','country': 'India', 'age': 32}, \
1007: {'name': 'Salma','country': 'Bangladesh', 'age': 23},}
# Works in Python 2.7 only
ss = sorted(employees.items(), key=lambda(x, y): y['age'])
print(ss)
# Works in Python 2.7 and 3.7
# Using parentheses to unpack the arguments in a lambda is not allowed in ss ss = sorted(employees.items(), key=lambda x: x[1]['age'])
print(ss)
Output:
[(1001, {'country': 'US', 'age': 21, 'name': 'Peter'}), (1007, {'country': 'Bangladesh', 'age': 23, 'name': 'Salma'}), (1000, {'country': 'India', 'age': 25, 'name': 'Sahasra'}), (1003, {'country': 'India', 'age': 30, 'name': 'Sarayu'}), (1006, {'country': 'India', 'age': 32, 'name': 'Vidya'}), (1002, {'country': 'US', 'age': 36, 'name': 'John'}), (1005, {'country': 'India', 'age': 50, 'name': 'Anand'}), (1004, {'country': 'Japan', 'age': 60, 'name': 'Akio'})]
[(1001, {'country': 'US', 'age': 21, 'name': 'Peter'}), (1007, {'country': 'Bangladesh', 'age': 23, 'name': 'Salma'}), (1000, {'country': 'India', 'age': 25, 'name': 'Sahasra'}), (1003, {'country': 'India', 'age': 30, 'name': 'Sarayu'}), (1006, {'country': 'India', 'age': 32, 'name': 'Vidya'}), (1002, {'country': 'US', 'age': 36, 'name': 'John'}), (1005, {'country': 'India', 'age': 50, 'name': 'Anand'}), (1004, {'country': 'Japan', 'age': 60, 'name': 'Akio'})]
1001: {'name': 'Peter','country': 'US', 'age': 21}, \
1002: {'name': 'John','country': 'US', 'age': 36}, \
1003: {'name': 'Sarayu','country': 'India', 'age': 30},\
1004: {'name': 'Akio','country': 'Japan', 'age': 60}, \
1005: {'name': 'Anand','country': 'India', 'age': 50}, \
1006: {'name': 'Vidya','country': 'India', 'age': 32}, \
1007: {'name': 'Salma','country': 'Bangladesh', 'age': 23},}
# Works in Python 2.7 only
ss = sorted(employees.items(), key=lambda(x, y): y['age'])
print(ss)
# Works in Python 2.7 and 3.7
# Using parentheses to unpack the arguments in a lambda is not allowed in ss ss = sorted(employees.items(), key=lambda x: x[1]['age'])
print(ss)
Output:
[(1001, {'country': 'US', 'age': 21, 'name': 'Peter'}), (1007, {'country': 'Bangladesh', 'age': 23, 'name': 'Salma'}), (1000, {'country': 'India', 'age': 25, 'name': 'Sahasra'}), (1003, {'country': 'India', 'age': 30, 'name': 'Sarayu'}), (1006, {'country': 'India', 'age': 32, 'name': 'Vidya'}), (1002, {'country': 'US', 'age': 36, 'name': 'John'}), (1005, {'country': 'India', 'age': 50, 'name': 'Anand'}), (1004, {'country': 'Japan', 'age': 60, 'name': 'Akio'})]
[(1001, {'country': 'US', 'age': 21, 'name': 'Peter'}), (1007, {'country': 'Bangladesh', 'age': 23, 'name': 'Salma'}), (1000, {'country': 'India', 'age': 25, 'name': 'Sahasra'}), (1003, {'country': 'India', 'age': 30, 'name': 'Sarayu'}), (1006, {'country': 'India', 'age': 32, 'name': 'Vidya'}), (1002, {'country': 'US', 'age': 36, 'name': 'John'}), (1005, {'country': 'India', 'age': 50, 'name': 'Anand'}), (1004, {'country': 'Japan', 'age': 60, 'name': 'Akio'})]
Labels:
python,
python_2_vs_3,
python_basics,
python_interview_questions,
python_sort_by_val,
python_sorted
Sep 19, 2019
Python multiple threads - how to join
from threading import Thread, active_count, current_thread
import time
def fun(val):
for _ in range(5):
print(val, current_thread())
time.sleep(3)
threads = []
for i in range(1, 11):
t = Thread(target=fun, args=(i*i,))
threads.append(t)
t.start()
print("Current Threads count: %i." % active_count())
#Join threads
for t in threads:
t.join()
print('bye')
Output:
######
1 <Thread(Thread-1, started 140645849839360)>
Current Threads count: 2.
4 <Thread(Thread-2, started 140645841446656)>
Current Threads count: 3.
9 <Thread(Thread-3, started 140645833053952)>
Current Threads count: 4.
16 <Thread(Thread-4, started 140645616318208)>
Current Threads count: 5.
25 <Thread(Thread-5, started 140645607925504)>
Current Threads count: 6.
36 <Thread(Thread-6, started 140645599532800)>
Current Threads count: 7.
49 <Thread(Thread-7, started 140645591140096)>
Current Threads count: 8.
64 <Thread(Thread-8, started 140645582747392)>
Current Threads count: 9.
81 <Thread(Thread-9, started 140645574354688)>
Current Threads count: 10.
100 <Thread(Thread-10, started 140645565961984)>
Current Threads count: 11.
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
49 <Thread(Thread-7, started 140645591140096)>
81 <Thread(Thread-9, started 140645574354688)>
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
100 <Thread(Thread-10, started 140645565961984)>
1 <Thread(Thread-1, started 140645849839360)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
4 <Thread(Thread-2, started 140645841446656)>
16 <Thread(Thread-4, started 140645616318208)>
9 <Thread(Thread-3, started 140645833053952)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
64 <Thread(Thread-8, started 140645582747392)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
bye
import time
def fun(val):
for _ in range(5):
print(val, current_thread())
time.sleep(3)
threads = []
for i in range(1, 11):
t = Thread(target=fun, args=(i*i,))
threads.append(t)
t.start()
print("Current Threads count: %i." % active_count())
#Join threads
for t in threads:
t.join()
print('bye')
Output:
######
1 <Thread(Thread-1, started 140645849839360)>
Current Threads count: 2.
4 <Thread(Thread-2, started 140645841446656)>
Current Threads count: 3.
9 <Thread(Thread-3, started 140645833053952)>
Current Threads count: 4.
16 <Thread(Thread-4, started 140645616318208)>
Current Threads count: 5.
25 <Thread(Thread-5, started 140645607925504)>
Current Threads count: 6.
36 <Thread(Thread-6, started 140645599532800)>
Current Threads count: 7.
49 <Thread(Thread-7, started 140645591140096)>
Current Threads count: 8.
64 <Thread(Thread-8, started 140645582747392)>
Current Threads count: 9.
81 <Thread(Thread-9, started 140645574354688)>
Current Threads count: 10.
100 <Thread(Thread-10, started 140645565961984)>
Current Threads count: 11.
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
49 <Thread(Thread-7, started 140645591140096)>
81 <Thread(Thread-9, started 140645574354688)>
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
100 <Thread(Thread-10, started 140645565961984)>
1 <Thread(Thread-1, started 140645849839360)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
4 <Thread(Thread-2, started 140645841446656)>
16 <Thread(Thread-4, started 140645616318208)>
9 <Thread(Thread-3, started 140645833053952)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
64 <Thread(Thread-8, started 140645582747392)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
bye
Labels:
python,
python_advanced,
python_basics,
python_interview_questions,
python_thread,
Threading
python reverse vs reversed
"""
reverse() modifies the list itself, whereas
reversed() returns an iterator ready to traverse the list in reversed order.
"""
#string reverse (best way for string reverse using slicing)
s = 'string'
print(s[::-1]) #gnirts
print(s) #string
#string reversed
rs = reversed(s)
print(''.join(rs)) #gnirts
print(s) #string
#reverse list
l = [1,2,3]
l.reverse()
print(l) #[3,2,1]
#reversed list
ll = reversed(l)
print(ll) #<list_reverseiterator object at 0x7fa572312790>
print(list(ll)) #[1,2,3]
reverse() modifies the list itself, whereas
reversed() returns an iterator ready to traverse the list in reversed order.
"""
#string reverse (best way for string reverse using slicing)
s = 'string'
print(s[::-1]) #gnirts
print(s) #string
#string reversed
rs = reversed(s)
print(''.join(rs)) #gnirts
print(s) #string
#reverse list
l = [1,2,3]
l.reverse()
print(l) #[3,2,1]
#reversed list
ll = reversed(l)
print(ll) #<list_reverseiterator object at 0x7fa572312790>
print(list(ll)) #[1,2,3]
Labels:
python,
python_basics,
python_interview_questions,
python_reverse,
python_reversed,
reverse,
reversed
Sep 17, 2019
Python Super method
class A:
def __init__(self):
print('A')
class AA(A):
def __init__(self):
print('AA')
super().__init__() #No need to pass self, only args if required
a = A()
aa = AA()
class B:
def __init__(self):
print('B')
class BB(B):
def __init__(self):
print('BB')
B.__init__(self) #need to pass self and args if required
b = B()
bb = BB()
Output:
A
AA
A
B
BB
B
def __init__(self):
print('A')
class AA(A):
def __init__(self):
print('AA')
super().__init__() #No need to pass self, only args if required
a = A()
aa = AA()
class B:
def __init__(self):
print('B')
class BB(B):
def __init__(self):
print('BB')
B.__init__(self) #need to pass self and args if required
b = B()
bb = BB()
Output:
A
AA
A
B
BB
B
Python Shallow vs Deep Copy
"""
1. Copy by Reference
2. Shallow Copy
3. Deep Copy
"""
import copy
#Copy Ref
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]
new_list = old_list
new_list[2][2] = 9
print('----------')
print('ID of Old List:', id(old_list))
print('ID of New List:', id(new_list))
print('Copy Ref - Old List:', old_list)
print('Copy Ref - New List:', new_list)
print('----------')
#Output
#ID of Old List: 140672073880512
#ID of New List: 140672073880512
#Copy Ref - Old List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Copy Ref - New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy
1. Copy by Reference
2. Shallow Copy
3. Deep Copy
"""
import copy
#Copy Ref
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]
new_list = old_list
new_list[2][2] = 9
print('----------')
print('ID of Old List:', id(old_list))
print('ID of New List:', id(new_list))
print('Copy Ref - Old List:', old_list)
print('Copy Ref - New List:', new_list)
print('----------')
#Output
#ID of Old List: 140672073880512
#ID of New List: 140672073880512
#Copy Ref - Old List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Copy Ref - New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy
##############
#A shallow copy creates a new object which stores the reference of the original elements.
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
print("Shallow Copy Old list:", old_list)
print("Shallow Copy New list:", new_list)
print('----------')
#Output:
#Shallow Copy Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy - append
#A shallow copy creates a new object which stores the reference of the original elements.
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
print("Shallow Copy Old list:", old_list)
print("Shallow Copy New list:", new_list)
print('----------')
#Output:
#Shallow Copy Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy - append
######################
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
new_list.append([10,11,12])
print("Shallow Copy add Old list:", old_list)
print("Shallow Copy add New list:", new_list)
print('----------')
#Output:
#Shallow Copy add Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy add New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
#Shallow Copy - nested update
new_list = copy.copy(old_list)
new_list.append([10,11,12])
print("Shallow Copy add Old list:", old_list)
print("Shallow Copy add New list:", new_list)
print('----------')
#Output:
#Shallow Copy add Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy add New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
#Shallow Copy - nested update
###########################
#Existing elements will get updated - since it has reference to original elements
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
new_list[1][1] = 400
print("Shallow Copy nested Old list:", old_list)
print("Shallow Copy nested New list:", new_list)
print('----------')
#Output:
#Shallow Copy nested Old list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]
#Shallow Copy nested New list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]
#Deep Copy###########
#It makes complete copy of elements
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.deepcopy(old_list)
new_list[1][1] = 400
print('ID of Old List:', id(old_list))
print('ID of New List:', id(new_list))
print("Deep Copy Old list:", old_list)
print("Deep Copy New list:", new_list)
#Output:
#ID of Old List: 140672073879792
#ID of New List: 140672073880992
#Deep Copy Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Deep Copy New list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]
#Existing elements will get updated - since it has reference to original elements
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
new_list[1][1] = 400
print("Shallow Copy nested Old list:", old_list)
print("Shallow Copy nested New list:", new_list)
print('----------')
#Output:
#Shallow Copy nested Old list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]
#Shallow Copy nested New list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]
#Deep Copy###########
#It makes complete copy of elements
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.deepcopy(old_list)
new_list[1][1] = 400
print('ID of Old List:', id(old_list))
print('ID of New List:', id(new_list))
print("Deep Copy Old list:", old_list)
print("Deep Copy New list:", new_list)
#Output:
#ID of Old List: 140672073879792
#ID of New List: 140672073880992
#Deep Copy Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Deep Copy New list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]
Labels:
python,
python_basics,
python_copy,
python_deep_copy,
python_interview_questions,
python_shallow_copy
Subscribe to:
Posts (Atom)