def f1():
print "Enter something: "
str= "Hello" #User-input
print str
#Variable decleration
str="This is str in main"
print str
f1() #function call
print str
#Variable decleration
choice=0
print "(1) add numbers or (2) concatenate strings?: "
choice=2 #User Input taken as 2
if choice==1:
print "Enter two numbers: "
a=5 #Variable decleration; User Input
b=7
print a+b #Result
else :
print "Enter two strings: "
s1="Hello" #Variable decleration; User Input
s2="World"
s1+=s2 #Concatenation
print s1 #Result
#Variable decleration
i=10
j=100
if j>0:
i=None
i= j/2
print "inner i: ",i #Result
print "outer i: ",i #Result
a=5 #Variable decleration; user input
b=10 #declaration of another variable; user input
#Result
print "Product: ",a*b
import random
#Variable decleration
count=None
num_right=0
#Function for the drill
def drill():
#Generate two numbers between 0 and 99.
a=random.randint(0,99)
b=random.randint(0,99)
#The user gets three tries to get it right.
for count in range(3):
print "What is ",a," + ",b,"? "
ans = random.randint(0,200) #user input
if ans==a+b:
print "Right"
num_right+=1
print "You've used up all your tries."
print "The answer is ",a+b
#Main function
print "How many practice problems: "
count=2 #User input taken as 2
num_right=0
while True:
drill()
count-=1
if(count==0):
break
#Result
print "You got ",num_right," right. "
from ctypes import *
def f(j):
j[0]=100 #j is assigned 100
#Variable decleration
i=c_int(1)
p=pointer(i)
#Calling the function
f(p)
print i
from ctypes import *
def f(j):
j[0]=100 #j is assigned 100
#Variable decleration
i=c_int(1)
p=pointer(i)
#Calling the function
f(p)
print i
def display(num):
for i in range(10):
print num[i],
#Variable declaration
t=[]
for i in range(10):
t.append(i)
#Pass list to a function
display(t)
def display(num):
print num,
#Variable declaration
t=[]
for i in range(10):
t.append(i)
#Printing without passing entire list
for i in range(10):
display(t[i])
def cube(n,num):
num-=1
while num:
n[num]=n[num]*n[num]*n[num]
num-=1
#Variable declaration
nums=[]
for i in range(10):
nums.append(i+1)
print "Original contents: ",
for i in range(10):
print nums[i],
print
cube(nums,10) #Compute cubes
#Result
print "Altered contents: ",
for i in range(10):
print nums[i],
import string
def stringupper(str):
str=string.upper(str) #convert to uppercase
return str
#Variable declaration
str="this is a test"
#Calling the function
str=stringupper(str)
#Result
print str
def mystrlen(str):
l=len(str)
return l
#Result
print "Length of Hello There is: ",
print mystrlen("Hello There")
import sys
def main():
if len(sys.argv)!=2:
print "You forgot to type your name." #CHECK!!!!
return
#Result
print "Hello ",sys.argv[1]
main()
import sys
#Result
for t in range(len(sys.argv)):
i=0
print sys.argv[t]
import sys
if len(sys.argv)!=3:
print "usage: add num num"
else :
#Variable Decleration
a=sys.argv[1]
b=sys.argv[2]
#Result
print a+b
import string
#Variable Decleration
i=string.atoi("100")
j=string.atoi("100000")
k=string.atof("-0.123")
#Result
print i," ",j," ",k
#Variable Decleration
i=abs(-10)
#Result
print abs(-23)
abs(100)
def find_substr(sub,str):
l=str.find(sub)
return l
#Variable decleration;Calling the function
index=find_substr("three","one two three four")
#Result
print "Index of three is ",index
import sys
def print_vertical(str):
l=len(str)
for i in range(l):
print str[i],
print_vertical(sys.argv[1])
def find_substr(sub,str):
return str.find(sub)
#Variable declaration
s="one two three four"
substr = find_substr("three",s)
#Result
print "substring found:",s[substr:]
def factr(n):
if n==1:
return 1
answer=factr(n-1)*n
return answer
#Iterative version
def fact(n):
answer=1
for t in range(n):
answer=answer*(t+1)
return answer
#Using recursion version
print "4 factorial is ",factr(4)
#Using iterative version
print "4 factorial is ",fact(4)
def reverse(s):
r = ""
for c in s:
r=c+r
print r
#VariabDecleration
str="this is a test"
#Calling function
reverse(str)