Python data Type--number--string

Source: Internet
Author: User

Let's start with a concept.

In Python, everything is objects, objects are created based on classes

Therefore, the following values are objects: "Wupeiqi", 38, [' Beijing ', ' Shanghai ', ' Shenzhen '), and are objects generated from different classes.

1. Variable types and objects

1. There are a lot of states that need to be processed in the program, it is necessary to have different types of variable values to represent
All data in Python is built around the concept of objects that contain some basic data types : Numbers, strings, lists, tuples, dictionaries, etc.
All data stored in the program is an object, containing 1. Identity (ID), 2. Type, 3 value (viewed by variable name)
Each object has its own method. Easy for developers to use,
Creating an object of a particular type is also known as creating an instance of that type   , and the concept of a factory function is derived from this

2. Variable and non-changeable
Once the instance is created, the identity and type are immutable,
If the value is not modifiable, the object is immutable
If the value can be modified, it is a mutable object

To divide by a mutable type:

Mutable type: list, Dictionary of value

Immutable types: Numbers, tuples, strings

According to the number of values to divide:

Take a value: number, string

Take multiple values: list, tuple, dictionary

According to the way the value is taken:

Direct value: number, obtained is the number of the whole, can not take a single

Index values: string, list, tuple

Map values: Dictionary, value values by keys

2. Container type

An object contains a reference to another object, which is called a container or a collection

3. Assigning values to variables

    • The difference from the C language is that the variable assignment operation has no return value
    • Chained assignment: y=x=a=1
    • Multi-value assignment: x,y=1,2 x,y=y,x
    • Incremental assignment: X+=1

4. Data type

Data types in Python
Python uses the object model to store data, each data type has a built-in class, and each new data is actually initialized to generate an object, that is, all the data is an object
Object Three Properties
    • Identity: Memory address, can be obtained with ID ()
    • Type: Determines what type of value the object can hold, what action to perform, what rules to follow, and the type () to get
    • Value: Real data saved by the object

Standard data types: numbers, strings, lists, tuples, dictionaries

Other data types: collections, Functions/methods, modules, NULL, type types, classes, files

4.1 Number types

Definition: a=1

Characteristics:

1. Only one value can be stored

2. Once defined, cannot be changed

3. Direct Access

Categories: Integer, Long, Boolean, floating point, plural

Python's integral type is equivalent to the long type in C

Integers in Python can be in decimal, Binary bin, octal Oct, hex notation hex

a=19Print(Bin (a))Print (Oct (a))print(Hex (a))
A=' 123 '           to convert a from a string to a number, a must have a number
Print (Type(a),a) prints out a type, value
b=Int (a) to a numeric type, re-assigns a value to B,
Print (type (b), B)
Results
<class ' str ' > 123
<class ' int ' > 123

Common sense:

1 bytes = 8 bits

1 Kanji = 3 bytes

gb2312=2 Kanji = 6 bytes = 48 bits

utf-8=3 Kanji = 9 bytes = 72 bits (indicates: Minimum of 3 kanji)

4.1.1

Python2, Python automatically converts integer data to a long integer if an overflow occurs.
So the fact that there is no letter L behind Long data is not going to cause serious consequences.

No plastic length limit in Python3, unified for shaping

In Python2, the shaping length is 32 bits,

4.1.2 Boolean, Ture,false, both 1 and 0,

4.1.3 floating-point float, floating-point number is a decimal in mathematics, similar to the C language of double, in Python, with scientific notation: 0.0013, Python is represented as 1.3e-3, =0.0013

It's 1.3*10*-3,,, if it's 1.3e3=1300,1.3*10*3.

4.1.4 plural

The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers.
Note that the letter J of the imaginary part can be case-sensitive,

5 String type

5.1 Definition: It is a collection of ordered characters used to store and represent basic textual information, "' or" "or" ' "" that contains what is called a string
Characteristics:
1. Only one value can be stored
2. Non-volatile
3. Define the character set in left-to-right order, the subscript is accessed sequentially from 0, ordered
Add:
1. Both single and double quotation marks cannot suppress the meaning of special characters, if you want all the characters in quotation marks to nonspacing special meaning, precede the quotation marks with R, such as Name=r ' L\THF '

5.2 Manipulation of strings

Remove whitespace
Segmentation
Length
Index
Slice
1.str=' Hello World '
Print (Str.count (' L ')--the result is 3, finding the length of a single character in a string,
---------------------------------------------------------------------
2.str=' HELLO World '
Print (Str.lower ())--The result is Hello World, which converts all characters of the string to uppercase,
----------------------------------------------------------------------------
3.str=' HELLO World '
Print (Str.split ())---result for [' HELLO ', ' World ' by default with a space as a delimiter, generate the list,
You can also specify a delimiter,
-------------------------------------------------------------------------------
4.str=' HELLO world '
Print (Str.replace (' o ',' a '))----result HELLO Warld, replacing a single character,
-----------------------------------------------------------------------------------------
5.str=' HELLO world '
Print (str.index ("H"))----Find the index value of a single character in the string, if the index value of the single character is not found, an error
-----------------------------------------------------------------------------------------
6.x=' HELLO world '
Print (x.strip ())-----Take out spaces on both sides of the string
------------------------------------------------------------------
7.x=' Aaff '
Print (X.istitle ())-----Determine if the first letter of the string is uppercase, the uppercase return value is true,
--------------------------------------------------------------------
8.x=' 123 '
print (X.isdigit ()) ----Determine if a string contains a number, a number, a return value of true, no, false
x=' 1 '
X.isdigit (): ---If X is a number,
new_x=Int (x)---Let x enter an integer and assign the value to new_x
Print (new_x,type (x))---conditions, printing new_x
ELSE:---otherwise, perform the following conditions
Print (' no ')---printing no
---------------------------------------------------------------
9.x=' ABC '
PrintX.find (' C ')-----Find the index value of the single character based on a single character in the string,
Returns 1 if the individual is not in the string
----------------------------------------------------------------
10.a=' BBBB '
PrintLen (a))--------the length of the output string
----------------------------------------------------------------
11.a=' BBBB '
Print (A.startswith (' B '))---determine what character to start with and return a Boolean value
----------------------------------------------------------------
12.a=' BBBB '
Print (A.endswith (' m ')----------decide what character to end with. Returns a Boolean value
---------------------------------------------------------------
13.a=' BBBB '
Print (A.center,' # ')---------to center the string in 30 characters, not enough # instead,
----------------------------------------------------------------
14.a=' bbbb '
print (A.capitalize ()) ------------to capitalize the first letter of a string
--------------------------------------------------------------
15.
Method 1
a=' name:{},age:{},sex:{} '
Print (a.format ("Egon", "male")-------Format the string, the corresponding null within the string must be {},
Method 2
A=' Name:{a},age:{b},sex:{c} '
Print (a.format (a="Egon",c=,b=' male '))----Specify the value to correspond to
Method 3
A=' name:{1},age:{0},sex:{2} '
Print (A.format (' A ',' C ', ')---results < Span style= "color: #008080; Font-weight:bold ">name:c,age:a,sex:12
Index value, 0,1,2, in the Order of format (), the index value to the string assignment,
----------------------------- -------------------------------------------
16.a= ' aaddcddd '
print (A.swapcase ()) -the result is-aaddcddd---the case of all characters that invert the string
---------- ------------------------------------------------------------------
17. a= aaddcddd '
print ( a.title ())---------------capitalize the beginning of the letter









Python data Type--number--string

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.