1 / 46

Chapter 3

Chapter 3 . Primitive Expressions. Overview. Variables. A variable connects a name to one value. Variables. One value could also wear multiple labels - meaning that a single value is connected to multiple variables. Variables.

Download Presentation

Chapter 3

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. Chapter 3 Primitive Expressions

  2. Overview

  3. Variables A variable connects a name to one value

  4. Variables One value could also wear multiple labels - meaning that a single value is connected to multiple variables.

  5. Variables But one important rule at this party is that labels must be unique - two values can’t have the same label (i.e., variable name) at once.

  6. Variable Assignment Variables are connected to values through through assignment - this is how we make a value wear a label. Consider this example where a new value associated with the variable force (39.6) is derived from a standard physics relationship:

  7. Variable Assignment Here we illustrate dynamic typing by letting the variable foo take on four different values and four different corresponding types:

  8. Variable Assignment With each assignment foo is connected to a different value in memory:

  9. Rules for Naming Variables the first character must be a letter of any case or language or an under- score, continuation characters may include most characters but not whitespace or punctuation that has special meaning in Python (e.g., period, comma, parenthesis, brackets), and identifiers can’t have the same name as words reserved by the Python language as a keyword (e.g., and, if , def, and else ).

  10. Rules for Naming Variables

  11. Rules for Naming Variables variable names are lower case and words are separated with underscores (e.g., standard_deviation), class names are capitalized camel case (e.g., ColorMatrix), identifiers that begin with one or more underscores have special meaning that we will discuss later, identifiers shouldn’t have the same name as built-in identifiers (e.g., int , float, list,tuple,dir).

  12. Choosing ‘Good’ Variable Names Is the name consistent with existing naming conventions? Does this value have important units (grams, meters, moles, etc.) that are not obvious from its type or usage? Does the name unnecessarily use negative logic or other counterintuitive conventions? You should consider using is_enabled instead of is_not_enabled.

  13. Choosing ‘Good’ Variable Names Is the name descriptive? If you had seen this variable for the first time would the name make sense? Is the name too wordy, long, or redundant? Is the name too short or does it use uncommon abbreviations?

  14. Principle 5(Code Should Read Like Poetry) Code written with good variable names allows the code to be read almost like poetry. Use variable names to tell a coherent story about what a computation accomplishes. Poor variable names hide the meaning of the code.

  15. Principle 5(Code Should Read Like Poetry) Consider this perfectly correct piece of code: Choose names that reveal the codes purpose:

  16. Booleans

  17. Logical Expressions

  18. Logical Operators

  19. Logical Operator Truth Table

  20. if statement

  21. Block

  22. Numbers

  23. Numbers

  24. Numbers

  25. Arithmetic Operators

  26. Arithmetic Expression

  27. Clock Arithmetic

  28. Clock Arithmetic

  29. Comparison Operators

  30. Comparison Operators

  31. Comparison Operators

  32. String Literal

  33. Literal String Syntax

  34. String Escape Sequences

  35. String Escape Sequences

  36. String Operators

  37. String Operators

  38. String Operators

  39. String Operators

  40. String Operators (Slicing)

  41. String Operators

  42. String Methods

  43. String Methods

  44. Conversion Functions

  45. Lists and Dictionaries

  46. Lists and Dictionaries

More Related