Last updated: Thursday 4th September 2008, 8:56 PT, AHD


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

 

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

 

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

 

 

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