an introduction to python - variables, math
TRANSCRIPT
![Page 1: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/1.jpg)
An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #3:
Variables, Math
![Page 2: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/2.jpg)
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
![Page 3: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/3.jpg)
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.0name = "John"
Image Credit: ClipArt Best
Python variable
names can have
unlimited length –
but keep them short!
![Page 4: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/4.jpg)
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
![Page 5: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/5.jpg)
Let’s Talk About Python Math!
Normal Stuff
Weird Stuff5%2 = 1
![Page 6: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/6.jpg)
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?
![Page 7: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/7.jpg)
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, andremainder.
– Multiplication, division, and remainder are evaluated before addition andsubtraction.
– 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 assignmentoperations 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
![Page 8: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/8.jpg)
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
![Page 9: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/9.jpg)
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
![Page 10: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/10.jpg)
Really, Really Long Expressions
• When an expression becomes long orcomplex, you can move to a new line by placing a backslash character \ at theend of the current line.
![Page 11: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/11.jpg)
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
![Page 12: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/12.jpg)
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
![Page 13: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/13.jpg)
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
![Page 14: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/14.jpg)
What’s In Your Python Toolbox?
print() math
![Page 15: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/15.jpg)
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/
![Page 16: An Introduction To Python - Variables, Math](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55ace0641a28ab3d058b459a/html5/thumbnails/16.jpg)
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/