#Variable declaration
sample = range(10) #Declares an array [0,1,2,3,4,5,6,7,8,9]
#Displays the array
for t in range(10):
print sample[t],
import random
#Variable declaration
list = [] #List of integers
for i in range(10):
list.append(random.randint(0,1000)) #randomely assigning integers
#Finding the minimum value
min_value = list[0]
for i in range(10):
if min_value>list[i]:
min_value=list[i]
print "minimum value: ",min_value #Result:Minimum value
#Finding maximum value
max_value = list[0]
for i in range(10):
if max_value<list[i]:
max_value=list[i]
print "maximum value: ",max_value #Result:Maximum value
import random
#Variable declaration
nums = []
size = 10
#Initializing list with random numbers
for t in range(size):
nums.append(random.randint(0,1000))
#Displays original array
print "Original array is: "
print nums
#Bubble Sort
for a in range(size):
for b in xrange(size-1,a,-1):
if nums[b-1]>nums[b]:
t=nums[b-1]
nums[b-1]=nums[b]
nums[b] = t
#Display sorted array
print "Sorted array is: "
print nums
#Variable decleration
str = "Hello"
#Result
print "Here is your string:",
print str
print "Enter a string: "
str = "Hello"
#Result
print "Here is your string:",
print str
#Variable decleration
s=str
str = s #copying s into str
#Result
print str
#Variable decleration
s1 = "Hello"
s2 = " There"
#Concatenation
s1+=s2
#Result
print s1
def password():
print "Enter password: " #User input for password
s= "pass"
if s=="password":
return True
else:
print "Invalid password."
return False
#Result
if password() :
print "Logged On."
else:
print "Access denied"
import string
s=raw_input("Enter a string: ") #User input of string;
if s=="quit": #Sting comparison
break
#Variable declaration
str = "Hello"
#Result
print "Length is: ",len(str)
str = "Hello World"
#Reversing a String
rev = str[::-1]
#Result
print rev
s1 = "Hello"
s2 = "there"
#Printing lengths
print "lengths: ",len(s1),' ',len(s2)
#Comparing
if(s1==s2):
print "The strings are equal"
else:
print "not equal"
#Concatenation
s1+=s2
print s1
#Copying
s1=s2
#Result
print s1,"and",s2," are now the same"
import string
#Variable Initialization
str= "this is a test"
str=string.upper(str)
#Result
print str
arr=[] #The 2-D list
new=[] #The nested list
#Initializing the 2-D array
for i in range(3):
new=[]
for j in range(4):
new.append(i*4+j+1)
arr.append(new)
#Result
for i in range(3):
for j in range(4):
print arr[i][j],
print
#Variabe Initialzation
sqrs= [[1,1],[2,4],[3,9],[4,16],[5,25],
[6,36],[7,49],[8,64],[9,81],[10,100]] #Array storing the squares
i=6 #User input of number whose square is to be looked up
#Search for the number
for j in range(10):
if sqrs[j][0]==i:
break
#Result
print "The square of ",i," is ", sqrs[j][1]
def f1():
s="this is a test" #initial s
print s
s="CHANGED" #s is now changed
print s
#Calling the function twice
f1()
f1()
#Variable decleration
text=[]
str=['eat','play','work'] #user input of strings
p=len(str)
for t in range(p):
print t,":"
text.append(str[t]) #Here, user input taken from the list
#Result; redisplay the strings
for i in range(p):
print text[i]
import random
#Variable decleration
name=[] #this list holds employee names
wage=[] #their phone numbers
phone=[] #hours worked per week
hours=[] #wage
num=0 #User choice
#Menu
def menu():
global num #All options are chosen one by one
print "0.Quit."
print "1.Enter information"
print "2.Report information"
print "Choose one: "
num=int(input())
return num #Return users selction
#Enter information
def enter():
for i in range(10):
n=raw_input("Enter your name: ")
name.append(n)
phone.append(int(input("Enter your phone number")))
hours.append(int(input("Enter number of hours worked: ")))
wage.append(int(input("Enter wage: ")))
#Display report
def report():
p=len(name)
for i in range(p):
print name[i],' ',phone[i]
print "Pay for the week: ",wage[i]*hours[i]
while True:
ch=menu() #get selection
if ch==0:
break
elif ch==1:
enter()
elif ch==2:
report()
else:
print "Try again."
if ch==0:
break