| 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 range. Show all posts
Showing posts with label range. 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
Jun 23, 2018
Learning For While loops in Python
#!/usr/local/bin/python2.7
'''
Learning For/While loops in Python
File name: for_while_loop_python.py
Author: Prabhath Kota
Date: June 22, 2018
Python Version: 2.7
'''
'''
Topics:
For loop
While loop
range
break
continue
enumerate
'''
print '----------------For loop---------------------'
freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
for each in freedom_fighters:
print each
print '----------------For loop Break---------------------'
print freedom_fighters
#Print only first 4 elements using Break statement
#Enumerate will get you index of the element as well - starting from 0
#enumerate(freedom_fighters) will start the index from 0
#enumerate(freedom_fighters, 1) will start the index from 1
for index,each in enumerate(freedom_fighters, 1):
print index, each
if index == 4:
break
print '----------------For loop Continue---------------------'
print freedom_fighters
#Print only even elements from the list
for index,each in enumerate(freedom_fighters, 1):
if index % 2 == 1: #Odd elements
continue
else: #Even Elements
print index, each
print '----------------For loop Range---------------'
squares = []
print range(1,10)
for x in range(0,10):
squares.append(x**2)
print (squares)
print '----------------While loop---------------'
#Try to avoild While loops, use only unless otherwise reequired
#If you sometimes don't handle it, it may end up in infinite loop
i = 0
squares = []
while(i < 10):
squares.append(i**2)
i += 1
print squares
'''
Learning For/While loops in Python
File name: for_while_loop_python.py
Author: Prabhath Kota
Date: June 22, 2018
Python Version: 2.7
'''
'''
Topics:
For loop
While loop
range
break
continue
enumerate
'''
print '----------------For loop---------------------'
freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
for each in freedom_fighters:
print each
print '----------------For loop Break---------------------'
print freedom_fighters
#Print only first 4 elements using Break statement
#Enumerate will get you index of the element as well - starting from 0
#enumerate(freedom_fighters) will start the index from 0
#enumerate(freedom_fighters, 1) will start the index from 1
for index,each in enumerate(freedom_fighters, 1):
print index, each
if index == 4:
break
print '----------------For loop Continue---------------------'
print freedom_fighters
#Print only even elements from the list
for index,each in enumerate(freedom_fighters, 1):
if index % 2 == 1: #Odd elements
continue
else: #Even Elements
print index, each
print '----------------For loop Range---------------'
squares = []
print range(1,10)
for x in range(0,10):
squares.append(x**2)
print (squares)
print '----------------While loop---------------'
#Try to avoild While loops, use only unless otherwise reequired
#If you sometimes don't handle it, it may end up in infinite loop
i = 0
squares = []
while(i < 10):
squares.append(i**2)
i += 1
print squares
Output:
----------------For loop---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
Lala Lajipati Rai
Dadabhai Naoroji
Rajendra Prasad
Sarojini Naidu
Dadabhai Naoroji
Lal Bahadur Shastri
----------------For loop Break---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
1 Lala Lajipati Rai
2 Dadabhai Naoroji
3 Rajendra Prasad
4 Sarojini Naidu
----------------For loop Continue---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
2 Dadabhai Naoroji
4 Sarojini Naidu
6 Lal Bahadur Shastri
----------------For loop Range---------------
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
----------------While loop---------------
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[Finished in 0.2s]
Labels:
break,
continue,
enumerate,
for loop,
python,
python_loop,
range,
while loop
Subscribe to:
Posts (Atom)