Python Logical Types and Sorting Algorithms

intro to python n.w
1 / 24
Embed
Share

Explore Python logical types and sorting algorithms with demos and explanations by Joel Grodstein from Tufts University. Learn about if-then logic, card sorting, and follow the bouncing ball concept in Python programming.

  • Python Programming
  • Logical Types
  • Sorting Algorithms
  • Joel Grodstein
  • Tufts University

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Intro to Python Tufts University Instructor: Joel Grodstein joel.grodstein@tufts.edu if then, logical types Python Joel Grodstein 1

  2. Demo a bubble sort https://www.youtube.com/watch?v=xli_FI7CuzA https://www.youtube.com/watch?v=k4RRi_ntQc8 Python Joel Grodstein 2

  3. Card sort with if then The code below is for a card sort. It already has an "if" And hopefully it even makes sense Let s just fix the format a bit n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): if card #i and #(i+1) are backwards then swap them Python Joel Grodstein 3

  4. Follow the bouncing ball n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): swap them 10 6 6 8 10 [0] [1] [2] 10>6 is True 3 U 0 U 0 U n_cards pass i card Python Joel Grodstein 4

  5. Follow the bouncing ball n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): swap them 8 10 8 10 6 [0] [1] [2] 10>8 is True 0 0 1 3 The for i loop is now done But the for pass loop is not done n_cards pass i card Python Joel Grodstein 5

  6. Follow the bouncing ball Starting this loop all over again from the top n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): swap them 8 10 6 [0] [1] [2] 6>8 is False skip over the if code 1 0 1 3 The for i loop is not done n_cards pass i card Python Joel Grodstein 6

  7. Follow the bouncing ball n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): swap them 8 10 6 [0] [1] [2] 8>10 is False skip over the if code 1 0 1 3 The for i= loop is done The pass= loop is done n_cards pass i card Finally finished!!! Python Joel Grodstein 7

  8. if then details The format is simple: if (condition): statement The usual Python note: if there are more than 1 statement s, they must all have the same indentation As usual, there are several variations: We can make the condition fairly complex There is an else Let s look at both of these Python Joel Grodstein 8

  9. if then else Here s some code with an obvious use: if (it s Sunday evening): do your homework if (it s not Sunday evening): go to sleep early We can rewrite it: if (it s Sunday evening): do your homework else: go to sleep early Else replaces the inverted condition Gain in clarity Python Joel Grodstein 9

  10. More complex conditions We don t have homework due every week if (it s Sunday evening and there s homework due) do your homework else: go to sleep early The condition can have and and or, or really be any arbitrary expression But how arbitrary can an expression get, anyway? In fact, very arbitrary Without the else , this would have been if ((it s not Sunday evening) or (there s no homework due)) . That s a bit of a mess! Python Joel Grodstein 10

  11. Logical expressions False and True are the simplest expressions if (False) do not execute the statements if (True) do execute the statements <, >, <=, >= work as expected 3<4 evaluates to True. 3>=4 evaluates to False And then see the case above So if (3<4) becomes if (True) , which executes the subsequent statements You can replace 3 with any variable that evaluates to 3 a=3 if (a<4): statements happen, since 3<4 These statements do Python Joel Grodstein 11

  12. Testing equality & inequality Testing for equality is a bit weird if (3=4) error if (3==4) evaluates to false Single = is used only for assignment to a variable Testing for equality is ==; test for inequality is != Example: a=3 b=4 if (a==b): statements These statements do not happen, since 3 is not equal to 4 Python Joel Grodstein 12

  13. AND, OR expression and expression is an expression Ditto for or And you can do this recursively Example: a=3; b=4; c=5; d=6; e=7 if ((a<4) and (b==c) and !((d==5) or (e==6))): So in the end, this big expression just evaluates to 'False' if ((3<4) and (4==5) and !((6==5)or(7==6))): if (True and False and !(False or False)): if (True and False and !False): if (True and False and True): if (False): Python Joel Grodstein 13

  14. elif Sometimes you have a sequence of conditions: if it s midnight-10am, keep the house at 55 if 10-11am, 70 11am-10pm, 55 10pm-midnight, 71 if (time <= 10): temp=55 if (time>10 && time<=11): temp=70 if (time>11 && time<=22): temp=55 if (time>22 && time<=24): temp=70 The program on the right works, but it s inefficient, since it tests the last 3 if s even when the first is true it s easy to mis-type the conditions Python Joel Grodstein 14

  15. Nested if How about this? Pros and cons? More obvious that it works More efficient But the indentation is ugly if (time <= 10): temp=55 else: if (time<=11): temp=70 else: if (time<=22): temp=55 These must line up else: temp=70 These too And these Python Joel Grodstein 15

  16. Elif The solution is elif Many (not all) languages have it Code is more obvious and elegant We won t need this until the Manduca homework if (time <= 10): temp=55 elif (time<=11): temp=70 elif (time<=22): temp=55 else: temp=70 Python Joel Grodstein 16

  17. Group activity What do each of these evaluate to? a=3 b=2 (a==3) and (b==2) (a==4) or (b==2) (a*b==6) and (a+b==6) (a*b==6) and ((a+b==6) or (1==1)) True True False True Python Joel Grodstein 17

  18. Group activity What do each of these print? Assume a=3 and b=2 if (a==3) and (b==2): print ('yes') if (a==4): if (b==2): yes print ('42') else: print ('4 not 2') nothing if (a==4) or (b==2): print ('yes') else: print ('no') yes for i in range(2:6): if (i>4): print (i, '*', i, '=', i*i) 5*5=25 if (a==3): if (b==2): print ('32') else: print ('4 not 2') 32 Python Joel Grodstein 18

  19. What does this do? def muscle_report (musc1_on, changed): green = pyb.LED(2) red = pyb.LED(1) if musc1_on: red.off() green.on() else: green.off() red.on() Python Joel Grodstein 19

  20. What does this do? current_color = None def muscle_report (musc1_on, changed): green = pyb.LED(2) red = pyb.LED(1) if musc1_on and changed: if (current_color == red): current_color = green green.on() red.off() else current_color = red red.on() green.off() Python Joel Grodstein 20

  21. Follow-up activities Try the examples from this lecture yourself Vary them, or even mis-type some to see what happens More exercises. Write a program that Prints all of the even numbers in an array Print all the elements of array1 that are not in array2 Makes a new array with pos , neg or zero based on a first array of numbers Determines the indices of the largest and smallest numbers from an array Checks whether a specified value is contained in an array Tests if a number N is within 100 of 1000 or 2000 Prints all divisors of a given number Creates a 5-element array of random integers between 1 and 10, and then sums up only those elements that are 3 or higher Python Joel Grodstein 21

  22. Another group exercise Take an array p. Multiply every element by a random number r, and then bound them all to be 0 and 1 You may want to Google numpy clip This will be useful for HW3 Python Joel Grodstein 22

  23. Swapping Let s say we have two variables x and y, and we want to swap their values. Consider this code: x=y y=x y is 7, so now x is too x is 7, so copy that to y. But y already was 7, so that does nothing 7 5 x 7 y Once we set x to 7, there was no longer any variable anywhere with a value of 5. So there is no way to assign y=5 23 Python Joel Grodstein

  24. Swapping take 2 As always, there s a trick to doing it. Try this code instead tmp=y y=x x=tmp y is 7, so now tmp is too x is 5, so copy that to y tmp=7, so copy 7 to x 7 5 x 7 5 y It works U 7 tmp Python Joel Grodstein 24

More Related Content