20 Subjects in 90 minutes: Themed Programs for Adults Workshop
Workshop programs
-
Upload
kracekumar-ramaraju -
Category
Technology
-
view
453 -
download
0
description
Transcript of Workshop programs
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 timesprint("file name :%s"%(__file__))#will reveal current file name printf("%s",__file__)name=raw_input("What is your name :")#get i/p from userprint ("Name:"+name)#print the nameprint(50*"=")
list_comp.py#! /usr/bin/env pythondef 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 disdis.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=0file="fav_movies.txt"file_="fav_movie_list.txt"movie_list=[]def write_to_file():
'''Writing string to file'''global file,countfp=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 filefp=open(file,'r')fp.close()print ("\n Reading one line")
def readlines():global filefp=open(file,"r")print fp.readline()#will read only one linefp.close()print "="*50
def write_list():global file_,movie_listprint("Writing entire list into a file ")f=open(file_,"w")f.writelines(movie_list)#write entire list to a filef.close()print "="*50
def read_using_for():global file
print ("reading input using for loop")for line in open(file):
print lineprint "="*50
def one_liner():global fileprint ("read entire contents in a single line")print(open(file).read())#will read all lines in one stretchprint 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 pythonfile="text.txt"import linecachetheline=linecache.getline(file,2)print thelinezip_length.py#! /usr/bin/env pythonimport zipfilez=zipfile.ZipFile("day1.odp.zip","r")for filename in z.namelist():
print "File:",filenamebytes=z.read(filename)print 'has',len(bytes),'bytes'
details.py#! /usr/bin/env pythonimport socketmyname=socket.getfqdn(socket.gethostname())myaddr=socket.gethostbyname(myname)print "System name:%s"%(myname),"Ip:",myaddr
password_gen.pyimport string,randomchars=string.lowercase+string.uppercase+string.digitsprint ''.join([random.choice(chars) for i in range(0,10)])