# A print demo
# The conversion specification for numbers.
print "Case 1 :%d:"%123
print "Case 2 :%0d:"%123
print "Case 3 :%2d:"%123
print "Case 4 :%8d:"%123
print "Case 5 :%-8d:"%123
print "Case 6 :%8d:"%-123
print "Case 7 :%f:"%123.456
print "Case 8 :%f:"%123.4567896
print "Case 9 :%e:"%123.456
print "Case 10 :%e:"%1.23456e2
print "Case 11 :%e:"%123.456e+0
print "Case 12 :%f:"%1.23456E+02
print "Case 13 :%4.2f:"%123.456
print "Case 14 :%8.2f:"%123.456
print "Case 15 :%.3e:"%123.456
print "Case 16 :%12.3e:"%123.456
# A print demo:
# Interpretation of values
# python is dynamically-typed language
# which is why declaration of variables is omitted
n = -123
c = "V"
print "%d"%n
print "%u"%n # python correctly interprets the negative(unsigned) integer
print "%c"%c
# print "%d"%c # python doesn't support implicit type-casting from integer to character or character to integer
print "%c"%86
# Printing out the ASCII value of a character
# There is no do..while loop in python
# Hence, we will be using while loop
answer = "y"
while answer == "y" :
print "Enter a character:",
in_char = "a"
print in_char
print "The ASCII value of %c is %d"%(in_char,ord(in_char)) # ord() function returns the ascii(int) value of a character
print "\nAnother? (y/n)",
answer = "n" # To terminate the loop
print answer
# Converting a digit to a number by using its ASCII value
print "Enter a digit:",
digit = "A"
print digit
if digit < "0" or digit > "9" :
print "That's not a digit"
else :
print "The digit you typed is %c"%digit
# Converting a letter from lower to upper case or vice-versa
answer = "y"
while answer == "y" :
print "\n\n\nEnter a letter:",
letter = "A"
print letter
if letter >= "A" and letter < "Z" : # Letter is uppercase
print "Lower case is: %c"%chr(ord(letter) + (ord("a")-ord("A")))
elif letter >= "a" and letter <= "z" : # Letter is lowercase
print "Upper case is: %c"%chr(ord(letter) - (ord("a")-ord("A")))
else :
print "That's not a letter"
# Ask if the user wants to go again
print "\nAnother? (y/n)",
answer = "n" # To terminate the loop
print answer
# Example of using control characters
print "The word apple\b\b\b\b\b_____ is underlined.\n"
print "The last word in this sentence is way over\t\there."
# How to print special characters
print "This is how to print a double-quote: \""
print "This is how to print a backslash: \\"
# this is how to write an apostrophe character
c = '\''
print "c now contains: %c"%c
print "This is how to print a percent-sign: %" # no escape sequence required for % in python
# Octal and hexadecimal notation
a = 27
b = 033
c = 0x1b
print "a = %d, b = %d, c = %d"%(a,b,c)
print "a = %o, b = %o, c = %o"%(a,b,c)
print "a = %x, b = %x, c = %x"%(a,b,c)
# Illustration of some ways to print a string
print "This is simply a control string."
print "%s"%("The control string just tells how to print this.")
print "%s"%("Newline may be put here, instead!.\n"),
print "%s %s %s %s %s"%("This","is","valid","in","C.")
print "%s %s also %s %s %s"%("This","is","valid","in","C.")
print "%s %s %s %s %s %s %s"%("This","is","valid","in","C","also.","\n"),
# %s conversion specification demonstrated
print "Case 1 :%s:"%("A string")
print "Case 2 :%3s:"%("A string")
print "Case 3 :%12s:"%("A string")
print "Case 4 :%-12s:"%("A string")
print "Case 5 :%12.6s:"%("A string")
print "Case 6 :%12.12s:"%("A string")
print "Case 7 :%.6s:"%("A string")
print "Case 8 :%-12.6s:"%("A string")
# Some miscellaneous conversion sppecifications
a = 123456789L
print "%d"%a # in python both int and long are unified..so %d specifier valid unlike in C
print "%ld"%a
print ":%c:"%("V")
print ":%0c:"%("V")
print ":%5c:"%("V")
print ":%-5c:"%("V")
# Reading decimal,octal and hex numbers
print "Enter a number in decimal:",
a = 27
print a
print "You entered %d"%a
print "\nEnter a number in octal:",
a = 033 # O is added in the front to specify octal value
print a
print "You entered %o, or %d in decimal"%(a,a)
print "\nEnter a number in hex:",
a = 0x1B
print a
print "You entered %x, or %d in decimal"%(a,a)