You are on page 1of 35

Variables & Types

Variables
 A variable is a name that refers to a
value which is used to refer to the value
later

x=3
message = ‘hello’
Variables

 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

x = 12.2 x 12.2 100


y = 14
x = 100 y 14
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
Named Constants
They are similar to variables: a memory
location that’s been given a name.
Unlike variables their contents shouldn’t
change.

>>> PI = 3.14
Literals
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)


Expressions
An expression is a combination of values,
variables, and operators.

17
x
x +17
Sentences or Lines

x=2 Assignment Statement

x=x+2 Assignment with expression

print(x) Print statement


Assignment statement
An assignment statement assigns a value
to a variable

x=5 x 5

x=x+1 x 6
Basic Form

<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 )
A variable is a memory location x 0.6
used to store a value (0.6).
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4

Right side is an expression. Once


expression is evaluated, the result 0.93
is placed in (assigned to) x.
A variable is a memory location
used to store a value. The value x 0.6 0.93
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 0.93
is placed in (assigned to) x.
Numeric Expressions
Operator Operation
 Because of the lack of mathematical
+ Addition
symbols on computer keyboards - we
- Subtraction
use “computer-speak” to express the
classic math operations * Multiplication

 Asterisk is multiplication / Division


** Power
 Exponentiation (raise to a power) looks
% Remainder
different from in math.
Numeric Expressions
Operator Operation
>>> xx = 2 >>> jj = 23
+ Addition
>>> xx = xx + 2 >>> kk = jj % 5
>>> print xx >>> print(kk) - Subtraction
4 3 * Multiplication
>>> yy = 440 * 12 >>>print(4 ** 3)
64 / Division
>>> print(yy)
5280 4R3 ** Power
>>> zz = yy / 1000 % Remainder
5 23
>>> print(zz) 20
5
3
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) Parenthesis


 Multiplication, Division, and Remainder
Power
Multiplication
 Addition and Subtraction Addition
 Left to right
Left to Right
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x) 1+8/4*5
11
>>> 1+2*5

Parenthesis
Power 1 + 10
Multiplication
Addition
Left to Right
11
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x) 1+8/4*5
11
>>> Note 8/4 goes before 4*5 1+2*5
because of the left-right
rule.
Parenthesis 1 + 10
Power
Multiplication
Addition 11
Left to Right
Operator Precedence
 When writing code - use parenthesis
Parenthesis
 When writing code - keep mathematical Power
Multiplication
expressions simple enough that they are Addition
Left to Right
easy to understand

 Break long series of mathematical

operations up to make them more clear


What does “Type” Mean?

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.
Numeric Types
Whole numbers are Numbers that can
represented using have fractional
the integer (int for parts are
short) data type. represented as
floating point (or
float) values.
Other Numeric Types

 Complex numbers

 Long integers

 Doubles
String
String is a sequence of characters.
‘hello’
‘a’
‘1’
‘1 + 3’
“’”
Boolean (bool)

True (1)

 False (0)
Types in Python
 In Python variables, literals, and
>>> ddd = 1 + 4
constants have a “type”
>>> print(ddd)
 Python knows the difference between 5
>>> eee = 'hello ' + 'there'
an integer number and a string
>>> print(eee)
 For example “+” means “addition” if hello there
something is a number and
“concatenate” if something is a string
Several Types of Numbers
 Numbers have two main types

 Integers are whole numbers: -14, -2, 0, 1, 100,


>>> xx = 1
>>> type (xx)
401233
<type 'int'>
 Floating Point Numbers have decimal parts: - >>> temp = 98.6
2.5 , 0.0, 98.6, 14.0 >>> type(temp)
<type 'float'>
 There are other number types - they are

variations on float and integer


Mixing Integer and Floating
>>> print(99 // 100)
 When you perform an operation
0
where one operand is an integer and >>> print(99 / 100.0)
the other operand is a floating point 0.99
>>> print(99.0 // 100)
the result is a floating point 0.0
 The integer is converted to a >>> print(1 + 2 * 3 // 4.0 - 5)
-3.0
floating point before the operation >>>
Type Matters >>> eee = 'hello ' + 'there'
>>> eee = eee + 1
 Python knows what “type” Traceback (most recent call last):
File "<stdin>", line 1, in <module>
everything is TypeError: cannot concatenate 'str'
 Some operations are prohibited
and 'int' objects

 You cannot “add 1” to a string >>> type(eee)


<type 'str'>
 We can ask Python what type >>> type('hello')
something is by using the type() <type 'str'>
function.
Type Conversions
>>> print(float(99 // 100))
 When you put an integer X
0.99
and floating point in an >>> i = 42
expression the integer is >>> type(i)
implicitly converted to a <type 'int'>
>>> f = float(i)
float >>> print(f)
 You can control this with 42.0
the built in functions int()
and float()
String Conversions
>>> sval = '123'
 You can also use int() >>> type(sval)
<type 'str'>
and float() to convert >>> print(sval + 1)
between strings and Traceback (most recent call last):
File "<stdin>", line 1, in <module>
integers TypeError: cannot concatenate 'str' and
'int'
 You will get an error if >>> ival = int(sval)
the string does not >>> type(ival)
<type 'int'>
contain numeric >>> print(ival + 1)
characters 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 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c=a*b
print x1q3p9afd print c

hours = 35.0
rate = 12.50
What is this
pay = hours * rate
code doing?
print pay

You might also like