Conditional expressions ======================= .. code:: ipython3 age = 28 min_age = 18 **Write an expression which prints ‘You are not allowed in’ if ``age < min_age``:** .. code:: ipython3 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 ```` years’, with an appropriate value for ``x`` - remember f-strings and the ``.format()`` method - add an ``else`` statement, such that ‘Welcome, ````’ is printed if ``age >= min_age``. .. code:: ipython3 name = 'Dave' .. code:: ipython3 if age < min_age: print('You are not allowed in') print(f'Come back in {min_age - age} years') else: print(f'Welcome, {name}') .. parsed-literal:: Welcome, Dave .. code:: ipython3 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`` .. code:: ipython3 allowed = adult and not scary and (cool or rich) allowed .. parsed-literal:: True Iteration --------- .. code:: ipython3 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: .. code:: ipython3 details .. parsed-literal:: (('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``:** .. code:: ipython3 inside = [] refused = [] .. code:: ipython3 for person in details: if person[1] >= min_age: inside.append(person[0]) else: refused.append(person[0]) print(inside) print(refused) .. parsed-literal:: ['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 .. code:: ipython3 percent = (len(inside) / (len(inside) + len(refused)) * 100) print(f'{round(percent, 1)}% of people are inside') .. parsed-literal:: 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 .. code:: ipython3 inside.clear() # or inside = [] refused.clear() # or refused = [] .. code:: ipython3 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:** .. code:: ipython3 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) .. parsed-literal:: ['Mandy', 'Robert', 'Saul', 'Paulina', 'Mahmood'] ['Ben', 'Angela', 'Vicky'] ['John', 'Siri', 'Fay'] .. code:: ipython3 birthdays = ['Saul', 'Mandy'] **For each of the people listed in ``birthdays``, find out their ``age`` from ``details`` and ``print()`` a string in the format ‘```` is ```` today!’:** .. code:: ipython3 for person in birthdays: for entry in details: if entry[0] == person: print(f'{person} is {entry[1]} today!') .. parsed-literal:: 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) .. code:: ipython3 print(names) print(ages) .. parsed-literal:: ['John', 'Mandy', 'Siri', 'Robert', 'Saul', 'Paulina', 'Fay', 'Mahmood', 'Ben', 'Angela', 'Vicky'] [17, 22, 14, 55, 23, 19, 16, 21, 33, 42, 25] .. code:: ipython3 for person in birthdays: idx = names.index(person) age = ages[idx] print(f'{person} is {age} today!') .. parsed-literal:: Saul is 23 today! Mandy is 22 today!