In Python, there are several types of data that can be processed directly:
Integer
Python can handle integers of any size, including, of course, negative integers, which are represented in the program in the same way as mathematically, for example:,,, 1
100
, and so on -8080
0
.
Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, and hexadecimal is 0x
represented by prefixes and 0-9,a-f, for example: 0xff00
,, and 0xa5b4c3d2
so on.
Floating point number
Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are exactly equal. Floating-point numbers can be written in mathematical notation, such as,, 1.23
3.14
, and -9.01
so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9
, or 12.3e8
, 0.000012 can be written 1.2e-5
, and so on.
Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.
String
A string is ‘
any text enclosed in single or double quotation marks "
, such as ‘abc‘
, and "xyz"
so on. Note that ‘‘
or ""
itself is just a representation, not part of a string, so the string is ‘abc‘
only a
, b
c
this 3 characters. If it ‘
is also a character, it can be ""
enclosed, such as the character that contains it,,, the space, the "I‘m OK"
I
‘
m
O
K
6 characters.
Boolean value
A Boolean value is exactly the same as a Boolean algebra, with a Boolean value of only True
False
two values, either, True
or, False
in Python, you can directly use True
and False
represent a Boolean value (note case). It can also be computed by Boolean operations.
>>> TrueTrue>>> FalseFalse>>> 3 > 2True>>> 3 > 5False
Boolean values can be used and
, or
and not
operations.
and
Operations are and operations, and only all are True
, the result of the and
operation is True
:
>>> True and TrueTrue>>> True and FalseFalse>>> False and FalseFalse>>> 5 > 3 and 3 > 1True
or
An operation is or an operation, as long as one of them is True
, the result of the or
operation is True
:
>>> True or TrueTrue>>> True or FalseTrue>>> False or FalseFalse>>> 5 > 3 or 1 > 3True
not
An operation is a non-operation, which is a single-mesh operator that turns True
into False
False
True
:
>>> not TrueFalse>>> not FalseTrue>>> not 1 > 2True
Boolean values are often used in conditional judgments, such as:
if age >= 18: print(‘adult‘)else: print(‘teenager‘)
Null value
A null value is a special value in Python, None
denoted by. None
cannot be understood as 0
, because 0
it is meaningful, and None
is a special null value.
In addition, Python provides a variety of data types such as lists, dictionaries, and also allows you to create custom data types, which we'll continue to talk about later.
Variable
The variable is the amount that the value is allowed to change in the course of the program's operation.
The function of a variable: to refer to what is stored in an address in memory
Rules for variable definitions:
- Variable names can only be any combination of letters, numbers, or underscores
- The first character of a variable name cannot be a number
- The following keywords cannot be declared as variable names
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
Variables can be not only numbers, but also arbitrary data types
Assigning values to variables
# !/usr/bin/env python # -*-coding:utf-8-*- "sunny" # name is a string of age =24 # age是整数
This type of variable itself is called Dynamic language, and it corresponds to a static language.
Constant
So-called constants are immutable variables, such as the usual mathematical constants π is a constant. In Python, constants are typically represented in all uppercase variable names:
PI = 3.14159265359
Python data type variable list tuple dictionary collection