SlideShare a Scribd company logo
1 of 35
 A variable is a name that refers to a
value which is used to refer to the value
later
x = 3
message = ‘hello’
 More formally a variable is a named place in the
memory where a programmer can store data and
later retrieve the data using the variable “name”
 Programmers get to choose the names of the
variables
Variables
You can change the contents of a variable in a later
statement
12.2
x
14
y
x = 12.2
y = 14
100
x = 100
Python Variable Name Rules
Must start with a letter or underscore _
Must consist of letters and numbers and underscores
Case Sensitive
Good: spam eggs spam23 _speed
Bad: 23spam #sign var.12
Different: spam Spam SPAM
Reserved Words
You can not use reserved words as variable names / identifiers
and del for is raise
assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print
They are similar to variables: a memory
location that’s been given a name.
Unlike variables their contents shouldn’t
change.
>>> PI = 3.14
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)
An expression is a combination of values,
variables, and operators.
17
x
x +17
Sentences or Lines
x = 2
x = x + 2
print(x)
Assignment Statement
Assignment with expression
Print statement
An assignment statement assigns a value
to a variable
x = 5
x=x+1
5
x
6
x
<variable> = <expr>
Assignment Statements
We assign a value to a variable using the
assignment statement (=)
An assignment statement consists of an expression
on the right hand side and a variable to store the
result
x = 3.9 * x * ( 1 - x )
x = 3.9 * x * ( 1 - x )
0.6
x
Right side is an expression.
Once expression is evaluated,
the result is placed in (assigned
to) x.
0.6 0.6
0.4
0.93
A variable is a memory location
used to store a value (0.6).
0.6 0.93
x
0.93
A variable is a memory location
used to store a value. The
value stored in a variable can be
updated by replacing the old
value (0.6) with a new value
(0.93).
x = 3.9 * x * ( 1 - x )
Right side is an expression.
Once expression is evaluated,
the result is placed in (assigned
to) x.
Numeric Expressions
 Because of the lack of mathematical
symbols on computer keyboards - we
use “computer-speak” to express the
classic math operations
 Asterisk is multiplication
 Exponentiation (raise to a power) looks
different from in math.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
** Power
% Remainder
Numeric Expressions
>>> xx = 2
>>> xx = xx + 2
>>> print xx
4
>>> yy = 440 * 12
>>> print(yy)
5280
>>> zz = yy / 1000
>>> print(zz)
5
>>> jj = 23
>>> kk = jj % 5
>>> print(kk)
3
>>>print(4 ** 3)
64
5 23
20
3
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
** Power
% Remainder
Order of Evaluation
 When we string operators together - Python must know which one
to do first
 This is called “operator precedence”
 Which operator “takes precedence” over the others
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule
 Parenthesis are always respected
 Exponentiation (raise to a power)
 Multiplication, Division, and Remainder
 Addition and Subtraction
 Left to right
Parenthesis
Power
Multiplication
Addition
Left to Right
Parenthesis
Power
Multiplication
Addition
Left to Right
1 + 2 ** 3 / 4 * 5
1 + 8 / 4 * 5
1 + 2 * 5
1 + 10
11
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11
>>>
Parenthesis
Power
Multiplication
Addition
Left to Right
1 + 2 ** 3 / 4 * 5
1 + 8 / 4 * 5
1 + 2 * 5
1 + 10
11
Note 8/4 goes before 4*5
because of the left-right
rule.
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11
>>>
Operator Precedence
When writing code - use parenthesis
When writing code - keep mathematical
expressions simple enough that they are
easy to understand
Break long series of mathematical
operations up to make them more clear
Parenthesis
Power
Multiplication
Addition
Left to Right
Type
In CS and programming, a data type(simply type) is a
classification of data which tells the compiler or
interpreter how the programmer intends to use the
data.
Whole numbers are
represented using
the integer (int for
short) data type.
Numbers that can
have fractional
parts are
represented as
floating point (or
float) values.
 Complex numbers
 Long integers
 Doubles
String is a sequence of characters.
‘hello’
‘a’
‘1’
‘1 + 3’
“’”
True (1)
 False (0)
 In Python variables, literals, and
constants have a “type”
 Python knows the difference between
an integer number and a string
 For example “+” means “addition” if
something is a number and
“concatenate” if something is a string
>>> ddd = 1 + 4
>>> print(ddd)
5
>>> eee = 'hello ' + 'there'
>>> print(eee)
hello there
Types
 Numbers have two main types
 Integers are whole numbers: -14, -2, 0, 1, 100,
401233
 Floating Point Numbers have decimal parts: -
2.5 , 0.0, 98.6, 14.0
 There are other number types - they are
variations on float and integer
>>> xx = 1
>>> type (xx)
<type 'int'>
>>> temp = 98.6
>>> type(temp)
<type 'float'>
Mixing Integer and Floating
 When you perform an operation
where one operand is an integer and
the other operand is a floating point
the result is a floating point
 The integer is converted to a
floating point before the operation
>>> print(99 // 100)
0
>>> print(99 / 100.0)
0.99
>>> print(99.0 // 100)
0.0
>>> print(1 + 2 * 3 // 4.0 - 5)
-3.0
>>>
Type
 Python knows what “type”
everything is
 Some operations are prohibited
 You cannot “add 1” to a string
 We can ask Python what type
something is by using the type()
function.
>>> eee = 'hello ' + 'there'
>>> eee = eee + 1
Traceback (most recent call last):
File "<stdin>", line 1, in
<module>
TypeError: cannot concatenate
'str' and 'int' objects
>>> type(eee)
<type 'str'>
>>> type('hello')
<type 'str'>
Type Conversions
When you put an integer
and floating point in an
expression the integer is
implicitly converted to a
float
You can control this with
the built in functions int()
and float()
>>> print(float(99 // 100))
0.99
>>> i = 42
>>> type(i)
<type 'int'>
>>> f = float(i)
>>> print(f)
42.0
String Conversions
You can also use int()
and float() to convert
between strings and
integers
You will get an error if
the string does not
contain numeric
characters
>>> sval = '123'
>>> type(sval)
<type 'str'>
>>> print(sval + 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str'
and 'int'
>>> ival = int(sval)
>>> type(ival)
<type 'int'>
>>> print(ival + 1)
124
Mnemonic Variable Names
Since we programmers are given a choice in how we
choose our variable names, there is a bit of “best
practice”
We name variables to help us remember what we intend
to store in them (“mnemonic” = “memory aid”)
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print x1q3p9afd
hours = 35.0
rate = 12.50
pay = hours * rate
print pay
a = 35.0
b = 12.50
c = a * b
print c
What is this
code doing?

More Related Content

Similar to Intro to CS Lec03 (1).pptx

Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxLecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxAbdulQadeerBilal
 
component of c language.pptx
component of c language.pptxcomponent of c language.pptx
component of c language.pptxAnisZahirahAzman
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxSahajShrimal1
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3sotlsoc
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variablesIntro C# Book
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.pptjaba kumar
 
Function in Python [Autosaved].ppt
Function in Python [Autosaved].pptFunction in Python [Autosaved].ppt
Function in Python [Autosaved].pptGaganvirKaur
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdfpaijitk
 
Cpphtp4 ppt 03
Cpphtp4 ppt 03Cpphtp4 ppt 03
Cpphtp4 ppt 03sanya6900
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2Warui Maina
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxabhi353063
 
cpphtp4_PPT_03.ppt
cpphtp4_PPT_03.pptcpphtp4_PPT_03.ppt
cpphtp4_PPT_03.pptSuleman Khan
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python BasicsRaghunath A
 

Similar to Intro to CS Lec03 (1).pptx (20)

Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxLecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
 
component of c language.pptx
component of c language.pptxcomponent of c language.pptx
component of c language.pptx
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 
Python
PythonPython
Python
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 
Python
PythonPython
Python
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 
Function in Python [Autosaved].ppt
Function in Python [Autosaved].pptFunction in Python [Autosaved].ppt
Function in Python [Autosaved].ppt
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdf
 
Cpphtp4 ppt 03
Cpphtp4 ppt 03Cpphtp4 ppt 03
Cpphtp4 ppt 03
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
 
cpphtp4_PPT_03.ppt
cpphtp4_PPT_03.pptcpphtp4_PPT_03.ppt
cpphtp4_PPT_03.ppt
 
2 1 expressions
2 1  expressions2 1  expressions
2 1 expressions
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python Basics
 

Recently uploaded

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
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
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
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
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 

Recently uploaded (20)

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
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Ữ Â...
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
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
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 

Intro to CS Lec03 (1).pptx

  • 1.
  • 2.  A variable is a name that refers to a value which is used to refer to the value later x = 3 message = ‘hello’
  • 3.  More formally a variable is a named place in the memory where a programmer can store data and later retrieve the data using the variable “name”  Programmers get to choose the names of the variables
  • 4. Variables You can change the contents of a variable in a later statement 12.2 x 14 y x = 12.2 y = 14 100 x = 100
  • 5. Python Variable Name Rules Must start with a letter or underscore _ Must consist of letters and numbers and underscores Case Sensitive Good: spam eggs spam23 _speed Bad: 23spam #sign var.12 Different: spam Spam SPAM
  • 6. Reserved Words You can not use reserved words as variable names / identifiers and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print
  • 7. They are similar to variables: a memory location that’s been given a name. Unlike variables their contents shouldn’t change. >>> PI = 3.14
  • 8. 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)
  • 9. An expression is a combination of values, variables, and operators. 17 x x +17
  • 10. Sentences or Lines x = 2 x = x + 2 print(x) Assignment Statement Assignment with expression Print statement
  • 11. An assignment statement assigns a value to a variable x = 5 x=x+1 5 x 6 x
  • 13. Assignment Statements We assign a value to a variable using the assignment statement (=) An assignment statement consists of an expression on the right hand side and a variable to store the result x = 3.9 * x * ( 1 - x )
  • 14. x = 3.9 * x * ( 1 - x ) 0.6 x Right side is an expression. Once expression is evaluated, the result is placed in (assigned to) x. 0.6 0.6 0.4 0.93 A variable is a memory location used to store a value (0.6).
  • 15. 0.6 0.93 x 0.93 A variable is a memory location used to store a value. The value stored in a variable can be updated by replacing the old value (0.6) with a new value (0.93). x = 3.9 * x * ( 1 - x ) Right side is an expression. Once expression is evaluated, the result is placed in (assigned to) x.
  • 16. Numeric Expressions  Because of the lack of mathematical symbols on computer keyboards - we use “computer-speak” to express the classic math operations  Asterisk is multiplication  Exponentiation (raise to a power) looks different from in math. Operator Operation + Addition - Subtraction * Multiplication / Division ** Power % Remainder
  • 17. Numeric Expressions >>> xx = 2 >>> xx = xx + 2 >>> print xx 4 >>> yy = 440 * 12 >>> print(yy) 5280 >>> zz = yy / 1000 >>> print(zz) 5 >>> jj = 23 >>> kk = jj % 5 >>> print(kk) 3 >>>print(4 ** 3) 64 5 23 20 3 Operator Operation + Addition - Subtraction * Multiplication / Division ** Power % Remainder
  • 18. Order of Evaluation  When we string operators together - Python must know which one to do first  This is called “operator precedence”  Which operator “takes precedence” over the others x = 1 + 2 * 3 - 4 / 5 ** 6
  • 19. Operator Precedence Rules Highest precedence rule to lowest precedence rule  Parenthesis are always respected  Exponentiation (raise to a power)  Multiplication, Division, and Remainder  Addition and Subtraction  Left to right Parenthesis Power Multiplication Addition Left to Right
  • 20. Parenthesis Power Multiplication Addition Left to Right 1 + 2 ** 3 / 4 * 5 1 + 8 / 4 * 5 1 + 2 * 5 1 + 10 11 >>> x = 1 + 2 ** 3 / 4 * 5 >>> print(x) 11 >>>
  • 21. Parenthesis Power Multiplication Addition Left to Right 1 + 2 ** 3 / 4 * 5 1 + 8 / 4 * 5 1 + 2 * 5 1 + 10 11 Note 8/4 goes before 4*5 because of the left-right rule. >>> x = 1 + 2 ** 3 / 4 * 5 >>> print(x) 11 >>>
  • 22. Operator Precedence When writing code - use parenthesis When writing code - keep mathematical expressions simple enough that they are easy to understand Break long series of mathematical operations up to make them more clear Parenthesis Power Multiplication Addition Left to Right
  • 23. Type In CS and programming, a data type(simply type) is a classification of data which tells the compiler or interpreter how the programmer intends to use the data.
  • 24. Whole numbers are represented using the integer (int for short) data type. Numbers that can have fractional parts are represented as floating point (or float) values.
  • 25.  Complex numbers  Long integers  Doubles
  • 26. String is a sequence of characters. ‘hello’ ‘a’ ‘1’ ‘1 + 3’ “’”
  • 28.  In Python variables, literals, and constants have a “type”  Python knows the difference between an integer number and a string  For example “+” means “addition” if something is a number and “concatenate” if something is a string >>> ddd = 1 + 4 >>> print(ddd) 5 >>> eee = 'hello ' + 'there' >>> print(eee) hello there
  • 29. Types  Numbers have two main types  Integers are whole numbers: -14, -2, 0, 1, 100, 401233  Floating Point Numbers have decimal parts: - 2.5 , 0.0, 98.6, 14.0  There are other number types - they are variations on float and integer >>> xx = 1 >>> type (xx) <type 'int'> >>> temp = 98.6 >>> type(temp) <type 'float'>
  • 30. Mixing Integer and Floating  When you perform an operation where one operand is an integer and the other operand is a floating point the result is a floating point  The integer is converted to a floating point before the operation >>> print(99 // 100) 0 >>> print(99 / 100.0) 0.99 >>> print(99.0 // 100) 0.0 >>> print(1 + 2 * 3 // 4.0 - 5) -3.0 >>>
  • 31. Type  Python knows what “type” everything is  Some operations are prohibited  You cannot “add 1” to a string  We can ask Python what type something is by using the type() function. >>> eee = 'hello ' + 'there' >>> eee = eee + 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects >>> type(eee) <type 'str'> >>> type('hello') <type 'str'>
  • 32. Type Conversions When you put an integer and floating point in an expression the integer is implicitly converted to a float You can control this with the built in functions int() and float() >>> print(float(99 // 100)) 0.99 >>> i = 42 >>> type(i) <type 'int'> >>> f = float(i) >>> print(f) 42.0
  • 33. String Conversions You can also use int() and float() to convert between strings and integers You will get an error if the string does not contain numeric characters >>> sval = '123' >>> type(sval) <type 'str'> >>> print(sval + 1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' >>> ival = int(sval) >>> type(ival) <type 'int'> >>> print(ival + 1) 124
  • 34. Mnemonic Variable Names Since we programmers are given a choice in how we choose our variable names, there is a bit of “best practice” We name variables to help us remember what we intend to store in them (“mnemonic” = “memory aid”)
  • 35. x1q3z9ocd = 35.0 x1q3z9afd = 12.50 x1q3p9afd = x1q3z9ocd * x1q3z9afd print x1q3p9afd hours = 35.0 rate = 12.50 pay = hours * rate print pay a = 35.0 b = 12.50 c = a * b print c What is this code doing?

Editor's Notes

  1. Like a dog .... food ... food ...