Académique Documents
Professionnel Documents
Culture Documents
Constans and e
# constant
print(cmath.pi) # 3.14159265359
# constant e
print(cmath.e) # 2.71828182846
Assignment Operators
c=0
c += 1
print c #1
c=0
c -= 2
print c # -2
c=2
c *= 3
print c #6
c=3
c **= 2
print c #9
c=7
c /= 2
c = 13
c %= 5
print c #3
c = 13
c //= 5
print c #2
Substring :string[begin_index:end_index] returns a substring of
string with index begin_index to end_index.
Index starts at 0.
The returned string does not include end_index.
Index can be negative, which counts from the end.
for x in range(1,9):
print x
if x == 5:
break
While Loop Demonstration
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
#!/usr/bin/python
print "My college name is %s and started year is %d " % (cvr, 2001)
Updating Lists :we can update single or multiple elements of lists by
giving the slice on the left-hand side of the assignment operator, and
we can add to elements in a list with the append() method. For
example
#!/usr/bin/python
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
print list[2]
Delete List Elements: To remove a list element, we can use either the
del statement or the remove() method
For example
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
print list1
del list1[2];
print "After deleting value at index 2 : "
print list1
The following example shows the usage of reverse() method.
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc', 'xyz'];
aList.reverse();
print "List : ", aList
When we run above program, it produces following result
List : ['xyz', 'abc', 'zara', 'xyz', 123]
The following example shows the usage of sort() method.
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc', 'xyz'];
aList.sort();
print "List : ", aList
When we run above program, it produces following result
List : [123, 'abc', 'xyz', 'xyz', 'zara']
The following example shows the usage of insert() method.
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc']
aList.insert( 3, 2009)
print "Final List : ", aList
When we run above program, it produces following result
Final List : [123, 'xyz', 'zara', 2009, 'abc']
The following example shows the usage of pop() method.
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc'];
print "A List : ", aList.pop()
print "B List : ", aList.pop(2)
When we run above program, it produces following result
A List : abc
B List : zara
The following example shows the usage of extend() method.
#!/usr/bin/python
aList = [123, 'xyz', abcd', 'abc', 123];
bList = [2009, 'mani'];
aList.extend(bList)
print "Extended List : ", aList
When we run above program, it produces following result
Extended List : [123, 'xyz', abcd', 'abc', 123, 2009, 'mani']
TUPLES
A tuple is a sequence of immutable Python objects. Tuples are sequences,
just like lists. The differences between tuples and lists are, the tuples cannot
be changed unlike lists and tuples use parentheses, whereas lists use square
brackets.
#!/usr/bin/python
#!/usr/bin/python
To explicitly remove an entire tuple, just use the del statement. For
example:
#!/usr/bin/python
print tup
del tup;
print "After deleting tup : "
print tup
('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
File "test.py", line 9, in <module>
print tup;
NameError: name 'tup' is not defined
List to tuple
The following example shows the usage of tuple() method.
#!/usr/bin/python
aList = (123, 'xyz', 'zara', 'abc');
aTuple = tuple(aList)
print "Tuple elements : ", aTuple
When we run above program, it produces following result
Tuple elements : (123, 'xyz', 'zara', 'abc')
The following example shows the usage of min() method.
#!/usr/bin/python
tuple1, tuple2 = (123, 'xyz', 'zara', 'abc'), (456, 700, 200)
print "min value element : ", min(tuple1)
print "min value element : ", min(tuple2)
When we run above program, it produces following result
min value element : 123
min value element : 200
A Pythagorean triple consists of three positive integers a, b, and c, such that
a2 + b2 = c2.
Such a triple is commonly written (a, b, c), and the best known example is (3,
4, 5).
The following list comprehension creates the Pythagorean triples:
[(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 +
y**2 == z**2]
Print [x,y,z]
Let A and B be two sets, the cross product (or Cartesian product) of A
and B, written AB, is the set of all pairs wherein the first element is
a member of the set A and the second element is a member of the set
B.
colours = [ "red", "green", "yellow", "blue" ]
things = [ "house", "car", "tree" ]
coloured_things = [ (x,y) for x in colours for y in things ]
print(coloured_things)
{0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97}