Title: And now for something completely different . . .
1And now for something completely different . .
.
2- Part 3
- Python Data Types
- and
- Processing
3PythonData Types and Processing
- 3.1 Data types
- 3.2 Number processing
- 3.3 String Processing
- 3.4 Converting one type to another
4PythonData Types and Processing
- 3.1 Data types
- 3.2 Number processing
- 3.3 String Processing
- 3.4 Converting one type to another
5A variable is a name that refers to a value. The
assignment statement creates new variables and
gives them valuesword "spam"print word
Variables in Python
6Every object has a data type (e.g. string), a
size (in bytes), a value (e.g. "spam") and a
location in the computer's memory. . .
Variables refer to Objects
Variable names such as word or sum actually
correspond to Python objects.
7Creation of a numeric variable
sum 10
sum
10
8A variable name is a reference
A variable name is a reference to an object. In
the following diagram, the object is shown as the
orange box. The size of the box is measured in
bytes.
sum 10
10
The value of the object is 10. The type of the
object is int.
9A variable name is a reference
A variable name is a reference to an object.
Variables are said to refer to (point to) the
object.
sum
Variable names actually store the address of the
location of the object in memory.
10
You don't need to know that
address.
By using a variable's name you gain access to the
value stored at that address.
10- http//www.annedawson.net/PythonPrograms.txt
- 03-01.py
- sum 10
11http//www.annedawson.net/PythonPrograms.txt 03-02
.py
12http//www.annedawson.net/PythonPrograms.txt 03-03
.py
- sum 10
- print sum
- print type (sum)
13How to get help on the Python Language
- When you start to use the Python language, you
may want to get more information on features such
as type, which allows you to inspect the data
type of an object. . .
14Python Help
- Python has its own Help system
- http//www.annedawson.net/Python_Help.htm
-
15Python's Data Types
- The main data types in Python are numbers and
strings (i.e. text). -
- int (integer, e.g. 12, 14, -101)
- float (floating point, e.g. 3.142e-10, 98.6)
- string (text, e.g. "Anne", 'Anne', "Hello
World!")
There are other data types (e.g. lists,
dictionaries, files) which will be introduced
later in the course.
16Storage Requirements of the Data types
- int (integer, e.g. 12, 14, -101)
- float (floating point, e.g. 3.142e-10, 98.6)
- string (text, e.g. "Anne", 'Anne', "Where's the
spam?")
How many bytes of memory do objects of the
different types occupy?
17Storage Requirements of the Data types
- int a minimum of 32 bits (4 bytes) to unlimited
precision - float 64 bits (8 bytes) precision (precision
machine dependent) - string regular ASCII code strings are 1 byte per
character
See the notes on the next slide taken from
the official Python web site...
18Python Library Reference2.3.4 Numeric Types
- There are four distinct numeric types plain
integers, long integers, floating point numbers,
and complex numbers. In addition, Booleans are a
subtype of plain integers. Plain integers (also
just called integers) are implemented using long
in C, which gives them at least 32 bits of
precision. Long integers have unlimited
precision. Floating point numbers are implemented
using double in C. All bets on their precision
are off unless you happen to know the machine you
are working with.
19PythonData Types and Processing
- 3.1 Data types
- 3.2 Number processing
- 3.3 String Processing
- 3.4 Converting one type to another
20Numeric Expressions (int)
- 2 4
- 6 - 4
- 6 3
- 6 / 3
- 6 3
- 6 // 3
- -5
- 32
03-04.py
21Numeric Expressions (float)
- 2.0 4.0
- 6.0 - 4.0
- 6.0 3.0
- 6.0 / 3.0
- 6.0 3.0
- 6.0 // 3.0
- -5.0
- 3.02.0
03-05.py
22Mixed Numeric Expressions
- 2 4.0
- 6 - 4.0
- 6 3.0
- 6 / 3.0
- 6 3.0
- 6 // 3.0
- -5.0
- 32.0
03-06.py
23Relational operators relate two operands
- 7 gt 10
- 4 lt 16
- 4 4
- 4 lt 4
- 4 gt 4
- 4 ! 4
- 4 ltgt 4
The result of these expressions is either true
or false (Boolean).
24Boolean expressions result in values true or false
- 7 gt 10
- 4 lt 16
- 4 4
- 4 lt 4
- 4 gt 4
- 4 ! 4
- 4 ltgt 4
03-07.py
25PythonData Types and Processing
- 3.1 Data types
- 3.2 Number processing
- 3.3 String Processing
- 3.4 Converting one type to another
26Python's Data Types
- The main data types in Python are numbers and
strings (i.e. text). -
- int (integer, e.g. 12, 14, -101)
- float (floating point, e.g. 3.142e-10, 98.6)
- string (text, e.g. "Anne", 'Anne', "Hello
World!")
There are other data types (e.g. lists,
dictionaries, files) which will be introduced
later in the course.
27String Objects
03-08.py
- string text, e.g.
- "Anne"
- 'Anne'
- "where's the
spam?"
28String Assignments
- a "Hello out there"
- print a
- b 'Hello'
- print b
- c "Where's the spam?"
- print c
- d 'x'
- print d
03-09.py
29String Concatenation (joining)
- a 'Hello out there'
- b "Where's the spam?"
- c a b
- print c
03-10.py
30PythonData Types and Processing
- 3.1 Data types
- 3.2 Number processing
- 3.3 String Processing
- 3.4 Converting one type to another
31Converting one data type to another (int to str)
- a 'Hello out there'
- b "Where's the spam?"
- c a b
- print c
- d c 10
- you cannot concatenate a string and an integer
- you must convert the integer to a string first
- d c str(10)
- print d
03-11.py
32Converting one data type to another (str to int)
- a "10"
- b '99'
- c a b
- print c
- print type(c)
- c int(c)
- print c
- print type(c)
03-12.py
33Rounding a floating point number to the nearest
integer
- 03-13.py
- How to round up a floating point number
- to the nearest integer
- x 1.6
- print x
- x round(x)
- print x
- x int(x)
- print x
03-13.py
34This presentation uses the following program
files
- http//www.annedawson.net/PythonPrograms.txt
- 03-01.py
- 03-02.py
- 03-03.py
- 03-04.py
- 03-05.py
- 03-06.py
- 03-07.py
- 03-08.py
- 03-09.py
- 03-10.py
- 03-11.py
- 03-12.py
- 03-13.py
35End of Python_Data_Types.ppt
36- Last updated Thursday 23rd October 2008, 926
PT, AHD