Skip to content

Commit

Permalink
Merging changes from py3 branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
kushaldas committed May 9, 2015
1 parent bf01e70 commit 6684e46
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 148 deletions.
28 changes: 14 additions & 14 deletions docs/datastructure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Tuples are data separated by comma.
>>> a[1]
'Debian'
>>> for x in a:
... print x,
... print(x, end=' ')
...
Fedora Debian Kubuntu Pardus

Expand Down Expand Up @@ -316,7 +316,7 @@ If you want to loop through a dict use *iteritems()* method.
>>> data
{'Kushal': 'Fedora', 'Jace': 'Mac', 'kart_': 'Debian', 'parthan': 'Ubuntu'}
>>> for x, y in data.iteritems():
... print "%s uses %s" % (x, y)
... print("%s uses %s" % (x, y))
...
Kushal uses Fedora
Jace uses Mac
Expand Down Expand Up @@ -355,7 +355,7 @@ If you want to loop through a list (or any sequence) and get iteration number at
::

>>> for i, j in enumerate(['a', 'b', 'c']):
... print i, j
... print(i, j)
...
0 a
1 b
Expand All @@ -368,7 +368,7 @@ You may also need to iterate through two sequences same time, for that use *zip(
>>> a = ['Pradeepto', 'Kushal']
>>> b = ['OpenSUSE', 'Fedora']
>>> for x, y in zip(a, b):
... print "%s uses %s" % (x, y)
... print("%s uses %s" % (x, y))
...
Pradeepto uses OpenSUSE
Kushal uses Fedora
Expand All @@ -392,11 +392,11 @@ In this example , you have to take number of students as input , then ask marks
data[name] = marks
for x, y in data.iteritems():
total = sum(y)
print "%s 's total marks %d" % (x, total)
print("%s 's total marks %d" % (x, total))
if total < 120:
print "%s failed :(" % x
print("%s failed :(" % x)
else:
print "%s passed :)" % x
print("%s passed :)" % x)

The output

Expand Down Expand Up @@ -426,24 +426,24 @@ In this example we will multiply two matrices. First we will take input the numb

#!/usr/bin/env python
n = int(raw_input("Enter the value of n: "))
print "Enter values for the Matrix A"
print("Enter values for the Matrix A")
a = []
for i in range(0, n):
a.append([int(x) for x in raw_input("").split(" ")])
print "Enter values for the Matrix B"
print("Enter values for the Matrix B")
b = []
for i in range(0, n):
b.append([int(x) for x in raw_input("").split(" ")])
c = []
for i in range(0, n):
c.append([a[i][j] * b[j][i] for j in range(0,n)])
print "After matrix multiplication"
print "-" * 10 * n
print("After matrix multiplication")
print("-" * 10 * n)
for x in c:
for y in x:
print "%5d" % y,
print ""
print "-" * 10 * n
print("%5d" % y, end=' ')
print("")
print("-" * 10 * n)

The output

Expand Down
30 changes: 15 additions & 15 deletions docs/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ You can even loop through the lines in a file object.
::

>>> fobj = open("sample.txt")
>>> for x in fobj:
... print(x, end='')
>>> for x in f:
... print(x, end=' ')
...
I love Python
Pradeepto loves KDE
Expand All @@ -90,10 +90,10 @@ Let us write a program which will take the file name as the input from the user

::

#!/usr/bin/env python
name = raw_input("Enter the file name: ")
#!/usr/bin/env python3
name = input("Enter the file name: ")
fobj = open(name)
print fobj.read()
print(fobj.read())
fobj.close()

In the last line you can see that we closed the file object with the help of close() method.
Expand Down Expand Up @@ -141,11 +141,11 @@ In this example we will copy a given text file to another file.

::

#!/usr/bin/env python
#!/usr/bin/env python3
import sys
if len(sys.argv) < 3:
print "Wrong parameter"
print "./copyfile.py file1 file2"
print("Wrong parameter")
print("./copyfile.py file1 file2")
sys.exit(1)
f1 = open(sys.argv[1])
s = f1.read()
Expand All @@ -162,12 +162,12 @@ The first value in *sys.argv* is the name of the command itself.

::

#!/usr/bin/env python
#!/usr/bin/env python3
import sys
print "First value", sys.argv[0]
print "All values"
print("First value", sys.argv[0])
print("All values")
for i, x in enumerate(sys.argv):
print i, x
print(i, x)

The output

Expand Down Expand Up @@ -224,7 +224,7 @@ Let us try to write an application which will count the spaces, tabs, and lines

::

#!/usr/bin/env python
#!/usr/bin/env python3

import os
import sys
Expand Down Expand Up @@ -261,7 +261,7 @@ Let us try to write an application which will count the spaces, tabs, and lines
"""
if os.path.exists(path):
spaces, tabs, lines = parse_file(path)
print "Spaces %d. tabs %d. lines %d" % (spaces, tabs, lines)
print("Spaces %d. tabs %d. lines %d" % (spaces, tabs, lines))
return True
else:
return False
Expand Down Expand Up @@ -318,4 +318,4 @@ line by line and find out the number of CPU(s).

.. tip:: Always remember to read files line by line than reading them as a whole. Sometimes you may have to read files which are way bigger than your available RAM.

After you do this, try to write your own lscpu command in Python :)
After you do this, try to write your own lscpu command in Python :)
46 changes: 23 additions & 23 deletions docs/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ Remember the palindrome program we wrote in the last chapter. Let us write a fun

::

#!/usr/bin/env python
#!/usr/bin/env python3
def palindrome(s):
return s == s[::-1]
if __name__ == '__main__':
s = raw_input("Enter a string: ")
s = input("Enter a string: ")
if palindrome(s):
print "Yay a palindrome"
print("Yay a palindrome")
else:
print "Oh no, not a palindrome"
print("Oh no, not a palindrome")

Now run the code :)

Expand All @@ -54,15 +54,15 @@ To understand local and global variables we will go through two examples.

::

#!/usr/bin/env python
#!/usr/bin/env python3
def change(b):
a = 90
print a
print(a)
a = 9
print "Before the function call ", a
print "inside change function",
print("Before the function call ", a)
print("inside change function", end=' ')
change(a)
print "After the function call ", a
print("After the function call ", a)

The output
::
Expand All @@ -76,16 +76,16 @@ First we are assigning *9* to *a*, then calling change function, inside of that

::

#!/usr/bin/env python
#!/usr/bin/env python3
def change(b):
global a
a = 90
print a
print(a)
a = 9
print "Before the function call ", a
print "inside change function",
print("Before the function call ", a)
print("inside change function", end=' ')
change(a)
print "After the function call ", a
print("After the function call ", a)

Here by using global keyword we are telling that *a* is globally defined, so when we are changing a's value inside the function it is actually changing for the *a* outside of the function also.

Expand Down Expand Up @@ -137,8 +137,8 @@ To avoid this you can write more idiomatic Python, like the following
>>> def f(a, data=None):
... if data is None:
... data = []
... data.append(a)
... return data
... data.append(a)
... return data
...
>>> print f(1)
[1]
Expand All @@ -152,7 +152,7 @@ Keyword arguments
::

>>> def func(a, b=5, c=10):
... print 'a is', a, 'and b is', b, 'and c is', c
... print('a is', a, 'and b is', b, 'and c is', c)
...
>>> func(12, 24)
a is 12 and b is 24 and c is 10
Expand All @@ -166,7 +166,7 @@ In the above example you can see we are calling the function with variable names
::

>>> def func(a, b=13, v):
... print a, b, v
... print(a, b, v)
...
File "<stdin>", line 1
SyntaxError: non-default argument follows default argument
Expand Down Expand Up @@ -201,7 +201,7 @@ In Python we use docstrings to explain how to use the code, it will be useful in

::

#!/usr/bin/env python
#!/usr/bin/env python3
import math

def longest_side(a, b):
Expand All @@ -216,7 +216,7 @@ In Python we use docstrings to explain how to use the code, it will be useful in
return math.sqrt(a*a + b*b)

if __name__ == '__main__':
print longest_side(4, 5)
print(longest_side(4, 5))

We will learn more on docstrings in reStructuredText chapter.

Expand All @@ -237,9 +237,9 @@ In Python any function can act as higher order function.
... return func(value)
...
>>> lst = high(dir, int)
>>> print lst[-3:]
>>> print(lst[-3:])
['imag', 'numerator', 'real']
>>> print lst
>>> print(lst)

.. note:: To know more read `this link <http://docs.python.org/2/faq/programming.html#how-do-you-make-a-higher-order-function-in-python>`_.

Expand All @@ -256,6 +256,6 @@ Example::
... "Returns the square of a given number."
... return num * num
...
>>> print map(square, lst)
>>> print(map(square, lst))
[1, 4, 9, 16, 25]

24 changes: 12 additions & 12 deletions docs/ifelse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ If the value of *expression* is true (anything other than zero), do the what is

::

#!/usr/bin/env python
number = int(raw_input("Enter a number: "))
#!/usr/bin/env python3
number = int(input("Enter a number: "))
if number < 100:
print "The number is less than 100"
print("The number is less than 100")

Then we run it

Expand All @@ -40,12 +40,12 @@ Now in the above example we want to print "Greater than" if the number is greate
::

#!/usr/bin/env python
number = int(raw_input("Enter a number: "))
#!/usr/bin/env python3
number = int(input("Enter a number: "))
if number < 100:
print "The number is less than 100"
print("The number is less than 100")
else:
print "The number is greater than 100"
print("The number is greater than 100")

The output

Expand All @@ -59,16 +59,16 @@ Another very basic example

::

>>> x = int(raw_input("Please enter an integer: "))
>>> x = int(input("Please enter an integer: "))
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... print('Negative changed to zero')
... elif x == 0:
... print 'Zero'
... print('Zero')
... elif x == 1:
... print 'Single'
... print('Single')
... else:
... print 'More'
... print('More')

Truth value testing
===================
Expand Down
3 changes: 1 addition & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Welcome to Python for you and me

This is a simple book to learn Python programming language, it is for the programmers who are new to Python.


The Python 2.x version of the same book can be found `here <http://pymbook.readthedocs.org/en/py2/>`_.
The Python 2.x version of the same book can be found `here <http://pymbook.readthedocs.org/en/latest/>`_.

Contents:

Expand Down
12 changes: 7 additions & 5 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@
Installation
============

In this chapter you will learn how to install Python
In this chapter you will learn how to install Python3, the latest of the language.


On Windows
==========

You have to download the latest Windows(TM) installer from the Python site http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi . Install it just as any other Windows software.
You have to download the latest Windows(TM) installer from the Python site, `x86_64 <https://www.python.org/ftp/python/3.4.1/python-3.4.1.amd64.msi>`_ and
`i686 <https://www.python.org/ftp/python/3.4.1/python-3.4.1.msi>`_. Install it just as any other Windows software.

On GNU/Linux
============

Generally all GNU/Linux distributions come with Python, so no need to worry about that :) If you don't have it then you can install it by either downloading from the Python website or from your distribution's repository.
You will have to install the latest Python from the distribution's repository.

For Fedora

::

[user@host]$ sudo yum install python
[user@host]$ sudo yum install python3

For Debian

::

[user@host]$ sudo apt-get install python
[user@host]$ sudo apt-get install python3


Loading

0 comments on commit 6684e46

Please sign in to comment.