codelabs 1-5
Write an expression that is the concatenation of the strings "Hello" and "World".
"Hello" + "World"
Given the already defined variables is_full_time_student and age, write an expression that evaluates to True if age is less than 19 or is_full_time_student is True.
age < 19 or is_full_time_student == True
Given a variable called average, write an expression that is True if and only if average is less than 60.5.
average < 60.5
Write the definition of a function add, that receives two int parameters and returns their sum.
def add (int1,int2): return int1+int2
Write the definition of a function named sum_list that has one parameter, a list whose elements are of type int. The function returns the sum of the elements of the list as an int.
def sum_list(x): return sum(x)
Given a String variable named sentence that has been initialized, write an expression whose value is the number of characters in the String referred to by sentence.
len(sentence)
Write an expression whose value is the concatenation of the three str values associated with name1, name2, and name3, separated by commas. So if name1, name2, and name3, were (respectively) "Neville", "Dean", and "Seamus", your expression's value would be "Neville,Dean,Seamus".
name1+ ','+name2+','+name3
Given the list my_list containing integers, create a list consisting of all the even elements of my_list. Associate the new list with the variable new_list.
new_list = [] for i in my_list: if i % 2 == 0: new_list.append(i)
The dimensions (width and length) of room1 have been read into two variables: width1 and length1. The dimensions of room2 have been read into two other variables: width2 and length2. Write a single expression whose value is the total area of the two rooms.
width1 * length1 + width2 * length2
Write an expression that evaluates to True if and only if the bool associated with worked_overtime is True.
worked_overtime == True
Given three int variables that have been given values, areaCode, exchange, and lastFour, write a string expression whose value is the string equivalent of each these variables joined by a single hyphen (-) So if areaCode, exchange, and lastFour, had the values 800, 555, and 1212, the expression's value would be "800-555-1212". Alternatively, if areaCode, exchange, and lastFour, had the values 212, 867 and 5309 the expression's value would be "212-867-5309".
(str(areaCode) + "-" + str(exchange) + "-" + str(lastFour))
Write an expression whose value is the character at index 3 of the str associated with s.
s[3]
Given the variable c, whose associated value is a str, write an expression that is True if and only if c is not equal to a string consisting of a single blank.
c!=" "
Assume that to_the_power_of is a function that expects two int parameters and returns the value of the first parameter raised to the power of the second parameter. Write a statement that calls to_the_power_of to compute the value of cube_side raised to the power of 3 and that associates this value with cube_volume.
cube_volume = to_the_power_of (cube_side,3)
Write the definition of a function power_to, which receives two parameters. The first is a double and the second is an int. The function returns a double. If the second parameter is negative, the function returns zero. Otherwise it returns the value of the first parameter raised to the power of the second.
def power_to(x,y): if y < 0: return 0 else: return x ** y
Write the definition of a function twice, that receives an int parameter and returns an int that is twice the value of the parameter.
def twice(x): return 2*x
Write a statement that defines the variable denominations, and associates it with a list consisting of the following six elements: 1, 5, 10, 25, 50, 100, in that order.
denominations = [1,5,10,25,50,100]
Given that add, a function that expects two int parameters and returns their sum, and given that two variables, euro_sales and asia_sales, have already been defined: Write a statement that calls add to compute the sum of euro_sales and asia_sales and that associates this value with a variable named eurasia_sales.
eurasia_sales = add(euro_sales,asia_sales)
Write a for loop that prints the integers 50 through 1, each on a separate line. Use no variables other than count.
for count in range(50,0,-1): print(count)
Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, each value on a separate line.
for x in range (1,35): print(x*5)
Write a for loop that prints the integers 0 through 39, each value on a separate line.
for x in range(0,40): print(x)
Write a for loop that prints the odd integers 11 through 121 inclusive, each value on a separate line.
for x in range(11,122,2): print(x)
Assume there is a variable, h already assigned a positive integer value. Write the code necessary to assign its square to the variable g. For example, if h had the value 8 then g would get the value 64.
g=h**2
Given two variables matric_age and grad_age, write a statement that makes the associated value of grad_age 4 more than that of matric_age.
grad_age=(4+matric_age)
Working overtime is defined as having worked more than 40 hours during the week. Given the variable hours_worked, write an expression that evaluates to True if the employee worked overtime.
hours_worked > 40
Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to compute the sum of the first n prime numbers. The sum should be associated with the variable total. Note: is_prime takes an integer as a parameter and returns True if and only if that integer is prime.
i=2 count=0 total=0 while (count<n): if(is_prime(i)): total+=i count+=1 i+=1
Write an if/else statement that adds 1 to minors if age is less than 18, adds 1 to adults if age is 18 through 64 and adds 1 to seniors if age is 65 or older.
if age < 18: minors += 1 elif age >= 65: seniors +=1 else: adults +=1
Write an if/else statement that compares age with 65, adds 1 to senior_citizens if age is greater than or equal to 65, and adds 1 to non_seniors otherwise.
if age >= 65: senior_citizens +=1 else: non_seniors +=1
Write a conditional that assigns 10,000 to bonus if the goods_sold is greater than 500,000.
if goods_sold > 500000: bonus = 10000
Assume that the variables gpa, deansList and studentName, have been initialized. Write a statement that adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5.
if gpa > 3.5: deansList = deansList +1 print(studentName)
Write a statement that toggles on_off_switch. That is, if on_off_switch is False, it is made equal to True; if on_off_switch is True, it is made equal to False.
if on_off_switch == False: on_off_switch =True else: on_off_switch=False
Write a conditional that decreases the value associated with shelf_life by 4 if the value associated with outside_temperature is greater than 90.
if outside_temperature > 90: shelf_life-=4
Write an if/else statement that compares sold_yesterday and sold_today, and based upon that comparison assigns sales_trend the value -1 (the case where sold_yesterday is greater than sold_today) or 1.
if sold_yesterday > sold_today: sales_trend =-1 else: sales_trend = 1
Write a statement that adds 1 to the variable reverseDrivers if the variable speed is less than 0,adds 1 to the variable parkedDrivers if the variable speed is less than 1,adds 1 to the variable slowDrivers if the variable speed is less than 40,adds 1 to the variable safeDrivers if the variable speed is less than or equal to 65, and otherwise adds 1 to the variable speeders.
if speed < 0: reverseDrivers +=1 elif speed < 1: parkedDrivers +=1 elif speed < 40: slowDrivers +=1 elif speed <= 65: safeDrivers +=1 else: speeders +=1
Write an if/else statement that assigns True to fever if temperature is greater than 98.6; otherwise it assigns False to fever.
if temperature > 98.6: fever = True else: fever = False
Write a conditional that assigns True to fever if temperature is greater than 98.6.
if temperature > 98.6: fever = True
Write a conditional that multiplies the value associated with pay by one-and-a-half if worked_overtime is associated with True.
if worked_overtime == True: pay *= 1.5
Given x and y, each associated with an int, write a fragment of code that associates the larger of these with another variable named max.
if x > y: max=x else: max=y
Given the variables x, y, and z, each associated with an int, write a fragment of code that assigns the smallest of these to min.
if x<=y and x<=z: min=x if y<=x and y<=z: min=y if z<=x and z<=y: min=z
Given the list names, find the largest element in the list and swap it with the last element. For example, the list ["Carlton", "Quincy" "Adam", "Bernard"] would become ["Carlton", "Bernard", "Adam", "Quincy"]. Assume names is not empty.
index = names.index(max(names)) names[index], names[-1] = names[-1], names[index]
Given: a variable current_members that refers to a list, and a variable member_id that has been defined. Write some code that assigns True to a variable is_a_member if the value associated with member_id can be found in the list associated with current_members, but that otherwise assigns False to is_a_member. Use only current_members, member_id, and is_a_member.
is_a_member = False for i in range(0,len(current_members)): if current_members[i] == member_id: is_a_member = True
Write an expression that evaluates to True if and only if is_a_member is False.
is_a_member == False
Given two variables, is_empty which is associated with a bool, indicating whether a class roster is empty or not, and number_of_credits which is associated with an int, containing the number of credits for a class, write an expression that evaluates to True if the class roster is not empty and the class is more than two credits.
is_empty == False and number_of_credits >2
Given two variables, is_empty which is associated with a bool indicating whether a class roster is empty or not, and number_of_credits which is associated with an int (the number of credits for a class), write an expression that evaluates to True if the class roster is empty or the class is exactly three credits.
is_empty or number_of_credits == 3
Given a variable n refers to a positive int value, use two additional variables, k and total to write a for loop to compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus your code should put 1*1*1 + 2*2*2 + 3*3*3 +... + n*n*n into total. Use no variables other than n, k, and total.
k = 0 total = 0 for k in range(n+1): total = total + k * k * k
Given variables first and last, each of which is associated with a str, representing a first and a last name, respectively. Write an expression whose value is a str that is a full name of the form "Last, First". So, if first were associated with "alan" and last with "turing", then your expression would be "Turing,Alan". (Note the capitalization! Note: no spaces!) And if first and last were "Florean" and "fortescue" respectively, then your expression's value would be "Fortescue,Florean".
last.capitalize()+','+ first.capitalize()
Given a String variable named sentence that has been initialized, write an expression whose value is the index of the very last character in the String referred to by sentence.
len(sentence[:-1])
Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that do not appear in lst2 together with all the elements of lst2 that do not appear in lst1. For example, if lst1 is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4, 1, 5], then the new list would be [1, 1, 3, 5, 6]. Note that duplicate elements are also duplicated in the new list. Associate the new list with the variable new_list, and don't forget to sort the new list.
new_list = [] for item in lst1: if item not in lst2: new_list.append(item) for item in lst2: if item not in lst1: new_list.append(item) new_list.sort()
Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lst2. For example, if lst1 is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4], then the new list would be [2, 2, 4]. Note that duplicate elements in lst1 that appear in lst2 are also duplicated in the new list. Associate the new list with the variable new_list, and don't forget to sort the new list.
new_list=[] for n in lst1: if n in lst2: new_list.append(n) new_list.sort()
Given two variables, is_empty of type bool, indicating whether a class roster is empty or not, and number_of_credits of type integer, containing the number of credits for a class, write an expression that evaluates to True if the class roster is not empty and the class is one or three credits.
not is_empty and (number_of_credits == 1 or number_of_credits == 3)
Write an expression that evaluates to True if the int associated with number_of_prizes is divisible (with no remainder) by the int associated with number_of_participants. Assume that the latter is not zero.
number_of_prizes%number_of_participants==0
Given four int variables that have been given values, octet1, octet2, octet3, and octet4, write a string expression whose value is the string equivalent of each these variables joined by a single period (.) So if octet1, octet2, octet3, and octet4, had the values 129, 42, 26, and 212 the expression's value would be "129.42.26.212". Alternatively, if octet1, octet2, octet3, and octet4, had the values 192, 168, 1and 44 the expression's value would be "192.168.1.44".
octet1 + octet2 + octet3 + octet4
Write a statement to open the file yearsummary.txt in a way that erases any existing data in the file.
open('yearsummary.txt' , 'w')
Use the file object output to write the string "3.14159" to a file called pi.
output = open('pi' , 'w') output.write('3.14159')
Write the code to call a function named send_variable and that expects a single int parameter. Suppose a variable called x refers to an int. Pass this variable as an argument to send_variable.
send_variable(x)
Assume that play_list refers to a non-empty list, and that all its elements refer to values of type int. Write a statement that associates a new value with the first element of the list. The new value should be equal to twice the value of the last element of the list.
play_list[0]=2*play_list[-1]
Write a statement that defines plist as the list containing exactly these elements (in order): "spam", "eggs", "vikings" .
plist = ["spam","eggs","vikings"]
Given a variable plist, that refers to a list with 34 elements, write an expression that refers to the last element of the list.
plist[-1]
Given a variable plist, that refers to a non-empty list, write an expression that refers to the first element of the list.
plist[0]
Given that a variable named plist has been defined and refers to a non-empty list, write a statement that associates its first element with 3.
plist[0] = 3
Assume that a variable named plist has been defined and is associated with a list that consists of 12 elements. Assume further that k refers to an int between 2 and 8. Assign 22 to the element just before the element in plist whose index is k .
plist[k-1] = 22
Assume that a variable named plist refers to a list with 12 elements, each of which is an int. Assume that the variable k refers to a value between 0 and 6. Write a statement that assigns 15 to the list element whose index is k.
plist[k]=15
Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the amount of change (in cents) that would have to be paid.
price % 100
You are given two variables, both already defined. One is named price and is associated with a float and is the price of an order. The other is total_number and is associated with an int and is the number of orders. Write an expression that calculates the total price for all orders.
price * total_number
Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the number of single dollars that would have to be paid.
price//100
Assume that print_error_description is a function that expects one int parameter and returns no value. Write a statement that invokes the function print_error_description, passing it the value 14.
print_error_description(14)
Given print_larger, a function that expects two parameters and returns no value and given two variables, sales1 and sales2, that have already been defined, write a statement that calls print_larger, passing it sales1 and sales2.
print_larger(sales1,sales2);
Assume that print_todays_date is a function that uses no parameters and returns no value. Write a statement that calls (invokes) this function.
print_todays_date ()
Write an expression that evaluates to True if and only if the variables profits and losses are exactly equal.
profits==losses
Write an expression whose value is the same as the str associated with s but with all lower caseletters. Thus, if the str associated with s were "McGraw15", the value of the expression would be "mcgraw15".
s.lower()
Write an expression that is the concatenation of the str associated with s1 and that associated with the str s2.
s1 + s2
Given the strings s1 and s2 that are of the same length, create a new string consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on (in other words the new string should consist of alternating characters of the reverse of s1 and s2). For example, if s1 contained hello" and s2 contained "world", then the new string should contain "odlllreohw". Assign the new string to the variable s3.
s3 = '' for i in range(len(s1)): s3 = s1[i] + s2[i] + s3
Write an expression whose value is the last character in the str associated with s.
s[-1]
Write an expression whose value is the str that consists of the second to last character of the str associated with s.
s[-2]
Write an expression whose value is the str that consists of the first four characters of the str associated with s.
s[0:4]
Given that s refers to a string, write an expression that evaluates to a string that is a substring of s and that consists of all the characters of s from its start through its ninth character.
s[0:9]
Write an expression whose value is the str that consists of the second through fifth characters of the str associated with s.
s[1:5]
Write an expression whose value is the result of converting the int value 42 to a str (consisting of the digit 4 followed by the digit 2).
str(42)
Write an expression whose value is the result of converting the int value associated with x to a str. So if 582 was the int associated with x you would be converting it to the str "582".
str(x)
Given the already defined variables temperature and humidity, write an expression that evaluates to True if and only if temperature is greater than 90 and humidity is less than 10.
temperature > 90 and humidity < 10
Use two variables k and total to write a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus, your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total = 0 k = 1 while k <= 50: total = total + k**2 k +=1 print(total)
Write an expression that evaluates to True if the int associated with width_of_box is not divisible (with no remainder) by the int associated with width_of_book. Assume that the latter is not zero.
width_of_box%width_of_book!=0
Assume that the variable plist has been defined and refers to a list. Write a statement that assigns the next to last element of the list to x.
x = plist[-2]
Write an expression that evaluates to True if and only if the value of x is equal to zero.
x == 0
Given that the variables x and y have been defined, write an expression that evaluates to True if x is non-negative and y is negative.
x >= 0 and y < 0
Write an expression that evaluates to True if the value associated with x is even, but False if the value is odd.
x%2==0
Given that the variables x and y have been defined, write an expression that evaluates to True if x is non-negative or y is negative.
x>=0 or y<0
Given the already defined variables years_with_company and department, write an expression that evaluates to True if years_with_company is less than 5 and department is not equal to 99.
years_with_company<5 and department != 99