【Python】Basics - Simple Data Types

Posted by 西维蜀黍 on 2019-09-17, Last Modified on 2024-05-07

Data Types

  • integer: 4
  • string: “Hello World!” - encloed in quotation marks (single quotes (’) or double quotes (""), or three of each (’’’ or “”"))

  • float: 3.2 - a number with decimal point
  • bool: True

Numbers

You can add (+), subtract (-), multiply (*), and divide (/) integers in Python.

>>> 2 + 3 
5
>>> 3 - 2 
1
>>> 2 * 3 
6
>>> 3 / 2
1.5

# // represents exact division
>>> 3 // 2
1

In a terminal session, Python simply returns the result of the operation. Python uses two multiplication symbols to represent exponents:

>>> 3 ** 2 
9

>>> 3 ** 3 
27

>>> 10 ** 6 
1000000

Integers in Python 2.X: normal and long

In Python 2.X there are two integer types, normal (often 32 bits) and long (unlimited precision), and an integer may end in an l or L to force it to become a long integer.

Because integers are automatically converted to long integers when their values overflow their allocated bits, you never need to type the letter L yourself—Python automatically converts up to long integer when extra precision is needed.

Integers in Python 3.X: a single type

In Python 3.X, the normal and long integer types have been merged—there is only integer, which automatically supports the unlimited precision of Python 2.X’s separate long integer type. Because of this, integers can no longer be coded with a trailing l or L, and integers never print with this character either. Apart from this, most programs are unaffected by this change, unless they do type testing that checks for 2.X long integers.

Hexadecimal(十六进制), octal(八进制), and binary(二进制) literals

Integers may be coded in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2), the last three of which are common in some programming do-mains.

  • Hexadecimals start with a leading 0x or 0X, followed by a string of hexadecimal digits (0–9 and A–F). Hex digits may be coded in lower- or uppercase.
  • Octal literals start with a leading 0o or 0O (zero and lower- or uppercase letter o), followed by a string of digits (0–7). In 2.X, octal literals can also be coded with just a leading 0, but not in 3.X—this original octal form is too easily confused with decimal, and is replaced by the new 0o format, which can also be used in 2.X as of 2.6.
  • Binary literals, new as of 2.6 and 3.0, begin with a leading 0b or 0B, followed by binary digits (0–1).

Note that all of these literals produce integer objects in program code; they are just alternative syntaxes for specifying values. The built-in calls hex(I), oct(I), and bin(I) convert an integer to its representation string in these three bases, and int(str, base) converts a runtime string to an integer per a given base.

Floats

>>> 0.1 + 0.1
0.2

>>> 0.2 + 0.2
0.4

>>> 2 * 0.1
0.2

>>> 2 * 0.2
0.4

Booleans

A Boolean expression is just another name for a conditional test. A Boolean value is either True or False, just like the value of a conditional expression after it has been evaluated.

Boolean values are often used to keep track of certain conditions, such as whether a game is running or whether a user can edit certain content on a website:

game_active = True 
can_edit = False

The Meaning of True and False in Python

Notice that the test results returned in the last two examples represent true and false values. They print as the words True and False, but now that we’re using logical tests like these in earnest, I should be a bit more formal about what these names really mean.

In Python, as in most programming languages, an integer 0 represents false, and an integer 1 represents true. In addition, though, Python recognizes any empty data structure as false and any nonempty data structure as true. More generally, the notions of true and false are intrinsic properties of every object in Python—each object is either true or false, as follows:

  • Numbers are false if zero, and true otherwise.
  • Other objects are false if empty, and true otherwise.

The table below gives examples of true and false values of objects in Python:

Reference

  • Learning Python
  • Python Crash Course (2nd Edition) : A Hands-On, Project-Based Introduction to Programming