Week 3
/// 1. Python Conditionals - Making decisions
x = 0 #declares a variable called x, which is initialised to 0
x = input("Enter a number ") #asks the user to enter a number
if(x < 12): #if x is lower than 12
print ("Lower than 12") #then it prints Lower than 12
else:
print("Another one") #If the statement above isn't true then it prints Another one
/// 2. More decisions
a) If the number entered by the user is lower than 7, the message “lower than 7” is printed to the console;
b) Otherwise, if the number is 7, “It is 7” is printed; and
c) Otherwise, the message “more than 7” is printed.
x = 0 #declares a variable called x, which is initialised to 0
x = input("Enter a number ") #asks the user to enter a number
if(x < 7):
print ("Lower than 7") #a) prints the message lower than 7
elif x==7:
print ("It is 7") #b) prints It is 7 if the number is 7
else:
print ("More than 7") #prints More than 7 if the number is not 7 and it's not lower than 7
/// 3. Python conditionals
a) declare a variable called y, which is initialised to a value between 1 and 100 (you can choose this value), then
b) write and test short scripts to do each of the checks below.
- If y is equal to 1, print out “y is 1”
- If y is greater than 5, print out “y is high”
- If y is less than 5, print out “y is low”
- If y is not 7, print out “y is unlucky”
- If y is either 2 or 3, print out “y is 2 or 3”
- If y is more than 4 but “less than or equal to” 7, print out “y is mid-range”
y = 10 #initializes a value to 0
if (y == 1): #nested if-statement
print("y is 1") #if y is equal to 1 then it prints y is 1
if (y>5):
print("y is high")
if (y!=7):
print("y is unlucky")
if (y<5):
print("y is low") #if the statement above is not true then it prints
if (y!=7):
print("y is unlucky")
if (y==2 or y==3):
print("y is 2 or 3")
if (y > 4 and y<=7):
print("y is mid-range")
For this code I needed to ensure than no indentation was wrongly placed. Also the nested if's need to be placed according to logical priority. Best examples to use when testing this script would ideally be numbers within and out of the limits of the if-statement.
/// 4. Short circuit
x = 6
y = 2
print(x >= 2 and (x/y) > 2)
The result is True because the two conditions are both True. 6>=2 and (6/2)>2. If only one condition was True then the output would be False
x = 1
y = 0
print(x >= 2 and (x/y) > 2)
This is a False output. x>=2 is False and (x/y)>2 would cause an error since we are dividing by zero. But the output wouldn't be an error since the code won't run until this command. It stops when it finds a False.
x = 6
y = 0
print(x >= 2 and (x/y) > 2)
The calculation would fail because dividing by 0 would cause an error. This time the first condition is True, so the circuit would go on to the next one which is an error.
///5. Guard evaluation
x = 1
y = 0
print(x >= 2 and y != 0 and (x/y) > 2)
# The first condition is False so the output will be False
x = 6
y = 0
print(x >= 2 and y != 0 and (x/y) > 2)
# The first conditions result is True but the second condition is False. So the output is False
x = 6
y = 0
print(x >= 2 and (x/y) > 2 and y != 0)
# The first condition is True but the second one would give an error. The output will be an error
Short-circuiting means that the code stops when it finds a True operation. The first value to be determined would be the one to the far left and the code will be read from left to right. It is important that we place the values we want first to their correspondent spot, or else we won't have the desired result.
///6. Lists
listOfLights = ["keyLight", "sideLight", "backLight", "fillLight"]
print(listOfLights[0]) #prints "keyLight". prints the first item of the list
print(listOfLights[2]) #prints "backLight". prints the third item of the list
print(listOfLights[3]) #prints "fillLight". prints the fourth item of the list
print(listOfLights[-1]) #prints "fillLight". since we count from 0 the list will count backwards and print the last item in the list.
print(listOfLights[0:2]) #prints "sideLight" and "backLight". prints the items in spot 0 and 1 of the list
print(len(listOfLights)) #prints the whole list
Print(myList[34]) #When you try to access an element that does not exist, Python will throw an error. since this list has only 4 elements this will be an error
///7. More Lists
Methods are built-in-functions within lists
listOfLights = ["keyLight", "sideLight", "backLight", "fillLight"]
listOfLights.sort() #The sort() method is created to rearange the lists elements by an ascending order. It is also possible to create a function that decides on the criterias for the sorting
print(listOfLights)
listOfLights.reverse() #This method (.reverse) reverses the sorting order of the lists elements
print(listOfLights)
listOfLights.append("newLight") #This method (.append) appends an element to the last spot of the list
print(listOfLights)
///8. Loops
#For-Loop that prints Python is awesome 6 times
for x in range(5): #counts from 0-5
print("Python is awesome")
///9.More loops
Write a for-loop script that prints out the following:
Monday is day 1 of the Week
Tuesday is day 2 of the Week
Wednesday is day 3 of the Week
Thursday is day 4 of the Week
... Sunday is day 7 of the Week
weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"]:
for z in range(1,8):
print( weekdays[z] "is day" z "of the Week")
#This code does not work as I would expect unfortunately. First off, I created a list with all the weekdays and then thought of printing the number according to the numbers within the range.
Leave a comment
Log in with itch.io to leave a comment.