際際滷

際際滷Share a Scribd company logo
Python examples

          Compile Python program:python filename.py

                         First.py

#! /usr/bin/env python
#Above is valid only if you are using the script in linux and you can
understand this is comment :)// /*
import os##include<os.h>
print(50*"-")#will print '-' fifty times
print("file name :%s"%(__file__))#will reveal current file name
printf("%s",__file__)
name=raw_input("What is your name :")#get i/p from user
print ("Name:"+name)#print the name
print(50*"=")



                     list_comp.py
#! /usr/bin/env python
def looping():
     x=[]
     for y in range(20):
           if y%2 == 0:
                x.append(y)
     print x

def list_comp():
      x=[y for y in range(20) if y%2 ==0 ]
      print x

looping()
list_comp()
import dis
dis.dis(looping)
dis.dis(list_comp)
fav_movies.py
#! /usr/bin/env python
"""This file contains few ways how one can read and write content to
file in python"""
count=0
file="fav_movies.txt"
file_="fav_movie_list.txt"
movie_list=[]
def write_to_file():
      '''Writing string to file'''
      global file,count
      fp=open(file,'w')
      print 50*"="
      print("Enter 5 movie names you like ")
      while count < 5 :
            movie=(raw_input("Enter the fav movie:"))+"n"
            movie_list.append(movie)
            fp.write(movie)
            count+=1
      fp.close()
      print 50*"="
def read_a_line():
      global file
      fp=open(file,'r')
      fp.close()
      print ("n Reading one line")
def readlines():
      global file
      fp=open(file,"r")
      print fp.readline()#will read only one line
      fp.close()
      print "="*50
def write_list():
      global file_,movie_list
      print("Writing entire list into a file ")
      f=open(file_,"w")
      f.writelines(movie_list)#write entire list to a file
      f.close()
      print "="*50
def read_using_for():
      global file
print ("reading input using for loop")
     for line in open(file):
           print line
     print "="*50
def one_liner():
     global file
     print ("read entire contents in a single line")
     print(open(file).read())#will read all lines in one stretch
     print 50*'='
write_to_file()
print ("doc for %s:%s "%
(write_to_file.func_name,write_to_file.__doc__))
read_a_line()
readlines()
write_list()
read_using_for()
one_liner()
print ("__doc__",__doc__)

                    get_particular_line.py
#! /usr/bin/env python
file="text.txt"
import linecache
theline=linecache.getline(file,2)
print theline
zip_length.py
#! /usr/bin/env python
import zipfile
z=zipfile.ZipFile("day1.odp.zip","r")
for filename in z.namelist():
      print "File:",filename
      bytes=z.read(filename)
      print 'has',len(bytes),'bytes'

                          details.py
#! /usr/bin/env python
import socket
myname=socket.getfqdn(socket.gethostname())
myaddr=socket.gethostbyname(myname)
print "System name:%s"%(myname),"Ip:",myaddr
password_gen.py
import string,random
chars=string.lowercase+string.uppercase+string.digits
print ''.join([random.choice(chars) for i in range(0,10)])

More Related Content

Workshop programs

  • 1. Python examples Compile Python program:python filename.py First.py #! /usr/bin/env python #Above is valid only if you are using the script in linux and you can understand this is comment :)// /* import os##include<os.h> print(50*"-")#will print '-' fifty times print("file name :%s"%(__file__))#will reveal current file name printf("%s",__file__) name=raw_input("What is your name :")#get i/p from user print ("Name:"+name)#print the name print(50*"=") list_comp.py #! /usr/bin/env python def looping(): x=[] for y in range(20): if y%2 == 0: x.append(y) print x def list_comp(): x=[y for y in range(20) if y%2 ==0 ] print x looping() list_comp() import dis dis.dis(looping) dis.dis(list_comp)
  • 2. fav_movies.py #! /usr/bin/env python """This file contains few ways how one can read and write content to file in python""" count=0 file="fav_movies.txt" file_="fav_movie_list.txt" movie_list=[] def write_to_file(): '''Writing string to file''' global file,count fp=open(file,'w') print 50*"=" print("Enter 5 movie names you like ") while count < 5 : movie=(raw_input("Enter the fav movie:"))+"n" movie_list.append(movie) fp.write(movie) count+=1 fp.close() print 50*"=" def read_a_line(): global file fp=open(file,'r') fp.close() print ("n Reading one line") def readlines(): global file fp=open(file,"r") print fp.readline()#will read only one line fp.close() print "="*50 def write_list(): global file_,movie_list print("Writing entire list into a file ") f=open(file_,"w") f.writelines(movie_list)#write entire list to a file f.close() print "="*50 def read_using_for(): global file
  • 3. print ("reading input using for loop") for line in open(file): print line print "="*50 def one_liner(): global file print ("read entire contents in a single line") print(open(file).read())#will read all lines in one stretch print 50*'=' write_to_file() print ("doc for %s:%s "% (write_to_file.func_name,write_to_file.__doc__)) read_a_line() readlines() write_list() read_using_for() one_liner() print ("__doc__",__doc__) get_particular_line.py #! /usr/bin/env python file="text.txt" import linecache theline=linecache.getline(file,2) print theline zip_length.py #! /usr/bin/env python import zipfile z=zipfile.ZipFile("day1.odp.zip","r") for filename in z.namelist(): print "File:",filename bytes=z.read(filename) print 'has',len(bytes),'bytes' details.py #! /usr/bin/env python import socket myname=socket.getfqdn(socket.gethostname()) myaddr=socket.gethostbyname(myname) print "System name:%s"%(myname),"Ip:",myaddr