Python Note 2
Python Note 2
Python Note 2
30/12/2023
print("2")
print(2)
The print() function presents them in exactly the same way - this example is
obvious, as their human-readable representation is also the same. Internally,
in the computer's memory, these two values are stored in completely
different ways - the string exists as just a string - a series of letters.
Integers
binary system
it's the system computers use for storing numbers, and that they can perform
any operation upon them.
The characteristic of the numeric value which determines its kind, range, and
application, is called the type.
If you encode a literal and place it inside Python code, the form of the literal
determines the representation (type) Python will use to store it in the
memory.
Therefore, you can write this number either like this: 11111111 , or like
that: 11_111_111 .
和十六进制)
print(0o123)
print(0x123)
Floats
Whenever we use a term like two and a half or minus zero point four, we think
of numbers which the computer considers floating-point numbers:
2.5
-0.4
Note: two and a half looks normal when you write it in a program, although if
your native language prefers to use a comma instead of a point in the
number, you should ensure that your number doesn't contain any
commas at all.
Ints vs. floats
4
4.0
You may think that they are exactly the same, but Python sees them in a
completely different way.
On the other hand, it's not only points that make a float. You can also use the
letter e .
When you want to use any numbers that are very large or very small, you can
use scientific notation.
Take, for example, the speed of light, expressed in meters per second.
Written directly it would look like this: 300000000 .
In Python, the same effect is achieved in a slightly different way - take a look:
3E8
The letter E (you can also use the lower-case letter e - it comes from the
word exponent) is a concise record of the phrase times ten to the power of.
Note:
Coding floats
Let's see how this convention is used to record numbers that are very small
(in the sense of their absolute value, which is close to zero).
If you would like to use it in a program, you should write it this way:
6.62607E-34
Note: the fact that you've chosen one of the possible forms of coding float
values doesn't mean that Python will present it the same way.
For example, let's say you've decided to use the following float literal:
0.0000000000000000000001
print(0.0000000000000000000001)
1e-22
Strings are used when you need to process text (like names of all kinds,
addresses, novels, etc.), not numbers.
You already know a bit about them, e.g., that strings need quotes the way
floats need points.
However, there is a catch. The catch is how to encode a quote inside a string
which is already delimited by quotes.
The first is based on the concept we already know of the escape character,
which you should remember is played by the backslash. The backslash can
escape quotes too. A quote preceded by a backslash changes its meaning -
it's not a delimiter, but just a quote. This will work as intended:
Note: there are two escaped quotes inside the string - can you see them both?
The second solution may be a bit surprising. Python can use an apostrophe
instead of a quote. Either of these characters may delimit strings, but you
must be consistent.
If you open a string with a quote, you have to close it with a quote.
Boolean values
Each time you ask Python if one number is greater than another, the question
results in the creation of some specific data - a Boolean value.
The name comes from George Boole (1815-1864), the author of the
fundamental work, The Laws of Thought, which contains the definition
of Boolean algebra - a part of algebra which makes use of only two distinct
values: True and False, denoted as 1 and 0.
True
False
You cannot change anything - you have to take these symbols as they are,
including case-sensitivity.
Key takeaways
1. Literals are notations for representing some fixed values in code. Python
has various types of literals - for example, a literal can be a number (numeric
literals, e.g., 123 ), or a string (string literals, e.g., "I am a literal.").
2. The binary system is a system of numbers that employs 2 as the base.
Therefore, a binary number is made up of 0s and 1s only, e.g., 1010 is 10 in
decimal.
3. Integers (or simply ints) are one of the numerical types supported by
Python. They are numbers written without a fractional component, e.g., 256 ,
or -1 (negative integers).
5. To encode an apostrophe or a quote inside a string you can either use the
escape character, e.g., 'I\'m happy.' , or open and close the string using an
opposite set of symbols to the ones you wish to encode, e.g., "I'm happy." to
encode an apostrophe, and 'He said "Python", not "typhoon"' to encode a
(double) quote.
6. Boolean values are the two constant objects True and False used to
represent truth values (in numeric contexts 1 is True , while 0 is False .
EXTRA
There is one more, special literal that is used in Python: the None literal. This
literal is a so-called NoneType object, and it is used to represent the absence
of a value. We'll tell you more about it soon.