Professional Documents
Culture Documents
Python Datatypes
Python Datatypes
Python numbers
• Integers, floating point numbers and complex numbers fall under Python
numbers category. They are defined as int, float and complex class in
Python.
• We can use the type() function to know which class a variable or a value
belongs to and the isinstance() function to check if an object belongs to a
particular class.
a=5
>>>print(a, "is of type", type(a))
5 is of type <class 'int'>
>>>a = 2.0
>>>print(a, "is of type", type(a))
2.0 is of type <class 'float'>
>>>a= 1+2j
>>>print(a, "is complex number?")
(1+2j) is complex number?
>>>print(isinstance(1+2j, complex))
True
Number Length
• Integers can be of any length, it is only limited by the memory available. A
floating point number is accurate up to few decimal places. Integer and
floating points are separated by decimal points. 1 is integer, 1.0 is floating
point number.
• Complex numbers are written in the form, x + yj, where x is the real part
and y is the imaginary part.
Examples
>>> a = 1234567890123456789
>>> a
1234567890123456789
>>> b = 0.1234567890123456789
>>> b
0.12345678901234568
>>> c = 1+2j
• >>> c
(1+2j)
Python List
• List is an ordered sequence of items. It is one of the most used datatype in
Python and is very flexible. All the items in a list do not need to be of the
same type.
• Declaring a list is pretty straight forward. Items separated by commas are
enclosed within brackets [ ].
• We can use the slicing operator [ ] to extract an item or a range of items
from a list. Index starts form 0 in Python.
>>> type(message)
<type 'str'>
• The underscore character (_) can appear in a name. It is often used in
names with multiple words, such as my_name or price_of_tea_in_china.
Contd…
• Variable names can be arbitrarily long. They can contain both letters and
numbers, but they have to begin with a letter.
• If uppercase letters are used, remember case matters. A and a are
different variables.
• If you give a variable an illegal name, you get a syntax error:
>>> x=2+3
>>> message = 'Hello, World!'
• When the Python interpreter displays the value of an expression, it uses the same
format you would use to enter a value. In the case of strings, that means that it
includes the quotation marks.
• But if you use a print statement, Python displays the contents of the string without
the quotation marks.
>>> message
'Hello, World!'
>>> print message
Hello, World!
Operators and Operands
• Operators are special symbols that represent computations like addition
and multiplication.
+ addition
- subtraction
* multiplication
/ division
** exponentiation
Order of operations
• When more than one operator appears in an expression, the order of
evaluation depends on the rules of precedence.
• Python follows the same precedence rules for its mathematical operators
that mathematics does.
• The acronym PEMDAS is a useful way to remember the order of
operations.
• Parentheses have the highest precedence and can be used to force an
expression to evaluate in the order you want. Since expressions in
parentheses are evaluated first, 2 * (3-1) is 4.
• Exponentiation has the next highest precedence, so 2**1+1 is 3 and not 4
• Multiplication and Division have the same precedence, which is higher
than Addition and Subtraction, which also have the same precedence.
So 2*3-1 yields 5 rather than 4, and 2/3-1 is -1, not 1
• Operators with the same precedence are evaluated from left to right. So in
the expression minute*100/60, the multiplication happens first,
yielding 5900/60, which in turn yields 98.
String Operations
• In general, mathematical operations can not be performed on
strings, even if the strings look like numbers. The following are
illegal (assuming that message has type string):
message-1 'Hello'/123 message*'Hello' '15'+2
• Interestingly, the + operator does work with strings. For strings,
the + operator represents concatenation, which means joining
the two operands by linking them end-to-end. Execute the
following:
>>>fruit = 'banana'
>>>baked= ' nut bread'
>>>print (fruit + baked)
• The * operator also works on strings; it performs repetition. For
example, 'Fun'*3 is 'FunFunFun'. One of the operands has to be a
string; the other has to be an integer.
String Operations
• ‘len’ command is used to find the length of a string
>>>astring = “Hello world!”
>>>print(len(astring))
• To find the index of a particular alphabet
>>>print(astring.index("o"))
• To count the number of times an alphabet occurs in a string
>>>print(astring.count("l"))
• To print a slice of string
>>>print(astring[3:7])
• To print a slice of string by skipping one character
>>>print(astring[3:9:2])
• To print uppercase or lowercase
>>>print(astring.upper())
>>>print(astring.lower())
Complex Number Operations
>>>x=4+5j
• To print the real and imaginary parts:
>>>print(x.real)
>>>print(x.imag)
• To find the magnitude of the complex number
>>>m=abs(x)
• To find the phase of the complex number, we can use phase() function in
the cmath library
>>>import cmath
>>>p=cmath.phase(x)
• For rectangular to polar form conversion and vice-versa, we cae use
functions from cmath library
>>>import cmath
>>>w=cmath.polar(x)
List Operations
append() - Add an element to the end of the list
extend() - Add all elements of a list to the another list
insert() - Insert an item at the defined index
remove() - Removes an item from the list
pop() - Removes and returns an element at the given index
clear() - Removes all items from the list
index() - Returns the index of the first matched item
count() - Returns the count of number of items passed as an argument
sort() - Sort items in a list in ascending order
reverse() - Reverse the order of items in the list
Set Operations
>>>my_set = {1,3}
>>>my_set.add(2)
>>>print(my_set)
>>>my_set.update([3,4,5],{5,6,7,8})
>>>print(my_set)
>>>my_set.remove(6)
>>>print(my_set)