An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #3:
Variables, Math
An Example Python Job Description
Python Developer: Cadatasoft,
Richardson, TX, 3 weeks ago
Job Description:
• Candidate mindset should align to the
agile manifesto…
• Individuals and interactions over
processes and tools
• Working software over comprehensive
documentation
• Customer collaboration over contract
negotiation
• Responding to change over following a
plan
Core Technology Competence:
• Python, Django, JavaScript, MongoDB
• Valued Technology Competence
• HTML5, Angular.js, RabbitMQ, Git,
Jenkins, Agile
Experience:
• Candidates should value software design
patterns
• Have a level of maturity allowing them to
work with a diverse set of clients
• Be able to demonstrate above average
competence with 3 out of 4 core
technologies
• Revel in using their mind and all it has to
offer
Let’s Talk About Variables…
• Variables are reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory.
• Variables in Python consist of an alphanumeric name beginning in a letter
or underscore. Variable names are case sensitive.
• Python variables do not have to be explicitly declared to reserve memory
space. The declaration happens automatically when you assign a value to
a variable. The equal sign (=) is used to assign values to variables.
• The operand to the left of the = operator is the name of the variable and
the operand to the right of the = operator is the value stored in the
variable.
• Examples:
counter = 100
miles = 1000.0
name = "John"
Image Credit: ClipArt Best
Python variable
names can have
unlimited length –
but keep them short!
Variable Names:
The Good, The Bad, The Ugly
Examples
• X -- bad
• student_ID -- good
• numStudents -- good
• numberofstudentsenrolledinclass
--- bad
• stuff --- bad
Bad Names
• Single-letter names: x
• Naming thing after their type:
my_number = 20, my_string =
"Homo sapiens“
• Extremely vague names:
do_stuff(), process_files()
• Sequential names: dna, dna2,
dna3
• Re-using names
• Names that only vary by case or
punctuation: mydna, myDNA
A good variable name
should indicate its use
Let’s Talk About Python Math!
Normal Stuff
Weird Stuff
5%2 = 1
Modulo (%) Is Your Friend
• A man has 113 cans of Coke. He also has a group of boxes that can hold 12 cans
each. How many cans will he have left over once he’s loaded all of the boxes?
• On a military base the clock on the wall says that the time is 23:00. What time of
day is this?
• My friend has 10,432 ping pong balls that he needs to put into storage. My car can
transport 7,239 balls. How many will be left after I leave?
What Comes First?
Precedence Rules
• The precedence rules you learned in algebra apply during the evaluation
of arithmetic expressions in Python:
– Exponentiation has the highest precedence and is evaluated first.
– Unary negation is evaluated next, before multiplication, division, and
remainder.
– Multiplication, division, and remainder are evaluated before addition and
subtraction.
– Addition and subtraction are evaluated before assignment.
– With two exceptions, operations of equal precedence are left associative,
so they are evaluated from left to right. Exponentiation and assignment
operations are right associative, so consecutive instances of these are evaluated from
right to left.
– You can use parentheses to change the order of evaluation
• "PEMDAS", which is turned into the phrase "Please Excuse My Dear Aunt
Sally". It stands for "Parentheses, Exponents, Multiplication and Division,
and Addition and Subtraction".
3**2
-5
2+3*4/6
sum = 2+3
2+3*4/6
sum = 3**2**5
(2+3)*4/5
Image Credit: www.pinterest.com
Precedence Rules: Examples
• Simplify 4 + 32
• Simplify 4 + (2 + 1)2
• Simplify 4 + [–1(–2 – 1)]2
• Simplify 4( –2/3 + 4/3)
• Three people ate dinner at a restaurant and want to split the
bill. The total is $35.27, and they want to leave a 15 percent
tip. How much should each person pay?
Image Credit: pixgood.com
Errors: Syntax & Sematic
• A syntax error occurs when you mistype something
and create an invalid statement.
• A semantic error is detected when the action which
an expression describes cannot be carried out, even
though that expression is syntactically correct.
Example: Although the expressions 45 / 0 and 45 % 0
are syntactically correct, they are meaningless,
because the computer cannot carry them out.
Image Credit: www.canstockphoto.com
Really, Really Long Expressions
• When an expression becomes long or
complex, you can move to a new line by
placing a backslash character  at the
end of the current line.
Not All Numbers Are Created Equal
• Integers are the numbers you can easily count, like 1,
2, 3, as well as 0 and the negative numbers, like –1, –2,
–3.
• Decimal numbers (also called real numbers) are the
numbers with a decimal point and some digits after it,
like 1.25, 0.3752, and –101.2.
• In computer programming, decimal numbers are also
called floating-point numbers, or sometimes floats for
short (or float for just one of them). This is because the
decimal point “floats” around. You can have the
number 0.00123456 or 12345.6 in a float.
Image Credit: www.clipartpanda.com
Mixed-Mode Arithmetic
and Type Conversions
• Performing calculations involving both integers and
floating-point numbers is called mixed-mode
arithmetic.
• Conversions
– Determine type: type()
– Drop decimal: int()
– Round up / down: round()
– Change to decimal: float()
– Change to string: str()
Image Credit: personaltrainerbusinesssystems.com
Cool Kid Stuff:
Increment / Decrement / E-Notation
• Incrementing: sum = sum + 1
– sum += 1
• Decrementing: sum = sum – 1
– sum -= 1
• E-Notation
– 1,000,000 = 1 x 10**6 = 1e6
Image Credit: www.toonvectors.com
What’s In Your Python Toolbox?
print() math
What We Covered Today
1. What variables are.
2. What makes a good / bad
variable name.
3. How to do math in
Python.
4. Operator precedence
5. Mixed-Mode Arithmetic
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
What We’ll Be Covering Next Time
1. Strings,
2. Input / Output
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

An Introduction To Python - Variables, Math

  • 1.
    An Introduction ToSoftware Development Using Python Spring Semester, 2015 Class #3: Variables, Math
  • 2.
    An Example PythonJob Description Python Developer: Cadatasoft, Richardson, TX, 3 weeks ago Job Description: • Candidate mindset should align to the agile manifesto… • Individuals and interactions over processes and tools • Working software over comprehensive documentation • Customer collaboration over contract negotiation • Responding to change over following a plan Core Technology Competence: • Python, Django, JavaScript, MongoDB • Valued Technology Competence • HTML5, Angular.js, RabbitMQ, Git, Jenkins, Agile Experience: • Candidates should value software design patterns • Have a level of maturity allowing them to work with a diverse set of clients • Be able to demonstrate above average competence with 3 out of 4 core technologies • Revel in using their mind and all it has to offer
  • 3.
    Let’s Talk AboutVariables… • Variables are reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. • Variables in Python consist of an alphanumeric name beginning in a letter or underscore. Variable names are case sensitive. • Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. • The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. • Examples: counter = 100 miles = 1000.0 name = "John" Image Credit: ClipArt Best Python variable names can have unlimited length – but keep them short!
  • 4.
    Variable Names: The Good,The Bad, The Ugly Examples • X -- bad • student_ID -- good • numStudents -- good • numberofstudentsenrolledinclass --- bad • stuff --- bad Bad Names • Single-letter names: x • Naming thing after their type: my_number = 20, my_string = "Homo sapiens“ • Extremely vague names: do_stuff(), process_files() • Sequential names: dna, dna2, dna3 • Re-using names • Names that only vary by case or punctuation: mydna, myDNA A good variable name should indicate its use
  • 5.
    Let’s Talk AboutPython Math! Normal Stuff Weird Stuff 5%2 = 1
  • 6.
    Modulo (%) IsYour Friend • A man has 113 cans of Coke. He also has a group of boxes that can hold 12 cans each. How many cans will he have left over once he’s loaded all of the boxes? • On a military base the clock on the wall says that the time is 23:00. What time of day is this? • My friend has 10,432 ping pong balls that he needs to put into storage. My car can transport 7,239 balls. How many will be left after I leave?
  • 7.
    What Comes First? PrecedenceRules • The precedence rules you learned in algebra apply during the evaluation of arithmetic expressions in Python: – Exponentiation has the highest precedence and is evaluated first. – Unary negation is evaluated next, before multiplication, division, and remainder. – Multiplication, division, and remainder are evaluated before addition and subtraction. – Addition and subtraction are evaluated before assignment. – With two exceptions, operations of equal precedence are left associative, so they are evaluated from left to right. Exponentiation and assignment operations are right associative, so consecutive instances of these are evaluated from right to left. – You can use parentheses to change the order of evaluation • "PEMDAS", which is turned into the phrase "Please Excuse My Dear Aunt Sally". It stands for "Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction". 3**2 -5 2+3*4/6 sum = 2+3 2+3*4/6 sum = 3**2**5 (2+3)*4/5 Image Credit: www.pinterest.com
  • 8.
    Precedence Rules: Examples •Simplify 4 + 32 • Simplify 4 + (2 + 1)2 • Simplify 4 + [–1(–2 – 1)]2 • Simplify 4( –2/3 + 4/3) • Three people ate dinner at a restaurant and want to split the bill. The total is $35.27, and they want to leave a 15 percent tip. How much should each person pay? Image Credit: pixgood.com
  • 9.
    Errors: Syntax &Sematic • A syntax error occurs when you mistype something and create an invalid statement. • A semantic error is detected when the action which an expression describes cannot be carried out, even though that expression is syntactically correct. Example: Although the expressions 45 / 0 and 45 % 0 are syntactically correct, they are meaningless, because the computer cannot carry them out. Image Credit: www.canstockphoto.com
  • 10.
    Really, Really LongExpressions • When an expression becomes long or complex, you can move to a new line by placing a backslash character at the end of the current line.
  • 11.
    Not All NumbersAre Created Equal • Integers are the numbers you can easily count, like 1, 2, 3, as well as 0 and the negative numbers, like –1, –2, –3. • Decimal numbers (also called real numbers) are the numbers with a decimal point and some digits after it, like 1.25, 0.3752, and –101.2. • In computer programming, decimal numbers are also called floating-point numbers, or sometimes floats for short (or float for just one of them). This is because the decimal point “floats” around. You can have the number 0.00123456 or 12345.6 in a float. Image Credit: www.clipartpanda.com
  • 12.
    Mixed-Mode Arithmetic and TypeConversions • Performing calculations involving both integers and floating-point numbers is called mixed-mode arithmetic. • Conversions – Determine type: type() – Drop decimal: int() – Round up / down: round() – Change to decimal: float() – Change to string: str() Image Credit: personaltrainerbusinesssystems.com
  • 13.
    Cool Kid Stuff: Increment/ Decrement / E-Notation • Incrementing: sum = sum + 1 – sum += 1 • Decrementing: sum = sum – 1 – sum -= 1 • E-Notation – 1,000,000 = 1 x 10**6 = 1e6 Image Credit: www.toonvectors.com
  • 14.
    What’s In YourPython Toolbox? print() math
  • 15.
    What We CoveredToday 1. What variables are. 2. What makes a good / bad variable name. 3. How to do math in Python. 4. Operator precedence 5. Mixed-Mode Arithmetic Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
  • 16.
    What We’ll BeCovering Next Time 1. Strings, 2. Input / Output Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

Editor's Notes

  • #2 New name for the class I know what this means Technical professionals are who get hired This means much more than just having a narrow vertical knowledge of some subject area. It means that you know how to produce an outcome that I value. I’m willing to pay you to do that.