Download Python Programming langauge and more Essays (university) Computer Science in PDF only on Docsity!
Python
Programming
LECTURE 9
Variables and Constants
Variable Naming Conventions
- Python requirements:
- Rules built into the Python language for writing a program.
- Somewhat analogous to the grammar of a ‘human’ language.
- If the rules are violated then the typical outcome is the program cannot be translated (nor run).
- A language such as Python may allow for a partial execution.
- Style requirements:
- Approaches for producing a well written program. (The real life analogy is that something written in a human language may follow the grammar but still be poorly written).
- If they are not followed then the program can still be translated but there may be other problems.
Variable Naming Conventions
1. Style requirement: The name should be meaningful.
2. Style and Python requirement: Names must start with a letter (Python
requirement) and should not begin with an underscore (style requirement).
3. Style requirement: Names are case sensitive but avoid distinguishing
variable names only by case.
4. Style requirement: Variable names should generally be all lower case (see
next point for the exception).
5. Style requirement: For variable names composed of multiple words
separate each word by capitalizing the first letter of each word (save for the
first word) or by using an underscore. (Either approach is acceptable but
don’t mix and match.)
6. Python requirement: Can not be a keyword.
Variables
◼ Variable : A named piece of memory that can store a value. ◼ Usage: ◼ Compute an expression's result, ◼ Store that result into a variable, ◼ Use that variable later in the program. ◼ Assignment statement : Stores a value into a variable. ◼ Syntax: name = value ◼ Examples: x = 5 gpa = 3. x 5 gpa 3. ◼ A variable that has been given a value can be used in expressions. x + 4 is 9 ◼ Exercise: Evaluate the quadratic equation for a given a , b , and c.
◼ No need to declare
◼ Need to assign (initialize)
◼ use of uninitialized variable raises exception
◼ Not typed
if friendly: greeting = "hello world" else: greeting = 12** Print(greeting)
◼ Everything is a "variable":
◼ Even functions, classes, modules
Variables
Named Constants
- They are similar to variables: a memory location that’s been given a name.
- Unlike variables their contents shouldn’t change.
- The naming conventions for choosing variable names generally apply to
constants but the name of constants should be all UPPER CASE.
- You can separate multiple words with an underscore.
- Example PI = 3.
- They are capitalized so the reader of the program can distinguish them from
variables.
- For some programming languages the translator will enforce the unchanging nature of the constant.
- For languages such as Python it is up to the programmer to recognize a constant for what it is and not to change it.
Terminology: Named Constants Vs. Literals
- Named constant: given an explicit name. TAX_RATE = 0. afterTax = income – (income * TAX_RATE)
- Literal/Unnamed constant/Magic number: not given a name, the value
that you see is literally the value that you have.
afterTax = 100000 – (100000 * 0.2)
Why Use Named Constants
- Make your program easier to read and understand # NO currentPopulation = 1000000 populationChange = (0.1758 – 0.1257) * currentPopulation #YES BIRTH_RATE = 17.58/ MORTALITY_RATE = 0. currentPopulation = 1000000 populationChange = (BIRTH_RATE - MORTALITY_RATE) * currentPopulation In this case the literals are Magic Numbers (avoid whenever possible!)
- Make the program easier to maintain
- If the constant is referred to several times throughout the program,
changing the value of the constant once will change it throughout the
program.
- Using named constants is regarded as “good” style when writing a
computer program.
Why Use Named Constants
BIRTH_RATE = 0. MORTALITY_RATE = 0. populationChange = 0 currentPopulation = 1000000 populationChange = ( BIRTH_RATE - MORTALITY_RATE) * currentPopulation if (populationChange > 0): print("Increase“) print("Birth rate:", BIRTH_RATE , " Mortality rate:", MORTALITY_RATE, " Population change:", populationChange) elif (populationChange < 0): print("Decrease“) print("Birth rate:", BIRTH_RATE , " Mortality rate:", MORTALITY_RATE, "Population change:", populationChange) else: print("No change“) print("Birth rate:", BIRTH_RATE , " Mortality rate:", MORTALITY_RATE, "Population change:", populationChange)
One change in the initialization of the constant
changes every reference to that constant
Purpose Of Named Constants
BIRTH_RATE = 0. MORTALITY_RATE = 0. populationChange = 0 currentPopulation = 1000000 populationChange = (BIRTH_RATE - MORTALITY_RATE ) * currentPopulation if (populationChange > 0): print("Increase“) print("Birth rate:", BIRTH_RATE, " Mortality rate:", MORTALITY_RATE , " Population change:", populationChange) elif (populationChange < 0): print("Decrease“) print("Birth rate:", BIRTH_RATE, " Mortality rate:", MORTALITY_RATE , "Population change:", populationChange) else: print("No change“) print("Birth rate:", BIRTH_RATE, " Mortality rate:", MORTALITY_RATE , "Population change:", populationChange)
One change in the initialization of the constant
changes every reference to that constant
Purpose Of Named Constants
Output: Displaying The Contents Of
Variables And Constants
Format: print (< variable name >) print (< constant name >) Example: aNum = 10 A_CONSTANT = 10 print (aNum) print (A_CONSTANT)
Mixed Output
- Mixed output: getting string output and the contents of variables (or
constants) to appear together.
- Format: print (“ string ”, < variable or constant >, “ string ”, < variable or constant > , etc…)
- Examples: myInteger = 10 myReal = 10. myString = "hello" print ("MyInteger:" , myInteger) print ("MyReal:" , myReal) print ("MyString:" , myString) The comma signals to the translator that the string and the contents of the variable should appear on the same line.