The document contains code snippets for several Python programs that demonstrate various Python features:
- The First.py program prints lines and gets user input
- The list_comp.py program shows looping vs list comprehension to create lists
- The fav_movies.py program demonstrates reading from and writing to files
- The get_particular_line.py program reads a specific line from a file
- The zip_length.py program reads files from a zip archive and prints their lengths
- The details.py program gets system details like name and IP address
- The password_gen.py program generates a random 10 character password
1 of 4
Download to read offline
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