Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #(1) Write Python expressions that correspond to each of the following statements:
- #The sum of the five even integers from 2 to 10.
- startingNumber = 2
- endingNumber = 10
- sum = startingNumber
- nextEven = 4
- while nextEven <= 10:
- sum = sum + nextEven
- nextEven = nextEven + 2
- print(sum)
- #The average of this group of test scores: 75%, 83.5%, 61%, 43%
- scores = [75.00,83.5,61.00,43.00]
- sumScores = 0
- for i in range(len(scores)):
- sumScores = sumScores + scores[i]
- average = sumScores / len(scores)
- print(average)
- #2 to the 10th power
- base = 2
- power = 10
- result = 0
- for i in range(power):
- if i == 0:
- result = base
- else:
- result = result * base
- print(base, 'to the power of ', power, ' will equal ', result)
- #A moving anteater has a mass of 50 kg and a velocity (speed) of 15 meters per second.
- #Compute its kinetic energy using this formula: 1/2 times the mass times the velocity squared.
- mass = 50.0
- velocity = 15.0
- kineticEnergy = 0.0
- print('The Kinetic Energy of a Moving Anteater is: ',(mass/2) * (velocity**2))
- #(2) We're designing a game where players can create castle defenses against invaders. Each side of the castle consists of a sequence of wall parts and cannons: We represent a six-segment side with no cannons as 'wwwwww'; a five-segment side with one cannon in the middle would be 'wwcww'.
- #Define these two variables:
- wall = 'w'
- cannon = 'c'
- #Using the variables wall and cannon, the string operators + and *, and parentheses, write string expressions that evaluate to:
- def wallNoCannon():
- return wall*6
- def wallWithCannon():
- return (wall*2+cannon+wall*2)
- #"wc"
- print(wall+cannon)
- #"wcw"
- print(wall+cannon+wall)
- #"wwwcwww"
- print(wall*3+cannon+wall*3)
- #"wccwccwccwcc"
- print((wall+cannon*2)*4)
- #"wwwcwwwcwwwcwwwcw"
- print((wall*3+cannon)*4+wall)
- #"wwwwcwwwwcwwwwcwwwwcwwww"
- print((wall*4+cannon)*3+wall*4)
- #(3) Scores on a quiz range from 0 to 5. Suppose we have the following assignment
- #statement (which represents the quiz scores of all the students in a class):
- #Using the variable test_scores and the indexing operators, write four
- #separate expressions whose values are each of the following: quiz score
- #for the 1st student, the 5th student, the 10th student, and the 16th student.
- #Remember zero-based indexing.
- test_scores = "4325220523455023"
- print(test_scores[3])
- print(test_scores[4])
- print(test_scores[9])
- print(test_scores[15])
- #(4) Suppose we have the following assignment statement:
- s = "anteater"
- #For each of the following, write a boolean expression that represents the
- #English statement:
- #The first character of string s is "a"
- print(s[0] == 'a')
- #The last character of string s is "r"
- print(s[len(s)-1] == 'r')
- #The fourth character of string s is "x"
- print(s[3] == 'x')
- #The first three characters of string s match the string "zot"
- print(s[0:2] == 'zot')
- #(5) Write Python assignment statements that correspond to each of the following:
- #Assign 3.14159 to variable pi
- pi = 3.14159
- print(pi)
- #Assign the values "Toyota", "Camry", and 2014 to the variables make, model,
- #and year
- make, model, year = 'Toyota','Camry','2014'
- print(make,model,year)
- #Assign a list containing strings "Computer Science", "Informatics", and
- #"Computer Game Science" to the variable ICS_majors
- ICS_majors= ['Computer Science','Informatics','Computer Game Science']
- #Assign the variable a to be the average of the odd numbers from 3 to 9.
- a = 3
- numbers = []
- average = 0
- sumNumbers = 0
- while a <= 9:
- if a == 3:
- numbers.append(a)
- average = average + 1
- if(a + 2 <= 9):
- average = average + 1
- a = a + 2
- numbers.append(a)
- else:
- break
- for i in range(len(numbers)):
- sumNumbers = numbers[i] + sumNumbers
- a = float(sumNumbers/average)
- print(a)
- #(6) Write boolean expressions corresponding to each of the following statements:
- #20 plus 35 is greater than 2 to the power of 4
- print((20+35)>(2**4))
- #The string "hello" is not equivalent to the string "goodbye"
- print('hello' == 'goodbye')
- #The remainder when 10 is divided by 3 is less or equal to 1.
- print((10%3) <= 1)
- #The list ["apple", "orange", "banana", "mango"] contains 5 elements.
- fruits = ["apple", "orange", "banana", "mango"]
- print(len(fruits)==5)
- #The number 63 is an even number.
- print((63%2)==0)
- #(7) Start with the following assignment statement:
- s = "abcdefghijklmnopqrstuvwxyz"
- #Using only string concatenation and the indexing operator on the string s,
- #write Python expressions that result in the following:
- #"dog"
- print(s[s.index('d')]+s[s.index('o')]+s[s.index('g')])
- #"tv"
- print(s[s.index('t')]+s[s.index('v')])
- #"ics"
- print(s[s.index('i')]+s[s.index('c')]+s[s.index('s')])
- #"uci"
- print(s[s.index('u')]+s[s.index('c')]+s[s.index('i')])
- #function version of previous problem
- def makeAWord(wordString):
- s = "abcdefghijklmnopqrstuvwxyz"
- returnWord = ''
- for i in range(len(wordString)):
- returnWord = returnWord + s[s.index(wordString[i])]
- print(returnWord)
- makeAWord('wowthisisreallycoolhellowow')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement