Python Tricks for Easier Coding

python tricks n.w
1 / 41
Embed
Share

Discover new Python tricks including slicing, multiple return values, global variables, and more to simplify your coding tasks. Learn about negative indexing, string slicing, list manipulation, and list copying techniques that can enhance your Python programming skills.

  • - Python Tricks - Coding Tips - List Slicing - String Manipulation

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. Python Tricks CMSC 201

  2. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global variables Break and continue Mutable and Immutable types Try / Except

  3. List Tricks

  4. Negative Index If you want to get the last element of a list or string, you can use negative indices! myString = abcdef print(myString[-1]) print(myString[-2]) Prints: f e

  5. Slicing Say we have the string My name is Max and know, for some reason, that we want only the characters from index 3 to index 7. myString = My name is Max slice = myString[3:7] This is called string slicing! This will return the characters in myString from character 3 (inclusive) to 7 (exclusive). In this case, it prints the string name .

  6. Slicing If we leave off a number, it just goes to the end or beginning of the string! myString = abcdef print(myString[3:]) print(myString[:5]) Prints: def abcde

  7. Slicing We can also control our step size. myString = abcdef print(myString[0:6:2]) Prints: ace

  8. Exercise What do the following print? myString = abcdef print(myString[:2]) print(myString[1:3]) print(myString[4:]) print(myString[::2])

  9. Exercise What do the following print? myString = abcdef print(myString[:2]) print(myString[1:3]) print(myString[4:]) print(myString[::2]) ab bc ef ace

  10. List Slicing This works for lists too! myList = [1, 2, 3, 4, 5] print(myList[3:]) Prints: [4, 5]

  11. Copying A List Want a list full of zeroes? myList = [0] * 3 print(myList) [0, 0, 0] Multiplying a list by something gives you that list, that many times! print([1, 2, 3] * 2) Prints: [1, 2, 3, 1, 2, 3]

  12. Danger Remember, lists are stored as references the * operator on lists of lists is extremely dangerous! references, so using myList = [1, [2, 3]] * 2 print(myList) Prints: [1, [2, 3], 1, [2, 3]]

  13. Danger myList = [1, [2, 3]] * 2 print(myList) Prints: [1, [2, 3] [2, 3], 1, [2, 3] [2, 3]] However, here s what s happening: [ , , , ] 1 [2, 3]

  14. Multiple Return Values

  15. Multiple Return Values Functions in python can return multiple things! def myFunction(arg1, arg2): # Some stuff happens here return var1, var2, var3 def main(): result1, result2, result3 = myFunction(1, 2)

  16. Global Variables

  17. Global Variables Earlier in class we mentioned you can have global constants! PI = 3.14 def area(radius): return PI * (radius ** 2) These variables are visible anywhere in the program.

  18. Global Variables What happens here? PI = 3.14 def area(radius): return PI * (radius ** 2) def main(): PI = 10 print(PI) This makes a new variable, also called PI, that lives in main. The original PI is unchanged. The new PI overshadows the old one.

  19. Global Variables PI = 3.14 def area(radius): print(PI) return PI * (radius ** 2) def main(): PI = 10 print(PI) area(1) Prints: 10 3.14

  20. Global Variables If you want to change a global variable (rather than making a new one), you must declare it as global. PI = 3.14 def area(radius): print(PI) return PI * (radius ** 2) def main(): global PI PI = 10 area(1) print(PI)

  21. Global Variables PI = 3.14 def area(radius): print(PI) return PI * (radius ** 2) def main(): global PI PI = 10 area(1) print(PI) Prints: 10 10

  22. Global Variables Summary: Global variables are visible anywhere in the program, but to change them, you have to put the global keyword in front of them first. In this class, you should never need to do this.

  23. Break and Continue

  24. Break Sometimes, we want to get out of a loop before the termination condition. Break immediately stops the loop!

  25. Break for i in range(0, 100): if i == 3: break print(i) print( I have escaped the loop! ) Prints: 0 1 2 I have escaped the loop.

  26. Break Break breaks out of whatever the innermost loop. for j in range(0, 10): for i in range(0, 10): if i == 3: break print(i) The break statement will stop the inner loop over and over, but not the outer loop.

  27. Continue Continue also changes how the loop works. When a loop hits a continue statement, it immediately stops what it s doing and goes back up to the next iteration of the loop. for i in range(0, 5): if i == 3: continue print(i) .

  28. Continue When a loop hits a continue statement, it immediately stops what it s doing and goes back up to the next iteration of the loop. for i in range(0, 5): if i == 3: continue print(i) Prints: 0 1 2 4

  29. Break and Continue Using break and continue correctly can be a bit complicated, so for this semester we ask that you not put them in your code!

  30. Mutable vs. Immutable

  31. Mutable vs. Immutable These are important vocab terms a variable that is immutable immutable can t be changed without being reassigned (that is, you can t change it without using the = operator). a = 10 a = 11 A mutable mutable variable can have parts of it changed without being reassigned: mylist = [1, 2, 3] myList[0] = 2 Notice that we only changed a part of it.

  32. Mutable vs. Immutable Mutable variables: Lists Immutable variables: Float String Integer Boolean

  33. References The way python stores information, each variable points to whatever value is in it. So when you say a = 5 print(a) Python has somewhere in the system that a variable called a points to 5. a 5

  34. References a = 5 print(a) So after setting a to 5, any time python goes to look up a, it finds 5. a 5 If you reassign a, python changes what a points at. If you make two variables equal to 5, the both point at 5.

  35. Reassignment Remember this example? a = 5 b = a a = a + 1 a 5 b a 6 When we change a, b doesn t change, because there s no connection to a. b points at the value 5, and so does a. Since you can t actually change 5, it doesn t matter that they are pointing to the same thing.

  36. Reassignment in Functions Functions work the same way. def someFunc(myVar): myVar = 10 myVar myVar 10 def main(): a = 5 someFunc(a) print(a) a 5 In this example, a just keeps pointing at 5 Notice that myVar and a both point to the number 5, there is just no way to change 5.

  37. Lists Lists are different, because the thing they are pointing at is capable of changing! a = [1, 2, 3] a [1, 2, 3] b = a b a[1] = 10 a [1, 10, 3] print(b) b Prints: [1, 10, 3]

  38. Lists What do we think this does? def main(): a = [1, 2, 3] myFunc(a) print(a) def myFunc(myList): myList[1] = 100

  39. Lists What do we think this does? def main(): a = [1, 2, 3] myFunc(a) print(a) a [1, 2, 3] myList def myFunc(myList): myList[1] = 100 Becomes: a [1, 100, 3] Prints [1, 100, 3] myList

  40. Lists What about this? def main(): a = [1, 2, 3] myFunc(a) print(a) def myFunc(myList): myList = [100]

  41. Lists The = operator changes what we re pointing at. def main(): a = [1, 2, 3] myFunc(a) print(a) a [1, 2, 3] myList def myFunc(myList): myList = [100] a [1, 2, 3] myList [100] Prints [1, 2, 3]

Related


More Related Content