| 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_2_vs_3. Show all posts
Showing posts with label python_2_vs_3. 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
Sep 24, 2019
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
Subscribe to:
Posts (Atom)