-->

The Integer, Floating-Point, and String Data Types On Python

The Integer, Floating-Point, and String Data Types
The Integer, Floating-Point, and String Data Types


Remember that expressions are just values combined with operators, and they always evaluate down to a single value. A data type is a category for values, and every value belongs to exactly one data type. The most Python Basics 17 common data types in Python are listed in Table 1-2. The values -2 and 30, for example, are said to be integer values. The integer (or int) data type indicates values that are whole numbers. Numbers with a decimal point, such as 3.14, are called floating-point numbers (or floats). Note that even though the value 42 is an integer, the value 42.0 would be a floating-point number.

Common Data Types
Common Data Types

Python programs can also have text values called strings, or strs (pronounced “stirs”). Always surround your string in single quote (') characters (as in 'Hello' or 'Goodbye cruel world!') so Python knows where the string begins and ends. You can even have a string with no characters in it, '', called a blank string.

If you ever see the error message SyntaxError: EOL while scanning string literal, you probably forgot the final single quote character at the end of the string, such as in this example:

  1. >>> 'Hello world!
  2. SyntaxError: EOL while scanning string literal

String Concatenation and Replication

The meaning of an operator may change based on the data types of the values next to it. For example, + is the addition operator when it operates on two integers or floating-point values. However, when + is used on two string values, it joins the strings as the string concatenation operator. Enter the following into the interactive shell:

  1. >>> 'Alice' + 'Bob'
  2. 'AliceBob'

The expression evaluates down to a single, new string value that combines the text of the two strings. However, if you try to use the + operator on a string and an integer value, Python will not know how to handle this, and it will display an error message.

  1. >>> 'Alice' + 42
  2. Traceback (most recent call last):
  3. File "<pyshell#26>", line 1, in <module>
  4. 'Alice' + 42
  5. TypeError: Can't convert 'int' object to str implicitly

The error message Can't convert 'int' object to str implicitly means that Python thought you were trying to concatenate an integer to the string 'Alice'. Your code will have to explicitly convert the integer to a string, because Python cannot do this automatically. (Converting data types will
be explained in “Dissecting Your Program” on page 22 when talking about the str(), int(), and float() functions.) The * operator is used for multiplication when it operates on two integer or floating-point values. But when the * operator is used on one string value and one integer value, it becomes the string replication operator. Enter a string multiplied by a number into the interactive shell to see this in action.

  1. >>> 'Alice' * 5
  2.  'AliceAliceAliceAliceAlice'

The expression evaluates down to a single string value that repeats the original a number of times equal to the integer value. String replication is a useful trick, but it’s not used as often as string concatenation.

The * operator can be used with only two numeric values (for multiplication) or one string value and one integer value (for string replication). Otherwise, Python will just display an error message.

  1. >>> 'Alice' * 'Bob'
  2.  
  3. Traceback (most recent call last):
  4.  
  5. File "<pyshell#32>", line 1, in <module>
  6.  
  7. 'Alice' * 'Bob'
  8.  
  9. TypeError: can't multiply sequence by non-int of type 'str'
  10.  
  11. >>> 'Alice' * 5.0
  12.  
  13. Traceback (most recent call last):
  14.  
  15. File "<pyshell#33>", line 1, in <module>
  16.  
  17. 'Alice' * 5.0
  18.  
  19. TypeError: can't multiply sequence by non-int of type 'float'

It makes sense that Python wouldn’t understand these expressions: You can’t multiply two words, and it’s hard to replicate an arbitrary string a fractional number of times.


0 Response to "The Integer, Floating-Point, and String Data Types On Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel