Python learning three "objects and types && operators"

Source: Internet
Author: User

[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 '.

Integers (integer), précis-writers as 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, "/" means a down-divide (floor division), a division of two integers, and an integer, rounding off the fractional part if there is a floating-point number, the result is floating-point numbers

For example, Fahrenheit temperature and Celsius temperature conversion calculation

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

Assuming F = 75, the corresponding Python code is:

5.0/9 * (75–32)


[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

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

Determines 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)

Determines 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

Used to determine the relationship of two values

Size, equality, or inequality

Only two of the results of the operation (Boolean)

If the result is true, the condition is set

If the result is false, the condition is not established

L logical operator (Logical Operators)

L-Operator Precedence



Python learning three "objects and types && 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.