Python learning three "objects and types && operators"

Source: Internet
Author: User
Tags arithmetic arithmetic operators floor division ming

[Objects and types]

Student's attributes:

Xiaoming Object

Name: Male
Gender:
Age:
Height:
Weight:
Origin:

Five basic types of objects

String, précis-writers to Str

Use a series of characters enclosed in ' or '.

Whole number (integer). Précis-writers to int

Decimal: 21. Octal: 025, 16 binary: 0x15
Floating point (float) 1.48. 21.0,21 ... 21,2.1e2

Boolean number (Boolean), précis-writers is bool True,false

Plural (complex)

Object type
Xiao Ming type (' Xiao Ming '), <type ' str ' >
Male type (' male ')--<type ' str ' >
15 type, <type ' int ' >
1.48 type (1.48), <type ' float ' >

Why are object types differentiated?

Different types of object operation rules
For example: the addition of integers and the addition of strings have different meanings

Different types of objects are represented differently within computers

5è. 101, ' 5 ' è. 1001101
Why distinguish between integers and floating-point numbers?
Floating point number means more power
Floating point number with precision loss

CPU has specialized floating-point arithmetic parts

Forcing type conversions
Int (' 123 ') 123
STR (123) ' 123 '
Float (' 123 ') 123.0
Float (123) 123.0
BOOL (123) True
BOOL (0) False

[Arithmetic operation]

    1. An example of the meaning of arithmetic operators
+ 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

Attention:

In Python 2, "/" represents a downward divide (floor division) and two integers. The result is also an integer. The fractional part is assumed to have a number of floating-point numbers. The result is a floating-point number

For example Fahrenheit temperature and Celsius temperature conversion calculation

Conversion formula: C = 5/9* (F? 32)

If F = 75, the corresponding Python code is:

5.0/9 * (75–32)


[Self-active type conversion]

The same as the type of two objects that are involved in the operation. The result type does not change

Example: 1/2 = 0

If the two object types of the operation are different, then the following rules make their own active type conversions

BOOL -" int -" float -" Complex

Such as:

1.0 + 3 = 4.0

True + 3.0 = 4.0

Find remainder operator (%)

Example: 10 3 = 1

Application

If today is Saturday, what is the day of the week after 10 days?

(6 + 10)% 7 = 2

Infer whether a number x is even

X% 2 is equal to 0

L Math Module

Modules (module)

A collection of Python scripts that implement certain functions

Introducing Modules

Import Module_name

Math Module

Import Math

View Module Contents

Dir (Math)

View Help

h elp (Math.sin)

L relational operator (relational Operators)

Infer whether a number x is even

X% 2 is equal to 0

X% 2 = = 0

If True, X is an even number

If False, X is an odd number

A relationship to infer two values

Size, equality, or inequality

The result of the operation is only two (Boolean type)

If the result is true. Indicates the condition is established

If the result is false, the condition is not established

L logical operator (Logical Operators)

L-Operator Precedence



Python learning three "objects and types &amp;&amp; operators"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.