Skip to content

Commit 2ceb1d6

Browse files
committed
Some sample code snippets to learn python syntax
1 parent cf446ce commit 2ceb1d6

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

samplefile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from sys import argv
2+
script , filename =argv
3+
txt=open(filename)
4+
print "here is your file %r" %filename
5+
print txt.read()
6+
print "i'll also ask you to type it again"
7+
fi=raw_input("> ")
8+
tx=open(fi)
9+
print tx.read()

samplefunc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def multiply(a,b):
2+
c=a*b
3+
print "product is %r" %c
4+
def add(a,b):
5+
e=a+b
6+
print "addition is %r" %e
7+
multiply(3,4)
8+
add(3,4)

sampleimport.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from sys import argv
2+
script, user_name = argv
3+
prompt = '> '
4+
print "Hi %s, I'm the %s script." % (user_name, script)
5+
print "I'd like to ask you a few questions."
6+
print "Do you like me %s?" % user_name
7+
likes = raw_input(prompt)
8+
print "Where do you live %s?" % user_name
9+
lives = raw_input(prompt)
10+
print "What kind of computer do you have?"
11+
computer = raw_input(prompt)
12+
print """
13+
Alright, so you said %r about liking me.
14+
You live in %r. Not sure where that is.
15+
And you have a %r computer. Nice.
16+
""" % (likes, lives, computer)

sampleinput.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from sys import argv
2+
s,f,ss,t=argv
3+
print "the script is" , s
4+
print "var1 1 is" , f
5+
print "var 2 is" , ss
6+
print "var 3 is" , t

sampleloops.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
arr=[1,2,3]
2+
arrstr=["ashu" , "himu" , "piyu"]
3+
for num in arr:
4+
print "numbers are %d" %num
5+
for name in arrstr:
6+
print "names are %s" %name
7+
arrinput=[]
8+
for i in range(0,4):
9+
print "enter element number %d" %i
10+
number = raw_input("> ")
11+
arrinput.append(number)
12+
for inp in arrinput:
13+
print "elements are %r" %inp

sampleprint.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
print #"Hello World!"
2+
print "Hello Again"
3+
print "I like typing this."
4+
print "This is fun."
5+
print 'Yay! Printing.'
6+
print "I'd much rather you 'not'."
7+
print 'I "said" do not touch this.'
8+
print 3 + 2 < 5 - 7
9+
cars = 100
10+
space_in_a_car = 4.0
11+
drivers = 30
12+
passengers = 90
13+
cars_not_driven = cars - drivers
14+
cars_driven = drivers
15+
carpool_capacity = cars_driven * space_in_a_car
16+
average_passengers_per_car = passengers / cars_driven
17+
print "We need to put about", average_passengers_per_car, "in each car."
18+
print "We have", passengers, "to carpool today."
19+
print "There will be", cars_not_driven, "empty cars today."
20+
print "We can transport", carpool_capacity, "people today."
21+
print "There are only", drivers, "drivers available."
22+
print "There are", cars, "cars available."

0 commit comments

Comments
 (0)