Last updated: Monday 1st March 2021, 11:38 PT, AD


 

PLEASE REFRESH THIS PAGE TO GET THE LATEST VERSION

 


And now for something completely different . . .

Part 3

 

Python 3 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

 

Please note: all Python 3 example programs can be found here:

http://www.annedawson.net/python3programs.html

 

# program 03-01.py

 

 

total = 10

print (total)

print (type (total))

 

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:

When in the Python Shell (interactive) window, at the >>> prompt, type: help()

Read the instructions provided.

 

 

The example programs shown here:

http://www.annedawson.net/python3programs.html

contain useful comments to help you understand what the programs are doing.

 

 

 

Please carefully read the following important document...

http://www.annedawson.net/PythonComments.txt

 

 

 

 

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.142, 98.6)

   string           (text, e.g. "Anne", 'Anne', "Hello World!")

 

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

 

http://www.annedawson.net/python3programs.html

 

# See program 03-04.py

 

2 + 4

6 - 4

6 * 3

6 / 3

6 % 3   

# % is the modulus operator - see below for more info

6 // 3   # floor division: always truncates fractional remainders

-5

3**2     # three to the power of two

 

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)

 

http://www.annedawson.net/python3programs.html

 

# See program 03-05.py

 

 

2.0 + 4.0

6.0 - 4.0

6.0 * 3.0

6.0 / 3.0

6.0 % 3.0

# % is the modulus operator - see below for more info

6.0 // 3.0  # floor division: always truncates fractional remainders

-5.0

3.0**2.0    # three to the power of two

 

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.

 

 

 

 

 

 

Mixed Numeric Expressions

 

http://www.annedawson.net/python3programs.html

 

# See program 03-06.py

 

2 + 4.0

6 - 4.0

6 * 3.0

6 / 3.0

6 % 3.0

# % is the modulus operator - see below for more info

6 // 3.0  # floor division: always truncates fractional remainders

-5.0

3**2.0    # three to the power of two

 

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.

 

 

 


Relational operators relate two operands

 

http://www.annedawson.net/python3programs.html

 

# See program 03-07.py

 

 

7 > 10   # 7 is greater than 10  - this expression has the value "false"

4 < 16   # 4 is less than 16  - this expression has the value "true"

4 == 4   # 4 is equal to 4 - this expression has the value "true"

4 <= 4   # 4 is less than or equal to 4 - this expression has the value "true"

4 >= 4   # 4 is greater than or equal to 4 - this expression has the value "true"

4 != 4   # 4 is not equal to 4 - this expression has the value "false"

 

 

Boolean expressions result in values true or false

 

http://www.annedawson.net/python3programs.html

 

# See program 03-07.py

 

 

7 > 10

4 < 16

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.142,  98.6)

   string           (text, e.g. "Anne", 'Anne', "Hello World!")

 

String Objects

 

   string           text, e.g.

"Anne"

'Anne'

"where's the spam?"

 

http://www.annedawson.net/python3programs.html

 

# See program 03-08.py

# See program 03-09.py

 

 

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)

 

http://www.annedawson.net/python3programs.html

 

# See program 03-10.py

 

a = 'Hello out there'

b = "Where's the spam?"

c = a + b

print (c)

 

Click here for more info on the + operation.

 

 

 

 

 

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)

 

http://www.annedawson.net/python3programs.html

 

# See program 03-11.py

 

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 on the following link to see how to use the + operator

to join together (concatenate) two strings or add together two numbers:

http://www.annedawson.net/Python_Plus_Operator.html

 

 

 

Converting one data type to another (str to int)

 

http://www.annedawson.net/python3programs.html

 

# See program 03-12.py

 

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

 

http://www.annedawson.net/python3programs.html

 

# See program 03-13.py

 

 

# 03-13.py

# How to round up a floating point number

# to the nearest integer

# Updated: Monday 24th January 2011, 16:24 PT, AD

 

x = 1.6

print (x)

x = round(x)

print (x)

#compare the above with

x = 1.6

x = int(x)

print (x)

 

 

This Presentation uses the following program files:

 

 

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

 

 

Please note: all Python 3 example programs can be found here:

http://www.annedawson.net/python3programs.html

 

 

End of Python3_Data_Types.htm