Abdullah Diab’s Blog

I’m Learning Python part 4

I’m Learning Python (part 4)

<p>
  <img title="Python Logo" src="http://www.python.org/images/python-logo.gif" alt="Python Logo" />
</p>

Why Use Built-In Types?

Most of the time in programming you’ll use lists, stacks, arrays, queues and dictionaries, so instead of constructing them, Python gives you a bunch of built-in types to use. The built-in types implement the semantic of the type ADT (Abstract Data Type), and they are fast, ‘cuz some of them are written in C and C++.

What Core Data Types Does Python Provide?

As in any language, Python provides the usual list of types besides other data types:

  • Numbers: 1234, 3.1415, 999L, 3+4j, Decimal
  • String: ‘Some text’, “a Text”
  • Lists: [1, [2, ‘Three’], 4]
  • Dictionaries: {‘food’ : ‘Lemon’, ‘taste’ : ‘Yummy!’ }
  • Tuples: (1, ‘Name’, 5, ‘u’ )
  • Files: myFile = open(‘someFile’, ‘r’ )
  • Other Types: Sets, Types, Boolean, Object, None

And don’t forget that everything is an object at the end 😉
Although Python has no variable declaration – you just assign the value to a variable name and it will be declared automatically -, it keeps track of your variables dynamically, so when you assign a string value to a variable you can use string operations only on it, and so on.
Now let’s discuss the types in a glance ‘cuz we’ll talk about them in depth later.

Numbers:

Python provides: integers (1234), floating point (3.1415), unlimited precision long (43251278364218357642386321984621734982137469321856412L), complex numbers (4568 + 789j), fixed precision decimals and sets.
Python also provides the simple operations:
+ is used for addition, – for subtraction, * for multiplication, ** for exponents, / for division.
You can calculate whatever you want (2 ** 1000000, but you don’t wanna print 300000 digit ;)).
When printing numbers there are two methods of printing, the first one is called ‘repr’ (object as code) and it prints the number with full precision, the second one is called ‘str’ which prints the number in a user-friendly way:
>>> 3.1415000000000002 * 2 #repr
6.2830000000000004
>>> print 3.1415000000000002 * 2 #str
6.283
We’ll discuss this method later when we introduce classes 🙂
Besides basic operations Python provides a module called ‘math’ which has useful methods and variables:
>>> import math
>>> math.pi
3.1415926535897931
>>> math.sqrt(123)
11.090536506409418

And it also provides a module called ‘random’ which generates random numbers:
>>> import random
>>> random.random()
0.8591504308650737 #You might get another value 😉

>>> random.choice([1, 2, 3, 4])
4 #You might also get another value 😉

We’ll discuss more numbers types soon, but now let’s move to another type.

Strings:

Strings as you might all now are arrays of characters (in Python we call the array a sequence).
Some of the sequence operations:
>>> s = ‘My Text’
>>> len(s)
7
>>> s[0]
‘M’
>>> s[3]
‘T’

A good indexing way is the negative indexing, which starts from left to right:
>>> s[-1] #Equivalent to s[len(s) – 1]
‘t’
>>> s[-2] #Equivalent to s[len(s) – 2]
‘x’

Slicing is another way of indexing in sequences, and it is used to extract a portion of the sequence:
>>> s[3:6] #A slice starts from 3 and ends at 5 not 6
‘Tex’

In slicing the left parameter’s default value is 0, and the right’s is the length of the sequence, which leads to some other slicing operations:
>>> s[:]
‘My Text’
>>> s[1:]
‘y Text’
>>> s[1:len(s)] #Same as s[1:]
‘y Text’
>>> s #Notice that s hasn’t changed, we were given new objects.
‘My Text’
>>>s[:-1] #Everything but the last character 😉
‘My Tex’

As you have noticed we were given new objects, which leads to the fact that slicing using [:] will copy the sequence into another new sequence, which is a great way to copy lists and other sequences.
As sequences, strings support concatenation and repetition:
>>> s + ‘ Is Beautiful’
‘My Text Is Beautiful’
>>> s #s hasn’t changed
‘My Text’
>>> s * 5
‘ My TextMy TextMy TextMy TextMy Text’

You see, + is used here for concatenation while in numbers it is used for addition, this is called operator overloading in other languages.

Bottom Line:

We’ll continue talking about types next time, until then create numbers and strings. Try to set a value to specified index of a string, what would you get?
Cheers.