CS计算机代考程序代写 python algorithm Digression: Scoring Matrices

Digression: Scoring Matrices

Lecture 4
Variables and numeric data types

L4 Variables and numeric data types – 2

Revision: functions and module file
# File: cel2fah.py
# A simple program is illustrating Celsius to Fahrenheit conversion

def c2f():
print(“This program converts Celsius into Fahrenheit”)
cel = float(input(“Enter temperature in Celsius: “))
fah = 9/5*cel+32
print(“The temperature in Fahrenheit is:”,fah)
print(“End of the program.”)

c2f()

• We use a filename ending in .py when we save our work to
indicate it’s a Python program.

• Click green button (run) on Thonny to run the program.

L4 Variables and numeric data types – 3

Objectives of this Lecture

• To understand the process of assigning values to
variables

• To understand simultaneous or unary operator
assignment

• To look into limitations of numeric data types

L4 Variables and numeric data types – 4

Assignment Statements

• Simple Assignment

=
variable is an identifier, expr is an expression

• The expression on the right is evaluated to produce a
value which is then associated with the variable
named on the left.

L4 Variables and numeric data types – 5

Assignment Statements – the Simple View

• Variables are like a box we can put values in.

• When a variable changes, the old value is erased and
a new one is written in.

10 11

Before Afterx = x+1

L4 Variables and numeric data types – 6

Assignment Statements

• x = 3.9 * x * (1-x)

• fahrenheit = 9/5 * celsius + 32

• x = 5

The spacing around parts of assignments is optional,
but makes the resulting program much more readable
– and hence maintainable.

L4 Variables and numeric data types – 7

Assignment Statements
Variables can be reassigned as many times as you
want!

>>> myVar = 0
>>> myVar
0
>>> myVar = 7
>>> myVar
7
>>> myVar = myVar + 1
>>> myVar
8

L4 Variables and numeric data types – 8

Simultaneous Assignment

• Several values can be calculated at the same time

, , … = , , …

• Evaluate the expressions on the right and assign
them to the variables on the left
– Must have same number of expressions as

variables!
>>>sum, diff = x+y, x-y

L4 Variables and numeric data types – 9

Simultaneous Assignment

• How could you swap the values for x and y?
– Why doesn’t this work?
x = y
y = x

– #assume x= 1, y= 9 initially….. x= y will make x= 9 (original
x is lost)

• We could use a temporary variable…
temp = x
x = y
y = temp

L4 Variables and numeric data types – 10

Simultaneous Assignment

We can swap the values of two variables easily in Python!

>>> x = 3

>>> y = 4

>>> print(x, y)

3 4

>>> x, y = y, x

>>> print(x, y)

4 3

L4 Variables and numeric data types – 11

Unary operator Assignment

• Certain types of assignment statement are so
common that a short-cut exists
x = x + n, (especially x = x +1)
x = x – n (n can be any expression)

• These become
x += n

x -= n

• Also
x *= n

x /= n, etc

L4 Variables and numeric data types – 12

Numeric Data Types

• There are two different kinds of numbers!
– 5, 4, 3, 6 are whole numbers – they don’t have a

fractional part
– 0.25, 1.10, 3.142 are decimal fractions

• Inside the computer, whole numbers and decimal
fractions are represented quite differently!
– We say that decimal fractions and whole numbers

are two different data types.

L4 Variables and numeric data types – 13

Numeric Data Types

• The data type of an object/variable determines

– what values it can have

– and what operations can be performed on it

– Taking 3 from 10: easy
– Taking 3 from J: ?

L4 Variables and numeric data types – 14

Numeric Data Types

• Whole numbers are represented using the integer
(int) data type.
– Size depends on machine using it

• These values can be positive or negative whole
numbers.

L4 Variables and numeric data types – 15

Numeric Data Types

• Numbers that can have fractional parts are
represented as floating point (or float) values.

• How can we tell which is which?

– A numeric literal without a decimal point
produces an int value

– A literal that has a decimal point is represented
by a float (even if the fractional part is 0)

L4 Variables and numeric data types – 16

Numeric Data Types
• Why do we need two number types?

– Values that represent counts can’t be fractional

– Most mathematical algorithms are very efficient
with integers

– The float type stores only an approximation to the
real number being represented!

– Since floats aren’t exact, use an int whenever
possible!

L4 Variables and numeric data types – 17

Numeric Data Types
• Operations on ints produce ints (excluding /)
• Operations on floats produce floats.
>>> 3.0+4.0

7.0

>>> 3+4

7

>>> 10.0/3.0

3.3333333333333335

>>> 10/3

3.3333333333333335

>>> 10 // 3

3

/ does floating point division

// does integer division

L4 Variables and numeric data types – 18

Numeric Data Types

• Integer division produces a whole number.
– That’s why 10//3 gives 3

• Think of it as ‘goes into’, where 10//3 gives 3 since 3
(goes into) 10, three times (with a remainder of 1)

• 10 % 3 = 1 is the remainder of the integer division of 10
by 3.

L4 Variables and numeric data types – 19

Limits of Int

• What’s going on?

– While there are an infinite number of integers,

there is a finite range of integers that can be

represented by int.

– This range depends on the number of bits a

particular CPU uses to represent an integer value.

• Does switching to float data types get us around the
limitations of int?

L4 Variables and numeric data types – 20

Approximation of float
>>> x1 = 10**121

10000…

>>> y1 = x1 + 1

10000… … … 1

>>> z1 = x1 – y1

-1

>>> x2 = 10e121

1e+122

>>> y2 = x2 + 1

1e+122

>>> z2 = x2 – y2

0.0

L4 Variables and numeric data types – 21

Handling Large Integers

• Floats are approximations

• Floats allow us to represent a larger range of values,
but with fixed precision.

• Python int is not a fixed size, but expands to handle
whatever value it holds.

• Newer versions of Python automatically convert int
to an expanded form when it grows so large as to
overflow.

• Can store and work with indefinitely large values
(e.g. 100!) at the cost of speed and memory

L4 Variables and numeric data types – 22

Type Conversion

• Combining an int with a float in an expression will return a
float

• We can explicitly convert between different data types

• For example the int and round functions convert a
float to integer
>>> int(6.8) # Truncate

6

>>> round(6.8)

7

>>> float(6)

6.0

L4 Variables and numeric data types – 23

Type Conversion : More examples

str: string/text

L4 Variables and numeric data types – 24

Find Data Type
• We can use the type function to find the data type
>>> type(4)

>>> type(4.3)

>>> x = 5.76
>>> type(x)

>>> type(hello) # Without quotes, assumes ‘hello’ is a
# variable. Not defined, so will generate
# an error

>>> type(‘hello’)

If a variable exists, the type of
the variable is the type of the
value assigned to it

L4 Variables and numeric data types – 25

Scientific Notation

• Typical precision is 16 digits (decimal places)

L4 Variables and numeric data types – 26

Float Problems

• Very large and very small floating point values can also
cause problems (current Python)

>>> x = 1.0e308
>>> x
1e+308
>>> x = 100* x
>>> x
inf

This is called
over-flow

>>> x = 1.0e-323
>>> x
1e-323
>>> x = x / 100.0
>>> x
0.0

This is called
under-flow

L4 Variables and numeric data types – 27

Lecture Summary

• Understanding the concept of assignment in Python

• We learned how to
– assign values to variables
– do multiple assignments in one statement
– definite simple definite loops
– Limitations of data types

Lecture 4�Variables and numeric data types
Revision: functions and module file
Objectives of this Lecture
Assignment Statements
Assignment Statements – the Simple View
Assignment Statements
Assignment Statements
Simultaneous Assignment
Simultaneous Assignment
Simultaneous Assignment
Unary operator Assignment
Numeric Data Types
Numeric Data Types
Numeric Data Types
Numeric Data Types
Numeric Data Types
Numeric Data Types
Numeric Data Types
Limits of Int
Approximation of float
Handling Large Integers
Type Conversion
Type Conversion : More examples
Find Data Type
Scientific Notation
Float Problems
Lecture Summary