Last updated: Monday 1st March 2021, 11:46 PT, AD
PLEASE
REFRESH THIS PAGE TO GET THE LATEST VERSION
And now for something
completely different . . .
Part 3
Python Data Types
and
Processing
3.1 Data types
3.2 Number processing
3.3 String Processing
3.4 Converting one type to another
A variable is a name that refers to a value. The assignment
statement creates new variables and gives them values:
word = "spam"
print word
Every 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. . .
A variable name is a reference
http://www.annedawson.net/PythonPrograms.txt
03-01.py
sum = 10
print sum
print type
(sum)
How 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. . .
Python Help
Python has its own Help system:
http://www.annedawson.net/Python_Help.htm
Python'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!")
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?")
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
Python
Library Reference
2.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.
Python Data Types and Processing
3.1 Data types
3.2 Number processing
3.3 String Processing
3.4 Converting one type to another
Numeric
Expressions (int)
2 + 4
6 - 4
6 * 3
6 / 3
6 % 3
6 // 3
-5
3**2
The % (modulus or modulo) operator yields the remainder
from the division of the first argument by the second. The arguments may be
floating point numbers, e.g., 3.14
% 0.7 equals 0.34 (since 3.14
equals 4 * 0.7 + 0.34.),
or integer numbers, e.g., 5 % 2 equals 1 (since 5 equals 2
* 2 + 1.).
Click here for info on when you might want to
use the modulus operator.
Numeric
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.0**2.0
Mixed
Numeric Expressions
2 + 4.0
6 - 4.0
6 * 3.0
6 / 3.0
6 % 3.0
6 // 3.0
-5.0
3**2.0
Relational operators relate two operands
7 > 10
4 < 16
4 == 4
4 <= 4
4 >= 4
4 != 4
4 <> 4
Boolean
expressions result in values true or false
7 > 10
4 < 16
4 == 4
4 <= 4
4 >= 4
4 != 4
4 <> 4
In expressions where there are a number of
different operators, do some have precedence over others? Yes they do... For
example, multiplications are always done before additions and subtractions.
Click here for more important
information on operator precedence.
Python Data Types and Processing
3.1 Data types
3.2 Number processing
3.3 String Processing
3.4 Converting one type to another
Python'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!")
String Objects
string text, e.g.
"Anne"
'Anne'
"where's the
spam?"
String Assignments
a = "Hello out there"
print a
b = 'Hello'
print b
c = "Where's the spam?"
print c
d = 'x'
print d
String Concatenation (joining)
a = 'Hello out there'
b = "Where's the spam?"
c = a + b
print c
Python Data Types and Processing
3.1 Data types
3.2 Number processing
3.3 String Processing
3.4 Converting one type to another
Converting 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
Click here for more
info on the + operation.
Converting 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)
Rounding 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
This 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
End of Python_Data_Types.htm