SlideShare uma empresa Scribd logo
1 de 4
Baixar para ler offline
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)])

Mais conteúdo relacionado

Mais procurados

جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲Mohammad Reza Kamalifard
 
Logrotate sh
Logrotate shLogrotate sh
Logrotate shBen Pope
 
The Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's PerspectiveThe Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's PerspectiveEleanor McHugh
 
Raspberry pi Part 4
Raspberry pi Part 4Raspberry pi Part 4
Raspberry pi Part 4Techvilla
 
Pop3stat sh
Pop3stat shPop3stat sh
Pop3stat shBen Pope
 
Scripting 101
Scripting 101Scripting 101
Scripting 101ohardebol
 
북스터디 디스커버리 Go 언어
북스터디 디스커버리 Go 언어북스터디 디스커버리 Go 언어
북스터디 디스커버리 Go 언어동규 이
 
Playing 44CON CTF for fun and profit
Playing 44CON CTF for fun and profitPlaying 44CON CTF for fun and profit
Playing 44CON CTF for fun and profit44CON
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMCoding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMRaveen Perera
 
File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?Christian Kauhaus
 
The Ring programming language version 1.5.1 book - Part 24 of 180
The Ring programming language version 1.5.1 book - Part 24 of 180The Ring programming language version 1.5.1 book - Part 24 of 180
The Ring programming language version 1.5.1 book - Part 24 of 180Mahmoud Samir Fayed
 
Cis 216 – shell scripting
Cis 216 – shell scriptingCis 216 – shell scripting
Cis 216 – shell scriptingDan Morrill
 
The Ring programming language version 1.2 book - Part 15 of 84
The Ring programming language version 1.2 book - Part 15 of 84The Ring programming language version 1.2 book - Part 15 of 84
The Ring programming language version 1.2 book - Part 15 of 84Mahmoud Samir Fayed
 

Mais procurados (19)

جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
gitfs
gitfsgitfs
gitfs
 
I phone
I phoneI phone
I phone
 
Logrotate sh
Logrotate shLogrotate sh
Logrotate sh
 
The Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's PerspectiveThe Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's Perspective
 
Raspberry pi Part 4
Raspberry pi Part 4Raspberry pi Part 4
Raspberry pi Part 4
 
01 linux basics
01 linux basics01 linux basics
01 linux basics
 
Pop3stat sh
Pop3stat shPop3stat sh
Pop3stat sh
 
Bash Programming
Bash ProgrammingBash Programming
Bash Programming
 
Shell and perl scripting classes in mumbai
Shell and perl scripting classes in mumbaiShell and perl scripting classes in mumbai
Shell and perl scripting classes in mumbai
 
Scripting 101
Scripting 101Scripting 101
Scripting 101
 
북스터디 디스커버리 Go 언어
북스터디 디스커버리 Go 언어북스터디 디스커버리 Go 언어
북스터디 디스커버리 Go 언어
 
Sender
SenderSender
Sender
 
Playing 44CON CTF for fun and profit
Playing 44CON CTF for fun and profitPlaying 44CON CTF for fun and profit
Playing 44CON CTF for fun and profit
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMCoding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBM
 
File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?
 
The Ring programming language version 1.5.1 book - Part 24 of 180
The Ring programming language version 1.5.1 book - Part 24 of 180The Ring programming language version 1.5.1 book - Part 24 of 180
The Ring programming language version 1.5.1 book - Part 24 of 180
 
Cis 216 – shell scripting
Cis 216 – shell scriptingCis 216 – shell scripting
Cis 216 – shell scripting
 
The Ring programming language version 1.2 book - Part 15 of 84
The Ring programming language version 1.2 book - Part 15 of 84The Ring programming language version 1.2 book - Part 15 of 84
The Ring programming language version 1.2 book - Part 15 of 84
 

Semelhante a Workshop programs

Semelhante a Workshop programs (20)

working with files
working with filesworking with files
working with files
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Python files / directories part16
Python files / directories  part16Python files / directories  part16
Python files / directories part16
 
Fileinc
FileincFileinc
Fileinc
 
Unit5
Unit5Unit5
Unit5
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Python File functions
Python File functionsPython File functions
Python File functions
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
File management
File managementFile management
File management
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Python build your security tools.pdf
Python build your security tools.pdfPython build your security tools.pdf
Python build your security tools.pdf
 
file handling1
file handling1file handling1
file handling1
 
Python 3.x File Object Manipulation Cheatsheet
Python 3.x File Object Manipulation CheatsheetPython 3.x File Object Manipulation Cheatsheet
Python 3.x File Object Manipulation Cheatsheet
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Satz1
Satz1Satz1
Satz1
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c language
 

Último

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 

Último (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

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