Skip to content

Commit

Permalink
Some sample code snippets to learn python syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutosh97 committed Jun 21, 2017
1 parent cf446ce commit 2ceb1d6
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions samplefile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from sys import argv
script , filename =argv
txt=open(filename)
print "here is your file %r" %filename
print txt.read()
print "i'll also ask you to type it again"
fi=raw_input("> ")
tx=open(fi)
print tx.read()
8 changes: 8 additions & 0 deletions samplefunc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def multiply(a,b):
c=a*b
print "product is %r" %c
def add(a,b):
e=a+b
print "addition is %r" %e
multiply(3,4)
add(3,4)
16 changes: 16 additions & 0 deletions sampleimport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from sys import argv
script, user_name = argv
prompt = '> '
print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input(prompt)
print "Where do you live %s?" % user_name
lives = raw_input(prompt)
print "What kind of computer do you have?"
computer = raw_input(prompt)
print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)
6 changes: 6 additions & 0 deletions sampleinput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from sys import argv
s,f,ss,t=argv
print "the script is" , s
print "var1 1 is" , f
print "var 2 is" , ss
print "var 3 is" , t
13 changes: 13 additions & 0 deletions sampleloops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
arr=[1,2,3]
arrstr=["ashu" , "himu" , "piyu"]
for num in arr:
print "numbers are %d" %num
for name in arrstr:
print "names are %s" %name
arrinput=[]
for i in range(0,4):
print "enter element number %d" %i
number = raw_input("> ")
arrinput.append(number)
for inp in arrinput:
print "elements are %r" %inp
22 changes: 22 additions & 0 deletions sampleprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
print #"Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'
print 3 + 2 < 5 - 7
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
print "We need to put about", average_passengers_per_car, "in each car."
print "We have", passengers, "to carpool today."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "There are only", drivers, "drivers available."
print "There are", cars, "cars available."

0 comments on commit 2ceb1d6

Please sign in to comment.