Chapter 5: Arrays ans Strings

Example 5.1, Page Number: 82

In [1]:
#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],
0 1 2 3 4 5 6 7 8 9

Example 5.2, Page Number: 83

In [2]:
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
minimum value:  192
maximum value:  684

Example 5.3, Page Number: 85

In [4]:
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

            
Original array is: 
[388, 661, 218, 595, 167, 46, 704, 140, 559, 428]
Sorted array is: 
[46, 140, 167, 218, 388, 428, 559, 595, 661, 704]

Example 5.4, Page Number: 87

In [2]:
#Variable decleration
str = "Hello"

#Result
print "Here is your string:",
print str
Here is your string: Hello

Example 5.5, Page Number: 88

In [1]:
print "Enter a string: "

 
str = "Hello"

#Result
print "Here is your string:",
print str
Enter a string: 
Here is your string: Hello

Example 5.6, Page Number: 89

In [5]:
#Variable decleration
s=str
str = s   #copying s into str

#Result
print str
Hello

Example 5.7, Page Number: 89

In [2]:
#Variable decleration
s1 = "Hello"
s2 = " There"

#Concatenation
s1+=s2

#Result
print s1
Hello There

Example 5.8, Page Number: 90

In [3]:
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"
    
    
        
Enter password: 
Invalid password.
Access denied

Example 5.9, Page Number: 91

In [1]:
import string

    s=raw_input("Enter a string: ")                 #User input of string;
    if s=="quit":       #Sting comparison
        break
    
    

        
                  
Enter a string: ddc
Enter a string: hello
Enter a string: quit

Example 5.10, Page Number: 91

In [5]:
#Variable declaration
str = "Hello"

#Result
print "Length is: ",len(str)
                
Length is:  5

Example 5.11, Page Number: 92

In [6]:
str = "Hello World"

#Reversing a String
rev = str[::-1]

#Result
print rev
dlroW olleH

Example 5.12, Page Number: 92

In [9]:
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"
lengths:  5   5
not equal
Hellothere
there and there  are now the same

Example 5.13, Page Number: 93

In [12]:
import string

#Variable Initialization
str= "this is a test"

str=string.upper(str)

#Result
print str
    
THIS IS A TEST

Example 5.14, Page Number: 94

In [6]:
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
1 2 3 4
5 6 7 8
9 10 11 12

Example 5.15, Page Number: 98

In [15]:
#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]
The square of  6  is  36

Example 5.16, Page Number: 99

In [17]:
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()
this is a test
CHANGED
this is a test
CHANGED

Example 5.17, Page Number: 101

In [18]:
#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]
 
0 :
1 :
2 :
eat
play
work

Example 5.18, Page Number: 103

In [2]:
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
0.Quit.
1.Enter information
2.Report information
Choose one: 
1
Enter your name: Anny
Enter your phone number987654321
Enter number of hours worked: 10
Enter wage: 50
Enter your name: Billy
Enter your phone number9456783652
Enter number of hours worked: 10
Enter wage: 50
Enter your name: Catherene
Enter your phone number9476836578
Enter number of hours worked: 10
Enter wage: 50
Enter your name: Dolly
Enter your phone number9831356748
Enter number of hours worked: 15
Enter wage: 50
Enter your name: Emily
Enter your phone number9576843721
Enter number of hours worked: 15
Enter wage: 40
Enter your name: Jack
Enter your phone number9485738567
Enter number of hours worked: 10
Enter wage: 45
Enter your name: Kevin
Enter your phone number9345678923
Enter number of hours worked: 10
Enter wage: 45
Enter your name: Lily
Enter your phone number9345672831
Enter number of hours worked: 10
Enter wage: 45
Enter your name: Monica
Enter your phone number9475867483
Enter number of hours worked: 10
Enter wage: 50
Enter your name: Irene
Enter your phone number5674356776
Enter number of hours worked: 10
Enter wage: 20
0.Quit.
1.Enter information
2.Report information
Choose one: 
2
Anny   987654321
Pay for the week:  500
Billy   9456783652
Pay for the week:  500
Catherene   9476836578
Pay for the week:  500
Dolly   9831356748
Pay for the week:  750
Emily   9576843721
Pay for the week:  600
Jack   9485738567
Pay for the week:  450
Kevin   9345678923
Pay for the week:  450
Lily   9345672831
Pay for the week:  450
Monica   9475867483
Pay for the week:  500
Irene   5674356776
Pay for the week:  200
0.Quit.
1.Enter information
2.Report information
Choose one: 
0
In [ ]: