Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Similar presentations


Presentation on theme: "Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License."— Presentation transcript:

1 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Chapter 3 Primitive Expressions

2 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Overview

3 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Variables A variable connects a name to one value

4 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Variables One value could also wear multiple labels - meaning that a single value is connected to multiple variables.

5 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. 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 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. 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 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Variable Assignment Here we illustrate dynamic typing by letting the variable foo take on four different values and four different corresponding types:

8 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Variable Assignment

9 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. 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 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Rules for Naming Variables

11 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. 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 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. 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 counter intuitive conventions? You should consider using is_enabled instead of is_not_enabled.

13 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. 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 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. 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 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Principle 5 (Code Should Read Like Poetry) Consider this perfectly correct piece of code: Choose names that reveal the codes purpose:

16 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Booleans

17 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Logical Expressions

18 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Logical Operators

19 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Logical Operator Truth Table

20 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. if statement

21 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Block

22 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Numbers

23 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Numbers

24 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Numbers

25 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Arithmetic Operators

26 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Arithmetic Expression

27 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Clock Arithmetic

28 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Clock Arithmetic

29 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Comparison Operators

30 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Comparison Operators

31 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Comparison Operators

32 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Literal

33 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Literal String Syntax

34 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Escape Sequences

35 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Escape Sequences

36 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Operators

37 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Operators

38 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Operators

39 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Operators

40 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Operators (Slicing)

41 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Operators

42 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Methods

43 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. String Methods

44 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Conversion Functions

45 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Lists and Dictionaries

46 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Lists and Dictionaries


Download ppt "Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License."

Similar presentations


Ads by Google