0% found this document useful (0 votes)
12 views

Variables and Types

The document discusses variables in Python. A variable is a name that refers to a value which can be used to refer to that value later. Variables are memory locations that store values and their contents can be changed by assigning new values. Variables must follow certain naming rules and cannot use reserved words. Numeric expressions in Python use operators like + - * / ** % to represent mathematical operations.

Uploaded by

Samuel Tafere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Variables and Types

The document discusses variables in Python. A variable is a name that refers to a value which can be used to refer to that value later. Variables are memory locations that store values and their contents can be changed by assigning new values. Variables must follow certain naming rules and cannot use reserved words. Numeric expressions in Python use operators like + - * / ** % to represent mathematical operations.

Uploaded by

Samuel Tafere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

▪ A variable is a name that refers to a

value which is used to refer to the value


later

x=3
message = ‘hello’

2
▪ 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 3
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

4
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

5
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
6
▪They are similar to variables: a memory
location that’s been given a name.
▪Unlike variables their contents shouldn’t
change.

>>> PI = 3.14

7
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)

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

▪ 17
▪ x
=> x +17
9
Sentences or Lines

x=2 Assignment Statement

x=x+2 Assignment with expression

print(x) Print statement

10
An assignment statement assigns a value
to a variable

x=5 x 5

x=x+1 x 6
11
<variable> = <expr>

12
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 )

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

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

15
Numeric Expressions
Operator Operation
▪ Because of the lack of mathematical
+ Addition
symbols on computer keyboards - we
use “computer-speak” to express the - Subtraction

classic math operations * Multiplication

▪ Asterisk is multiplication / Division


** Power
▪ Exponentiation (raise to a power) looks
% Remainder
different from in math.
16
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)
>>> print(yy) 64 / Division

5280 ** Power
>>> zz = yy / 1000 % Remainder
5 23
>>> print(zz) 20
5
3 17
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

18
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

19
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 11
Left to Right

20
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
21
Operator Precedence
▪ When writing code - use parenthesis
Parenthesis
▪ When writing code - keep mathematical Power
Multiplication
expressions simple enough that they are Addition
easy to understand Left to Right

▪ Break long series of mathematical


operations up to make them more clear
22
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.

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

24
▪ Complex numbers

▪ Long integers

▪ Doubles

25
String is a sequence of characters.
‘hello’
‘a’
‘1’
‘1 + 3’
“’”

26
▪True (1)

▪ False (0)

27
▪ In Python variables, literals, and

constants have a “type”


>>> ddd = 1 + 4
>>> print(ddd)
▪ Python knows the difference between 5
an integer number and a string >>> eee = 'hello ' + 'there'
>>> print(eee)
▪ For example “+” means “addition” hello there
if something is a number and
“concatenate” if something is a
string 28
Types
▪ Numbers have two main types
▪ Integers are whole numbers: -14, -2, 0, 1, 100, >>> xx = 1
401233 >>> type (xx)
<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
29
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
>>>

30
Type >>> 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 >>> type(eee)


string <type 'str'>
>>> type('hello')
▪ We can ask Python what type <type 'str'>
something is by using the type()
31
Type Conversions
>>> print(float(99 // 100))
▪ When you put an integer 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()
32
String Conversions
>>> sval = '123'
>>> type(sval)
▪ You can also use int() <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
33
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”)

34
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print x1q3p9afd

What is this
code doing?

35
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c=a*b
print x1q3p9afd print c

What is this
code doing?

36
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

37

You might also like