02-python Basic data types

Source: Internet
Author: User

Python variables do not need to be declared, but they must be copied before they are used, because all of the content in Python is an object

A variable is of no type and has a type that points to the type of the memory object

' 123 '  123  is legal.

In addition, Python can be continuously assigned to multiple variables.

A  11'qwe'c'

1, Number type

1 1 2 .  2. A variable can point to different types of objects by assigning values.  3, the value of division (/) always returns a floating-point number, to get an integer using // operator.  4. In mixed calculations, Python converts an integer to a floating-point number.

The math function of Python

function return value (description) ABS (x) returns the absolute value of the number, such as ABS (-Ten) returnsTenceil (x) returns the integer of the number, such as Math.ceil (4.1) returns5cmp (x, y) if x< y return-1, if x = = y returns0, if x > y returns1exp (x) returns the X Power (ex) of E, such as Math.exp (1) returns 2.718281828459045fabs (x) returns the absolute value of a number, such as Math.fabs (-Ten) returns 10.0Floor (x) returns the number of the lower-rounding integer, such as Math.floor (4.9) returns4log (x), such as Math.log (MATH.E), returns 1.0, Math.log ( -,Ten) returns 2.0log10 (x) returns the logarithm of x with a base of 10, such as MATH.LOG10 ( -) returns2.0Max (x1, x2,...) Returns the maximum value of a given parameter, which can be a sequence.    Min (x1, x2,...) Returns the minimum value for a given parameter, which can be a sequence. MODF (x) returns the integer part and fractional part of X, the two-part numeric symbol is the same as x, and the integer part is represented by a floating-point type. Pow (x, y) x**the value after the Y operation. Round (x [, N]) returns the rounding value of the floating-point number x, given the n value, which represents the digits rounded to the decimal point. SQRT (x) returns the square root of the number x, the number can be negative, and the return type is real, such as MATH.SQRT (4) returns2+0j

2, str type

msg ="What's is Your Name?"#find, find the substring location, and specify where to start looking for index= Msg.find ('Name',1, the) print (index) # rfind, reverse lookup index= Msg.rfind ('Name',1, -) The conversion between print (index) # and list Msg_list= Msg.split (' ') join_msg=' '. Join (msg_list) print (msg_list) print (join_msg) # uses [:] to intercept print (msg[3:8])

In addition: the operation of the string is:

+ String connection >>>a + b 'hellopython'
* Repeating output string >>>a * 2 'hellohello'
[] Getting characters in a string by index >>>a[1] 'e'
[ : ] Intercept part of a string >>>a[1:4] 'ell'
Inch Member operator-Returns TRUE if the string contains the given character >>>"H" in a True
Not in Member operator-Returns TRUE if the string does not contain the given character >>>"M" not in a True
R/r Raw string-Raw string: all strings are used directly as literal meanings, without escaping special or non-printable characters. The original string has almost exactly the same syntax as a normal string, except that the first quotation mark of the string is preceded by the letter "R" (which can be case). >>>print r \ n ' \n >>> print r'\ n ' \n

formatted output;

Class C:%s,%d,%c,%f
Format: ' {0}, {1} '. Format (STR0, STR1)

3, list type [value, value2, Value3]

# List of common operations # Order Msg_list= [1,2,3,4,1,2,3,4,1,2,3,4]msg_list.append (5) Msg_list.reverse () Msg_list.remove (5) Msg_list.index (3) Msg_list.insert (2,'a') # Insert List2= ['a','b']msg_list.extend (LIST2) # list Add # slice new_list= msg_list[2:4] # bag left without packet right msg_list[-5:] # Starting from the bottom fifth position cut print (msg_list) print (new_list) print (msg_list) # for 2 to appear first_position=0 forIinchRange (Msg_list.count (2)): New_position= Msg_list.index (2) +1msg_list=msg_list[first_position:] first_position+=new_position print first_position# or use index# forIinchRange (Msg_list.count (2)):#     ifFirst_position = =0: # first_position= Msg_list.index (2)#     Else: # first_position= Msg_list.index (2, First_position +1) # Print First_position

4, meta-group

The difference between a tuple and a list is that the data in the tuple is immutable and needs to be written in () and separated from each other.

Other methods and lists

5, sets

Sets is a sequence of unordered repeats that functions as a test for membership and removes duplicate elements

Creating an empty collection requires set (), because {} is used to create an empty dict

Used primarily for set operations

#!/usr/bin/Python3 Student= {'Tom','Jim','Mary','Tom','Jack','Rose'} print (student) # output set, duplicate elements are automatically removed # member testif('Rose' inchstudent): Print ('Rose in the collection')Else: Print ('Rose is not in the collection.') # Set can perform set operation a=Set('Abracadabra') b=Set('Alacazam') print (a) print (a-b) # A and B difference set print (a|b) # A and B of the set print (a&b) The intersection of # A and B print (a^ b) # elements that do not exist simultaneously in A and B

6, Dict

A dictionary is a collection of types of Java maps, stored in the form of key-value values

Dict = {'Alice':'2341','Beth':'9102','Cecil':'3258'}# access to the value in the dictionary, no error print"dict[' Name ']:", dict['Name'];p rint"dict[' age ']:", dict[' Age'];# Modifying the dictionary dict[' Age'] =8; # Update existing entrydict['School'] ="DPS School"; # ADDNewentry# Delete dictionary del dict['Name']; # The DELETE key is'Name'entry dict.clear ();        # Empty dictionary all entries del dict; # Delete Dictionary

The value of the dictionary can be any Python object, the key cannot be changed and cannot be duplicated, so the key can only be a number, string, or tuple type

02-python Basic data types

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.