![]() ![]() In the if condition, we use the string method startswith(…) twice connected by the logical or operator to check whether the respective name starts with letter ‘M’ or the letter ‘N’. The new value expression is simply n because we want to keep those elements from the original list that satisfy the condition unchanged. We are also using a different variable name here ( n) so that you can see that you can choose any name here but, of course, you need to use that variable name consistently directly after the for and in the condition following the if. This time, the original list is defined before the actual list comprehension rather than inside it as in the previous examples. To show that this not only works with numbers, here is an example in which we use list comprehension to simply reduce a list of names to those names that start with the letter ‘M’ or the letter ‘N’: names = This example makes use of the optional if condition to make sure that the new value expression is only evaluated for certain elements from the original list, namely those for which the remainder of the division by 2 with the Python modulo operator % is zero. We can easily extend this example to only include the squares of numbers that are even: evenNumbersSquared = The results are collected to form the new list produced by the entire list comprehension. What happens when Python evaluates this list comprehension is that it goes through the numbers in the list produced by range(1,11), so the numbers from 1 to 10, and then evaluates the expression x**2 with each of these numbers assigned to variable x. In case you haven’t seen this before, ** is the Python operator for a to the power of b. To give a first example, here is how this notation can be used to create a list containing the squares of the numbers from 1 to 10: squares = The if and following condition are optional. ![]() ![]() The fixed parts are written in bold here, while the parts that need to be replaced by some expressions using some variable are put into angular brackets. A list comprehension has the general form This kind of compact notation has been adopted by Python for defining lists and it is called list comprehension. The notation uses to indicate that we are defining a set, then an expression that describes the elements of the set based on some variable (x 2) followed by a set of criteria specifying the values that this variable (x) can take (x ∈ M and x > 11). What is being said here is that the set N should contain the squares of all numbers in set M that are larger than 11. From mathematics, you probably are familiar with the elegant way of defining sets based on other sets using a compact notation as in the example below: Like the first lesson, we are going to start Lesson 2 with a bit of Python theory. Remember to visit the Lesson 2 Discussion Forum to post/answer any questions or comments pertaining to Lesson 2 ![]() Submit your code for the programming assignment and 400 words write-up with reflections You may find it useful to print this page out first so that you can follow along with the directions. To finish this lesson, you must complete the activities listed below. Please refer to the Calendar for specific time frames and due dates. Finally, we will discuss Python package management and package managers, and how they can be used to disseminate Python code. Then we will focus on how write Python programs with a graphical user interface based on the QT5 library and PyQt5 package. We will look at some more advanced Python concepts and how to access data on the web from within a Python program. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |