Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added awesome script, added important difference between python and j… #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Awesome-Scripts/myfirstprogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import time
import webbrowser
total_breaks=3
break_count=0
print("This program started on "+time.ctime())
while(break_count < total_breaks):
time.sleep(10)
webbrowser.open("https://www.youtube.com/watch?v=cDyp1fMQhuM")
break_count = break_count+1
158 changes: 158 additions & 0 deletions Important differences between Python 2.x and Python 3.x with examples
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,161 @@ Unicode
xrange
Error Handling
_future_ module

Division Operator
If we are porting our code or executing the python 3.x code in python 2.x, it can be dangerous if integer division changes go unnoticed (since it doesn’t raise any error). It is preferred to use the floating value (like 7.0/5 or 7/5.0) to get the expected result when porting our code.

print 7 / 5
print -7 / 5

'''
Output in Python 2.x
1
-2
Output in Python 3.x :
1.4
-1.4

# Refer below link for details
# http://www.geeksforgeeks.org/division-operator-in-python/
'''
print function

This is the most well known change. In this the print function in Python 2.x is replaced by print() function in Python 3.x,i.e, to print in Python 3.x an extra pair of parenthesis is required.

print 'Hello, Geeks' # Python 3.x doesn't support
print('Hope You like these facts')

'''
Output in Python 2.x :
Hello, Geeks
Hope You like these facts

Output in Python 3.x :
File "a.py", line 1
print 'Hello, Geeks'
^
SyntaxError: invalid syntax

Refer below link for details
http://www.geeksforgeeks.org/g-fact-25-print-single-multiple-variable-python/
'''
As we can see, if we use parenthesis in python 2.x then there is no issue but if we don’t use parenthesis in python 3.x, we get SyntaxError.

Unicode:

In Python 2, implicit str type is ASCII. But in Python 3.x implicit str type is Unicode.

print(type('default string '))
print(type(b'string with b '))

'''
Output in Python 2.x (Bytes is same as str)
<type 'str'>
<type 'str'>

Output in Python 3.x (Bytes and str are different)
<class 'str'>
<class 'bytes'>
'''
Python 2.x also supports Unicode

print(type('default string '))
print(type(u'string with b '))

'''
Output in Python 2.x (Unicode and str are different)
<type 'str'>
<type 'unicode'>

Output in Python 3.x (Unicode and str are same)
<class 'str'>
<class 'str'>
'''

xrange:

xrange() of Python 2.x doesn’t exist in Python 3.x. In Python 2.x, range returns a list i.e. range(3) returns [0, 1, 2] while xrange returns a xrange object i. e., xrange(3) returns iterator object which work similar to Java iterator and generates number when needed.
If we need to iterate over the same sequence multiple times, we prefer range() as range provides a static list. xrange() reconstructs the sequence every time. xrange() doesn’t support slices and other list methods. The advantage of xrange() is, it saves memory when task is to iterate over a large range.

In Python 3.x, the range function now does what xrange does in Python 2.x, so to keep our code portable, we might want to stick to using range instead. So Python 3.x’s range function is xrange from Python 2.x.

for x in xrange(1, 5):
print(x),

for x in range(1, 5):
print(x),

'''
Output in Python 2.x
1 2 3 4 1 2 3 4

Output in Python 3.x
NameError: name 'xrange' is not defined
'''

Error Handling:

There is a small change in error handling in both versions. In python 3.x, ‘as’ keyword is required.

try:
trying_to_check_error
except NameError, err:
print err, 'Error Caused' # Would not work in Python 3.x

'''
Output in Python 2.x:
name 'trying_to_check_error' is not defined Error Caused

Output in Python 3.x :
File "a.py", line 3
except NameError, err:
^
SyntaxError: invalid syntax
'''
try:
trying_to_check_error
except NameError as err: # 'as' is needed in Python 3.x
print (err, 'Error Caused')

'''
Output in Python 2.x:
(NameError("name 'trying_to_check_error' is not defined",), 'Error Caused')

Output in Python 3.x :
name 'trying_to_check_error' is not defined Error Caused
'''

_future_module:

This is basically not a difference between two version, but useful thing to mention here. The idea of __future__ module is to help in migration. We can use Python 3.x
If we are planning Python 3.x support in our 2.x code,we can ise_future_ imports it in our code.

For example, in below Python 2.x code, we use Python 3.x’s integer division behavior using __future__ module

# In below python 2.x code, division works
# same as Python 3.x because we use __future__
from __future__ import division

print 7 / 5
print -7 / 5


Output :

1.4
-1.4
Another example where we use brackets in Python 2.x using __future__ module

from __future__ import print_function

print('GeeksforGeeks')

Output :

GeeksforGeeks
Refer this for more details of __future__ module.

This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
60 changes: 60 additions & 0 deletions Important differences between python and java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Java
statically typed
In Java, all variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception.That’s what it means to say that Java is a statically typed language.

Java container objects (e.g. Vector and ArrayList) hold objects of the generic type Object, but cannot hold primitives such as int. To store an int in a Vector, you must first convert the int to an Integer. When you retrieve an object from a container, it doesn’t remember its type, and must be explicitly cast to the desired type.

Python
dynamically typed
In Python, you never declare anything. An assignment statement binds a name to an object, and the object can be of any type. If a name is assigned to an object of one type, it may later be assigned to an object of a different type. That’s what it means to say that Python is a dynamically typed language.

Python container objects (e.g. lists and dictionaries) can hold objects of any type, including numbers and lists. When you retrieve an object from a container, it remembers its type, so no casting is required.

Java
not compact

Python
compact
In The New Hacker’s Dictionary, Eric S. Raymond gives the following definition for “compact”:

Compact adj. Of a design, describes the valuable property that it can all be apprehended at once in one’s head. This generally means the thing created from the design can be used with greater facility and fewer errors than an equivalent tool that is not compact.

Java Example
*public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hellold!");
}
}

*int myCounter = 0;
String myString = String.valueOf(myCounter);
if (myString.equals("0")) ...

*// print the integers from 1 to 9
for (int i = 1; i < 10; i++)
{
System.out.println(i);
}

Python Example
*print "Hello, world!"

*print("Hello, world!") # Python version 3

*myCounter = 0
myString = str(myCounter)
if myString == "0": ...

*print the integers from 1 to 9
for i in range(1,10):
print i

Example:
Your application has 15 classes. (More precisely, it has 15 top-level public classes.)
Java
Each top-level public class must be defined in its own file. If your application has 15 such classes, it has 15 files.

Python
Multiple classes can be defined in a single file. If your application has 15 classes, the entire application could be stored in a single file, although you would probably want to partition it sensibly into perhaps 4, 5, or 6 files.
2 changes: 2 additions & 0 deletions Python_Resources_2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,8 @@ Inspired by [awesome-php](https://github.com/ziadoz/awesome-php).
* [PDFMiner](https://github.com/euske/pdfminer) - A tool for extracting information from PDF documents.
* [PyPDF2](https://github.com/mstamy2/PyPDF2) - A library capable of splitting, merging and transforming PDF pages.
* [ReportLab](http://www.reportlab.com/opensource/) - Allowing Rapid creation of rich PDF documents.
* [ThinkPython](http://www.greenteapress.com/thinkpython/thinkpython.pdf) - A good guide to think like software developer.
* [Cheat-Sheet](https://github.com/ehmatthes/pcc/releases/download/v1.0.0/beginners_python_cheat_sheet_pcc_all.pdf)- A very good cheat sheet for beginers.
* Markdown
* [Mistune](https://github.com/lepture/mistune) - Fastest and full featured pure Python parsers of Markdown.
* [Python-Markdown](https://github.com/waylan/Python-Markdown) - A Python implementation of John Gruber’s Markdown.
Expand Down