Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory

pdf

School

Smithfield-selma High *

*We aren’t endorsed by this school

Course

10

Subject

Computer Science

Date

Nov 24, 2024

Type

pdf

Pages

9

Uploaded by DukeDanger9183

Report
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 1/9 I learned about containment operator precedence and identity I did not have trouble Lucas DuBois 12/11/23 Containment (in, not in) Identity (is, is not) Operator precedence Students will be able to: Test whether a list contains a certain element Test whether a string is contained in another string Test the identity of objects (i.e. int, ±oat, lists, string) Recognize the effects of operator precedence (including assignment (=), relational (<, >=,...), Boolean (and, or, not), arithmetic (/ // % * + -), identity (is), and containment (in)) Before submission : Remember for max credit, you need to add a complete header comment in a code cell at the very top of this ²le and descriptive comments in each code cell below (examples AND tasks) that explains what your code is doing. ALL functions need a formatted docstring – see the bottom of Mod 3 Lesson 1 Activity for the requirements. Module Seven Lesson Seven Activity Concepts View Containment (in, not in) video You will commonly encounter the need to test whether an element is contained in another container (i.e. list, tuple, string, etc.). For example, you might want to test whether 5 is an element of the list [4, 8, 5, 6] , or you might need to know if a substring "or" is contained within another string "Hello World" . It is possible to perform these tests by iterating over the elements of the container (list) and comparing them one by one to the element of interest. However, because this procedure is commonly used, Python has a containment operator ( in ), which can perform this procedure much more e³ciently. NOTE: You can test if an element is NOT contained in another container by using the ( not in ) operator. Containment ( in , not in ) Examples Is a number contained in a list? In this example, you will see how to test if a number is contained in another list of numbers List containment
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 2/9 lst_container = [4, 8, 5, 6] x = 5 if (x in lst_container): print(x, "is contained in list") else: print(x, "is NOT contained in list") x = 10 if (x in lst_container): print(x, "is contained in list") else: print(x, "is NOT contained in list") A Python list can contain elements of different types. In this example, you will test if an element is contained in a list containing a: number, list, string. Is an element contained in a list? lst_container = [4, [7, 3], 'string element'] # 4 is an element in lst_container x = 4 print(x, "contained in lst_container:", x in lst_container) # 7 is an element of a list inside lst_container, but it is NOT an element of the lst_container x = 7 print(x, "contained in lst_container:", x in lst_container) # [7, 3] is an element of lst_container x = [7, 3] print(x, "contained in lst_container:", x in lst_container) You can test if a substring is contained in another larger string. String containment sentence = "This is a test sentence" word1 = "test" word2 = "something" # testing if word1 is a substring of sentence if (word1 in sentence): print(word1, "is contained in:", sentence) else: print(word1, "is not contained in:", sentence) # testing if word2 is a substring of sentence if (word2 in sentence): print(word2, "is contained in:", sentence) else: print(word2, "is not contained in:", sentence) # another method to test if word2 is a substring of sentence # using the not operator if (word2 not in sentence): print(word2, "is not contained in:", sentence) else: print(word2, "is contained in:", sentence) Number Containment ( in , not in ) Write a program to prompt the user for an integer input between 0 and 100 print if the number is contained in lst Task 1
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 3/9 lst = [22, 89, 69, 78, 58, 22, 56, 13, 74, 8, 32, 58, 8, 63, 46, 79, 9, 38, 25, 96] #You enter a number 0-100 and if its on the list it num = int(input("Enter a number between 0 and 100: ")) if (num in lst): print(num, "is contained in",lst) else: print(num, "is not contained in",lst) Enter a number between 0 and 100: 67 67 is not contained in [22, 89, 69, 78, 58, 22, 56, 13, 74, 8, 32, 58, 8, 63, 46, 79, 9, 38, 25, 96] List Containment ( in , not in ) The records list below contains information about a company's employees Each of the elements in records is a list containing the name and ID of an employee. Write a program to test if the applicant below is contained in records and display an appropriate message Task 2 records = [['Colette', 22347], ['Skye', 35803], ['Alton', 45825], ['Jin', 24213]] #If the applicant is in the companys employess record applicant = ['Joana', 20294] if applicant in records: print(applicant, "is in the company's employees record") else: print(applicant, "is not in the company's employees record") ['Joana', 20294] is not in the company's employees record String Containment ( in , not in ) Write a program to prompt the user for a letter (capital or small) Print if the letter is a vowel HINT: Use a string containing all the vowels and the in or not in operator Task 3 letter = input("Enter a letter (capital or small): ").lower() #If a letter is a vowel upper or lowercase it will tell you vowels = "aeiou" if letter in vowels: print(letter,"is a vowel") else: print(letter,"is not a vowel") Enter a letter (capital or small): A a is a vowel Concepts View Identity (is, is not) video Python is an object-oriented programming language that utilizes objects. You have been using objects; however, you have called them variables and lists. Python saves objects in certain memory locations, knowing the locations is of little importance; however, knowing if two seemingly different objects are at the same memory location is important. This will be critically important when dealing with sequences such as list, tuples, strings, and dictionaries. In Python, the concepts of identity and equality are related but not the same, for example: When two objects are saved in the same memory location, they are equal and identical Identity ( is , is not )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 4/9 When two objects are saved in different memory locations and contain the same information, they are equal but not identical When two objects are saved in different memory locations and contain different information, they are not equal nor identical You can test whether two objects contain the same information using the equality operators == or != . To test whether two objects are identical, you need to use the identity operators is or is not . Examples In the following examples, when objects are identical, it is implied they are saved in the same memory location. int literals Equal int literals are saved in the same memory location Identity of variables containing numerical literals # x, y: equal, identical x = 5 y = 5 print("x equal y ? ", x == y) print("x is identical to y ?", x is y) # x, y: not equal, not identical x = 5 y = 6 print("x equal y ? ", x == y) print("x is identical to y ?", x is y) Equal float literals are not identical. In other words, equal float literals are saved in different memory locations. float literals # x, y: equal, not identical x = 5.6 y = 5.6 print("x equal y ? ", x == y) print("x is identical to y ?", x is y) # x, y: not equal, not identical x = 5.6 y = 10.6 print("x equal y ? ", x == y) print("x is identical to y ?", x is y) The following examples are particularly important to understanding the concept of identity and equality. You will see how you can create two equal but not identical lists, then you will see how to create two identical (and equal) lists and the effect of changing one on the other. Identity of variables containing lists You can create two equal but not identical lists, by assigning the same list literal to two different variables. A change in one of the list does not have any effect on the content of the other. Equal but not identical lists
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 5/9 # Different lists containing the same data x = [4, 9, 8] y = [4, 9, 8] # x and y are equal, because they contain the same data # x and y are NOT identical, because they are saved in different memory locations print("x equal y ? ", x == y) print("x is identical to y ?", x is y) # Because they are not identical, changing x does not affect y x[1] = 5 print() print("After changing x[1]") print("x =", x) print("y =", y) print("x equal y ? ", x == y) print("x is identical to y ?", x is y) Two variables referring to the same list are called identical variables. They can be treated as two names for the same list; in other words, both of the variables refer to the same memory location and a change in one is re±ected as the same change in the other. You can simply create an identical variable by assigning it the variable of interest. The following example illustrates this idea. Equal and identical lists # Identical list x = [4, 9, 8] y = x # x and y are equal, because they contain the same data # x and y are identical, because they are saved in the same memory location print("x equal y ? ", x == y) print("x is identical to y ?", x is y) # Because they are identical, changing x also changes y x[1] = 5 print() print("After changing x[1]") print("x =", x) print("y =", y) print("x equal y ? ", x == y) print("x is identical to y ?", x is y) String identity and equality is very similar to that of lists. However, when you assign equal strings to different variables, the interpreter might detect this and optimize the code by making the variables identical. Identity of variables containing string literals # s1, s2: equal, not identical s1 = 'whole milk' s2 = 'whole milk' print("s1 equal s2 ? ", s1 == s2) print("s1 is identical to s2 ?", s1 is s2) print("s1 is not identical to s2 ?", s1 is not s2) # s1, s2: equal, identical s1 = 'whole milk' s2 = s1 print("s1 equal s2 ? ", s1 == s2) print("s1 is identical to s2 ?", s1 is s2) print("s1 is not identical to s2 ?", s1 is not s2) # s1, s2: equal, identical (after interpreter optimization) s1 = 'python' s2 = 'python' print("s1 equal s2 ? ", s1 == s2) print("s1 is identical to s2 ?", s1 is s2) print("s1 is not identical to s2 ?", s1 is not s2)
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 6/9 # s1, s2: not equal, not identical s1 = 'python' s2 = 'java' print("s1 equal s2 ? ", s1 == s2) print("s1 is identical to s2 ?", s1 is s2) print("s1 is not identical to s2 ?", s1 is not s2) Identity of variables containing strings( is , is not ) Write a program to: Create a variable e that is equal but NOT identical to s Test the equality and identity of s and e and print the results Create a variable i that is equal and identical to s Test the equality and identity of s and i and print the results Test the equality and identity of e and i and print the results Task 4 # In this code, the '==', 'is', and 'is not' operators are used to test if variables s, e, or i are equal, identical, and not identical. e = "Whole Wheat Bread" print("s equal e ? ", s == e) print("s is identical to e ?", s is e) print("s is not identical to e ?", s is not e) print() i = s print("s equal i ? ", s == i) print("s is identical to i ?", s is i) s = "Whole Wheat Bread" # Since s and e equal "Whole wheat bread", they are equal. However, they are not identical because they are saved in different memory lo # Changing one doesn't affect the other. # Since s and i equal "Whole wheat bread", they are equal.They are also identical because they are saved in the same memory location. Ch # the other. # Lastly, e and i equal "Whole wheat bread", so they are equal. However, they are not identical because they are saved in different memo print("s is not identical to i ?", s is not i) print() print("e equal i ? ", e == i) print("e is identical to i ?", e is i) print("e is not identical to i ?", e is not i) s equal e ? True s is identical to e ? False s is not identical to e ? True s equal i ? True s is identical to i ? True s is not identical to i ? False e equal i ? True e is identical to i ? False e is not identical to i ? True Identity of variables containing lists( is , is not ) Write a program to: Create a variable e that is equal but NOT identical to x Test the equality and identity of x and e and print the results Create a variable i that is equal and identical to x Test the equality and identity of x and i and print the results Test the equality and identity of e and i and print the results Task 5
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 7/9 # In this code, the '==', 'is', and 'is not' operators are used to test if variables x, e, or i are equal, identical, and not identical. e = [[-1, 2],[3, 4],[-5, 6]] print("x equal e ? ", x == e) print("x is identical to e ?", x is e) print("x is not identical to e ?", x is not e) print() i = x print("x equal i ? ", x == i) print("x is identical to i ?", x is i) print("x is not identical to i ?", x is not i) x = [[-1, 2],[3, 4],[-5, 6]] # Since x and e equal "Whole wheat bread", they are equal. However, they are not identical because they are saved in different memory lo # Changing one doesn't affect the other. # Since x and i equal "Whole wheat bread", they are equal. They are also identical because they are saved in the same memory location. C # the other. # Lastly, e and i equal "Whole wheat bread", so they are equal. However, they are not identical because they are saved in different memo print() print("e equal i ? ", e == i) print("e is identical to i ?", e is i) print("e is not identical to i ?", e is not i) x equal e ? True x is identical to e ? False x is not identical to e ? True x equal i ? True x is identical to i ? True x is not identical to i ? False e equal i ? True e is identical to i ? False e is not identical to i ? True Concepts View Operator Precedence video You have seen that the Boolean operator not has a higher precedence than the Boolean operators and and or . You also know that in arithmetic expressions, multiplication has a higher precedence than addition and subtraction. In Python, you can combine different operator types in the same expression ( 3 + 1 &gt; 5 ); when you do, operator precedence still applies. The following table summarizes operator precedence from lowest precedence to highest precedence. Operators in the same row have the same precedence, and the precedence is resolved from left to right, for example in ( 3 * 6 / 9 ), * and / have the same precedence, and Python will perform the multiplication ²rst ( 18 / 9 ) followed by the division ( 2 ). Operator Short Description or Boolean or and Boolean and not Boolean not in, not in, is , is not, <, <=, >, >=, !=, == Containment and identity test, relational comparison +, - Arithmetic addition and subtraction *, /, //, % Arithmetic, multiplication, division, int division, modulo ** Exponentiation (), [], {} Parentheses, brackets NOTE: When in doubt, use the parentheses operator to control the precedence in an expression Operator Precedence Examples Use the operator precedence table to predict the outcome of these examples before looking at the answers. Arithmetic operations # * has higher precedence 2 + 3 * 6
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 8/9 # To change precedence, we add ( ) (2 + 3) * 6 Combined operations # Arithmetic and relational operations 3 * 2 < 10 # Exponentiation has a higher precedence 2**3 + 1 == 16 # Adding () changes the precedence of 3 + 1 and the exponentiation operator 2 ** (3 + 1) == 16 # Arithmetic, relational, and Boolean operators 2 ** (3 + 1) == 16 and 3 * 2 < 10 # Arithmetic, relational, Boolean, and containment operators 2 ** (3 + 1) != 16 or 3 * 2 in [5, 6, 3] The following 2 examples generate unexpected outcomes! Unexpected outcome! # Unexpected outcome! 6 < 10 != True # Unexpected outcome! 6 < 10 != False What is actually being evaluated is: First case: (6 &lt; 10) and (10 != True) Second case: (6 &lt; 10) and (10 != False) In both cases, 10 is not logical and doesn't equal True or False . Therefore, both expressions are evaluated as ( True and True ) which is True . You might face similar confusing cases. It is highly recommended that you use () to ²x and debug such cases. # Expected outcome after adding () (6 < 10) != True Operator Precedence Correct the following expression so the answer is True Task 6 (6 + 2 < 9) == True # Had to put parenthesis True
12/11/23, 12:14 PM Lucas DuBoisModuleSevenLessonSevenActivity.ipynb - Colaboratory https://colab.research.google.com/drive/1n0Lr2A9PcWOJqrBJOOSYzIPoyt-zWcDO#scrollTo=-IYqDMaWkIS9&printMode=true 9/9 Operator Precedence Correct the following expression so the answer is True Task 7 3 ** (2 + 1) >= 3 * (8 + 1) # Had to add parenthsis True Operator Precedence Correct the following expression so the answer is True Task 8 (5 + 3) * 2 == 16 # Had to add parenthesis True Operator Precedence Correct the following expression so the answer is True Task 9 ( 3 d ) d t dd th i
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help