age = []*4 #array 'age' of 4
for j in range(4): #get 4 ages
x = input("Enter an age: ")
age.append(x) #access array element
for j in range(4): #display 4 ages
print '\nYou entered',age[j],
SIZE = 6 #size of array
sales = []*SIZE #array of 6 variables
print 'Enter widget sales for 6 days'
for j in range(SIZE): #put figures in array
x = input()
sales.append(x)
total = 0
for j in range(SIZE): #read figures from array
total += sales[j] #to find total
average = total / SIZE #find average
print 'Average =',average,'\n'
days_per_month = (31,28,31,30,31,30,31,31,30,31,30,31)
month = input("Enter month (1 to 12): ") #get date
day = input("Enter day (1 to 31): ")
total_days = day #separate days
for j in range(month-1): #add days each month
total_days += days_per_month[j]
print 'Total days from start of year is:',total_days
DISRICTS = 4 #array dimensions
MONTHS = 3
sales = [[0] * (DISRICTS+1) for i in range(MONTHS+1)] #two-dimensional array definition
print '',
for d in range(DISRICTS): #get array values
for m in range(MONTHS):
print 'Enter sales for district',d+1,
print ', month',m+1,': ',
sales[d][m] = input() #put number in array
print '\n'
print ' month'
print ' 1 2 3',
for d in range(DISRICTS):
print '\nDistrict',d+1,
for m in range(MONTHS):
print '%10.2f' %(sales[d][m]), #get number from array
DISRICTS = 4 #array dimensions
MONTHS = 3
#initialize array elements
sales = [[1432.07,234.50,654.01],
[322.00,13838.32,17589.88],
[9328.34,934.00,4492.30],
[12838.29,2332.63,32.98]]
print '\n'
print ' month'
print ' 1 2 3',
for d in range(DISRICTS): #display array elements
print '\nDistrict',d+1,
for m in range(MONTHS):
print '%10.2f' %(sales[d][m]), #access array elements
DISRICTS = 4 #array dimensions
MONTHS = 3
def display(funsales): #function for display array elements
print '\n'
print ' month'
print ' 1 2 3',
for d in range(DISRICTS):
print '\nDistrict',d+1,
for m in range(MONTHS):
print '%10.2f' %(sales[d][m]), #access array elements
sales = [[1432.07,234.50,654.01], #initialize two-dimentional array
[322.00,13838.32,17589.88],
[9328.34,934.00,4492.30],
[12838.29,2332.63,32.98]]
display(sales) #call function; array as argument
SIZE = 4 #number of parts in array
class part: #specify a structure
modelnumber = None #ID number of widget
partnumber = None #ID number of widget part
cost = None #cost of part
apart = [part() for i in range(SIZE)] #define array of structures
for n in range(SIZE): #get values for all members
apart[n].modelnumber = input('Enter model number: ') #get model number
apart[n].partnumber = input('Enter part number: ') #get part number
apart[n].cost = input('Enter cost: ') #get cost
print
for n in range(SIZE): #show values for all members
print 'Model',apart[n].modelnumber,
print ' Part',apart[n].partnumber,
print ' Cost',apart[n].cost
class Stack:
__max = 10 #define maximum size of stack array
def __init__(self): #constructor
self.__st = [0 for i in range(self.__max)] #stack: array of integers
self.__top = -1 #number of top of stack, initially -1
def push(self,var): #put number on stack
self.__top += 1
self.__st[self.__top] = var
def pop(self): #take number from stack
x = self.__st[self.__top]
self.__top -= 1
return x
s1 = Stack() #define a stack variable
s1.push(11) #push value on stack
s1.push(22)
print '1:',s1.pop() #pop value from stack, and display it
print '2:',s1.pop()
s1.push(33) #push values on stack
s1.push(44)
s1.push(55)
s1.push(66)
print '3:',s1.pop() #pop values from stack
print '4:',s1.pop()
print '5:',s1.pop()
print '6:',s1.pop()
class Distance: #Distance class
def getdist(self): #get length from usser
self.__feet = input(' Enter feet: ')
self.__inches = input(' Enter inches: ')
def showdist(self): #display distance
print self.__feet,'\' -',self.__inches,'\"',
dist = [Distance() for i in range(100)] #array of distances
n = 0 #count the entries
while True: #get distances from user
print '\nEnter distance number',n+1
dist[n].getdist() #store distances in array
n += 1
ans = raw_input('Enter another (y/n)?: ')
if(ans == 'n'): #quit if user types 'n'
break
for j in range(n): #display all distances
print '\nDistance number',j+1,'is',
dist[j].showdist()
from random import randrange #for selecting random number
symbols = ['♣','♦','♥','♠'] #array for printing the symbol of cards
Suit = ["clubs","diamonds","hearts","spades"]
#from 2 to 10 are integers without names
jack = 11
queen = 12
king = 13
ace = 14
class card:
def __init__(self): #constructor
self.__number = None
self.__suit = None
def set(self,n,s):
self.__number = n #2 to 10, jack, queen, king, ace
self.__suit = s #clubs, diamonds, hearts, spades
def display(self): #display the cards
if self.__number >= 2 and self.__number <= 10: #display the card number
print self.__number ,
else:
if self.__number == jack:
print 'J',
elif self.__number == queen:
print 'Q',
elif self.__number == king:
print 'K',
elif self.__number == ace:
print 'A',
if self.__suit == "clubs": #dispaly the symbol
print symbols[0],
elif self.__suit == "diamonds":
print symbols[1],
elif self.__suit == "hearts":
print symbols[2],
elif self.__suit == "spades":
print symbols[3],
deck = [card() for j in range(52)] #array of cards
for j in range(52): #make an ordered deck
num = (j % 13) + 2 #cycles through 2 to 14, 4 times
su = Suit[j / 13] #cycles through 0 to 3, 13 times
deck[j].set(num,su) #set cards
print '\nOrderd deck:'
for j in range(52): #display ordered deck
deck[j].display()
print '',
if (j+1)%13 == 0: #new line every 13 cards
print
for j in range(52): #for each card in deck,
k = randrange(2,52)%52 #pick card at random
temp = deck[j] #and swap them
deck[j] = deck[k]
deck[k] = temp
print '\nshuffled deck:'
for j in range(52): #display suffled card
deck[j].display()
print '',
if (j+1)%13 == 0: #new line every 13 cards
print
MAX = 80 #max characters in string
str = [0 for i in range(MAX)] #string variable str
str = raw_input("Enter a string ") #put string in str
print 'You entered:',str #display string from str
MAX = 20 #max characters in string
str = []*MAX #string variable str
str = raw_input("Enter a string: ") #put string in str
print 'You entered: %20s' %str #display string from str
str = []
str = "Farewell! thou art too dear for my possessing." #store string in an array
print str,'\n' #print array
MAX = 20 #max characters in string
str = []*MAX #string variable str
str = raw_input("Enter a string: ") #put string in str
print 'You entered:',str #display string from str
print 'Enter a string:'
str = "" #string variable
stopword = "$" #taking for checking the termination condition
while True:
line = raw_input() #taking input line by line
if line.strip() == stopword: #stop when tenmination condition matches
break
str += "%s\n" % line
print 'You entered:'
print str #print the whole string
str1 = [] #initialized string
str1 = "Oh, Captain, my Captain! our fearful trip is done"
MAX = 80 #size of str2 buffer
str2 = [None]*MAX #empty string
str2 = ''.join(str1) #copy str1 character to str2
print str2 #print str2
str1 = []
str1 = "Tiger, tiger, burning bright\nIn the forests of the night"
MAX = 80 #size of str2 buffer
str2 = str1 #copy str1 to str2
print str2 #display str2
Days = 7 #number of string in array
star = ["Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday"] #arrray of string
for j in range(Days): #display every string
print star[j]
class part:
def setpart(self,pname,pn,c): #set data
self.__partname = pname
self.__partnumber = pn
self.__cost = c
def showpart(self): #display data
print 'Name %s,' %self.__partname,
print 'number %d,' %self.__partnumber,
print 'costs $%.2f' %self.__cost
#define part objects
part1 = part()
part2 = part()
part1.setpart("handle bolt",4473,217.55) #set parts
part2.setpart("start level",9924,419.25)
print 'First part:' #show part
part1.showpart()
print 'Second parrt:'
part2.showpart()
class String:
def __init__(self,s = '\0'): #overloaded constructor
self.__str = s
def display(self): #display string
print self.__str
def concat(self,s2): #add argument string to this string
if len(self.__str)+len(s2.__str) < 80:
self.__str = self.__str + s2.__str
else:
print 'String too long'
s1 = String("Merry Christmas! ") #define the string
s2 = String("Season's Greetings!")
s3 = String()
print 's1 =', #display them all
s1.display()
print 's2 =',
s2.display()
print 's3 =',
s3.display()
s3 = s1 #assignment
print 's3 =', #display s3
s3.display()
s3.concat(s2) #concatenation
print 's3 =', #display s3
s3.display()
s1 = "Man" #initialize
s2 = "Beast"
s3 = s1 #assign
print 's3 =',s3
s3 = "Neither " + s1 + " Nor " #concatenate
s3 += s2 #concatenate
print 's3 =',s3
s4 = s2 #swap s1 and s2
s2 = s1
s1 = s4
print s1 , 'nor' , s2
greeting = "Hello, "
full_name = raw_input("Enter your full name: ") #get the name of user
print 'Your full name is:',full_name
nickname = raw_input("Enter your nickname: ") #get the nickname
greeting += nickname #append name with greeting
print greeting #output: "Hello, Jim"
print 'Enter your address on separate lines'
print 'Terminate with \'$\''
address = ""
stopword = "$"
while True:
line = raw_input() #taking input line by line
if line.strip() == stopword: #stop when tenmination condition matches
break
address += "%s\n" % line
print 'Your address is:'
print address #print the whole string
def find_first_not_of(s1,s2):
a = len(s2) #length of the string to be checked
arr = [99 for j in range(a)] #array for storing the matches index
for j in range(a):
n = s1.find(s2[j]) #A character of second string is present in the first string then
if n!=-1:
arr[j] = n #put the index in the array
arr.sort() #sort the array to check first occarence
return arr[0]+1 #return the first index
s1 = "In xanada did Kubla Kahn a stately pleasure dome decree"
n = s1.find("Kubla")
print 'Found kubla at',n
n = s1.index("d")
print 'First of spde at',n
n = find_first_not_of(s1,"aeiouAEIOU")
print 'Found consonant at',n
s1 = "Quick! Send for Count Graystone."
s2 = "Lord"
s3 = "Don't "
s1 = s1[7:] #remove "Quick! "
s1 = s1.replace("Count",s2) #replace "Count" with "Lord"
s1 = s1.replace("S","s") #replace 'S' with 's'
s1 = s3 + s1 #insert "Don't " at beginning
s1 = s1[:-1] #remove '.'
s1 = s1 + "!!!" #append "!!!"
s1 = s1.replace(" ","/") #replace all white space with '/'
print s1
aName = "George"
username = raw_input("Enter your first name: ")
if username == aName: #operator ==
print "Greetings, George"
elif username < aName: #operator <
print 'You come before George'
else:
print 'You come after George'
print 'The first two letters of your name',
username = username[:2]
aName = aName[:2]
if username == aName:
print "match",
elif username < aName:
print 'come before',
else:
print 'come after',
print aName
word = raw_input("Enter a word: ") #take string from user
wlen = len(word) #length of string object
print 'One character at a time: ',
for j in range(wlen):
print word[j],
charray = []*80
charray = word #copy string object to another object
print '\nArray contains:',charray