How to display the "%" in python.
I found so far two methods:
>>> print "See %d%%" % (0.77 * 100)
See 77%
>>> print "See {0:.0%}".format(0.77)
See 77%
>>>
Is a blog about python programming language. You can see my work with python programming language, tutorials and news.
>>> print "See %d%%" % (0.77 * 100)
See 77%
>>> print "See {0:.0%}".format(0.77)
See 77%
>>>
>>> import geo
>>> geo.adress("Santa Claus")
{'status': '200', 'latitude': '32.1715776', 'longitude': '-82.3315138', 'accuracy': '4'}
from xml.dom import minidom as dom import urllib def fetchPage(url): a = urllib.urlopen(url) return ''.join(a.readlines()) def extract(webpage): a = dom.parseString(webpage) item2 = a.getElementsByTagName('SendingDate')[0].firstChild.wholeText print "DATA ",item2 item = a.getElementsByTagName('Cube') for i in item: if i.hasChildNodes() == True: eur = i.getElementsByTagName('Rate')[10].firstChild.wholeText dol = i.getElementsByTagName('Rate')[26].firstChild.wholeText print "EURO ",eur print "DOLAR ",dol if __name__=='__main__': webpage = fetchPage("http://www.bnro.ro/nbrfxrates.xml") extract(webpage)The result is:
$python xmlparse.py DATA 2010-02-04 EURO 4.1214 DOLAR 2.9749With "urllib" package I read the url.
from PIL import Image
import glob, os
size_file = 300,300
for f in glob.glob("*.png"):
file, ext = os.path.splitext(f)
img = Image.open(f)
img.thumbnail(size_file, Image.ANTIALIAS)
img.save("thumb_" + file, "JPEG")
import glob
import PIL
from PIL import Image
for f in glob.glob("*.jpg"):
img = Image.open(f)
dim_percent=(100/float(img.size[0]))
dim_size=int((float(img.size[1])*float(dim_percent)))
img = img.resize((100,dim_size),PIL.Image.ANTIALIAS)
if f[0:2] != "trumb_":
img.save("thumb_" + f, "JPEG")
>>> import gtk >>> dgd=gtk.gdk.display_get_default() >>> gsd=dgd.get_default_screen() >>> height=gsd.get_height() >>> width=gsd.get_width() >>> print "height=",height,"width=",width height= 1024 width= 1280
p = md5.new ()
pass = md5.new ()
>>> pass = md5.new() File "", line 1 pass = md5.new() ^ SyntaxError: invalid syntax
import md5 p = md5.new() p.update("password") p.hexdigest() '5f4dcc3b5aa765d61d8327deb882cf99'
win.clear ()
img1.blit (5, 5)
import pyglet
from pyglet.gl import *
#set colors with red,green,blue,alpha values
colors = {
'playfield' : (255, 255, 255, 255),
'red' : (255, 0, 0, 255,0),
'sub_red' : (255, 50, 20, 255),
'green' : (0, 255, 0, 255),
'sub_green' : (20, 255, 50, 255),
'blue' : (0, 0, 255, 255),
'sub_blue' : (20, 50, 255, 255)}
#give img1 the place of image
img1= pyglet.image.load('20000.jpg')
#w , h the size of image
w = img1.width+15
h = img1.height+10
# create the 640x480 window size
win = pyglet.window.Window(640, 480, resizable=True, visible=True)
#or use next line to see with size of image
#win=pyglet.window.Window(width=w, height=h)
# set caption of window
win.set_caption("TEST")
#use * to unpack tuple and set background color
pyglet.gl.glClearColor(*colors['red'])
@win.event
def on_draw():
win.clear()
img1.blit(5, 5)
# to run application
pyglet.app.run()
win = pyglet.window.Window(640, 480, resizable=True, visible=True)
win=pyglet.window.Window(width=w, height=h)
import random
list_input = """ Apple
Crabapple
Hawthorn
Pear
Apricot
""".strip().split("\n")
text_input = "This is a text without newline char".split()
print "List is =", list_input
print "---------------------------------------"
list_result = random.choice(list_input)
print "Random list result is =", list_result
print "---------------------------------------"
print "Text is =", text_input
print "---------------------------------------"
text_result = random.choice(text_input)
print "Random word result is =", text_result
List is = ['Apple', ' Crabapple', ' Hawthorn', ' Pear', ' Apricot']
---------------------------------------
Random list result is = Pear
---------------------------------------
Text is = ['This', 'is', 'a', 'text', 'without', 'newline', 'char']
---------------------------------------
Random word result is = is
>>> try :
... input_str = int(input ("string "))
... except StandardError :
... print " This is not a number"
...
string 12
>>> try :
... input_str = int(input ("string "))
... except StandardError :
... print " This is not a number"
...
string aaa
This is not a number
input_str = int(input ("string "))
#!/usr/bin/env python
def permut(lst):
remove = lambda lst0, index: lst0[:index] + lst0[index+1:]
if lst:
for index, x in enumerate(lst):
for y in permut(remove(lst, index)):
yield (x,)+y
print y
else: yield ()
lst='catalin'
for p in permut(['c','a','t','a','l','i','n']):
print p
import gdata.youtube import gdata.youtube.service client_yt = gdata.youtube.service.YouTubeService() query = gdata.youtube.service.YouTubeVideoQuery() query.vs = "%s" % ('News') feed = client_yt.YouTubeQuery(query) for entry in feed.entry: print entry.title.text for link in entry.link: if link.rel == 'alternate': print link.href
import gdata.photos import gdata.photos.service import urllib p_client = gdata.photos.service.PhotosService() query_parameters = map(urllib.quote, ['Sexy','Bucharest']); feed = p_client.GetFeed("/data/feed/api/all?q=%s%s&max-results=10" % ('Sexy','Girl')) for entry in feed.entry: print entry.title.text for link in entry.link: if link.rel == 'alternate': print link.href
#!/usr/bin/python import sqlite3 def select_db(): print "Select database" co=sqlite3.connect("cata2.db") cursor=co.execute("CREATE TABLE report (nr INT,user VARCHAR(20),descriere VARCHAR(100));") cursor=co.execute("INSERT INTO report (nr,user,descriere) VALUES (1,'root','root administration');") cursor=co.execute("SELECT * FROM report;") print cursor.fetchall()
co=sqlite3.connect("new_database.db") cursor=co.execute("CREATE TABLE tabela (some_value_integer INT,some_value_chars VARCHAR(20));")
cursor=co.execute("SELECT * FROM new_database;")