Lecture #2 Python Data Type and Variables data data type ...

[Pages:34]Lecture #2 Introduction

Over of Python data types

Python Data Type and Variables

In terms of programming, a "data" is an individual object that represents a qualitative or a quantitative value, such as a color or 23 units of a product. In order for the Python interpreter to implement an object to comply with the IPO (input, processing, and output) model of a computing, the object must be represented by an appropriate type of data.

A girl's first name, such as "Taylor", is an intangible object which is represented by a combination of English characters: T-a-y-l-o-r. Since a girl's first name can only consist of alphabets (no numerals, no symbols), it is necessary to define rules for picking only the proper characters to form a data that represents of a girl's first name. With the rules, no one can enter a combination of characters like 452 (obviously a numerical value) to a data field that stores data of a girl's first name. Similarly, a data field that holds girl's age should not be assigned a value like "Alicia".

The term "data type" (or simply "type") refers to the definition of nature settings of a Python object in order to properly represent the object and process it with Python's language core. In computing, a value that carries a meaning of some kind is known as a "literal". A string literal is a sequence of characters enclosed by either a pair of single quotes or a pair of double quotes. A string literal represents a string value. A number is a sequence of digits that are not enclosed by quotes. A number represents a numerical value in units. In Python, the most frequently used types are data of numbers and string values.

As discussed in a previous lecture, the "data type" is implicitly determined by the Python interpreter as the moment when a value is to assign to a variable. This lecture, thus, will start with the discussion of available Python data types.

Python's language core comes with a set pre-defined data types, known as "primitive data type". They are the built-in data types encapsulated in several "object classes" of Python. Each object class has a unique identifier, such as "int", "float", "str", "bool", and "NoneType". The term "to be encapsulated" in programming means "to be defined".

Similar types of data are grouped in categories. The following table provides an overview of available categories of Python's primitive data types.

Categories numbers

Description

Numbers are typically made of numerical characters and the decimal sign (.). Some numbers are expressed in scientific notation, such as 2.3e25 (which means 2.3?1025).

Python class int and float

string

A string is a combination of any Python recognizable

str

characters (even in other natural language like Chinese,

Japanese, and Farsi).

Boolean

Boolean value are strictly limited to two possible values: bool True and False. Sometime, True is represented by 1 while False is represented by 0.

container

The "container" type refers to a group of data types that collect Python objects in a "container" object.

tuple, list, dictionary

Python Programming ? Penniel P. Wu, PhD.

49

Concept of variables

set

A Python set is an unordered collection of elements. It is set

used for calculation based on the Set Theory of

mathematics.

special

The keyword None is a single value that signify "absence NoneType of a value".

The next several sections will start with the discussion about what a variable is in Python, and continue to discuss each of these categories in detail.

In programming, the term "variable" refers to a programmer-defined name that represents a group of registers in the physical memory (typically the DRAMs). These registers make up a fixed number of bits, as specified by the data type, to temporarily host the value that is assigned to the variable. Since the value stored in these registers can be modified, they are "variable" values. In the following statement, "x" is the identifier of a variable that is assigned 4265 as value. In other words, a Python value "x" is declared to hold an integer 4265.

>>> x = 4265

The following figure illustrates how a series of consecutive registers in a DRAM, from 21st to the 24th, are grouped to keep an integer 4265. In this scenario, "x" is the identifier of the variable and is also the temporarily name of the group of registers.

Register ... 21 22 23 24 ...

identifier x

The value it holds 4265

Python programmers can assign an identifier to a Python object through the declaration of a variable. The following illustrates how to: (a) designate an identifier to a variable; (b) assign a value to the variable; and (c) set the data type of the variable.

>>> x = 2.1 >>> type(x) >>> id(x) 20027688 >>> y = "orange" >>> type(y)

In the above example, the statement x = 2.1 reads "assign a number 2.1 to the variable x" and the statement y = "orange" reads " assign a string literal `orange' to y". The equal sign (=) represents the "assignment" operator which assign a value to a variable. A later lecture will discuss about Python operators in details. By the way, in terms of Python, the value 2.1 in the statement x = 2.1 and the value "orange" of the y = "orange" statements are referred to a "literal".

Variables are useful in problem solving, particularly when some mathematical calculation is required. In Physics, the concept of "uniform motion" is described as x = vt, where x is the distance traveled, v is the constant velocity, and t is the time the object is in motion. The equation, x = vt, uses three variables: x, v, and t. A Python programmer can write a program to solve the following problem.

Python Programming ? Penniel P. Wu, PhD.

50

"Jennifer drives along the freeway to Las Vegas. She sets the cruise on 80 mile per hour. After 30 minutes, how far has she travelled?"

The following is a sequential list of statements to solve this problem in Python. The answer is 40.0 miles.

>>> v = 80 >>> t = 0.5 >>> x = v*t >>> x 40.0

In terms of object-oriented programming, officially designate a meaningful identifier to a variable is known as "declaration". Declaring a variable provides a way to label and access data without the need to know exactly where in the physical memory the data is stored. However, unlike other language, Python does not require the programmer to specify the data type when creating a variable. Python interpreter will automatically regulate the data type of a variable according to the value it is assigned to host. In the following, the variable x is set to be "int" type first, then "str" and "tuple" accordingly.

>>> x = 2 >>> type(2) >>> x = "Python" >>> type(x) >>> x = (9, 8, 7, 6) >>> type(x)

The following is another example that demonstrates the feasibility to use one variable name to represent different kinds of objects at different times.

>>> x = 0 >>> x = "Hello" >>> x = [1, 2, 3]

# x bound to an integer object # Now it's a string. # And now it's a list.

As illustrated in a previous lecture, another way to display the value stored in a variable is to use the print() function, as shown below.

>>> firstname = "Larry" >>> print(firstname) 'Larry'

The above statements create a variable called "firstname" and assign it with the value "Larry". The print() function then displays the value assigned to firstname on the screen.

Python recommends a naming convention for creating variable identifiers. Basically, a variable identifier (or variable name) must: (a) start with either an underscore (_) or a letter (such as "a" or "A"); (b) followed by a combination of alphanumerical characters (letters and digits), except the blank space; (c) use the underscore (_) to con; and (d) avoid using any Python reserved keywords. The following table lists some of the reserved keywords.

and assert break class

Table: Python reserved keywords

del

for

is

elif

from

lamda

else

global

not

except

if

or

raise return try while

Python Programming ? Penniel P. Wu, PhD.

51

Numerical types

continue def

exec finally

import in

pass print

yield

Under this naming convention, the following are examples of legal variable identifiers. However, class, 1_Spam, spam$, and my@#! are illegal variable names. Also, Python is casesensitive, SPAM, spam, SpAm, sPAm, and SpAM are different identifiers.

>>> first_name = "Taylor" >>> _last_name = "Swift" >>> _age = 24 >>> best_song = "You Belong With Me" >>> year = 2009

It is also necessary to note that identifiers that have two leading and trailing underscores, such as __main__, are system-defined names. They have special meanings to the interpreter; therefore, programmers should avoid using two leading or trailing underscore (__) to name Python variables. A later lecture will discuss about such identifiers in detail.

In the "numbers" category, Python 2 data could be one of the four types: int, long, float, and complex. Interestingly, Python 3 drop the long type and preserves only three distinct numeric types: integers, floating point numbers, and complex numbers.

Numerical data are values that represent a number of units. A numerical literal consists of digits only (no letters, not symbols) and is used to express a number of units. They should not be enclosed by quotes. Python 3 provide three object class to define numerical data: "int" (short for integer), "float" (short for floating-point), and "complex" (short for complex numbers). The following is a complete list of numerical types supported by Python 3.

Type int

Table: Numeric literals

Mathematic name

Examples

Integers

>>>1234 1234

>>> -24

-24

>>> 0

0

float

Floating-point

>>> 1.23 >>> 3.14e-10 >>> 5.07E210 >>> 5.07e+210

complex Complex number

3+4j, 3.0+4.0j, 3J

Integers are whole number that can be positive, negative, or 0 and is written in a form without comma(s). In Python, the int type can be expressed in decimal format (the base is 10), which is the default format. The following prints out a negative integer expressed in the decimal format.

>>> print(-25) -25

In Python, an integer can also be expressed in binary (the base is 2), octal (the base is 8), or hexadecimal (the best is 16) formats. Except the decimal format, all other formats required a prefix to specify their formats. The following table lists the formats and their prefixes with examples. By the way, prefixes are not case-sensitive.

Format

Prefix

Example

Python Programming ? Penniel P. Wu, PhD.

52

Decimal Binary Octal hexadecimal

N/A 0b (or 0B) 0o (or 0O) 0x (or 0X)

>>> 124 124 >>> 0b1111100 124 >>> 0o174 124 >>> 0x7c 124

Binary literals start with a leading prefix zero-b, "0b" (or "0B"), followed by a series of 0's and 1's. The following demonstrates how to specify a value in binary format. The value 124 in decimal format is equivalent to 1111100 in binary, 174 in octal, and 7c in hexadecimal.

>>> 0b1111100 124

The following demonstrates how Python interpreter automatically convert non-decimal literals

to their decimal equivalents and display only the decimal values as results. Apparently, the

conversion is based on the formula: ? -1 =a1?b0 + a2?b1 + a3?b2 + ... + an?bn-1,

where an is the n-th digit (counting from the right to left) and b is the base.

>>> 1, 10, 100 (1, 10, 100) >>> 0b1 , 0b10, 0b100 (1, 2, 4) >>> 0o1, 0o10, 0o100 (1, 8, 64) >>> 0x01, 0x10, 0xFF (1, 16, 255)

# decimal literals # binary literals # Octal literals # Hex literals

The binary literal, 0b1010, is calculated by the Python interpreter with the base being 2 to find its decimal equivalent: 1?23 + 0?22 + 1?21 +1?20 = 11.

>>> 0b1011 11

Octal literals start with a leading prefix zero-o, 0o (or 0O), followed by a series of octal digits 0-7; hexadecimals start with a leading prefix 0x (or 0X), followed by hexadecimal digits 0~9, and A~F. In hexadecimal, "A", "B", "C", "D", "E", and "F" (or "a", "b", "c", "d", "e", and "f"), as digits that represent 10, 11, 12, 13, 14, and 15 respectively. Hexadecimal digits are not case sensitive.

>>> 0XFF == 0Xff == 0XFf == 0XfF True

A number express in 0o37 = 3 ? 81 + 7 ? 80 = 31.

>>> y = 0o37 31

The octal literal, 0o177, is calculated with the base being 8: 1?82 + 7?81 + 7?80 = 127

>>> 0o177 127

The hexadecimal literal, 0x9ff, is calculated with the base being 16: 9?162 + f?161 + f?160 = 9?162 + 15?161 + 15?160 = 2559.

>>> 0x9ff

Python Programming ? Penniel P. Wu, PhD.

53

2559

A number expressed in 0xA0F = A ? 162 + 0 ? 161 + F ? 160 = 10 ? 162 + 0 ? 161 + 15 ? 160 = 2575.

>>> x = 0xA0F 2575

All the above numbers are created by specifying numeric literals. Yet, Python numbers can also be created as the result of built-in functions and operators. The following use the "division" operator to produce a number of float type.

>>> print(5.0/9.0) 0.5555555555555556

The following use the "bin()" function to convert a number 1234 to its binary format.

>>> bin(1234) '0b10011010010'

Although Python prints in decimal by default, Python also provides few built-in functions to convert decimal integers to their octal and hexadecimal equivalents. The oct() function converts decimal to octal, and the hex() to hexadecimal.

>>> oct(64), hex(64), hex(255) ('0o100', '0x40', '0xff')

The int() function can also convert a "string of digits" to an integer with an optional second argument that specifies the numeric base. A "string of digits" means a combination of digits that is enclosed by a pair of quotes. If the second argument is absent, the base is assumed to be 10.

>>> int('0100'), int('0100', 8), int('0x40', 16) (100, 64, 64)

The eval() function treats strings as though they were Python code. It therefore has a similar effect:

>>> eval('100'), eval('0o100'), eval('0x40') (100, 64, 64)

Python also supports long integers, yet the instructor could not find any definition of range to specify the maximum and minimum. The following are three very large integers. By the way, two asterisk signs (**) represents the exponentiation operator. The statement 3 ** 2 reads "3 raised to the second power".

>>> 999999999999999999999999999 + 1 1000000000000000000000000000 >>> (123456789 * 987654321) ** 2 14867566530049990397812181822702361 >>> -1234567890098765432112345678900987654321 -1234567890098765432112345678900987654321

The term "floating-point number" refers any number that can contain a fractional part next to the decimal point, such as 3.14, -6.1723, and 18238.04956321697. In other words, floating point numbers are numerical values with fractional parts, such as 53.16148.

>>> 53.16148

Python Programming ? Penniel P. Wu, PhD.

54

As stated in a previous lecture, the precision of calculation result varies from one operating systems (OS) to another. This is not a bug or error of the operation system. It is a result of the fact that most decimal fractions cannot be represented exactly as a float. Floating-point numbers are represented in computer hardware as binary fractions (base 2), yet, they must be converted to decimal fractions (base 10). Unfortunately, the result of conversion is usually a close approximate, not the exact value. The following compares the outputs between Windows 7/8 and Linux. The statement simply asks the Python prompt to display 0.3. The result of Windows 7/8 is much closer to human readers' expectation.

Windows 7/8 >>> 0.3 0.3

Linux >>> 0.3 0.2999999999999999

One way to resolve the precision issue is to use the format() function. The directive, ".1f", specifies that only 1 digit next to the decimal point.

>>> format(0.3, ".1f") '0.3'

In Python, float literals may be expressed in scientific notation, with E or e indicating the power of 10. The number, 4.17?10-25, can be expressed as either 4.17E-25 or 4.17e-25, as proved below. 6.25e-23 is the scientific form of 6.25?10-23, while 4.173e15 is 4.173?1015.

>>> 4.17E25 == 4.17e25 True >>> 6.25e-23 6.25e-23 >>> 4.173e15 4.173e15

In Python, two equal signs (==) represents the equality operator which checks if two literals are equivalent. The returned value, True, indicates that the two sides of literal are the same. As specified in scientific notation, the positive sign (+) of an exponent is optional.

>>> 5.07e+210 == 5.07e210 True >>> 5.07e+210 == 5.07E210 True

By the way, the instructor was not able to find reliable reference that can provide the limit of floating-point values.

>>> 4.5e219 * 3.1e14 1.395e+234

Numerical values can be either positive or negative. The sign of an integer is denoted by + or for positive or negative. The positive sign is default; therefore, any numerical value with a negative sign (-) is assumed to be positive. In some operating systems, -5.19e24 might be displayed as -5.18999999999999e+24.

>>> -51 -51 >>> -3.173 -3.173 >>> -5.19e24 -5.19e24

Any number, when multiplied with -1, change its sign.

Python Programming ? Penniel P. Wu, PhD.

55

>>> -51 * -1 51

In mathematics, a complex number is an ordered pair of real floating-point numbers denoted by a + bj, where a is the real part and b is the imaginary part of the complex number. Python support complex data type to define a complex literal, as shown below, in which 3 is a real number, while 5j is an imaginary value. The imaginary part must be suffixed by a j or J.

>>> 3 + 5j

The following is another sample of complex number.

>>> 3 + 26J

A complex number with a real part of 2 and an imaginary part of -3 is written as 2 + -3j or 2 ? 3j.

>>> (2 + -3j) == (2 - 3j) True

When the real part is 0, a complex number can be simplified to nj or nJ.

>>> (0 + 7J) == 7J True >>> (0 - 4j) == -4j True

The followings are some examples of complex math at work:

>>> 1j * 1J (-1+0j) >>> 2 + 1j * 3 (2+3j) >>> (2+1j)*3 (6+3j)

The following demonstrates how to assign a numerical value to a variable. In some operating systems, 37.4 might be displayed as 37.399999999999999.

>>> x = 12 >>> y = 37.4 >>> z = 9.1e6 >>> x, y, z (12, 37.4, 9100000.0) >>> x = (3 + 5j) >>> y = (2 ? 1j) >>> x, y ((3+5j), (2-1j))

Variables of numerical type can undergo arithmetic operations, such as addition (+), subtraction (-), multiplication (*), and division (/).

>>> x = 12 >>> y = 37.4 >>> z = 9.1e6 >>> x + y + z 9100049.4 >>> x - y - z -9100025.4

Python Programming ? Penniel P. Wu, PhD.

56

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download