Python determines whether a variable is an int, a string, a list, a tuple, a dictionary, and so on

Source: Internet
Author: User

In the actual writing program, the variable type is often judged, in addition to the type (variable) This method, you can also use the isinstance method to determine:

#!/usr/bin/env pythona = 1b = [1,2,3,4]c  =  (1,2,3,4) d = {' a ': 1, ' B ': 2, ' C ':3}e =  "abc" if isinstance (a,int):     print  "A is int" else:    print  "A is not  int "If isinstance (b,list):    print " B is list "else:     print  "B is not list" If isinstance (c,tuple):     print   "C is tuple" else:    print  "C is not tuple" if  Isinstance (d,dict):    print  "D is dict" Else:    print   "D is not dict" If isinstance (e,str):    print  "D is  str "else:    print " D is not str "



This article is from the "bitter coffee ' s Yun ' an Road" blog, please be sure to keep this source http://alsww.blog.51cto.com/2001924/1787848

Python determines whether a variable is an int, a string, a list, a tuple, a dictionary, and so on

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.