Examples of operators, data types, strings, and list operations in Python

Source: Internet
Author: User
Tags arithmetic operators logical operators

1. Operators


(1) Arithmetic operators:

(2) Relational operators:

(3) Assignment operator:

(4) Logical operators:


Priority: () > Not > and > or


2. Data type:

Raw_input (): Receive String type

Input (): Receive number Type

Integral type:

A =-100

ABS (a) to find the absolute value of a

A.__abs__ () to find the absolute value of a

Fu Type:

The round () method retains 1 decimal places by default, is calculated using the rounding method, and the last one is an even number.

A = 3.0 round (a) round (2)

Rounding is performed first, if the last digit of the decimal precision is even if the last digit of the decimal precision is rounded and then is odd, then all digits after the fiction point precision are discarded to guarantee the decimal precision, and the last one is even.

c = 2.555 D = 1.545 print (round (c,2)) print (round (d,2)) 2.56 1.54


Boolean type:

True False


3. String

dir () see what built-in methods

Type () to see what kind of

    str1 =  ' Aaaaabasdfxqs353235asdf '     print  ( Str1.find (' fxq '))     9        str1 =  ' Aaaaabasdfxqs353235asdf '     print  (str1.replace (' fxq ', ' fengxiaoqing ')      aaaaabasdFengxiaoqings353235asdf        str1 =  ' Aaaaabasdfxqs353235asdf '     print  (str1.split (' s '))     [' Aaaaaba ' ,  ' dfxq ',  ' 353235a ',  ' df ']        str1 =  ' Aaaaabasdfxqs353235asdf '     print  (' SSS '. Join (Str1.split (' s '))      aaaaabaSSSdfxqSSS353235aSSSdf        a =  '   aaaaaba  sdfx qs353 235asdf  '     print  (A.strip ())      print  (a)  &Nbsp;  print  (A.lstrip ())   #去掉左边空格     print (A.rstrip ())      #去掉右边空格         AAAAABA  SDFX QS353 235ASDF      aaaaaba  sdfx qs353 235asdf      Aaaaaba  sdfx qs353 235asdf      aaaaaba  sdfx  qs353 235asdf         format () method:     name =  ' fengxiaoqing '     age = 30    home  =  ' Chengde '     print (' Hello ' +name)     print (' hello {0} '). Format (name)     print (' hello %s ')  % name    print (' hello %d ')  % age    print (' My age is:{0}  my home: {1} '). Format (age,home)      print (' {name}:{age} '. Format (name= ' Fxq ', age=20))         hellofengxiaoqing     hello fengxiaoqing    hello fengxiaoqing     hello 30     my age is:30  my home: chengde    fxq:20



4.list operation

      ' Append ',  ' count ',  ' extend ',  ' index ',  ' Insert ',  ' Pop ',   ' remove ',  ' reverse ',  ' sort '     str1 =  ' 12poqiwrtgopwert '      str2 = list (STR1)     print (Type (str1))      Print (Type (STR2))     print (list (str1))     print (dir (str2))      print (' # # # ' *20)         a = [123, ' BBB ', ' Ace ')     print (A[1])     print (A.index (' BBB '))          a.insert (1, ' AAA ')     print (a)          a.sort ()     print (a)         a.reverse ()     print (a)         a.append (' ooo ')      print (a)          a.pop ()     print (a)          a.remove (123)     print (a)          a.pop (1)     print (a)         <type   ' str ' >    <type  ' list ' >    [' 1 ',  ' 2 ',  ' P ',  ' o ',  ' q ',  ' i ',  ' W ',  ' r ',  ' t ',  ' G ',  ' o ',  ' P ',  ' W ',  ' E ',  ' r ',  ' t ']    [' __add__ ',  ' __class__ ',  ' __contains__ ',  ' __ delattr__ ',  ' __delitem__ ',  ' __delslice__ ',  "__doc__ ',  ' __eq__ '",  ' __format__ ',  ' _ _ge__ ',  ' __getattribute__ ',  ' __getitem__ ',  ', __getslice__ ',  ' __gt__ ',  ' __hash__ ',   ' __iadd__ ',  ' __imul__ ',  ' __init__ ',  ' __iter__ ',  ' __le__ ',  ' __len__ ',  ' __lt __ ',  ' __mul__ ',  ' __ne__ ',  ' __new__ ',  ' __reduce__ ',  ' __reduce_ex__ ',  ' __repr__ ',  ', __reversed__ ',  ' __ rmul__ ',  ' __setattr__ ',  ' __setitem__ ',  "__setslice__ ',  ' __sizeof__ ',  ' __str__ ',  ' __subclasshook__ ',  ' append ',  ' count ',  ' extend ',  ' index ',  ' Insert ',  ' Pop ',  ' Remove ',  ' reverse ',  ' sort ']    ################################################ ################################    bbb    1    [ 123,  ' aaa ',  ' BBB ',  ' Ace ']    [123,  ' aaa ',  ' Ace ',  ' BBB ']     [' BBB ',  ' Ace ',  ' aaa ', 123]    [123,  ' AAA ',   ' Ace ',  ' bbb ',  ' ooo ']    [123,  ' aaa ',  ' Ace ',  ' BBB ']     [' aaa ',  ' Ace ',  ' BBB ']    [' aaa ',  ' BBB ']


List slices:

A = [one, ' 222 ', ' $ ', 444,555,666] print (a[3:]) print (A[1:5]) print (A[1:6:2]) print (A[:4]) print (a[-1]) pri NT (a[-2:]) print (a[-4:-2]) [444, 555, 666] [' 222 ', ' 33 ', 444, 555] [' 222 ', 444, 666] [11, ' 222 ', ' 33 ', 444] 666 [555, 666] [' 33 ', 444]

Examples of operators, data types, strings, and list operations in Python

Related Article

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.