Monday, December 31, 2012

[3] - Strings

String Literals

String literals can be written with a pair of single quotes, double quotes, and Triple quotes.

Strings with single quotes and double quotes are essentially the same. For example:

>>> 'hello world'
'hello world'
>>> "hello world"
'hello world'


The reason to support both is that Python supports you to embed a quote character for the other variety inside a string without escaping it with a backslash. A single quote character can be embedded in a string enclosed in double quote characters, and vice versa:

>>> "There is a 't' in the word 'cat'."
"There is a 't' in the word 'cat'."

>>> 'There is a "t" in the word "cat".'
'There is a "t" in the word "cat".'


The following two are invalid strings:

>>> 'There is a 't' in the word 'cat'.'
SyntaxError: invalid syntax
>>> "There is a "t" in the word "cat"."
SyntaxError: invalid syntax


When a string is too long, it may be written into multiple lines, with a pair of triple quotes around it. For instance:

>>> s = '''I am going
to write a
long sentence'''
>>> s
'I am going\nto write a\nlong sentence'


Sometimes a string is inside a pair of parentheses and each line is inside a pair of quotes:

>>> s = ('I am going'
     'to write a'
     'long sentence')
>>> s
'I am goingto write along sentence'


Please notice that the two strings above are different. The first string inside a pair of triple quotes contains a newline characters at the end of each line, but the second one does not.

Escape Sequences and Raw Strings

Similar to many common programming languages, strings in Python may have escape sequences, which are backslashes used to introduce special byte codings. For example:

>>> s = '1\t2\n3'
>>> print s
1 2
3
>>> s = 'c:\\new\\test.py'
>>> print s
c:\new\test.py


In the code above, \t means a horizontal tab, \n means a newline, and \\ stands for a \.

Similar to C/++, the character \0 is a character with ASCII value 0. However, the \0 character does not terminate a string:

>>> s = 'ab\0cd'
>>> s
'ab\x00cd'


The character \ appears frequently in paths, it prone to make mistakes on paths for many programmers due to escape sequences. For instance, if the path in the code above 'c:\\new\\test.py' is written as 'c:\new\test.py', it is not a valid path anymore.

Raw strings make life easy. If the letter r (in lowercase or uppercase) appears just before the opening quote of a string, the escape mechanism gets turned off. For instance:

>>> s = r'c:\new\test.py'
>>> s
'c:\\new\\test.py'


Characters and Strings

In many programming languages such as C# and Java, the types for characters and strings are different. However, a single character is essentially a string with length 1. For example:

>>> type('a')
<type 'str'>
>>> type('abcdefg')
<type 'str'>

 

Sunday, December 30, 2012

[2] - Numbers

Numeric Operations

In Python, the arithmetic addition, subtraction, multiplication operations are intuitive and similar to other common programming languages, but division is somewhat tricky. Python defines two operator / and // for division.

In 2.X, the / operator does classic division, performing truncating integer division if both operands are integers and float division (keeping remainders) otherwise. The // operator does floor division which always truncates the reminder no matter the operands are integers or floats. The following are some sample results for / and // in Python 2.X:

>>> 5 / 2
2
>>> 5 // 2
2
>>> 5 / 2.0
2.5
>>> 5 // 2.0
2.0


The / operator differs in 3.X, which always performs true division and returns a float result that include any remainder regardless of operand types. For example, the following are some results in Python 3.X:

>>> 5 / 2
2.5
>>> 5 / 2.0
2.5


There are no differences for the // operator between 2.X and 3.X.

In Python, there are no incremental or decremental operators (++ or --). For instance:

>>> x = 1
>>> x++
SyntaxError: invalid syntax


In order to increase a number, we have to add 1 explicitly:

>>> x = 1
>>> x += 1
>>> print x
2


Besides a built-in function pow, Python also provides  a ** operator for exponentiation. For example:

>>> pow(5, 2)
25
>>> 5 ** 2
25
>>> pow (25, 0.5)
5.0
>>> 25 ** 0.5
5.0


Big Numbers

Python 2.X has a separate type for long integers, and it automatically converts any number too large to store in a normal integer to this type:
 
>>> 2 ** 10, type(2 ** 10)
(1024, <type 'int'>)
>>> 2 ** 50, type(2 ** 50)
(1125899906842624L, <type 'long'>)

The built-in function type returns the type of any objects. Note the type difference in the output above.

Python 3.X integers support unlimited size.

Therefore, it is not necessary to worry about overflow problems when programming in Python.

Complex Numbers

Complex Numbers are represented as two floating-point numbers: the real and imaginary parts. The imaginary part is coded as number with a j or J suffix. For example:

>>> type(1-3j)
<type 'complex'>
>>> (1 - 3j) + (3 + 5j)
(4+2j)


Fraction Type

There are some inaccuracies and limitations of floating-point math, so a new numeric type, Fraction is introduced in Python, which essentially keeps both a numerator and denominator explicitly.

In order to utilize the Faction type, the type should be imported from a module named fractions first. Additionally, we can pass a numerator and denominator to create an Faction instance, as shown below:

>>> print x
1/6
>>> y = Fraction(1, 3)
>>> print y
1/3

>>> print x + y
1/2


Pool for Small Integers

Small integers are frequently used in Python. In order to avoid recreating the small integers over and over again, Python creates a pool for small integers. Let's have a look at the following Python code:

>>> a = 257
>>> b = 257
>>> id(a)
35351736
>>> id(b)
35351712
>>> c = 256
>>> d = 256
>>> id(c)
30341244
>>> id(d)
30341244

The built-in function id is used to get the identity of an object in Python. The two integers a and b are the same in value, but their identity are different. Therefore, a and b are two different integers. However, the identities of c and d are the same, so they are essentially identical to each other.

The range of the small integers in the pool is from -5 to 256 by default. If you want to modify the range in the pool, you have to modify the source code of Python and then rebuild it.

Monday, November 5, 2012

[1] Introduction to This Blog

I used to be a C/C++/C# programmer for several years, and I began to learn Python in the Jan. of 2012. I started from the book <Learning Python>, with about 1200 pages. It is time-consuming, and a little bit tedious to read this book. Because I have been a programmer for many years and I am familiar with many concepts, such as what functions and classes are, I do not need a book with too much details.
Python is not their first programming language for many programmers. When they start to learn Python with understanding of other programming languages, it may be more helpful to tell them what the main differences between Python and others like C or C++ or Java. It will save their lots of time if they skip the similarities and just focus on the differences. 
That's the purpose of this blog: Focusing on the differences between Python and other common programming languages.
I'm going to cover the following topics:
  • Execution flow: if/elif/else and for/while loops.
  • Types: numbers, strings, lists, tuples, maps, dictionaries, files. Iterator will also covered in this section.
  • Functions: functions, including lambda and yield. The frequently used built-in functions will also discussed here.
  • Classes: class definition and usage.
  • Modules: including the most frequently used modules, such as time, threading, os, re, and urlib2.
  • Exceptions.
More posts will come. Please be patient ^_^