SlideShare una empresa de Scribd logo
1 de 16
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/

Más contenido relacionado

Similar a An Introduction To Python - Variables, Math

Python Programming | JNTUK | UNIT 1 | Lecture 4
Python Programming | JNTUK | UNIT 1 | Lecture 4Python Programming | JNTUK | UNIT 1 | Lecture 4
Python Programming | JNTUK | UNIT 1 | Lecture 4FabMinds
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsYashJain47002
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With DataBlue Elephant Consulting
 
Foundations of Machine Learning - StampedeCon AI Summit 2017
Foundations of Machine Learning - StampedeCon AI Summit 2017Foundations of Machine Learning - StampedeCon AI Summit 2017
Foundations of Machine Learning - StampedeCon AI Summit 2017StampedeCon
 
DutchMLSchool. Logistic Regression, Deepnets, Time Series
DutchMLSchool. Logistic Regression, Deepnets, Time SeriesDutchMLSchool. Logistic Regression, Deepnets, Time Series
DutchMLSchool. Logistic Regression, Deepnets, Time SeriesBigML, Inc
 
Machine Learning on Azure - AzureConf
Machine Learning on Azure - AzureConfMachine Learning on Azure - AzureConf
Machine Learning on Azure - AzureConfSeth Juarez
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
Introduction to machine learning and model building using linear regression
Introduction to machine learning and model building using linear regressionIntroduction to machine learning and model building using linear regression
Introduction to machine learning and model building using linear regressionGirish Gore
 
probability.pptx
probability.pptxprobability.pptx
probability.pptxbisan3
 
DutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingDutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingBigML, Inc
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basicsLuigi De Russis
 
Tips and tricks to win kaggle data science competitions
Tips and tricks to win kaggle data science competitionsTips and tricks to win kaggle data science competitions
Tips and tricks to win kaggle data science competitionsDarius Barušauskas
 
JavaVariablesTypes.pptx
JavaVariablesTypes.pptxJavaVariablesTypes.pptx
JavaVariablesTypes.pptxcharusharma165
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data TypesRokonuzzaman Rony
 
CSC111-Chap_02.pdf
CSC111-Chap_02.pdfCSC111-Chap_02.pdf
CSC111-Chap_02.pdf2b75fd3051
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methodsPranavSB
 
CPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramCPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramMichael Heron
 

Similar a An Introduction To Python - Variables, Math (20)

Python Programming | JNTUK | UNIT 1 | Lecture 4
Python Programming | JNTUK | UNIT 1 | Lecture 4Python Programming | JNTUK | UNIT 1 | Lecture 4
Python Programming | JNTUK | UNIT 1 | Lecture 4
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 
Ml ppt at
Ml ppt atMl ppt at
Ml ppt at
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With Data
 
Foundations of Machine Learning - StampedeCon AI Summit 2017
Foundations of Machine Learning - StampedeCon AI Summit 2017Foundations of Machine Learning - StampedeCon AI Summit 2017
Foundations of Machine Learning - StampedeCon AI Summit 2017
 
DutchMLSchool. Logistic Regression, Deepnets, Time Series
DutchMLSchool. Logistic Regression, Deepnets, Time SeriesDutchMLSchool. Logistic Regression, Deepnets, Time Series
DutchMLSchool. Logistic Regression, Deepnets, Time Series
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Machine Learning on Azure - AzureConf
Machine Learning on Azure - AzureConfMachine Learning on Azure - AzureConf
Machine Learning on Azure - AzureConf
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
Decision Tree.pptx
Decision Tree.pptxDecision Tree.pptx
Decision Tree.pptx
 
Introduction to machine learning and model building using linear regression
Introduction to machine learning and model building using linear regressionIntroduction to machine learning and model building using linear regression
Introduction to machine learning and model building using linear regression
 
probability.pptx
probability.pptxprobability.pptx
probability.pptx
 
DutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingDutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision Making
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Tips and tricks to win kaggle data science competitions
Tips and tricks to win kaggle data science competitionsTips and tricks to win kaggle data science competitions
Tips and tricks to win kaggle data science competitions
 
JavaVariablesTypes.pptx
JavaVariablesTypes.pptxJavaVariablesTypes.pptx
JavaVariablesTypes.pptx
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
CSC111-Chap_02.pdf
CSC111-Chap_02.pdfCSC111-Chap_02.pdf
CSC111-Chap_02.pdf
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
CPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramCPP02 - The Structure of a Program
CPP02 - The Structure of a Program
 

Último

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 

Último (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 

An Introduction To Python - Variables, Math

  • 1. An Introduction To Software Development Using Python Spring Semester, 2015 Class #3: Variables, Math
  • 2. 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
  • 3. 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!
  • 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 About Python Math! Normal Stuff Weird Stuff 5%2 = 1
  • 6. 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?
  • 7. 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
  • 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 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.
  • 11. 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
  • 12. 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
  • 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 Your Python Toolbox? print() math
  • 15. 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/
  • 16. 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/

Notas del editor

  1. 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.