Monday, July 25, 2011

Python syntax examples

පහත දැක්වෙන පයිතන් ප‍්‍රකාශ සලකා බලන්න:


A - s="It's correct"

B - x=y=z=2.5

C - x,y,z=1,2.3,'string'

D - #This is not correct

E - a=['string',123]
F - b=5
while 4<=b>10:
b+=1
G - for x in range(0,12,3):
print x
H - a={'a':'b',4:40}
I - def fun1(a,b=4,l=[]):
return b*a


==================



Reserved Words:

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.
Keywords contain lowercase letters only.
and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield



Multi-Line Statements:

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:
total = item_one + \
        item_two + \
        item_three
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.'''



# This is a comment.
# This is a comment, too.

raw_input("\n\nPress the enter key to exit.")
input("\n\nPress the enter key to exit.")
print("Press the enter key to exit") # Python 3.x
print "Press the enter key to exit" # Python 2.x
var1 = 1
var2 = 10
del var1, var2
Strings
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'
\n 0x0a Newline
a holds 'Hello' and b holds 'Python'  then:

 
print ("My name is %s and weight is %d kg!" % ('Sinali', 21) 
This will produce following result:
My name is Zara and weight is 21 kg!

Format Symbol Conversion
%c character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase 'e')
%E exponential notation (with UPPERcase 'E')
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E

Unicode String:

print u'Hello, world!'
This would print following result:
Hello, world!

count = 0 

while (count < 9):
      print 'The count is:', count
      count = count + 1
      print "Good bye!"


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=Hello
b=Python 

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 

No comments:

Post a Comment