tag:blogger.com,1999:blog-18565987837787383952024-03-04T22:24:23.207-08:00Python for Advanced Level ICTNiranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.comBlogger23125tag:blogger.com,1999:blog-1856598783778738395.post-72921362372685130252016-03-15T12:09:00.000-07:002016-03-15T12:09:01.298-07:00A/L ICT 2015 Python Code for Structured QuestionWrite a Python program to record the marks obtained by students at the term test. Each student has sat for the same three papers and each
mark was given as an integer value out of 100 marks. Each student is identified by a unique index member which is also an integer. You should record the marks of student in a text file named 'marks.txt' in the following format.<br />
<br />
lndex_no_l ,mark_ll ,mark_l2,mark_l3<br />
Index_no_2 ,mark_2l ,mark_22 ,mark_23
<br />
<br />
Answer<br />
<br />
<pre>file = open("marks.txt", "a")
running=1
while (running ==1) :
indexNo = raw_input("Enter Index No: ")
if (indexNo=="-1"):
running=-1
else :
mark1 = raw_input("Enter marks 1: ")
mark2 = raw_input("Enter marks 2: ")
mark3 = raw_input("Enter marks 3: ")
str=indexNo + "," + mark1 + "," + mark2 + "," + mark2 + "\n"
file.write (str)
file.close()
</pre>
Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-88988646962310360072011-09-30T14:21:00.000-07:002011-09-30T14:21:18.828-07:00Flow Charting Tools : Free and Open Source<div style="color: #0b5394;"><span style="font-size: large;">The flowchart is a means of visually presenting the flow of data through an information processing systems, the operations performed within the system and the sequence in which they are performed. In this lesson, we shall concern ourselves with the program flowchart, which describes what operations (and in what sequence) are required to solve a given problem. <br />
<br />
<br />
The program flowchart can be likened to the blueprint of a building. As we know a designer draws a blueprint before starting construction on a building. Similarly, a programmer prefers to draw a flowchart prior to writing a computer program.<br />
</span> </div><br />
<div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjS3iBB9P6g85-yYLcBAYeak2A2EPsh4LKbhMSlhUPWybmF3MQi7J_BCtqpYxE0UmQI6tXVJmqiYOJrrh0-R6ez9dmW77fRDBUhqlagGXVs8ie_5EHUV_6Iy2Ps5Oy8-zl1MAl8hsaursvZ/s1600/Flow_chart_symbols.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjS3iBB9P6g85-yYLcBAYeak2A2EPsh4LKbhMSlhUPWybmF3MQi7J_BCtqpYxE0UmQI6tXVJmqiYOJrrh0-R6ez9dmW77fRDBUhqlagGXVs8ie_5EHUV_6Iy2Ps5Oy8-zl1MAl8hsaursvZ/s1600/Flow_chart_symbols.png" /></a></div><br />
<span style="font-size: large;"><b>American National Standard Flow Chart Symbols<br />
</b></span><br />
<br />
<span style="font-size: large;"><b>Gliffy</b> is a free web-based diagram editor to create, share, and collaborate on a wide range of diagrams.</span><br />
<span style="font-size: large;"><a href="http://www.gliffy.com/">http://www.gliffy.com/</a></span><br />
<br />
<br />
<span style="font-size: large;"><b>Project Draw</b> is a free web-based diagram editor to create basic network, flowchart or quick form layout diagrams.</span><br />
<span style="font-size: large;">h<a href="ttp://draw.labs.autodesk.com/ADDraw/draw.html">ttp://draw.labs.autodesk.com/ADDraw/draw.html</a></span><br />
<span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;"><b>DIA</b> is a desktop application on Windows and Linux to draw E-R diagrams, flowcharts.</span><br />
<span style="font-size: large;"><a href="http://live.gnome.org/Dia">http://live.gnome.org/Dia<br />
<br />
</a></span><br />
<br />
<br />
<span style="font-size: large;">MEANING OF A FLOWCHART</span><br />
<span style="font-size: large;"><br />
</span><br />
<ul><li><span style="font-size: large;">A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem. <br />
</span></li>
<li><span style="font-size: large;">Flowcharts are generally drawn in the early stages of formulating computer solutions. Flowcharts facilitate communication between programmers and business people. <br />
</span></li>
<li><span style="font-size: large;">Flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. <br />
</span></li>
<li><span style="font-size: large;">Once the flowchart is drawn, it becomes easy to write the program in any high level language. Often we see how flowcharts are helpful in explaining the program to others. <br />
</span></li>
<li><span style="font-size: large;">Flowchart is a must for the better documentation of a complex program.<br />
<br />
<a href="http://www.edrawsoft.com/flow-chart-design.php">Examples</a><br />
<br />
<br />
Source : eDrawSoft<br />
</span></li>
</ul>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com3tag:blogger.com,1999:blog-1856598783778738395.post-53860300546171386222011-08-04T04:40:00.000-07:002011-08-04T04:40:39.534-07:00Step By Step Programing to Teach Python<img border="0" height="305px" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQX2xvqwNS3UG7rB1jH9y0QoqUInqvtuQTz2h8n3OcHxuNgWi26AgbleJxpjaTifK3oEuAVMpKWcHAT-3Nt22EZGL2MlNLhKIjClS3cr-VsolWj-uaq5oefZpY89bnNrO8WO0Cmhg9UJDy/s400/objects.jpg" t$="true" width="400px" /><br />
<span style="font-size: large;"><br />
Step By Step Programing to Learn Python<br />
<br />
Used to explain syntax elements typically starts with a programming example. This example is then stepped through line by line which illustrates the evaluation of expressions. </span><br />
<br />
<span style="font-size: large;">Type 1.1 <br />
Input fix, Output fix, max. one step per line FindError locate error in source code </span><br />
<span style="font-size: large;">Type 2.1 <br />
1. Step: find erroneous line, 2. Step: Dialog with different possible explanations for the error, max. </span><br />
<br />
<span style="font-size: large;">Type 3.1<br />
For a given output the user has to find the adequate initialization of a variable. </span><br />
<br />
<span style="font-size: large;">Type 3.2 <br />
User initializes Variable, finds the correct output from a collection of (five) answers </span><br />
<br />
<span style="font-size: large;">Type 3.3 <br />
A variable is initialized randomly and the user has to find the output from a List of answers </span><br />
<br />
<span style="font-size: large;">Type 3.4 <br />
A variable is initialized randomly and the user has to type in the output of the program </span><br />
<br />
<span style="font-size: large;">Type 3.5 <br />
Program is fixed, user gives output of the program in text input dialog</span><br />
<span style="font-size: large;">Type 3.6 Program is fixed, user gives output of the program in check box dialog, </span><br />
<br />
<span style="font-size: large;">Type 4.1 <br />
A line of Code is missing. The user has to point to where to insert the line. A list of 5 possibilities is given to chose from.</span><br />
<br />
<span style="font-size: large;">Type 4.2 <br />
A line of Code is missing. The user has to point to where to insert the line. The user has to type in the missing line.</span><br />
<br />
<span style="font-size: large;">Source <br />
INTERACTIVE LEARNING OBJECTS: A FRAMEWORK BASED APPROACH: <br />
Friedbert KasparUniversity of Applied ScienceFaculty of Computer Science</span><br />
<a href="http://www.codewitz.net/papers/MMT_32-36_Friedbert_Kaspar.pdf"><span style="font-size: large;">http://www.codewitz.net/papers/MMT_32-36_Friedbert_Kaspar.pdf</span></a>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com2tag:blogger.com,1999:blog-1856598783778738395.post-86042725543851195442011-07-25T21:52:00.000-07:002011-07-25T21:52:10.049-07:00Python Recursion#!/usr/bin/python<br />
<br />
def factorial(n):<br />
space = '*' * (4 * n)<br />
print space, 'factorial', n<br />
if n == 0:<br />
print space, 'returning 1'<br />
return 1<br />
else:<br />
recurse = factorial(n-1)<br />
result = n * recurse<br />
print space, 'returning', result<br />
return result<br />
<br />
x=factorial(3)<br />
print x<br />
<br />
----<br />
Draw a flow chart for this program <br />
What if we change x=factorial(3) to x=factorial(5)<br />
Change the flow chartNiranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-30560898071410204312011-07-25T21:50:00.000-07:002011-07-25T21:50:12.685-07:00Python Data Types Code Example - පයිතන් දත්ත වර්ග<span style="font-size: small;"><span style="color: black;">x = []</span><br style="color: black;" /><span style="color: black;">for i in range(0,10):</span><br style="color: black;" /><span style="color: black;"> x.append(i)</span><br style="color: black;" /><span style="color: black;">print x</span><br style="color: black;" /><span style="color: black;">print "______________________________________"</span><br style="color: black;" /><br style="color: black;" /><span style="color: black;">y = x * 10</span><br style="color: black;" /><br style="color: black;" /><span style="color: black;">x = "Hello World!"</span><br style="color: black;" /><br style="color: black;" /><span style="color: black;">print x</span><br style="color: black;" /><span style="color: black;">print "______________________________________"</span><br style="color: black;" /><span style="color: black;">print y</span><br style="color: black;" /><br style="color: black;" /><span style="color: black;"># What are X and Y?</span><br />
<br />
===================================</span><br />
<span style="font-size: small;"><br />
</span><br />
<span style="font-size: small;">#############<br />
# Arithmetic<br />
#############<br />
4 + 4 == 8 <br />
3 - 5 == -2<br />
16 / 2 == 8<br />
16 * 4 == 64<br />
3 % 2 == 1 # Modulus<br />
<br />
# Long Int Conversion - Useful for function type mismatches - Not needed here.<br />
100000000000000000000L + long(100) == 100000000000000000100L<br />
<br />
======================<br />
<br />
##########<br />
# Strings<br />
##########<br />
<br />
a = "Hello"<br />
b = "World!"<br />
<br />
# Concatenation<br />
a + " " + b == "Hello World!"<br />
<br />
# Repetition and Concatenation<br />
a * 3 + " " + b == "HelloHelloHello World!"<br />
(a + " ") * 3 + b == "Hello Hello Hello World!"<br />
<br />
# Indexing<br />
b[2] == "r"<br />
<br />
# Conversion of data to string - Sesame Street:<br />
c = 7 # Backquote ` ` or str() operations<br />
( a + " number " + `c` ) and ( a + " number " + str(c) ) == "Hello number 7"<br />
<br />
<br />
============================</span><br />
<span style="font-size: small;"><br />
</span><br />
<span style="font-size: small;"><br />
</span>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com1tag:blogger.com,1999:blog-1856598783778738395.post-4350549437129661542011-07-25T21:46:00.000-07:002011-07-25T21:46:01.847-07:00Python dice roll code example#dice roll function (6-sided by default)<br />
import random<br />
<br />
def roll(d=6):<br />
return random.randint(1, d)<br />
while 1 > 0 :<br />
<br />
a=raw_input("enter number 1-6 : ")<br />
b=int(a)<br />
print ("entered :" , b)<br />
c=roll()<br />
print ("received :" , c)<br />
if (c == b):<br />
print ("you win ")<br />
break<br />
else :<br />
print ("you loose " , b , " <> ", c)<br />
print ("---------------------")<br />
<br />
<br />
<br />
what does break do?<br />
What is the use of random ?<br />
<br />
======================Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com1tag:blogger.com,1999:blog-1856598783778738395.post-16303270123559371332011-07-25T21:41:00.000-07:002011-07-25T21:41:54.343-07:00Python Dictionaries and Lists Difference#debugger example<br />
<br />
print "This function creates a list."<br />
<br />
def makelist():<br />
a = [] # list<br />
for i in range(1, 20):<br />
a.append(i)<br />
print "appending", i<br />
print "now list", a<br />
return a<br />
<br />
def printlist(a):<br />
for i in a:<br />
print i,<br />
<br />
b=makelist()<br />
printlist(b)<br />
<br />
<br />
<br />
#Dictionaries<br />
<br />
#rock, paper, scissors game using dictionary<br />
<br />
choice = raw_input("Enter s, r, or p: ")<br />
results = {"s": "cut paper", "r": "crushes scissors", "p": <br />
"covers rock"}<br />
print choice, results[choice]<br />
<br />
#<br />
#Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com3tag:blogger.com,1999:blog-1856598783778738395.post-22649113568129615712011-07-25T21:39:00.000-07:002011-07-25T21:39:10.037-07:00Python Classes Revision<span style="font-size: large;">#class example<br />
class Student:<br />
def __init__(self, myname, myage): # <span style="color: #990000;">note double underscores </span><br />
self.myname = myname </span><span style="font-size: large;"># <span style="color: #990000;">take passed value to a var</span></span><br />
<span style="font-size: large;"> self.myage = myage </span><span style="font-size: large;"># <span style="color: #990000;">take next value</span></span><br />
<span style="font-size: large;"> def sayname(self): </span><span style="font-size: large;"># <span style="color: #990000;">take next function in class</span></span><br />
<span style="font-size: large;"> print "Hello, my name is", self.myname <br />
<br />
def sayage(self):<br />
print "Hello, my age is", self.myage<br />
<br />
def jump(self,jump): # the jump function<br />
if (jump > 0) :<br />
print "I am jumping up ", jump<br />
else :<br />
print "I am jumping down ", jump<br />
<br />
name1 = Student("Amara", 17) # create object from class<br />
name1.sayname() # print name<br />
name1.sayage() <br />
name1.jump(4) # move up 4<br />
name1.jump(- 4) </span><span style="font-size: large;"># move down 4</span>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-68811517066951146202011-07-25T21:25:00.000-07:002011-07-25T21:32:46.754-07:00Python Lists : Code Examples for learningprint "This function creates a list."<br />
def makelist():<br />
a = []<br />
for i in range(1, 20):<br />
a.append(i)<br />
print ("appending", i, ":", a)<br />
return a<br />
makelist()<br />
<br />
<span style="font-size: large;"><b><span style="color: blue;">How does this work?<br />
<br />
hints<br />
<br />
<span style="font-size: small;">First you create and empty list and fill it later []<br />
you add item one by one - it get added to last<br />
<br />
Why lists are powerful than tuples <br />
What is the advantage of dictioneries?<br />
Can you have a tuple and a dictionary in a list?<br />
Can you also add an object to it?<br />
<br />
<br />
<span style="color: black;"><span style="font-size: large;">#building a list from another list</span></span><br style="color: black;" /><span style="color: black;">L1 = ['spa', 'spa', 'spa', 'beans', None, 'spa', 'spa']</span><br style="color: black;" /><span style="color: black;">L2 = []</span><br style="color: black;" /><span style="color: black;">for i in L1:</span><br style="color: black;" /><span style="color: black;"> if i is not None:</span><br style="color: black;" /><span style="color: black;"> L2.append(i)</span><br style="color: black;" />print (L2)<br style="color: black;" /><span style="color: black;">#</span><br style="color: black;" /><span style="color: black;">#</span><br />
</span></span></b></span><br />
<span style="font-size: large;"><b><span style="color: blue;"><span style="font-size: small;"><br />
</span></span></b></span><br />
<span style="font-size: large;"><b><span style="color: blue;"><span style="font-size: small;"><br />
</span></span></b></span>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-19548326099587457332011-07-25T21:20:00.000-07:002011-07-25T21:20:18.400-07:00What is the output of phyton from codeWhat is the output of phyton<br />
<br />
# mile/kilometer conversion<br />
for miles in range(10, 70, 10):<br />
km = miles * 1.609<br />
print "%d miles --> %3.2f kilometers" % (miles, km)<br />
<br />
#<br />
<br />
Hints<br />
range work among all numbers<br />
multiplication by float number create float variable<br />
%d digit %f float<br />
<br />
<br />
=============<br />
How do you call tiny function<br />
<br />
def simplefunction(x):<br />
print ("functions work!")<br />
print ("You passed parameter", x)<br />
z = x**2<br />
print (x, "squared value =", z)<br />
return z<br />
<br />
Hints<br />
functions need to be called form program to work<br />
some functions need parameters<br />
what error would you get if you call it<br />
simplefunction()<br />
or <br />
simplefunction("Kandy")<br />
<br />
======================<br />
<br />
What happens here ?<br />
<br />
#while loop example<br />
countdown = 10<br />
while countdown:<br />
print countdown,<br />
countdown -= 1<br />
print "blasted!"<br />
<br />
#<br />
<br />
explain each line to your self ?<br />
how many times will the loop run ?<br />
what is the value of countdown at last ?Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-76554522365845198082011-07-25T20:28:00.000-07:002011-07-25T20:28:18.430-07:00Python Code Completion Exersizeපන්තියක සිසුන් විෂය තුනක් සඳහා ලබාගත් ලකුණු එක් එක් සිසුවාගේ නම සමග පහතදැක්වෙන අයුරු "input.text" නම් ගොනුවේ සටහන් කර ඇත.<br />
<br />
Nimal, 20, 10, 30<br />
Saman, 40, 60, 45<br />
Mala, 22, 65, 75<br />
<br />
එක් එක් සිසුවා විෂය තුන සඳහා ලබාගත් ලකුණුවල එකතුව හා මධ්යන්යය (Mean) පහත දැක්වෙන ආකාරයට ආදානය කළ යුතු යැයි සලකන්න.<br />
<br />
1. Nimal 60 20.0<br />
2. Saman 145 48.3<br />
3. Mala 162 54.0<br />
<br />
මේ සඳහා නිර්මාණය කළ අසම්පූර්ණ පයිතන් කේතයක් පහත දැක්වේ. මෙම කේතයේ සම්පූර්ණ ස්ථාන කඩ ඉරි මගින් දක්වා ඇත.<br />
<br />
def total(marks):<br />
#compute the total<br />
total=0<br />
for mark in marks<br />
_ _ _ _ _ _ _ _ _<br />
return total<br />
<br />
f=open("inpit.text")<br />
line=f.readline()<br />
i= _ _ _ _ _ _ _ _ _ _<br />
while(line ='')<br />
record=line.strip('\n').split(",")<br />
aggregate=total_ _ _ _ _ _ _ _ _ _<br />
print i,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _<br />
i +=1<br />
line = _ _ _ _ _ _ _ _ _ <br />
f.close()<br />
<br />
ඉහත කේතයට අදාළ ප්රතිදානය ලබාගැනීම සඳහා සුදුසු ලෙස සම්පූර්ණ කරන්න.<br />
<br />
<span style="color: blue;">def total(marks): #compute the total</span><br style="color: blue;" /><span style="color: blue;"> total=0</span><br style="color: blue;" /><span style="color: blue;"> for mark in marks</span><br style="color: blue;" /><span style="color: blue;"> total=total+marks</span><br style="color: blue;" /><span style="color: blue;">return total</span><br style="color: blue;" /><br style="color: blue;" /><span style="color: blue;">f=open("input.text")</span><br style="color: blue;" /><span style="color: blue;">line=f.readline()</span><br style="color: blue;" /><span style="color: blue;">i=1</span><br style="color: blue;" /><span style="color: blue;">while(line <> '') :</span><br style="color: blue;" /><span style="color: blue;"> record=line.split(",")</span><br style="color: blue;" /><span style="color: blue;"> total=record[1]+record[2] + record[3]</span><br style="color: blue;" /><span style="color: blue;"> aggregate=total/3 </span><br style="color: blue;" /><span style="color: blue;"> print ("%. %s %d, f%") % (i,record[1], total, aggregate)</span><br style="color: blue;" /><span style="color: blue;"> i +=1</span><br style="color: blue;" /><span style="color: blue;"> line =line=f.readline()</span><br style="color: blue;" /><span style="color: blue;">f.close()</span><br />
<br />
<br />
=====<br />
<br />
The Mean and the Median<br />
<br />
100, 100, 130, 140, 150<br />
<br />
To find the median, arrange values from smallest to largest. <br />
<br />
If there is an odd number of values, the median is the middle value. If there is an even number of values, the median is the average of the two middle values. <br />
<br />
130 is the middle value. <br />
<br />
<br />
The mean is computed by adding all of the observations and dividing by the number of observations. <br />
<br />
(100 + 100 + 130 + 140 + 150)/5 = 620/5 = 124<br />
<br />
Population mean = μ = ΣX / N OR <br />
Sample mean = x = Σx / n <br />
<br />
<br />
Four friends take an IQ test. Their scores are 96, 100, 106, 114. Which of the following statements is true?<br />
<br />
I. The mean is 103. <br />
II. The mean is 104. <br />
III. The median is 100. <br />
IV. The median is 106. <br />
<br />
(A) I only <br />
(B) II only <br />
(C) III only <br />
(D) IV only <br />
(E) None is true <br />
<br />
Solution<br />
<br />
The correct answer is (B). <br />
<br />
Mean score = Σx / n = (96 + 100 + 106 + 114) / 4 = 104<br />
<br />
Since there are an even number of scores (4), the median is the average of the two middle scores. Thus, the median is (100 + 106) / 2 = 103. <br />
------------------------------------------<br />
<br />
<a href="http://stattrek.com/lesson1/formulas.aspx">http://stattrek.com/lesson1/formulas.aspx</a>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-49606368590681736182011-07-25T19:55:00.000-07:002011-07-25T19:55:57.982-07:00Code Correctionපහත පයිතන් ක්රමලේඛ අතුරින් දෝෂ රහිත ක්රමලේඛ මොනවාදැයි දක්වන්න. දෝෂ රහිත ක්රමලේඛ ක්රියාත්මක කිරීමේදී ලැබෙන ප්රතිදාන මොනවාදැයි දක්වන්න.<br />
<br />
<br />
දෝෂ සහිත ක්රමලේඛ නිදොස් කර දක්වා, එම නිදොස් ක්රමලේඛ ක්රියාත්මක කිරීමේදී ලැබෙන ප්රතිදාන මොනවාදැයි සඳහන් කරන්න.<br />
<br />
animals=['Dog','Rat','Cat']<br />
animals.sort()<br />
for animal in animals:<br />
print i,animal.strip(),len(animal)<br />
i t=1<br />
<br />
<span style="color: blue;">animals=['Dog','Rat','Cat']</span><br style="color: blue;" /><span style="color: blue;">animals.sort()</span><br style="color: blue;" /><span style="color: blue;">for animal in animals:</span><br style="color: blue;" /><span style="color: blue;"> i=len(animal)</span><br style="color: blue;" /><span style="color: blue;"> t=animal.strip(),</span><br style="color: blue;" /><span style="color: blue;"> print (t)</span><br />
<br />
<br />
<br />
def times(a):<br />
for i in range(1,12):<br />
print a,' x ', i,' = ',a*i<br />
times(5)<br />
<br />
<br />
<br style="color: blue;" /><span style="color: blue;">def times(a):</span><br style="color: blue;" /><span style="color: blue;"> for i in range(1,12):</span><br style="color: blue;" /><span style="color: blue;"> print (a, i,' = ',a*i)</span><br style="color: blue;" /><br style="color: blue;" /><span style="color: blue;">times(5)</span><br />
<br />
===<br />
<br />
<h1><a href="" name="SECTION006100000000000000000"><tt class="module">string</tt> -- Common string operations</a> </h1><br />
This module defines some constants useful for checking character classes and some useful string functions. See the module <tt class="module"><a href="http://www.blogger.com/module-re.html">re</a></tt><a href="" name="l2h-828"> </a>for string functions based on regular expressions. <br />
The constants defined in this module are: <br />
<br />
<dl><dt><b><a href="" name="l2h-783"><tt>ascii_letters</tt></a></b> </dt>
<dd>The concatenation of the <tt class="constant">ascii_lowercase</tt> and <tt class="constant">ascii_uppercase</tt> constants described below. This value is not locale-dependent. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-784"><tt>ascii_lowercase</tt></a></b> </dt>
<dd>The lowercase letters <code>'abcdefghijklmnopqrstuvwxyz'</code>. This value is not locale-dependent and will not change. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-785"><tt>ascii_uppercase</tt></a></b> </dt>
<dd>The uppercase letters <code>'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</code>. This value is not locale-dependent and will not change. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-786"><tt>digits</tt></a></b> </dt>
<dd>The string <code>'0123456789'</code>. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-787"><tt>hexdigits</tt></a></b> </dt>
<dd>The string <code>'0123456789abcdefABCDEF'</code>. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-788"><tt>letters</tt></a></b> </dt>
<dd>The concatenation of the strings <tt class="constant">lowercase</tt> and <tt class="constant">uppercase</tt> described below. The specific value is locale-dependent, and will be updated when <tt class="function">locale.setlocale()</tt> is called. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-789"><tt>lowercase</tt></a></b> </dt>
<dd>A string containing all the characters that are considered lowercase letters. On most systems this is the string <code>'abcdefghijklmnopqrstuvwxyz'</code>. Do not change its definition -- the effect on the routines <tt class="function">upper()</tt> and <tt class="function">swapcase()</tt> is undefined. The specific value is locale-dependent, and will be updated when <tt class="function">locale.setlocale()</tt> is called. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-790"><tt>octdigits</tt></a></b> </dt>
<dd>The string <code>'01234567'</code>. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-791"><tt>punctuation</tt></a></b> </dt>
<dd>String of ASCII characters which are considered punctuation characters in the "<tt class="samp">C</tt>" locale. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-792"><tt>printable</tt></a></b> </dt>
<dd>String of characters which are considered printable. This is a combination of <tt class="constant">digits</tt>, <tt class="constant">letters</tt>, <tt class="constant">punctuation</tt>, and <tt class="constant">whitespace</tt>. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-793"><tt>uppercase</tt></a></b> </dt>
<dd>A string containing all the characters that are considered uppercase letters. On most systems this is the string <code>'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</code>. Do not change its definition -- the effect on the routines <tt class="function">lower()</tt> and <tt class="function">swapcase()</tt> is undefined. The specific value is locale-dependent, and will be updated when <tt class="function">locale.setlocale()</tt> is called. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-794"><tt>whitespace</tt></a></b> </dt>
<dd>A string containing all characters that are considered whitespace. On most systems this includes the characters space, tab, linefeed, return, formfeed, and vertical tab. Do not change its definition -- the effect on the routines <tt class="function">strip()</tt> and <tt class="function">split()</tt> is undefined. </dd></dl>Many of the functions provided by this module are also defined as methods of string and Unicode objects; see ``String Methods'' (section <a href="http://www.blogger.com/string-methods.html#string-methods">2.2.6</a>) for more information on those. The functions defined in this module are: <br />
<br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-795"><tt class="function">atof</tt></a></b>(</nobr></td> <td><var>s</var>)</td></tr>
</tbody></table></dt>
<dd> <div class="versionnote"><b>Deprecated since release 2.0.</b> Use the <tt class="function">float()</tt> built-in function.</div>Convert a string to a floating point number. The string must have the standard syntax for a floating point literal in Python, optionally preceded by a sign ("<tt class="samp">+</tt>" or "<tt class="samp">-</tt>"). Note that this behaves identical to the built-in function <tt class="function">float()</tt><a href="" name="l2h-796"> </a> when passed a string.
<span class="note"><b class="label">Note:</b> When passing in a string, values for NaN<a href="" name="l2h-797"> </a> and Infinity<a href="" name="l2h-798"> </a>may be returned, depending on the underlying C library. The specific set of strings accepted which cause these values to be returned depends entirely on the C library and is known to vary.</span> </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-799"><tt class="function">atoi</tt></a></b>(</nobr></td> <td><var>s</var><big>[</big><var>, base</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd> <div class="versionnote"><b>Deprecated since release 2.0.</b> Use the <tt class="function">int()</tt> built-in function.</div>Convert string <var>s</var> to an integer in the given <var>base</var>. The string must consist of one or more digits, optionally preceded by a sign ("<tt class="samp">+</tt>" or "<tt class="samp">-</tt>"). The <var>base</var> defaults to 10. If it is 0, a default base is chosen depending on the leading characters of the string (after stripping the sign): "<tt class="samp">0x</tt>" or "<tt class="samp">0X</tt>" means 16, "<tt class="samp">0</tt>" means 8, anything else means 10. If <var>base</var> is 16, a leading "<tt class="samp">0x</tt>" or "<tt class="samp">0X</tt>" is always accepted, though not required. This behaves identically to the built-in function <tt class="function">int()</tt> when passed a string. (Also note: for a more flexible interpretation of numeric literals, use the built-in function <tt class="function">eval()</tt><a href="" name="l2h-800"> </a>.) </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-801"><tt class="function">atol</tt></a></b>(</nobr></td> <td><var>s</var><big>[</big><var>, base</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd> <div class="versionnote"><b>Deprecated since release 2.0.</b> Use the <tt class="function">long()</tt> built-in function.</div>Convert string <var>s</var> to a long integer in the given <var>base</var>. The string must consist of one or more digits, optionally preceded by a sign ("<tt class="samp">+</tt>" or "<tt class="samp">-</tt>"). The <var>base</var> argument has the same meaning as for <tt class="function">atoi()</tt>. A trailing "<tt class="samp">l</tt>" or "<tt class="samp">L</tt>" is not allowed, except if the base is 0. Note that when invoked without <var>base</var> or with <var>base</var> set to 10, this behaves identical to the built-in function <tt class="function">long()</tt><a href="" name="l2h-802"> </a> when passed a string. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-803"><tt class="function">capitalize</tt></a></b>(</nobr></td> <td><var>word</var>)</td></tr>
</tbody></table></dt>
<dd>Return a copy of <var>word</var> with only its first character capitalized. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-804"><tt class="function">capwords</tt></a></b>(</nobr></td> <td><var>s</var>)</td></tr>
</tbody></table></dt>
<dd>Split the argument into words using <tt class="function">split()</tt>, capitalize each word using <tt class="function">capitalize()</tt>, and join the capitalized words using <tt class="function">join()</tt>. Note that this replaces runs of whitespace characters by a single space, and removes leading and trailing whitespace. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-805"><tt class="function">expandtabs</tt></a></b>(</nobr></td> <td><var>s</var><big>[</big><var>, tabsize</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Expand tabs in a string, i.e. replace them by one or more spaces, depending on the current column and the given tab size. The column number is reset to zero after each newline occurring in the string. This doesn't understand other non-printing characters or escape sequences. The tab size defaults to 8. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-806"><tt class="function">find</tt></a></b>(</nobr></td> <td><var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>,end</var><big>]</big><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Return the lowest index in <var>s</var> where the substring <var>sub</var> is found such that <var>sub</var> is wholly contained in <code><var>s</var>[<var>start</var>:<var>end</var>]</code>. Return <code>-1</code> on failure. Defaults for <var>start</var> and <var>end</var> and interpretation of negative values is the same as for slices. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-807"><tt class="function">rfind</tt></a></b>(</nobr></td> <td><var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Like <tt class="function">find()</tt> but find the highest index. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-808"><tt class="function">index</tt></a></b>(</nobr></td> <td><var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Like <tt class="function">find()</tt> but raise <tt class="exception">ValueError</tt> when the substring is not found. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-809"><tt class="function">rindex</tt></a></b>(</nobr></td> <td><var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Like <tt class="function">rfind()</tt> but raise <tt class="exception">ValueError</tt> when the substring is not found. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-810"><tt class="function">count</tt></a></b>(</nobr></td> <td><var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Return the number of (non-overlapping) occurrences of substring <var>sub</var> in string <code><var>s</var>[<var>start</var>:<var>end</var>]</code>. Defaults for <var>start</var> and <var>end</var> and interpretation of negative values are the same as for slices. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-811"><tt class="function">lower</tt></a></b>(</nobr></td> <td><var>s</var>)</td></tr>
</tbody></table></dt>
<dd>Return a copy of <var>s</var>, but with upper case letters converted to lower case. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-812"><tt class="function">maketrans</tt></a></b>(</nobr></td> <td><var>from, to</var>)</td></tr>
</tbody></table></dt>
<dd>Return a translation table suitable for passing to <tt class="function">translate()</tt> or <tt class="function">regex.compile()</tt>, that will map each character in <var>from</var> into the character at the same position in <var>to</var>; <var>from</var> and <var>to</var> must have the same length. <span class="warning"><b class="label">Warning:</b> Don't use strings derived from <tt class="constant">lowercase</tt> and <tt class="constant">uppercase</tt> as arguments; in some locales, these don't have the same length. For case conversions, always use <tt class="function">lower()</tt> and <tt class="function">upper()</tt>.</span> </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-813"><tt class="function">split</tt></a></b>(</nobr></td> <td><var>s</var><big>[</big><var>, sep</var><big>[</big><var>, maxsplit</var><big>]</big><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Return a list of the words of the string <var>s</var>. If the optional second argument <var>sep</var> is absent or <code>None</code>, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument <var>sep</var> is present and not <code>None</code>, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. The optional third argument <var>maxsplit</var> defaults to 0. If it is nonzero, at most <var>maxsplit</var> number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most <code><var>maxsplit</var>+1</code> elements). </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-814"><tt class="function">splitfields</tt></a></b>(</nobr></td> <td><var>s</var><big>[</big><var>, sep</var><big>[</big><var>, maxsplit</var><big>]</big><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>This function behaves identically to <tt class="function">split()</tt>. (In the past, <tt class="function">split()</tt> was only used with one argument, while <tt class="function">splitfields()</tt> was only used with two arguments.) </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-815"><tt class="function">join</tt></a></b>(</nobr></td> <td><var>words</var><big>[</big><var>, sep</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Concatenate a list or tuple of words with intervening occurrences of <var>sep</var>. The default value for <var>sep</var> is a single space character. It is always true that "<tt class="samp">string.join(string.split(<var>s</var>, <var>sep</var>), <var>sep</var>)</tt>" equals <var>s</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-816"><tt class="function">joinfields</tt></a></b>(</nobr></td> <td><var>words</var><big>[</big><var>, sep</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>This function behaves identically to <tt class="function">join()</tt>. (In the past, <tt class="function">join()</tt> was only used with one argument, while <tt class="function">joinfields()</tt> was only used with two arguments.) Note that there is no <tt class="method">joinfields()</tt> method on string objects; use the <tt class="method">join()</tt> method instead. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-817"><tt class="function">lstrip</tt></a></b>(</nobr></td> <td><var>s</var><big>[</big><var>, chars</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Return a copy of the string with leading characters removed. If <var>chars</var> is omitted or <code>None</code>, whitespace characters are removed. If given and not <code>None</code>, <var>chars</var> must be a string; the characters in the string will be stripped from the beginning of the string this method is called on. <span class="versionnote">Changed in version 2.2.3: The <var>chars</var> parameter was added. The <var>chars</var> parameter cannot be passed in earlier 2.2 versions.</span> </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-818"><tt class="function">rstrip</tt></a></b>(</nobr></td> <td><var>s</var><big>[</big><var>, chars</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Return a copy of the string with trailing characters removed. If <var>chars</var> is omitted or <code>None</code>, whitespace characters are removed. If given and not <code>None</code>, <var>chars</var> must be a string; the characters in the string will be stripped from the end of the string this method is called on. <span class="versionnote">Changed in version 2.2.3: The <var>chars</var> parameter was added. The <var>chars</var> parameter cannot be passed in 2.2 versions.</span> </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-819"><tt class="function">strip</tt></a></b>(</nobr></td> <td><var>s</var><big>[</big><var>, chars</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Return a copy of the string with leading and trailing characters removed. If <var>chars</var> is omitted or <code>None</code>, whitespace characters are removed. If given and not <code>None</code>, <var>chars</var> must be a string; the characters in the string will be stripped from the both ends of the string this method is called on. <span class="versionnote">Changed in version 2.2.3: The <var>chars</var> parameter was added. The <var>chars</var> parameter cannot be passed in earlier 2.2 versions.</span> </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-820"><tt class="function">swapcase</tt></a></b>(</nobr></td> <td><var>s</var>)</td></tr>
</tbody></table></dt>
<dd>Return a copy of <var>s</var>, but with lower case letters converted to upper case and vice versa. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-821"><tt class="function">translate</tt></a></b>(</nobr></td> <td><var>s, table</var><big>[</big><var>, deletechars</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Delete all characters from <var>s</var> that are in <var>deletechars</var> (if present), and then translate the characters using <var>table</var>, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-822"><tt class="function">upper</tt></a></b>(</nobr></td> <td><var>s</var>)</td></tr>
</tbody></table></dt>
<dd>Return a copy of <var>s</var>, but with lower case letters converted to upper case. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-823"><tt class="function">ljust</tt></a></b>(</nobr></td> <td><var>s, width</var>)</td></tr>
</tbody></table></dt>
<dd> </dd>
<dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><tt class="function">rjust</tt></b>(</nobr></td> <td><var>s, width</var>)</td></tr>
</tbody></table></dt>
<dd> </dd>
<dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><tt class="function">center</tt></b>(</nobr></td> <td><var>s, width</var>)</td></tr>
</tbody></table></dt>
<dd>These functions respectively left-justify, right-justify and center a string in a field of given width. They return a string that is at least <var>width</var> characters wide, created by padding the string <var>s</var> with spaces until the given width on the right, left or both sides. The string is never truncated. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-826"><tt class="function">zfill</tt></a></b>(</nobr></td> <td><var>s, width</var>)</td></tr>
</tbody></table></dt>
<dd>Pad a numeric string on the left with zero digits until the given width is reached. Strings starting with a sign are handled correctly. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-827"><tt class="function">replace</tt></a></b>(</nobr></td> <td><var>str, old, new</var><big>[</big><var>, maxsplit</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Return a copy of string <var>str</var> with all occurrences of substring <var>old</var> replaced by <var>new</var>. If the optional argument <var>maxsplit</var> is given, the first <var>maxsplit</var> occurrences are replaced.
<h1><a href="" name="SECTION007500000000000000000"><tt class="module">math</tt> -- Mathematical functions</a> </h1>
This module is always available. It provides access to the mathematical functions defined by the C standard.
These functions cannot be used with complex numbers; use the functions of the same name from the <tt class="module"><a href="http://www.blogger.com/module-cmath.html">cmath</a></tt> module if you require support for complex numbers. The distinction between functions which support complex numbers and those which don't is made since most users do not want to learn quite as much mathematics as required to understand complex numbers. Receiving an exception instead of a complex result allows earlier detection of the unexpected complex number used as a parameter, so that the programmer can determine how and why it was generated in the first place.
The following functions are provided by this module. Except when explicitly noted otherwise, all return values are floats:
</dd>
<dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1093"><tt class="function">acos</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the arc cosine of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1094"><tt class="function">asin</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the arc sine of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1095"><tt class="function">atan</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the arc tangent of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1096"><tt class="function">atan2</tt></a></b>(</nobr></td> <td><var>y, x</var>)</td></tr>
</tbody></table></dt>
<dd>Return <code>atan(<var>y</var> / <var>x</var>)</code>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1097"><tt class="function">ceil</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the ceiling of <var>x</var> as a float. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1098"><tt class="function">cos</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the cosine of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1099"><tt class="function">cosh</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the hyperbolic cosine of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1100"><tt class="function">degrees</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Converts angle <var>x</var> from radians to degrees. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1101"><tt class="function">exp</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return <code>e**<var>x</var></code>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1102"><tt class="function">fabs</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the absolute value of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1103"><tt class="function">floor</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the floor of <var>x</var> as a float. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1104"><tt class="function">fmod</tt></a></b>(</nobr></td> <td><var>x, y</var>)</td></tr>
</tbody></table></dt>
<dd>Return <code>fmod(<var>x</var>, <var>y</var>)</code>, as defined by the platform C library. Note that the Python expression <code><var>x</var> % <var>y</var></code> may not return the same result. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1105"><tt class="function">frexp</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the mantissa and exponent of <var>x</var> as the pair <code>(<var>m</var>, <var>e</var>)</code>. <var>m</var> is a float and <var>e</var> is an integer such that <code><var>x</var> == <var>m</var> * 2**<var>e</var></code>. If <var>x</var> is zero, returns <code>(0.0, 0)</code>, otherwise <code>0.5 <= abs(<var>m</var>) < 1</code>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1106"><tt class="function">hypot</tt></a></b>(</nobr></td> <td><var>x, y</var>)</td></tr>
</tbody></table></dt>
<dd>Return the Euclidean distance, <code>sqrt(<var>x</var>*<var>x</var> + <var>y</var>*<var>y</var>)</code>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1107"><tt class="function">ldexp</tt></a></b>(</nobr></td> <td><var>x, i</var>)</td></tr>
</tbody></table></dt>
<dd>Return <code><var>x</var> * (2**<var>i</var>)</code>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1108"><tt class="function">log</tt></a></b>(</nobr></td> <td><var>x</var><big>[</big><var>, base</var><big>]</big>)</td></tr>
</tbody></table></dt>
<dd>Returns the logarithm of <var>x</var> to the given <var>base</var>. If the <var>base</var> is not specified, returns the natural logarithm of <var>x</var>. <span class="versionnote">Changed in version 2.3: <var>base</var> argument added.</span> </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1109"><tt class="function">log10</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the base-10 logarithm of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1110"><tt class="function">modf</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the fractional and integer parts of <var>x</var>. Both results carry the sign of <var>x</var>. The integer part is returned as a float. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1111"><tt class="function">pow</tt></a></b>(</nobr></td> <td><var>x, y</var>)</td></tr>
</tbody></table></dt>
<dd>Return <code><var>x</var>**<var>y</var></code>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1112"><tt class="function">radians</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Converts angle <var>x</var> from degrees to radians. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1113"><tt class="function">sin</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the sine of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1114"><tt class="function">sinh</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the hyperbolic sine of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1115"><tt class="function">sqrt</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the square root of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1116"><tt class="function">tan</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the tangent of <var>x</var>. </dd></dl><br />
<dl><dt> <table cellpadding="0" cellspacing="0"><tbody>
<tr valign="baseline"> <td><nobr><b><a href="" name="l2h-1117"><tt class="function">tanh</tt></a></b>(</nobr></td> <td><var>x</var>)</td></tr>
</tbody></table></dt>
<dd>Return the hyperbolic tangent of <var>x</var>. </dd></dl>Note that <tt class="function">frexp()</tt> and <tt class="function">modf()</tt> have a different call/return pattern than their C equivalents: they take a single argument and return a pair of values, rather than returning their second return value through an `output parameter' (there is no such thing in Python). <br />
The module also defines two mathematical constants: <br />
<br />
<dl><dt><b><a href="" name="l2h-1118"><tt>pi</tt></a></b> </dt>
<dd>The mathematical constant <i>pi</i>. </dd></dl><br />
<dl><dt><b><a href="" name="l2h-1119"><tt>e</tt></a></b> </dt>
<dd>The mathematical constant <i>e</i>. </dd></dl><dl><dd>
</dd></dl>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-318662071058626202011-07-25T19:23:00.000-07:002011-07-25T19:33:11.260-07:00Interpreters and Compilers Differencesභාෂා සම්පාදකවල (Compilers) හා අර්ථ වින්යාසකවල(Interpreters) මූලික කාර්යය කුමක්දැයි දක්වා, ඒවා භාවිතයේදී ලැබෙන වාසි සහ අවාසි සන්සන්දනය කරන්න.<br />
<br />
Compiler vs Interpreter<br />
Both basically serve the same purpose. <br />
Convert one level of language to another level. <br />
<br />
Compiler converts the high level instructions into machine language.<br />
<br />
Interpreter converts the high level instruction into an intermediate form, and the instructions are executed.<br />
<br />
Compiler<br />
Convert to binary form understood by the computer. <br />
<br />
The complier programs are evolving in this way which improves their ease of use.<br />
<br />
Compliers can be multiple or multistage pass. The first pass can convert the high level language into a language that is closer to computer language. Then the further passes can convert it into final stage for the purpose of execution.<br />
<br />
The advantage of using an interpreter is that the high level instruction does not goes through compilation stage which can be a time consuming method. <br />
<br />
Almost all high level programming languages have compilers and interpreters. <br />
<br />
Difference between compiler and interpreter<br />
<br />
• A complier converts the high level instruction into machine language <br />
• Interpreter converts the high level instruction into an intermediate form.<br />
<br />
• Before execution, entire program is executed by the compiler<br />
<br />
interpreter translates a line then executes it and so on.<br />
<br />
• List of errors is created by the compiler after the compilation process <br />
<br />
• interpreter stops translating after the first error.<br />
<br />
• Compiler creates an independent executable file<br />
<br />
• interpreter is required by an interpreted program each time.<br />
<br />
Assembler<br />
<br />
Dual Purpose<br />
Java<br />
C++<br />
Python<br />
VB<br />
ASP.NET<br />
<br />
Interpreter<br />
PHP<br />
ASP<br />
Java ScriptNiranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com2tag:blogger.com,1999:blog-1856598783778738395.post-91478961263735243022011-07-25T18:53:00.000-07:002011-07-25T19:17:47.772-07:00Flow Chart to Python Code<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpmNtqbDgWEPMGIeNVRCL50vHdjbbaznAKOrPyyZGSn-n4uPUZKmbrNOVkEapJ60Zm6AdtAFdvjRawYuGrTijDckr2kvWL4jvDM7h7dUmM6FPmQDNuKGmJpWws0ee4f_Q9zbU4K4K2og32/s1600/a.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpmNtqbDgWEPMGIeNVRCL50vHdjbbaznAKOrPyyZGSn-n4uPUZKmbrNOVkEapJ60Zm6AdtAFdvjRawYuGrTijDckr2kvWL4jvDM7h7dUmM6FPmQDNuKGmJpWws0ee4f_Q9zbU4K4K2og32/s1600/a.png" /></a></div>(python)<br />
x=1<br />
total=0<br />
while x<=10 :<br />
math=input("enter marks")<br />
x=x+1<br />
total=total+math<br />
<br />
avg=total/x-1<br />
if (avg > 50):<br />
print "Good"<br />
else:<br />
print "Bad"<br />
<br />
<br />
<span style="font-size: large;">Python Code</span><br />
<br />
x=1<br />
<br />
total=0<br />
while x<=10 :<br />
xmath=<span style="color: red;">int(input("enter marks"))</span><br />
type(xmath)<br />
x=x+1<br />
total=total+xmath<br />
<br />
avg=(total/x-1)<br />
<br />
if (avg > 50):<br />
print ("Good")<br />
else:<br />
print ("Bad")<br />
<br />
======================<br />
<br />
ffor x in "python-programming":<br />
print(x)<br />
if x=='-':<br />
break<br />
print x<br />
<br />
What is your explanation - will this work ? on indentinng<br />
<br />
what are the output of following codes ?<br />
<br />
for x in "python-programming":<br />
print(x)<br />
if x=='-':<br />
break<br />
print (x)<br />
<br />
for x in "python-programming":<br />
print(x)<br />
if x=='-':<br />
continue<br />
print (x)Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com1tag:blogger.com,1999:blog-1856598783778738395.post-84568773326032796122011-07-25T18:21:00.000-07:002011-07-25T18:33:36.028-07:00පහත දැක්වෙන පයිතන් ක්රමලේඛය සලකා බලන්න:පහත දැක්වෙන පයිතන් ක්රමලේඛය සලකා බලන්න:<br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">from date import date</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">class dtcheck:</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">def_init_(self,y,m,d):</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"> self.now=date.today()</span> <br />
<span style="font-family: Arial,Helvetica,sans-serif;"> self.dob=date(y,m,d)</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"> self.age=self.now - self.dob</span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">def printage(self):</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"> return "your age %d" % ((self.age.days)/365)</span><br />
<br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">මෙහි දැක්වෙන්නේ උපන් දිනය ලබාදීමෙන් පසු වයස අවුරුදුවලින් ප්රතිපානය කිරීම සඳහා සැකසූ පන්ති මොඩියුලයකි. මෙම මොඩියුලය "dcheck.py" යන ලිපිගොනු නාමයෙන් පයිතන් ක්රමලේඛයක් ලෙස සුරැකී ඇත. මෙම පන්ති මොඩියුලයෙන් වස්තුවක් (object) නිර්මාණය කර, එයට ආදානය ලබාදෙමින් ප්රතිදානය ලබාගැනීමට භාවිත කළ හැකි නිවැරදිථ පයිතන් මොඩියුලය කුමක් ද?</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><br />
<br />
import dcheck<br />
w=dcheck.dtcheck(1976,11,10)<br />
result=w.printage()<br />
print w</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"> </span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">import dcheck<br />
w.dcheck(1976/11/10)<br />
result=w.printage()<br />
print result<br />
</span><br />
<div style="color: blue;"><span style="font-family: Arial,Helvetica,sans-serif;">import dcheck<br />
u=dcheck.dtcheck(1976,11,10)<br />
print u.printage()</span></div><span style="font-family: Arial,Helvetica,sans-serif;"> </span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">import dcheck<br />
w.dtcheck.dcheck(1976,11,10)<br />
result=w.printage()<br />
print result</span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">w=dcheck.dcheck(1976/11/10)<br />
result=w.printage()<br />
print result </span><br />
<br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;"><b>Python 3.x code<br />
<br />
from datetime import date<br />
<br />
<br />
class dtcheck:<br />
def __init__(self,y,m,d):<br />
self.now=date.today()<br />
self.dob=date(y,m,d)<br />
self.age=self.now - self.dob<br />
<br />
def printage(self):<br />
return "your age %d"%((self.age.days)/365)<br />
</b></span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><b>import dcheck<br />
u=dcheck.dtcheck(1976,11,10)<br />
print (u.printage())</b></span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><b></b></span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><b></b></span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><b><br />
<br />
================</b></span><br />
<h1>The <i>import</i> Statement:</h1>You can use any Python source file as a module by executing an import statement in some other Python source file. <i>import</i> has the following syntax:<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre>import module1[, module2[,... moduleN]</pre></td></tr>
</tbody></table><br />
# Import module hello<br />
import hello # Now you can call defined function<br />
<br />
hello.print_func("Dinuka")<br />
<br />
Hello : Dinuka<br />
<br />
<h1>The <i>from...import *</i> Statement:</h1>It is also possible to import all names from a module <br />
<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre>from modname import *</pre></td></tr>
</tbody></table><br />
<h2>The <i>PYTHONPATH</i> Variable:</h2>The PYTHONPATH is an environment variable, consist list of directories. <br />
<br />
<table cellpadding="5" class="src"><tbody>
<tr><td><pre>set PYTHONPATH=c:\python20\lib;</pre></td></tr>
</tbody></table>UNIX system:<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre>set PYTHONPATH=/usr/local/lib/python</pre></td></tr>
</tbody></table><br />
#!/usr/bin/python <br />
<br />
=================<br />
<br />
<h1>Namespaces and Scoping:</h1>Variables are names map to objects. <br />
<br />
A <i>namespace</i> is a dictionary of variable names (keys) and their corresponding objects (values).<br />
<br />
Python can access variables in a <i>local namespace</i> and in the <i>global namespace</i>. I<br />
<br />
if a local and a global variable have the same name, the local variable shadows the global variable.<br />
<br />
Each function has its own local namespace. <br />
<br />
Python makes guesses on whether variables are local or global.<br />
<br />
It assumes that any variable assigned a value in a function is local.<br />
<br />
To assign a value to a global variable within a function, <br />
<br />
use <i>global VarName</i> which tells Python that VarName is a global variable. <br />
<br />
<br />
<table cellpadding="5" class="src"><tbody>
<tr><td><pre>#!/usr/bin/python
Honey = 2000
def AddHoney():
# Uncomment the following line to fix the code:
# global Honey
Honey = Honey + 1
print Honey
AddHoney()
print Honey</pre></td></tr>
</tbody></table><br />
<br />
what happens ? if # global Honey uncommented like<br />
<pre>global Honey
The Global variable get updated
</pre><pre>=================</pre><pre></pre><h1>Packages in Python:</h1>A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages,<br />
<br />
<h2>Example:</h2>Consider a file <i>Mobs.py</i> available in <i>Phone</i> directory. This file has following line of source code:<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre>#!/usr/bin/python
def Mobs():
print "I'm a mobile phone"</pre></td></tr>
</tbody></table>Similar way we have another two files having different functions with the same name as above:<br />
<ul><li><i>Phone/Isdn.py</i> file having function Isdn() </li>
<li><i>Phone/G3.py</i> file having function G3() </li>
</ul>Now create one more file __init__.py in <i>Phone</i> directory :<br />
<ul><li>Phone/__init__.py </li>
</ul>To make all of your functions available when you've imported Phone, you need to put explicit import statements in __init__.py as follows:<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre>from Pots import Pots
from Isdn import Isdn
from G3 import G3</pre></td></tr>
</tbody></table>After you've added these lines to __init__.py, you have all of these classes available when you've imported the Phone package:<br />
<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre>#!/usr/bin/python
# Now import your Phone Package.
import Phone
Phone.Mobs()
Phone.Isdn()
Phone.G3()</pre></td></tr>
</tbody></table>This would produce following result:<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre>I'm a mobile phone
I'm 3G Phone
I'm ISDN Phone</pre></td></tr>
</tbody></table><br />
<pre></pre>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-48226836167991342472011-07-25T17:19:00.000-07:002011-07-25T17:19:54.141-07:00Python Operators<h1>3 = 1+2<br />
in <i>1 + 2 =3</i> , 1 and 2 are called operands and + is called operator. </h1><ul><li> Arithmetic Operators +, - , /<br />
</li>
<li> Comparision Operators <><br />
</li>
<li> Logical (or Relational) Operators or and<br />
</li>
<li> Assignment Operators = <br />
</li>
<li> Conditional (or ternary) Operators </li>
</ul>Lets have a look on all operators one by one.<br />
<h1>Python Arithmetic Operators:</h1>Assume variable a holds 10 and variable b holds 20 then:<br />
<table border="1" cellpadding="5" cellspacing="0" class="src"><tbody>
<tr> <th width="10%">Operator</th> <th width="45%">Description</th> <th>Example</th></tr>
<tr> <td>+</td> <td>Addition - Adds values on either side of the operator</td> <td>a + b will give 30</td></tr>
<tr> <td>-</td> <td>Subtraction - Subtracts right hand operand from left hand operand</td> <td>a - b will give -10</td></tr>
<tr> <td>*</td> <td>Multiplication - Multiplies values on either side of the operator</td> <td>a * b will give 200</td></tr>
<tr> <td>/</td> <td>Division - Divides left hand operand by right hand operand</td> <td>b / a will give 2</td></tr>
<tr> <td>%</td> <td>Modulus - Divides left hand operand by right hand operand and returns remainder</td> <td>b % a will give 0</td></tr>
<tr> <td>**</td> <td>Exponent - Performs exponential (power) calculation on operators</td> <td>a**b will give 10 to the power 20</td></tr>
<tr> <td>//</td> <td>Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.</td> <td>9//2 is equal to 4 and 9.0//2.0 is equal to 4.0</td></tr>
</tbody></table><h1>Python Comparison Operators:</h1>Assume variable a holds 10 and variable b holds 20 then:<br />
<table border="1" cellpadding="5" cellspacing="0" class="src"><tbody>
<tr> <th width="10%">Operator</th> <th width="45%">Description</th> <th>Example</th></tr>
<tr> <td>==</td> <td>Checks if the value of two operands are equal or not, if yes then condition becomes true.</td> <td>(a == b) is not true. </td></tr>
<tr> <td>!=</td> <td>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.</td> <td>(a != b) is true. </td></tr>
<tr> <td><></td> <td>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.</td> <td>(a <> b) is true. This is similar to != operator.</td></tr>
<tr> <td>></td> <td>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.</td> <td>(a > b) is not true. </td></tr>
<tr> <td><</td> <td>Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.</td> <td>(a < b) is true. </td></tr>
<tr> <td>>=</td> <td>Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.</td> <td>(a >= b) is not true. </td></tr>
<tr> <td><=</td> <td>Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.</td> <td>(a <= b) is true. </td></tr>
</tbody></table><h1>Python Assignment Operators:</h1><br />
<br />
<b><span style="font-size: large;">Python Logical Operators:</span></b><br />
<br />
<br />
<table _base_href="http://www.tutorialspoint.com" border="1" cellpadding="5" cellspacing="0" class="src"><tbody _base_href="http://www.tutorialspoint.com">
<tr> <td>and</td> <td>Logical AND operator. <br />
If both the operands are true then condition becomes true.</td> <td>(a and b) is true.</td></tr>
<tr> <td>or</td> <td>Logical OR Operator. I<br />
f any of the two operands are non zero then then condition becomes true.</td> <td>(a or b) is true.</td></tr>
<tr> <td>not</td> <td>Logical NOT Operator. <br />
Use to reverses the logical state of its operand. <br />
If a condition is true then Logical NOT operator will make false.</td> <td>not(a and b) is false. </td></tr>
</tbody> </table><br />
<br />
<br />
<br />
<br />
<h1>Python Membership Operators:</h1><table _base_href="http://www.tutorialspoint.com" border="1" cellpadding="5" cellspacing="0" class="src"><tbody _base_href="http://www.tutorialspoint.com">
<tr> <th width="10%">Operator</th> <th width="45%">Description</th> <th>Example</th></tr>
<tr> <td>in</td> <td>Evaluates to true if it finds a variable in the specified sequence and false otherwise.</td> <td>x in y, here <b>in</b> results in a 1 if x is a member of sequence y.</td></tr>
<tr> <td>not in</td> <td>Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.</td> <td>x not in y, here <b>not in</b> results in a 1 if x is a member of sequence y.</td></tr>
</tbody> </table><h1>Python Assignment Operators:</h1>Assume variable a holds 10 and variable b holds 20 then:<br />
<table border="1" cellpadding="5" cellspacing="0" class="src"><tbody>
<tr> <th width="10%">Operator</th> <th width="40%">Description</th> <th>Example</th></tr>
<tr> <td>=</td> <td>Simple assignment operator, Assigns values from right side operands to left side operand</td> <td style="color: blue;"><b>c = a + b will assigne value of a + b into c</b></td></tr>
<tr> <td>+=</td> <td>Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand</td> <td style="color: blue;"><b>c += a is equivalent to c = c + a</b></td></tr>
<tr> <td>-=</td> <td>Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand</td> <td style="color: blue;"><b>c -= a is equivalent to c = c - a</b></td></tr>
<tr> <td>*=</td> <td>Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand</td> <td style="color: blue;"><b>c *= a is equivalent to c = c * a</b></td></tr>
<tr> <td>/=</td> <td>Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand</td> <td style="color: blue;"><b>c /= a is equivalent to c = c / a</b></td></tr>
<tr> <td>%=</td> <td>Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand</td> <td style="color: blue;"><b>c %= a is equivalent to c = c % a</b></td></tr>
<tr> <td>**=</td> <td>Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand</td> <td style="color: blue;"><b>c **= a is equivalent to c = c ** a</b></td></tr>
<tr> <td>//=</td> <td>Floor Dividion and assigns a value, Performs floor division on operators and assign value to the left operand</td> <td style="color: blue;"><b>c //= a is equivalent to c = c // a</b></td></tr>
</tbody> </table><h1>Python Comparison Operators:</h1>Assume variable a holds 10 and variable b holds 20 then:<br />
<table border="1" cellpadding="5" cellspacing="0" class="src"><tbody>
<tr> <th width="10%">Operator</th> <th width="45%">Description</th> <th>Example</th></tr>
<tr> <td>==</td> <td>Checks if the value of two operands are equal or not, if yes then condition becomes true.</td> <td>(a == b) is not true. </td></tr>
<tr> <td>!=</td> <td>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.</td> <td>(a != b) is true. </td></tr>
<tr> <td><></td> <td>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.</td> <td>(a <> b) is true. This is similar to != operator.</td></tr>
<tr> <td>></td> <td>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.</td> <td>(a > b) is not true. </td></tr>
<tr> <td><</td> <td>Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.</td> <td>(a < b) is true. </td></tr>
<tr> <td>>=</td> <td>Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.</td> <td>(a >= b) is not true. </td></tr>
<tr> <td><=</td> <td>Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.</td> <td>(a <= b) is true. </td></tr>
</tbody> </table><pre></pre><h1>Python Arithmetic Operators:</h1>Assume variable a holds 10 and variable b holds 20 then:<br />
<table border="1" cellpadding="5" cellspacing="0" class="src"><tbody>
<tr> <th width="10%">Operator</th> <th width="45%">Description</th> <th>Example</th></tr>
<tr> <td>+</td> <td>Addition - Adds values on either side of the operator</td> <td>a + b will give 30</td></tr>
<tr> <td>-</td> <td>Subtraction - Subtracts right hand operand from left hand operand</td> <td>a - b will give -10</td></tr>
<tr> <td>*</td> <td>Multiplication - Multiplies values on either side of the operator</td> <td>a * b will give 200</td></tr>
<tr> <td>/</td> <td>Division - Divides left hand operand by right hand operand</td> <td>b / a will give 2</td></tr>
<tr> <td>%</td> <td>Modulus - Divides left hand operand by right hand operand and returns remainder</td> <td>b % a will give 0</td></tr>
<tr> <td>**</td> <td>Exponent - Performs exponential (power) calculation on operators</td> <td>a**b will give 10 to the power 20</td></tr>
<tr> <td>//</td> <td>Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.</td> <td>9//2 is equal to 4 and 9.0//2.0 is equal to 4.0</td></tr>
</tbody> </table><pre> </pre><pre> </pre><h1>Python Bitwise Operators:</h1>Bitwise operator works on bits and perform bit by bit operation.<br />
Assume if a = 60; and b = 13; Now in binary format they will be as follows:<br />
a = 0011 1100<br />
b = 0000 1101<br />
-----------------<br />
a&b = 0000 1100<br />
a|b = 0011 1101<br />
a^b = 0011 0001<br />
~a = 1100 0011<br />
<br />
There are following Bitwise operators supported by Python language<br />
<table border="1" cellpadding="5" cellspacing="0" class="src"><tbody>
<tr> <th width="10%">Operator</th> <th width="45%">Description</th> <th>Example</th></tr>
<tr> <td>&</td> <td>Binary AND Operator copies a bit to the result if it exists in both operands. </td> <td>(a & b) will give 12 which is 0000 1100</td></tr>
<tr> <td>|</td> <td>Binary OR Operator copies a bit if it exists in eather operand. </td> <td>(a | b) will give 61 which is 0011 1101</td></tr>
<tr> <td>^</td> <td>Binary XOR Operator copies the bit if it is set in one operand but not both. </td> <td>(a ^ b) will give 49 which is 0011 0001</td></tr>
<tr> <td>~</td> <td>Binary Ones Complement Operator is unary and has the efect of 'flipping' bits. </td> <td>(~a ) will give -60 which is 1100 0011</td></tr>
<tr> <td><<</td> <td>Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. </td> <td>a << 2 will give 240 which is 1111 0000</td></tr>
<tr> <td>>></td> <td>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. </td> <td>a >> 2 will give 15 which is 0000 1111</td></tr>
</tbody></table>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-66879941807258608422011-07-25T17:04:00.000-07:002011-07-25T17:48:59.874-07:00Python syntax examplesපහත දැක්වෙන පයිතන් ප්රකාශ සලකා බලන්න: <br />
<br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">A - s="It's correct"</span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">B - x=y=z=2.5</span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">C - x,y,z=1,2.3,'string'</span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">D - #This is not correct</span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">E - a=['string',123]</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">F - b=5</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">while 4<=b>10:</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">b+=1</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">G - for x in range(0,12,3):</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">print x</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">H - a={'a':'b',4:40}</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">I - def fun1(a,b=4,l=[]):</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;">return b*a<br />
<br />
<br />
==================</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><br />
</span><br />
<h1>Reserved Words:</h1>The following list shows the reserved words in Python. These reserved words may not be used as constant or variable or any other identifier names.<br />
Keywords contain lowercase letters only.<br />
<table border="1" cellpadding="5" class="src"><tbody>
<tr> <td>and</td> <td>exec</td> <td>not</td></tr>
<tr> <td>assert</td> <td>finally</td> <td>or</td></tr>
<tr> <td>break</td> <td>for</td> <td>pass</td></tr>
<tr> <td>class</td> <td>from</td> <td>print</td></tr>
<tr> <td>continue</td> <td>global</td> <td>raise</td></tr>
<tr> <td>def</td> <td>if</td> <td>return</td></tr>
<tr> <td>del</td> <td>import</td> <td>try</td></tr>
<tr> <td>elif</td> <td>in</td> <td>while</td></tr>
<tr> <td>else</td> <td>is</td> <td>with </td></tr>
<tr> <td>except</td> <td>lambda</td> <td>yield</td></tr>
</tbody></table><br />
<br />
<br />
<h1>Multi-Line Statements:</h1>Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example:<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre></pre><pre>total = item_one + \
item_two + \
item_three</pre><pre></pre><pre></pre><pre>word = 'word'
sentence = "isn't this a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
paragraph = '''This is a paragraph. It is
made up of multiple lines and sentences.'''</pre></td></tr>
</tbody></table><br />
<br />
<br />
<pre># This is a comment.
# This is a comment, too.
raw_input("\n\nPress the enter key to exit.")</pre><pre>input("\n\nPress the enter key to exit.")</pre><pre>print("Press the enter key to exit") # Python 3.x
</pre><pre>print "Press the enter key to exit" # Python 2.x</pre><pre></pre><pre></pre><pre></pre><pre></pre><pre>var1 = 1</pre><pre>var2 = 10</pre><pre></pre><pre>del var1, var2</pre><pre>Strings</pre><pre>var1 = 'Hello World!'
var2 = "Python Programming"
print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
var1 = 'Hello World!'
print "Updated String :- ", var1[:6] + 'Python'
</pre><table _base_href="http://www.tutorialspoint.com" border="1" cellpadding="5" cellspacing="5" class="src"><tbody _base_href="http://www.tutorialspoint.com">
<tr _base_href="http://www.tutorialspoint.com"><td>\n</td> <td>0x0a</td> <td>Newline</td></tr>
</tbody></table><pre></pre>a holds 'Hello' and b holds 'Python' then:<br />
<br />
<table cellpadding="5" class="src"><tbody>
<tr> <td><pre> </pre><pre>print ("My name is %s and weight is %d kg!" % ('Sinali', 21)
</pre></td></tr>
</tbody></table>This will produce following result:<br />
<table border="1" cellpadding="5" cellspacing="5" class="src"><tbody>
<tr> <td><pre>My name is Zara and weight is 21 kg!</pre></td></tr>
</tbody></table><br />
<pre></pre><table _base_href="http://www.tutorialspoint.com" border="1" cellpadding="5" cellspacing="0" class="src"><tbody _base_href="http://www.tutorialspoint.com">
<tr> <th width="30%">Format Symbol</th> <th>Conversion</th></tr>
<tr> <td>%c</td> <td>character</td></tr>
<tr> <td><b>%s</b></td> <td>string conversion via str() prior to formatting</td></tr>
<tr> <td><b>%i</b></td> <td>signed decimal integer</td></tr>
<tr> <td><b>%d</b></td> <td>signed decimal integer</td></tr>
<tr> <td>%u</td> <td>unsigned decimal integer</td></tr>
<tr> <td><b>%o</b></td> <td>octal integer</td></tr>
<tr> <td><b>%x</b></td> <td>hexadecimal integer (lowercase letters)</td></tr>
<tr> <td>%X</td> <td>hexadecimal integer (UPPERcase letters)</td></tr>
<tr> <td>%e</td> <td>exponential notation (with lowercase 'e')</td></tr>
<tr> <td>%E</td> <td>exponential notation (with UPPERcase 'E')</td></tr>
<tr> <td><b>%f</b></td> <td>floating point real number</td></tr>
<tr> <td>%g</td> <td>the shorter of %f and %e</td></tr>
<tr> <td>%G</td> <td>the shorter of %f and %E</td></tr>
</tbody></table><pre></pre><h1>Unicode String:<br />
<br />
</h1><table cellpadding="5" cellspacing="5" class="src"><tbody>
<tr> <td><pre>print u'Hello, world!'</pre></td></tr>
</tbody></table>This would print following result:<br />
<table cellpadding="5" cellspacing="5" class="src"><tbody>
<tr> <td><pre>Hello, world!</pre></td></tr>
</tbody></table><h1><span style="font-size: small;">count = 0 </span></h1><h1><span style="font-size: small;">while (count < 9): <br />
print 'The count is:', count <br />
count = count + 1 <br />
print "Good bye!" </span></h1><h1><span style="font-size: small;"><br />
</span></h1><pre><span style="font-size: small;">for letter in 'Python': # First Example
if letter == 'h':
break
print 'Current Letter :', letter
var = 10 # Second Example
while var > 0:
print 'Current variable value :', var
var = var -1
if var == 5:
break
print "Good bye!"
a=</span><span style="font-size: small;">Hello
b=Python</span><span style="font-size: small;"> </span></pre><pre><span style="font-size: small;">
a + b will give HelloPython
a*2 will give -HelloHello
a[1] will give e
[1:4] will give ell
H in a will give 1
M not in a will give 1
print r'\n' prints \n and print R'\n' prints \n
</span></pre>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-45540428590807036962011-07-25T16:59:00.000-07:002011-07-25T17:15:37.582-07:00Python If then elseකොටුව තුළ දීඇති පයිතන් ක්රමලේඛයේ ප්රතිදානය කුමක් ද?<br />
<br />
j=10<br />
y=4<br />
if j<y or j!=4:<br />
j-=y<br />
print j<br />
else:<br />
y*=j<br />
print y<br />
<br />
(1) 4<br />
(2) 6<br />
(3) 10<br />
(4) 14<br />
(5) 40<br />
<br />
<b style="color: blue;">Python 3.0</b><br />
<br />
<span style="color: blue;">j=10</span><br />
<span style="color: blue;">y=4</span><br />
<span style="color: blue;">if ((j<y) or (j!=4)):</span><br />
<span style="color: blue;"> j-=y</span><br />
<span style="color: blue;"> print (j)</span><br />
<span style="color: blue;">else:</span><br />
<span style="color: blue;"> y*=j </span><br />
<span style="color: blue;"> print (y)<br />
<br />
<br />
==================</span><br />
<h2>if, elif, and else program branches</h2><pre>mark=float(raw_input("Please enter a mark"))
if mark >= 40:
print "You passed."
print "Well done."
else:
print "You failed."
print "too bad."</pre><pre></pre><pre></pre>-----------------<br />
<br />
<br />
<br />
<pre>age = 17
if age > 18:
print "Driving licence issued"
else:
print "Driving licence not permitted"
</pre><pre></pre><pre>if (age >= 12) or (age < 18)):
print "teen"</pre><pre>else :</pre><pre>if (age < 12) : </pre><pre>print "kid"
else:
print "adult</pre><pre></pre><pre><b></b></pre><pre><b></b></pre><pre><b>
Comparing data values
</b></pre>None The null object <br />
0 Integer zero 0<br />
0 Floating point zero <br />
"" Empty strings <br />
{} Empty dictionaries <br />
[] Empty lists <br />
() Empty tuples <br />
<br />
<pre>magic=input("Enter No ? ")
if not (magic == 8 or magic == 9 or magic ==10 ) :
print '"Time for Tea", said Boss '
else:
print '"No Tea !" said Boss'</pre><br />
<br />
<pre></pre>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-50634554647319161642011-07-25T16:13:00.000-07:002011-07-25T16:35:52.377-07:00Python Order of operationsr=11; y=2.5; c=4 වශයෙන් r, y, c විචල්යවලට අගය පවරා ඇත.<br />
<br />
ඒ අනුව, r%3*c+10/y යන පයිතන් ප්රකාශයේ නිවැරත්ථ අගය කුමක් ද?<br />
<br />
<br />
(1) 6.2 (2) 8.0 (3) 12.0 (4) 24.0 (5) 40.0<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div class="highlight"><pre><span class="o"> </span>
</pre></div><span style="font-size: large;"><br />
</span><br />
<table class="TableBody"><tbody>
<tr> <td class="TableContent" width="25%"><span style="font-size: large;"><b>command</b></span></td> <td class="TableContent" width="25%"><span style="font-size: large;"><b>name</b></span></td> <td class="TableContent" width="25%"><span style="font-size: large;"><b>example</b></span></td> <td class="TableContent" width="25%"><span style="font-size: large;"><b>output</b></span></td></tr>
<tr> <td class="TableContent"><span style="font-size: large;">+</span></td> <td class="TableContent"><span style="font-size: large;">Addition</span></td> <td class="TableContent"><span style="font-size: large;">4+5</span></td> <td class="TableContent"><span style="font-size: large;">9</span></td></tr>
<tr> <td class="TableContent"><span style="font-size: large;">-</span></td> <td class="TableContent"><span style="font-size: large;">Subtraction</span></td> <td class="TableContent"><span style="font-size: large;">8-5</span></td> <td class="TableContent"><span style="font-size: large;">3</span></td></tr>
<tr> <td class="TableContent"><span style="font-size: large;">*</span></td> <td class="TableContent"><span style="font-size: large;">Multiplication</span></td> <td class="TableContent"><span style="font-size: large;">4*5</span></td> <td class="TableContent"><span style="font-size: large;">20</span></td></tr>
<tr> <td class="TableContent"><span style="font-size: large;">/</span></td> <td class="TableContent"><span style="font-size: large;">Division</span></td> <td class="TableContent"><span style="font-size: large;">19/3</span></td> <td class="TableContent"><span style="font-size: large;">6</span></td></tr>
<tr> <td class="TableContent"><span style="font-size: large;">%</span></td> <td class="TableContent"><span style="font-size: large;">Remainder</span></td> <td class="TableContent"><span style="font-size: large;">19%3</span></td> <td class="TableContent"><span style="font-size: large;">5</span></td></tr>
<tr> <td class="TableContent"><span style="font-size: large;">**</span></td> <td class="TableContent"><span style="font-size: large;">Exponent</span></td> <td class="TableContent"><span style="font-size: large;">2**4</span></td> <td class="TableContent"><span style="font-size: large;">16</span></td></tr>
</tbody></table><span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;">The order of operations</span><br />
<ol><li><span style="font-size: large;">parentheses () </span></li>
<li><span style="font-size: large;">exponents ** </span></li>
<li><span style="font-size: large;">multiplication *, division \, and remainder % </span></li>
<li><span style="font-size: large;">addition + and subtraction -</span></li>
</ol><span style="font-size: large;"> <b>Multiplication (*) and modulo operator (%) have the same precedence, so you evaluate from left to right for this example</b> </span><br />
<br />
<span style="font-size: large;">r%3*c+10/y </span><br />
<span style="font-size: large;">((r% 3)*c)+(10/y)<br />
((11% 3)*4)+(10/2.5)</span><br />
<span style="font-size: large;">(2*4)+(4)<br />
12<br />
<br />
<b>All operators</b></span><br />
<div id="index-858"><span style="font-size: large;"><br />
</span></div><pre><span style="font-size: large;"><span class="o">+</span> <span class="o">-</span> <span class="o">*</span> <span class="o">**</span> <span class="o">/</span> <span class="o">//</span> <span class="o">%</span>
<span class="o"><<</span> <span class="o">>></span> <span class="o">&</span> <span class="o">|</span> <span class="o">^</span> <span class="o">~</span>
<span class="o"><</span> <span class="o">></span> <span class="o"><=</span> <span class="o">>=</span> <span class="o">==</span> <span class="o">!=</span></span></pre><pre><span style="font-size: large;"><span class="o">
</span></span>100 - 25 * 3 % 4 the result is 97 how?</pre>25*3 = 75<br />
75 % 4 = 3 (4*18 = 72; remainder is 3)<br />
100 - 3 = 97<br />
<br />
<br />
<br />
<table border="1" cellpadding="2" cellspacing="2" style="background-color: #ffff66; text-align: left; width: 100%;"><tbody>
<tr> <th style="vertical-align: top;">Operator </th> <th style="vertical-align: top;">Description </th> <th style="vertical-align: top;">Example </th></tr>
<tr> <td style="vertical-align: top;"><span>+, - </span></td> <td style="vertical-align: top;">Addition, Subtraction </td> <td style="vertical-align: top;"><span>10 -3 </span></td></tr>
<tr> <td style="vertical-align: top;"><span>*, /, % </span></td> <td style="vertical-align: top;">Multiplication, Division, Modulo</td> <td style="vertical-align: top;"><span>27 % 7<br />
Result: 6 </span></td></tr>
<tr> <td style="vertical-align: top;"><span>// </span></td> <td style="vertical-align: top;">Truncation Division (also known as floordivision)<br />
The result of the division is truncated to an integer. Works for integers and floating-point numbers as well</td> <td style="vertical-align: top;"><span>27 % 7<br />
Result: 6 </span></td></tr>
<tr> <td style="vertical-align: top;"><span>+x, -x </span></td> <td style="vertical-align: top;">Unary minus and Unary plus (Algebraic signs)</td> <td style="vertical-align: top;"><span>-3 </span></td></tr>
<tr> <td style="vertical-align: top;"><span>~x </span></td> <td style="vertical-align: top;">Bitwise negation</td> <td style="vertical-align: top;"><span>~3 - 4<br />
Result: -8 </span></td></tr>
<tr> <td style="vertical-align: top;"><span>** </span></td> <td style="vertical-align: top;">Exponentiation </td> <td style="vertical-align: top;"><span>10 ** 3<br />
Result: 1000 </span></td></tr>
<tr> <td style="vertical-align: top;"><span>or, and, not </span></td> <td style="vertical-align: top;">Boolean Or, Boolean And, Boolean Not</td> <td style="vertical-align: top;"><span>(a or b) and c </span></td></tr>
<tr> <td style="vertical-align: top;"><span>in </span></td> <td style="vertical-align: top;">"Element of" </td><td style="vertical-align: top;"><span>1 in [3, 2, 1] </span></td></tr>
<tr> <td style="vertical-align: top;"><span><, <=, >, >=, !=, == </span></td> <td style="vertical-align: top;">The usual comparison operators </td><td style="vertical-align: top;"><span>2 <= 3 </span></td></tr>
<tr> <td style="vertical-align: top;"><span>|, &, ^ </span></td> <td style="vertical-align: top;">Bitwise Or, Bitwise And, Bitwise XOR </td><td style="vertical-align: top;"><span>6 ^ 3 </span></td></tr>
<tr> <td style="vertical-align: top;"><span><<, >> </span></td> <td style="vertical-align: top;">Shift Operatoren </td><td style="vertical-align: top;"><span>6 << 3 </span></td></tr>
</tbody></table>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-9721181124832353022011-07-25T15:51:00.000-07:002011-07-25T15:51:28.651-07:00Python Formating Strings#program to convert Celsius Temperature to Fahrenheit<br />
c=input ('Enter Temperature in Celsius:')<br />
f=c*9/5.0+32<br />
print "Fahrenheit %d" %f<br />
<br />
<br />
දී ඇති ක්රමලේඛය දෝෂ සහිත ය. අපේක්ෂිත ප්රතිදානය ලබා ගැනීම සඳහා එය නිවැරදි විය යුත්තේ පහත සඳහන් කුමන ආකාරයට ද ?<br />
<br />
1. #program to convert Celsius Temperature to Fahrenheit යන පේළිය ඉවත් කිරීම<br />
2. c= input('Enter Temperature in Celsius:') පේළිය c-input('Enter Temperature in Celsius.') ලෙස වෙනස් කිරීම<br />
<br />
3. F=c*9/5.0+32 පේළිය f=c*9.0/5.0+32 ලෙස වෙනස් කිරීම<br />
4. print "Fahrenheit %d" %f පේළිය print "Fahrenheit %f" %f ලෙස වෙනස් කිරීම<br />
5. print "Fahrenheit %d" %f පේළිය print "Fahrenheit %f" ලෙස වෙනස් කිරීම<br />
<br />
============Theory================<br />
# is a comment<br />
# the calculation gives a floating point data type<br />
<br />
>>> k = "user"<br />
>>> v = "niranjan"<br />
>>> "%s=%s" % (k, v) <br />
'user=niranjan' <br />
<br />
<br />
The whole expression evaluates to a string. The first %s is replaced by the value of k; the second %s is replaced by the value of v. All other characters in the string stay as they are <br />
<br />
>>> "Name: %s, age: %d" % ('John', 35) <br />
'Name: John, age: 35' <br />
<br />
C style string formatting:<br />
<br />
"%d:%d:d" % (hours, minutes, seconds)<br />
<br />
<br />
>>> print "Today's stock price: %f" % 50.4625 <br />
50.462500<br />
>>> print "Today's stock price: %.2f" % 50.4625 <br />
50.46<br />
>>> print "Change since yesterday: %+.2f" % 1.5 <br />
+1.50Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-31253076429508730392011-07-25T13:37:00.000-07:002011-07-25T15:28:08.244-07:00Quick Guide to Python List Operations<span style="font-size: large;">mylist=[]<br />
x=20<br />
<br />
<span style="color: blue;">mylist.append(x)</span> <br />
# Add an item to the end of the list; <br />
<br />
<span style="color: blue;">mylist.insert(i, x) </span> <br />
# insert at i , i should exsit<br />
<br />
<b style="color: blue;">list.exytend(L) </b><br />
# Extend the list by appending all the items in the given list<br />
<br />
<span style="color: blue;">mylist.insert(len(a), x) </span>is equivalent to a.append(x).<br />
<br />
<span style="color: blue;">mylist.remove(x) </span><br />
Remove the first item from the list whose value is x. <br />
<br />
<span style="color: blue;">mylist.pop([i]) </span><br />
Remove the item at the given position in the list, and return it. <br />
<br />
<span style="color: blue;">Mylist.pop() </span><br />
# removes and returns the last item in the list.<br />
<br />
<span style="color: blue;">mylist.index(x)</span> <br />
# Return the index in the list of the first item whose value is x.<br />
<br />
<span style="color: blue;">mylist.sort() </span><br />
# Sort the items of the list, in place.<br />
<br />
<span style="color: blue;">mylist.reverse()</span> <br />
# Reverse the elements of the list, in place.<br />
<br />
<br />
>>> <span style="color: #134f5c;">a = [66.25, 333, 333, 1, 1234.5]</span><br />
>>> <span style="color: blue;">print a.count(333), a.count(66.25), a.count('x')</span><br />
2 1 0<br />
>>> <span style="color: blue;">a.insert(2, -1)</span><br />
>>> <span style="color: blue;">a.append(333)</span><br />
>>> a<br />
<span style="color: blue;">[66.25, 333, -1, 333, 1, 1234.5, 333]</span><br />
>>> <span style="color: blue;">a.index(333)</span><br />
1<br />
>>> <span style="color: blue;">a.remove(333)</span><br />
>>> a<br />
[66.25, -1, 333, 1, 1234.5, 333]<br />
>>> <span style="color: blue;">a.reverse()</span><br />
>>> a<br />
[333, 1234.5, 1, 333, -1, 66.25]<br />
>>> <span style="color: blue;">a.sort()</span><br />
>>> a<br />
[-1, 1, 66.25, 333, 333, 1234.5]<br />
<br />
============ADVANCED ==============</span><br />
<span style="font-size: large;"><br />
</span><br />
<br />
<div class="section" id="more-on-lists"><div class="section" id="using-lists-as-stacks"><span id="tut-lists-as-stacks"></span> <br />
<h3>Using Lists as Stacks</h3>The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use <tt class="xref docutils literal"><span class="pre">append()</span></tt>. To retrieve an item from the top of the stack, use <tt class="xref docutils literal"><span class="pre">pop()</span></tt> without an explicit index. For example:<br />
<br />
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">stack</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">stack</span>
<span class="go">[3, 4, 5, 6, 7]</span>
<span class="gp">>>> </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="go">7</span>
<span class="gp">>>> </span><span class="n">stack</span>
<span class="go">[3, 4, 5, 6]</span>
<span class="gp">>>> </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="go">6</span>
<span class="gp">>>> </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="go">5</span>
<span class="gp">>>> </span><span class="n">stack</span>
<span class="go">[3, 4]</span>
</pre></div></div></div><div class="section" id="using-lists-as-queues"><span id="tut-lists-as-queues"></span> <br />
<h3>Using Lists as Queues</h3>It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).</div></div>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-30474156556402930722011-07-25T12:21:00.000-07:002011-07-25T13:27:17.319-07:00Python File Names - පයිතන ලිපිගොනු නාමයපහත දැක්වෙන්නේ උෂ්ණත්වය සෙල්සියස් පරිමාණයේ සිට ෆැරන්හයිට් පරිමාණයට පරිවර්ථනය කිරීම සඳහා සැකසූ ක්රමලේඛයකි. ඒ ඇසුරින් අංක 1 හා 2 ප්රශ්නවලට පිළිතුරු සපයන්න.<br />
<br />
<div style="color: blue;"><span style="font-family: Arial,Helvetica,sans-serif; font-size: large;">#program to convert Celsius Temperature to Fahrenheit</span><span style="font-size: large;"><br style="font-family: Arial,Helvetica,sans-serif;" /></span></div><div style="color: blue;"><span style="font-family: Arial,Helvetica,sans-serif; font-size: large;">c=input ('Enter Temperature in Celsius:')</span><span style="font-size: large;"><br style="font-family: Arial,Helvetica,sans-serif;" /></span></div><div style="color: blue;"><span style="font-family: Arial,Helvetica,sans-serif; font-size: large;">f=c*9/5.0+32</span><span style="font-size: large;"><br style="font-family: Arial,Helvetica,sans-serif;" /></span></div><div style="color: blue;"><span style="font-family: Arial,Helvetica,sans-serif; font-size: large;">print "Fahrenheit %d" %f</span></div><br />
මෙම පයිතන් ක්රමලේඛය සුරැකීමට (Save) භාවිතා කළ හැකි වඩාත් උචිත සහිත ලිපිගොනු නාමය වන්නේ පහත දැක්වෙන ඒවා අතුරින් කුමක් ද?<br />
<br />
1. Temperature Celsius into Fahrenheit.python<br />
2. Temperature Celsius into Fahrenheit.pyp<br />
3. cel_into_fah.pyc<br />
4. cel_into_fah.py<br />
5. temp_conv.pyc<br />
<br />
Hints <br />
<b>Python don't use python and pyp extensions<br />
Temperature is a common word for Celsius, Fahrenheit as well as Kelvin<br />
<br />
A module is a file containing Python definitions and statements. <br />
The file name is the module name with the suffix .py appended. </b><br />
<b><br />
</b>hello.pyc is a compiled version of hello.py to speed-up of the start-up time for short programs.<br />
a -“byte-compiled” version of the module <tt class="xref docutils literal"><span class="pre">spam</span></tt>.<br />
<br />
Packages structure Python namespace by using “dotted module names”.<br />
See Below Advanced...<br />
<br />
<h3><a href="" id="Naming" name="Naming">Naming</a><span class="link_button" id="link-Naming__button" name="link-Naming__button" style="display: block;"><a href="http://www.blogger.com/post-edit.g?showone=Naming#Naming"> </a></span><span class="showhide_button" id="Naming__button" name="Naming__button" onclick="javascript:ShowHideByName('Naming__body','Naming__button')">▽</span> </h3><div class="" style="display: inline;"><code>file_name.py<br />
<br />
<br />
Inside code<br />
ClassName, method_name, ExceptionName, function_name, GLOBAL_VAR_NAME, instance_var_name, function_parameter_name, local_var_name.</code> </div><br />
<br />
<b>Modules (program.py)</b><br />
<span style="color: blue;">short, </span><br style="color: blue;" /><span style="color: blue;">all_lowercase names, </span><br style="color: blue;" /><span style="color: blue;">_underscores can be used</span><br />
<br />
Python packages<span style="color: blue;"></span><span style="color: blue;"></span><br style="color: blue;" /><span style="color: blue;">_underscores is </span><span style="color: blue;">discouraged.</span><br style="color: blue;" /><br />
<br />
inside<br />
case_senitivity<br />
<br />
joined_lower : functions, methods, attributes<br />
<br />
joined_lower or ALL_CAPS : constants<br />
<br />
StudlyCaps : classes<br />
<br />
Attributes: interface, _internal, __private<br />
<br />
avoid __python_internal_form_ <br />
<b></b><br />
<b><br />
</b><br />
<b><br />
</b><br />
<b>====================Advanced ===============</b><br />
<b>Package Sound<br />
</b><br />
<pre>sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
...
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
filters/ Subpackage for filters
__init__.py
equalizer.py
vocoder.py
karaoke.py
...</pre><br />
<br />
<div style="color: blue;"><span class="kn">import</span> <span class="nn">sound.effects.echo<br />
</span><span class="n">sound</span><span class="o">.</span><span class="n">effects</span><span class="o">.</span><span class="n">echo</span><span class="o">.</span><span class="n">echofilter</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">atten</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <br />
<br />
<br />
</div>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com0tag:blogger.com,1999:blog-1856598783778738395.post-23677331215620666572011-07-25T11:25:00.000-07:002011-07-25T11:52:03.362-07:00Python Data Types - පයිතන් දත්ත වර්ගපහත සඳහන් පයිතන් දත්ත වර්ග සලකන්න<br />
එම දත්ත <strike><span style="color: red;">අයත් වන පයිතන් දත්ත</span></strike> අනුපිළිවෙළින්<br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif; font-size: small;">'win', ['Nimal',55,'male'], ('abc',45,'xyz'), {2:'name','tel':'0721475454'}<br />
<br />
1. string, float, double, array<br />
2. string, tuple, dictionary, list<br />
3. string, long, float, complex<br />
4. string, array, integer, list<br />
5. string, list, tuple, dictionary<br />
<br />
</span> <br />
<div align="center" style="font-family: Arial,Helvetica,sans-serif;"><table border="1" cellpadding="3"><tbody>
<tr> <td style="text-align: left;"><span style="font-size: small;"><b>Data type</b></span></td> <td align="center"><span style="font-size: small;"><b>Description and examples</b></span></td> <td align="center"><span style="font-size: small;">Bits</span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: large;">integer</span></td> <td style="text-align: left;"><span style="font-size: small;">signed integer, +/ - 2147483647</span></td> <td align="center"><span style="font-size: small;">32 </span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: large;">float</span></td> <td style="text-align: left;"><span style="font-size: small;">double precision, like 1.23 or 7.8e-28</span></td> <td align="center"><span style="font-size: small;">64 </span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: large;">long integer</span></td> <td style="text-align: left;"><span style="font-size: small;">large integer, trailing L, 234187626348292917L, 7L</span></td> <td align="center"><span style="font-size: small;"><br />
</span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: small;">octal integer</span></td> <td style="text-align: left;"><span style="font-size: small;">base-8 integer, leading 0 as in 0177</span></td> <td align="center"><span style="font-size: small;">32</span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: small;">hexadecimal integer</span></td> <td style="text-align: left;"><span style="font-size: small;">base-16 integer, leading 0x as in 0x9FC</span></td> <td align="center"><span style="font-size: small;">32</span></td></tr>
<tr> <td style="color: black; text-align: left;"><span style="font-size: small;">complex</span></td> <td style="text-align: left;"><span style="font-size: small;">real and imaginary parts written as 3 + 4j or 1.23 - 0.0073j</span></td> <td align="center"><span style="font-size: small;"><br />
</span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: large;">string</span></td> <td style="text-align: left;"><span style="font-size: small;">ordered collection of characters, enclosed by pairs of ', or " characters</span></td> <td align="center"><span style="font-size: small;"><br />
</span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: large;">list</span></td> <td style="text-align: left;"><span style="font-size: small;">ordered collection of objects, like [1,22,[321,'cow'],'horse']</span></td> <td align="center"><span style="font-size: small;"><br />
</span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: large;">dictionary</span></td> <td style="text-align: left;"><span style="font-size: small;">collection of associated key:data pairs {'first':'alpha','last':'omega'}</span></td> <td align="center"><span style="font-size: small;"><br />
</span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: large;">tuples</span></td> <td style="text-align: left;"><span style="font-size: small;"> ('hen','duck',('rabbit','hare'),'dog','cat')</span></td> <td align="center"><span style="font-size: small;"><br />
</span></td></tr>
<tr> <td style="color: blue; text-align: left;"><span style="font-size: small;">file</span></td> <td style="text-align: left;"><span style="font-size: small;">file in disk</span></td> <td align="center"><span style="font-size: small;"><br />
</span></td></tr>
</tbody></table></div>Niranjan Meegammanahttp://www.blogger.com/profile/03233425107844672492noreply@blogger.com1