I. Objects and Types
1. Five basic types of data:
1, string, précis-writers to STR, a series of strings enclosed in "or" "
2, integers (integer), précis-writers int, decimal, octal, hexadecimal
3. Floating point (float), e.g. 1.48, 21.0, 21.E2
4, Boolean number (Boolean), précis-writers to Bool,true,false
5, plural (complex), 1+1j
2. Object type
Xiao Ming type (' Xiao Ming ') <type ' str ' >
Male type (' male ') <type ' str ' >
Type (<type) ' int ' >
1.48 type (1.48) <type ' float ' >
43.2 type (43.2) <type ' float ' >
Jiangsu type (' Jiangsu ') <type ' str ' >
1. Why is the object type differentiated?
1), different types of object operation rules are different: the addition of integers and the addition of strings have different meanings
2), different types of objects are represented in different ways in the computer
3), why distinguish between integers and floating-point numbers: floating-point number is more powerful, the floating-point number has a loss of precision, the CPU has a special floating-point arithmetic parts
2. Forced type conversion
1 Printint'123')2 PrintSTR (123)3 PrintFloat'123')4 PrintFloat (123)5 PrintBOOL (123)6 Printbool (0)7 Printing results:81239 '123'Ten123.0 One123.0 A True -False
Second, operator
1. Arithmetic operators
Arithmetic operators |
Meaning |
Example |
+ |
Addition (addition) |
10 + 20 = 30 |
- |
Subtraction (subtraction) |
10-20 = 10 |
* |
Multiplication (multiplication) |
10 * 20 = 200 |
/ |
Division (Division) |
10/2 = 5 |
% |
Redundancy (modulus) |
10% 3 = 1 |
** |
Index (Exponent) |
2 * * 3 = 8 |
1. Examples of arithmetic elements
Convert degrees Fahrenheit (F) to Celsius (C)
Conversion formula:
Assuming F = 75, the corresponding Python code is:
5/9 * (75–32) x
5.0/9 * (75–32) √
Why?
In Python 2, "/" means a down-divide (floor division)
Divide two integers, and the result is an integer, which is the number of fractional parts
If there is a floating-point number, the result is floating-point numbers
2. Automatic type conversion
If the type of two objects participating in the operation is the same, the result type is unchanged
Example: 1/2 = 0
If the two objects participating in the operation are of different types, automatic type conversion is performed according to the following rules
Bool-> int-> float-> Complex
Such as:
1.0 + 3 = 4.0
True + 3.0 = 4.0
3, the remainder operator, such as: 10 3 = 1
Application 1, if today Saturday, 10 days after the day of the week?
(6 + 10)% 7 = 2
Apply 2 to determine if a number x is even
X% 2 is equal to 0
2. Math Module
1, modules (module)
A collection of Python scripts that implement certain functions
2. Introduction module
Import Math Introduction Module
Dir (Math) View module contents
Help (Math.sin) View module contents
1 ImportMath2 Printdir (math)3 PrintHelp (Math.sin)4 5 Printing results:6['__doc__','__name__','__package__','ACOs','Acosh','ASIN','Asinh','Atan','atan2','Atanh','Ceil','copysign','Cos','cosh','degrees','e','Erf','ERFC','Exp','EXPM1','fabs','factorial',' Floor','Fmod','Frexp','Fsum','Gamma','Hypot','Isinf','isNaN','Ldexp','Lgamma','Log','log10','log1p','MODF','Pi','POW','radians','Sin','Sinh','sqrt','Tan','Tanh','trunc']7Help on built-inchfunction sininchModule Math:8 9 sin (...)Ten sin (x) One AReturn the sine of x (measuredinchradians). - -None
3. Relational operators
1, determine whether a number x is even
x% 2 is equal to 0
x 2 = = 0
If true, X is even
if False, X is odd
2, the relationship used to determine two values br> size, equality, or inequality the result of the
operation is only two (Boolean)
If the result is true, the condition is set
If the result is false, the condition is not valid
Relational operators |
Meaning |
Example |
== |
Equals (equal) |
Ten = = False |
! =, <> |
Not equal to (not equal) |
Ten! = is true |
> |
Greater than (greater) |
> is False |
< |
Smaller than (less) |
< is True |
>= |
Greater than or equal to (greater or equal) |
>= is False |
<= |
Less than or equal to (lesser or equal) |
<= is True |
4. Logical operators
Relational operators |
Meaning |
Example |
and |
With (Quanjin only true) |
True and False = = False |
Or |
or (all fake) |
True or False = = True |
Not |
Non (true change false, false change true) |
Not True = = False |
example, judging leap year
If the year Y can be divisible by 4 but not divisible by 100, or divisible by 400, it is leap years:
1 and or (y% 400 = = 0)
5, Operator precedence :
Brackets: ()
Unary operations: +,-
Power Times: * *
Arithmetic operations: *,/,%,//
Arithmetic operations: +,-
Comparison operations: = =,! =, <> <= >=
Logical non: not
Logic with: and
Logical OR: OR
Assignment operation: =, *=,/=,+=,-=,%=,//=
Rule 1:
Top
Highest bracket
Minimum logic
Rule 2:
From left to right
Combined in turn
Third, variable
1.
Cloud Classroom-python Learning notes (2)