Menu bar

Saturday, August 30, 2014

Python Tutorials 1 - Number,String,List

Python is just the language for you.
Programs written in Python are typically much shorter than equivalent C, C++, or Java programs, for several reasons:
  • the high-level data types allow you to express complex operations in a single statement;
  • statement grouping is done by indentation instead of beginning and ending brackets;
  • no variable or argument declarations are necessary.The language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles.
Numbers
>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128
Python also has built-in support for complex numbers, and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).

String
Strings can be concatenated (glued together) with the + operator, and repeated with *
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
>>> 'Py' 'thon'
'Python'
Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:
>>> word = 'Python'
>>> word[0]  # character in position 0
'P'

Indices may also be negative numbers, to start counting from the right:
>>> word[-1]  # last character
'n'
>>> word[-2]  # second-last character
'o'
slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain a substring:
>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
>>> word[:2]  # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:]  # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'
 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1
Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:
>>> word[0] = 'J'
  ...
TypeError: 'str' object does not support item assignment\

Unicode Strings
Creating Unicode strings in Python is just as simple as creating normal strings:
>>> u'Hello World !'
u'Hello World !'
>>> u'Hello\u0020World !'
u'Hello World !'
The built-in function unicode() provides access to all registered Unicode codecs (COders and DECoders)
When a Unicode string is printed, written to a file, or converted with str(), conversion takes place using this default encoding.
>>> u"abc"
u'abc'
>>> str(u"abc")
'abc'
To convert a Unicode string into an 8-bit string using a specific encoding, Unicode objects provide an encode() method that takes one argument, the name of the encoding. Lowercase names for encodings are preferred.
>>> u"äöü".encode('utf-8')
'\xc3\xa4\xc3\xb6\xc3\xbc'

 Lists
 list, which can be written as a list of comma-separated values (items) between square brackets
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
Unlike strings, which are immutable, lists are a mutable type
>>> cubes = [1, 8, 27, 65, 125]
>>> cubes[3] = 64  
>>> cubes
[1, 8, 27, 64, 125
You can also add new items at the end of the list, by using the append() method
>>> cubes.append(216)  # add the cube of 6
It is possible to nest lists (create lists containing other lists), for example:
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

Reference : https://docs.python.org/2/tutorial/


No comments :

Post a Comment