程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learn the type() function in Python

編輯:Python

type()函數是在PythonA great tool when dealing with programs.在PythonEverything in is an object,But there are many different types of objects available.有字符串對象、函數對象、整數對象、Float objects and Boolean objects.在編寫PythonWhen scripting or debugging,It is useful to know the type of an object.函數 type() Takes an object as input,and returns the data type of a given object.在本教程中,We'll go through a variety of examples to see how Python 中使用 type() 函數.


hello type

打印 "Hello World "Pretty much the first thing you do when you learn any programming language.讓我們用 type() function to check.

my_var = 'Hello World'
print(type(my_var))
復制代碼
<class 'str'>
復制代碼

We will use the same one in all the examples in this article my_varVariables are used in all examples in this tutorial,to indicate that any given variable can store any type of data.當然,用 type() 函數檢查'Hello World'Show it's type is'str'.讓我們看一個不同的例子.

my_var = '123456789'
print(type(my_var))
復制代碼
<class 'str'>
復制代碼

This variable holds again'str'的類型.Although we saw the numbers,但它是一個數字字符串,instead of the actual numeric type.


type()和數字

現在讓我們通過 type() 函數來看看 Python Some numeric types in .在這個例子中,We will simply remove the parentheses around these numbers '周圍的字符.See how the results have changed now.

my_var = 123456789
print(type(my_var))
復制代碼
<class 'int'>
復制代碼

現在我們可以看到,my_var持有的是一個整數類型,用來表示整數.Let's make a small change to the code.

my_var = 1.23456789
print(type(my_var))
復制代碼
<class 'float'>
復制代碼

我們可以看到,The number now has a decimal point in it.當使用type()When the function checks for a number with a decimal point,我們發現它是float類型的.A number can also be one復數類型.Plural means adding two numbers together(A real number and an imaginary number).這裡有一個例子.

my_var = 1 + 1j
print(type(my_var))
復制代碼
<class 'complex'>
復制代碼

序列類型

讓我們在my_varVariables store some different sequence types,然後用type()函數檢查結果.

my_var = ['my', 'favorite', 'type']
print(type(my_var))
復制代碼
<class 'list'>
復制代碼

正如你所看到的,my_varThe variable now holds one[列表]類型的對象,This is one of my favorite types of jobs,Because it is very useful and flexible.Let's take a look at this interesting one tuple Examples of data types.

my_var = (1, 2, 3, 'fee', 'fi', ['fo'], {'fum'})
print(type(my_var))
復制代碼
<class 'tuple'>
復制代碼

Now we can look at the sequence type of the dictionary.

my_var = {'key': 'value'}
print(type(my_var))
復制代碼
<class 'dict'>
復制代碼

in the next snippet,We saw the boolean type.

my_var = True
print(type(my_var))
復制代碼
<class 'bool'>
復制代碼

自定義數據類型

The section above shows that Python Some of the built-in types in .When you are using different libraries and code,You may need to see what type you are using.Consider the code here.

import datetime
my_var = datetime
print(type(my_var))
復制代碼
<class 'module'>
復制代碼
import pandas as pd
my_var = pd.Series(['some', 'cool', 'stuff'])
print(type(my_var))
復制代碼
<class 'pandas.core.series.Series'>
復制代碼

Python type() 函數摘要

Sometimes you may want to know the types of variables in a program.Maybe the type isn't obvious from the code,Or you got this information from a source that the code doesn't have access to.Whenever you want to check the type of a variable,你可以使用type()函數.As we have seen from the many examples in this article,在 Python There are many different types available.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved