Lesson 3 Assignment
Assignment Date: 13-03-2022
Due Date: 21-03-2022 9PM
Anchor Links
Introduction
Hi all! The purpose of this assignment is to get you familiar with the basics of Python that we covered in Lesson 3 which include:
- Variables and Data Types
- Looping By Element/Index
- If Statements
- Functions
There are 12 questions below which all test all of the concepts you have learnt. If you want, you can refer to the slides from Lesson 3 (go back to the section home to access) for help. Do not hesitate to ask any one of us for further clarifications on the questions below. Approximate completion time is 45-60 minutes at maximum. Do try to distribute the questions among members of your groups to lessen the workload and you might finish it as fast as 15 minutes.
For some of the questions, they ask you to write/complete the code as well as write down some other information in normal english. These kinds of questions are for us to test your understanding of the concepts and hence do please clearly indicate when you are writing normal english (to answer the question) and when you are writing code (to answer the question as well).
We noticed that for the last assignment only 1 or two of the people in a group did the assignment and basically carried the group. Please distribute the questions among the group members as it is very important that everyone gets the practice and we do not anyone lagging behind. Get your entire group to do the assignment together.
You might require a Python development environment for some of these questions and hence we recommend you to use Browxy or replit.com to write your code.
Questions
Question 1
- Define a variable called
num_negand set its value to be a negative integer. - Define a variable called
num_posand set its value to be a positive version ofnum_neg; for e.g ifnum_negis -5, thennum_posshould be 5. - Define a variable called
myStringand set its value to a string. - Define a variable called
myListand set its value to a list containing at least 5 elements. - Define a variable called
myFloatand set its value to the decimal point version ofnum_pos; for e.g ifnum_posis 5, thenmyFloatshould be 5.0.
Question 2
Define two integers. Print the product of the two integers divided by two. Write down what data type the resulting output is in.
For e.g, if the two integers are 5 and 10, then the output should be (5 * 10) / 2 = 25.
Question 3
Define a variable with a data type of float. Define another variable called result which is the result of the multiplication of the float variable and 10. Print out the result variable.
Question 4
Given a list:
lst = ["5", 5]
Use looping by element to loop through lst and print out each element. Write down the key difference between the two elements in the list.
Question 5
Complete the code below that would give the output:
names = ["Bob", "Ashton", "Jack", "Neo"]
for ___ in range(___, ____):
print(names[____])
OUTPUT:
Bob
Ashton
Jack
Neo
Question 6
Given the list:
grocery_items = ["Carrots", "Capsicums", "Onions", "Milk", "Oranges"]
Write a for loop to list all the items in the grocery_list list in a neat manner like so in the sample output below:
1. Carrots
2. Capsicums
3. Onions
4. Milk
5. Oranges
Question 7
Provide the output for the following code (or write down the error if any):
prakhar = "trivedi"
ashton = "neo"
if prakhar == ashton:
print("Prakhar is the imposter.")
else:
print("Prakhar is Prakhar")
elif prakhar == "trivedi":
print("Prakhar is Trivedi.")
Question 8
Given two variables:
apple_cost = 1.5
orange_cost = 1.2
Write an if statement that compares the two variables in the sense that if apple_cost is higher than orange_cost, "Apple is more expensive than orange" is printed out, if not, "Apple is less expensive than orange" is printed out.
Question 9
NOTE: This question is one of the more tedious and difficult ones. It is meant to confuse you. Please look at the code extremely carefully and identify the difference between
falseandFalseandTrueandtruebefore answering.
Provide the output for the following code:
false = True
true = False
if True == False:
print("True is false...life is a lie.")
elif false and true:
print("Both false and true are true, I think...")
elif (not false) and true:
print("Both true and true are true, obviously.")
elif (not true) and (not false):
print("The opposite of both variables is true.")
elif false or (not true):
print("Either false or false is true..., what?")
elif not (false and true):
print("The opposite of false and true is true?????")
else:
print("I give up....")
Question 10
Write a function called percent_calculator() that takes in two arguments of score and out_of and calculates the percentage a student scored for their test.
For example, if a student got 21/30 for a test, the argument score would be 21 and out_of would be 30 and the value returned by the function would be 70 (because 21/30 is 70%).
Question 11
Write the output for the code below:
def needToCharge(current_battery, low_power_mode):
if current_battery <= 10:
return True
elif current_battery <= 20 and low_power_mode == True:
return False
elif current_battery <= 20 and low_power_mode == False:
return True
else:
return False
print(needToCharge(14, False))
Question 12
Complete the code below with the most likely values:
def checkPassword(____):
if ____ == "P@55W04D123":
return True
else:
return False
print(checkPassword(input("Enter your password: ")))
Submission
You are allowed to look up online for help, we recommend w3schools and stackoverflow. Among your groups, create a Google Document and put your answers to the questions in there. Share the said document to Issac at lip_kin_cung_issac@ngeeannsec.net or Prakhar at trivedi_prakhar_nilesh@ngeeannsec.net
Submit your solutions by Monday, 21st March 2022, 9pm