Shortcuts

Conditional expressions

age = 28
min_age = 18

Write an expression which prints ‘You are not allowed in’ if ``age < min_age``:

if age < min_age:
    print('You are not allowed in')

Copy your code from above to the cell below, then do the following: - add a second print() statement to be executed if age < min_age, which uses string formatting to print ‘Come back in <x> years’, with an appropriate value for x - remember f-strings and the .format() method - add an else statement, such that ‘Welcome, <name>’ is printed if age >= min_age.

name = 'Dave'
if age < min_age:
    print('You are not allowed in')
    print(f'Come back in {min_age - age} years')

else:
    print(f'Welcome, {name}')
Welcome, Dave
adult = True
scary = False
cool = False
rich = True

Write a boolean expression which assigns ``True`` to a variable called ``allowed`` if the following conditions are satisfied:

  • adult must be True

  • scary must not be True and

  • either cool or rich must be True

allowed = adult and not scary and (cool or rich)
allowed
True

Iteration

ages = [17, 22, 14, 55, 23, 19, 16, 21, 33, 42, 25]
names = ['John', 'Mandy', 'Siri', 'Robert', 'Saul', 'Paulina', 'Fay', 'Mahmood', 'Ben', 'Angela', 'Vicky']
details = tuple(zip(names, ages))

You don’t need to worry about the code above, but can see that it has been used to create a list of tuples:

details
(('John', 17),
 ('Mandy', 22),
 ('Siri', 14),
 ('Robert', 55),
 ('Saul', 23),
 ('Paulina', 19),
 ('Fay', 16),
 ('Mahmood', 21),
 ('Ben', 33),
 ('Angela', 42),
 ('Vicky', 25))

Use a ``for`` loop to iterate through each tuple in ``details``. For any element which has a number ``>= min_age``, ``.append()`` the name to the ``inside`` list; otherwise, add the name to ``refused``:

inside = []
refused = []
for person in details:
    if person[1] >= min_age:
        inside.append(person[0])
    else:
        refused.append(person[0])

print(inside)
print(refused)
['Mandy', 'Robert', 'Saul', 'Paulina', 'Mahmood', 'Ben', 'Angela', 'Vicky']
['John', 'Siri', 'Fay']

Calculate the percentage of all people who are ``inside`` and print ‘``x``% of people are inside’: - use the built-in round() function to display the value to one decimal place

percent = (len(inside) / (len(inside) + len(refused)) * 100)
print(f'{round(percent, 1)}% of people are inside')
72.7% of people are inside

Run the cell below to .clear() the inside and outside lists: - Alternatively, assign an empty list to each variable

inside.clear() # or inside = []
refused.clear() # or refused = []
capacity = 5
reserves = []

Copy your ``for`` loop code from above and update it so that if the number is ``>=min_age`` but ``len(inside) > capacity``, ``.append()`` the name to the ``reserves`` list:

for person in details:
    if person[1] >= min_age:

        if len(inside) >= capacity:
            reserves.append(person[0])

        else:
            inside.append(person[0])

    else:
        refused.append(person[0])

print(inside)
print(reserves)
print(refused)
['Mandy', 'Robert', 'Saul', 'Paulina', 'Mahmood']
['Ben', 'Angela', 'Vicky']
['John', 'Siri', 'Fay']
birthdays = ['Saul', 'Mandy']

For each of the people listed in ``birthdays``, find out their ``age`` from ``details`` and ``print()`` a string in the format ‘``<name>`` is ``<age>`` today!’:

for person in birthdays:
    for entry in details:
        if entry[0] == person:
            print(f'{person} is {entry[1]} today!')
Saul is 23 today!
Mandy is 22 today!

Can you do the previous task without using ``details``? - Try using .index() with names and ages defined earlier in the notebook (run the cell below to see these again)

print(names)
print(ages)
['John', 'Mandy', 'Siri', 'Robert', 'Saul', 'Paulina', 'Fay', 'Mahmood', 'Ben', 'Angela', 'Vicky']
[17, 22, 14, 55, 23, 19, 16, 21, 33, 42, 25]
for person in birthdays:
    idx = names.index(person)
    age = ages[idx]
    print(f'{person} is {age} today!')
Saul is 23 today!
Mandy is 22 today!

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources