Concept of type:
1, the type is the programming language to the data one kind divides.
2. Type of Python language
numeric types, string types, meta-ancestor types, list types, file types, dictionary types.
Number Type
· Program elements: 010/10, there are many possibilities
• Represents a decimal value of 10
• A string similar to a person's name
Python contains three basic types of numbers:
• Integer type
• Floating-point type
• plural type
1, Integer type
Consistent with the concept of integers in mathematics, there is no limit to the range of values;
Pow (x, y) function: Calculates x^y (with a question, how to calculate the number of digits)
Integer types can also be binary, octal, hexadecimal.
2. Floating-point number type
Numbers with decimal points and decimals
There are limits to the numeric range of floating-point numbers in the Python language, and there are limits to decimal precision, which are related to different computer systems.
Use floating-point type:
>>> Import Sys
>>> Sys.float_info
The range of floating-point numbers for the current computer will be output
3. Plural type
Consistent with the plural concept in mathematics, Z=a+bj,a is the real part, B is the imaginary part, A and b are floating-point types, and the imaginary part is identified by J or J.
Relationship of numeric types
1. There is a gradual "extended" relationship between the three types:
Complex number, floating point number, Integer (integer is a special case of floating-point number, floating-point number is a special case of complex number)
Mixed operations can be performed between different numeric types, resulting in the widest type after the operation.
2. Three types can be converted to each other
Functions: Int (), float (), complex () complex numbers do not support conversion to floating-point numbers and integers.
The judgment of the number type
Function: Type (x) returns the types of x, suitable for all types of judgments
Example:
>>>type (4.5)
<class ' float ' >
Many data operations are available in the math library
Operation: +_*/four basic
X//y is not greater than the largest integer of the quotient of X and Y
X%y the remainder of the quotient of X and Y.
X**y X's Y power
B. String type
1. The string is one or more characters enclosed in double quotation marks "" or single quotation marks.
>>>str1= "Hello"
>>>str2= "John"
Strings can be stored in variables, or they can exist separately.
You can test the type in a string with the type () function.
>>>type (STR1)
<class ' str ' >
Absolute value of ABS (x) x
Divmod (x, y) (x//y,x%y)
Y power of the POW (x, y) x
String data type:
Python language escape character: \
You can use the escape character to output a string with quotation marks.
For example:
>>>print ("\" good everyone \ ")
"Good for everyone."
Use \ \ To output a string with an escape character.
The string is a sequence of characters: the leftmost position of the string is marked "0" and incremented in turn. The string number is called "Index". The string index in Python starts at 0, and the last character of a string with a length of L is L-1. Python also allows negative numbers to be reversed from the right end of the string to the left, with the rightmost index value-1.
In addition, a substring of this range can be returned within a position range by two index values.
<string>[<start>:<end>]
Both start and end are integer values that begin at index start until the end of the index, but do not include the end position.
>>>greet[0:3]
' Hel '
You can use a single index to assist in accessing specific locations in a string.
Strings can be connected by + or *,
The addition operation (+) joins two strings into a new string
>>> "Pine" + "apple"
Pineapple
Multiplication operation (*) constructs a string that is repeatedly concatenated by its own string
>>>3* "Pine"
' Pinepinepine '
The Len () function returns the length of a string
Most data types can be converted to strings through the STR () function.
String Usage instance:
Enter a month number to return the corresponding month name abbreviation.
The IPO model is:
Input: Enter a number that represents the month (1-12)
Processing: Implement this function with string basic operations
Output: Enter an abbreviation for the month name for the number
Store all month name abbreviations in a string
months =
"Janfebmaraprmayjunjulaugsepoctnovdec"
Intercept the appropriate substring in a string to find a special month
Find out where to cut the sub-string
Abbreviations for each month are made up of 3 letters, and if POS represents the first letter of a month, then months[pos:pos+3] represents the abbreviation for this month, i.e. Monthabbrev = months[pos:pos+3]
(find corresponding relationship)
The code is as follows:
#month. py
months = "Janfebmaraprmayjunjulaugsepoctnovdec"
n = input ("Please enter the number of months (1-12):")
pos= (int (n)-1)
Monthdayabbrev = months[pos:pos+3]
Print (the shorthand for the month is "+monthsabbrev+". ")
The method of manipulating a string takes the form:
<string>.func () Common functions:
Iterate through each character in a string
For <var> in <string>:
Escape characters can express some information in a string that cannot be printed directly.
For example: line break with \ n
Python Basics Advanced (ii) data types for Python languages