Learn Python Tutorial - Numbers and other data types

Python Basics

(Learn Python Programming in 20 Minutes)

 

3 Python commands

3.2 Numbers and other data types

Python recognizes several dierent types of data. For instance, 23 and -75 are integers, while 5.0 and -23.09 are oats or oating point numbers. The type oat is (roughly) the same as a real number in mathematics. The number 12345678901 is a long integer; Python prints it with an “L” appended to the end.
Usually the type of a piece of data is determined implicitly.

3.2.1 The type function

To see the type of some data, use Python’s builtin type function:
>>> type(-75) 

<type ’int’> 
>>> type(5.0) 
<type ’float’> 
>>> type(12345678901) 
<type ’long’>
Another useful data type is complex, used for complex numbers. For example:
>>> 2j 

2j 
>>> 2j-1 
(-1+2j) 
>>> complex(2,3) 
(2+3j) 
>>> type(-1+2j) 
<type ’complex’> 
Notice that Python uses j for the complex unit (such that j2 = −1) just as physicists do, instead of the letter i preferred by mathematicians.

3.2.2 Strings

Other useful data types are strings (short for “character strings”); for example "Hello World!". Strings are sequences of characters enclosed in single or double quotes:

>>> "This is a string" 

'This is a string' >
>> ’This is a string, too’ 
’This is a string, too’ 
>>> type("This is a string") 
<type ’str’>
Strings are an example of a sequence type.

 

3.2.3 Lists and tuples

Other important sequence types used in Python include lists and tuples. A sequence type is formed by putting together some other types in a sequence. Here is how we form lists and tuples:
>>> [1,3,4,1,6] 

[1, 3, 4, 1, 6] 
>>> type( [1,3,4,1,6] ) 
<type ’list’> 
>>> (1,3,2) (1, 3, 2) 
>>> type( (1,3,2) ) 
<type ’tuple’>
Notice that lists are enclosed in square brackets while tuples are enclosed in parentheses. Also note that lists and tuples do not need to be homogeneous; that is, the components can be of dierent types:
>>> [1,2,"Hello" ,(1,2)] 

[1, 2, ’Hello’, (1, 2)]
Here we created a list containing four components: two integers, a string, and a tuple. Note that components of lists may be other lists, and so on:
>>> [1, 2, [1,2], [1,[1,2]], 5] 

[1, 2, [1, 2], [1, [1, 2]], 5]
By nesting lists within lists in this way, we can build up complicated structures. Sequence types such as lists, tuples, and strings are always ordered, as opposed to a set in mathematics, which is always unordered. Also, repetition is allowed in a sequence, but not in a set.


3.2.4 The range function

The range function is often used to create lists of integers. It has three forms. In the simplest form, range(n) produces a list of all numbers 0,1,2,...,n-1 starting with 0 and ending with n−1. For instance, 
>>> range(17) 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
You can also specify an optional starting point and an increment, which may be negative. For instance, we have
>> range(1,10) 

[1, 2, 3, 4, 5, 6, 7, 8, 9] 
>>> range(-6,0) 
[-6, -5, -4, -3, -2, -1] 
>>> range(1,10,2) 
[1, 3, 5, 7, 9] 
>>> range(10,0,-2) 
[10, 8, 6, 4, 2]

Note the use of a negative increment in the last example.




3.2.5 Boolean values

Finally, we should mention the boolean type. This is a value which is either True or False.
>>> True True 

>>> type(True) 
<type ’bool’> 
>>> False False 
>>> type(False) 
<type ’bool’>
Boolean types are used in making decisions.






3.3 Expressions

Python expressions are not commands, but rather form part of a command. An expression is anything which produces a value. Examples of expressions are: 2+2, 2**100, f((x-1)/(x+1)). Note that in order for Python to make sense of the last one, the variable x must have a value assigned and f should be a previously dened function. 
Expressions are formed from variables, constants, function evaluations, and operators. Parentheses are used to indicate order of operations and grouping, as usual.


3.4 Operators

The common binary operators for arithmetic are + for addition, - for subtraction, * for multiplication, and / for division. As already mentioned, Python uses ** for exponentiation. Integer division is performed so that the result is always another integer (the integer quotient):

 >>> 25/3 

>>> 5/2 
2

This is a wrinkle that you will always have to keep in mind when working with Python. To get a more accurate answer, use the oat type:
>>> 25.0/3 

8.3333333333333339 
>>> 5/2.0 
2.5
If just one of the operands is of type oat, then the result will be of type oat. Here is another example of this pitfall:
>>> 2**(1/2) 

1

where we wanted to compute the square root of 2 as the 1/ 2 power of 2, but the division in the exponent produced a result of 0 because of integer division. A correct way to do this computation is:
>>> 2**0.5 

1.4142135623730951

Another useful operator is %, which is read as ”mod”. This gives the remainder of an integer division, as in
>>> 5 % 2

 1 
>>> 25 % 3 
1

which shows that 5 mod 2 = 1, and 25 mod 3 = 1. This operator is useful in number theory and cryptography. 

Besides the arithmetic operators we need comparison operators: <, >, <=, >=, ==, !=, <>. In order these are read as: is less than, is greater than, is less than or equal to, is greater than or equal to, is equal to, is not equal to, is not equal to. The result of a comparison is always a boolen value True or False.
 
>>> 2 < 3 

True 
>>> 3<2 
False 
>>> 3 <= 2 
False

Note that != and <> are synonomous; either one means not equal to. Also, the operator == means is equal to.
>>> 2 <> 3 

True 
>>> 2 != 3 
True 
>>> 0 != 0 
False
>>> 0 == 0 
True 

 
 



 




 





 




 

No comments:

Post a Comment

Your feedback is highly appreciated and will help us to improve our content.