1 / 29

Python Programming Basics: Review, Computing Basics, Variables and Expressions, Decision Logic, Lists and Loops

Get an overview of the Python programming basics, including computing fundamentals, variables and expressions, decision logic, and lists and loops. Learn essential concepts and practical Python tricks.

iser
Download Presentation

Python Programming Basics: Review, Computing Basics, Variables and Expressions, Decision Logic, Lists and Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ReviewCMSC 201

  2. Overview • What we’ve covered so far: • Computer Basics • Python syntax • Variables and expressions • Decision logic • Lists and Loops • Python Tricks

  3. Overview • What we’ve covered so far: • Computing Basics • Python syntax • Variables and expressions • Decision logic • Lists and Loops • Functions • Python Tricks

  4. Computing Basics • What you should know: • Basic linux commands (ls, cd, mkdir, rmdir, mv, cp) • Binary numbers

  5. Exercise • Convert the following binary numbers to decimal: • 1011 • 11101 • Convert the following decimal numbers to binary: • 81 • 36

  6. Exercise • Convert the following binary numbers to decimal: • 1011 => 11 • 11101 => 29 • Convert the following decimal numbers to binary: • 81 => 1010001 • 36 => 100100

  7. Variables and Expressions • You need to know: • Order of operations • Python operators (**, %) • How to assign and use variables

  8. Exercise What does the following code snippet print? a = 2 ** 4 + 8 % 3 print(a)

  9. Decision Logic • What you need to know: • Boolean logic (how and, or, and not work) • How if/elif/else statements work.

  10. Exercise What does the following code snippet print? a = True b = 24 c = 12 if(a or b == c) and (b % c == 0 and (not b < c and b != c)): print(“Here we are!”) elifa: print(“There we were”) else: print(“We are here”)

  11. Lists and Loops • You should know: • How to write a while loop • How to write a for loop • How to create, access, and mutate a list • How to iterate over a list with a loop • How to iterate over a list using the range() function • How to access information in nested lists • List slicing

  12. Exercise • Given the list: • myList = [ [1, 2, “hello”], [4, 5], 6] • Change the “hello” to the number 7.

  13. Exercise • Given the list: • myList = [ [1, 2, “hello”], [4, 5], 6] • Change the “hello” to the number 7. • myList[0][2] = 7

  14. Exercise Write a program that uses a while loop to find the sum of the numbers between 1 and 10. Rewrite the same program using a for loop and the range() function.

  15. Exercise Write a program that uses a while loop to find the sum of the numbers between 1 and 10. Rewrite the same program using a for loop and the range() function. count = 0 listCounter = 0 while listCounter < 11: count = count + listCounter listCounter = listCounter + 1 print(count)

  16. Exercise Write a program that uses a while loop to find the sum of the numbers between 1 and 10. Rewrite the same program using a for loop and the range() function. count = 0 for i in range(1, 11): count = count + i print(count)

  17. Exercise Use nested loops to create the following list: [ [0, 0, 0], [0, 0, 0], [0, 0, 0]]

  18. Exercise Use nested loops to create the following list: [ [0, 0, 0], [0, 0, 0], [0, 0, 0]] result = [] for i in range(0, 2): temp = [] for j in range(0, 2): temp.append(0) result.append(temp)

  19. Exercise Use list slicing to print only the second half of myList. Use list slicing to print every third element of myList.

  20. Exercise Use list slicing to print only the second half of myList. print(myList[len(myList // 2):]) Use list slicing to print every third element of myList. print(myList[::3])

  21. Exercise What does the following print? for i in range(0, 10, 2): for j in range(0, i): print(j)

  22. Exercise What does the following print? for i in range(0, 10, 2): for j in range(0, i): print(j) 0 1 0 1 2 3 0 1 2 3 4 5 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7 8

  23. Functions • You need to know: • How to write a function • What arguments and return values are • Scope • Mutability • How references work • Global variables

  24. Exercise What does this print? def main(): myList = [3, 4, 5] otherList = myList otherList[0] = 0 someFunc(otherList) print(myList) defsomeFunc(tempList): tempList= [0] * 10 main()

  25. Exercise def main(): myList = [3, 4, 5] otherList = myList otherList[0] = 0 someFunc(otherList) print(myList) defsomeFunc(tempList): tempList= [0] * 10 main() [0, 4, 5]

  26. Exercise What does this print? def main(): myList = [3, 4, 5] otherList = myList otherList[0] = 0 otherList = someFunc(otherList) print(myList) defsomeFunc(tempList): tempList= [0] * 10 return tempList main()

  27. Exercise def main(): myList = [3, 4, 5] otherList = myList otherList[0] = 0 otherList = someFunc(otherList) print(myList) defsomeFunc(tempList): tempList= [0] * 10 return tempList main() [0, 4, 5]

  28. Exercise What will this code print? NUM_QUESTIONS = 20 def main(): NUM_QUESTIONS = 100 someFunc() defsomeFunc(): print(NUM_QUESTIONS)

  29. Exercise What will this code print? NUM_QUESTIONS = 20 def main(): NUM_QUESTIONS = 100 someFunc() defsomeFunc(): print(NUM_QUESTIONS) 20

More Related