Computer Programming 简明教程

Computer Programming - Data Types

让我们讨论一下几乎所有编程语言中都有的一个非常简单但非常重要的概念,称为 data types 。顾名思义,数据类型表示您可以使用计算机程序处理的数据类型。它可以是数字、字母数字、十进制等。

Let’s discuss about a very simple but very important concept available in almost all the programming languages which is called data types. As its name indicates, a data type represents a type of the data which you can process using your computer program. It can be numeric, alphanumeric, decimal, etc.

让我们暂时搁置计算机编程,以 10 和 20 这两个整数的加法为例,该计算可以简单地执行如下:

Let’s keep Computer Programming aside for a while and take an easy example of adding two whole numbers 10 & 20, which can be done simply as follows −

10 + 20

让我们再举一个问题,我们要加两个小数 10.50 和 20.50,其书写如下:

Let’s take another problem where we want to add two decimal numbers 10.50 & 20.50, which will be written as follows −

10.50 + 20.50

这两个示例很简单。现在,让我们再来看另一个示例,其中我们要在笔记本中记录学生信息。这里我们希望记录以下信息 -

The two examples are straightforward. Now let’s take another example where we want to record student information in a notebook. Here we would like to record the following information −

Name:
Class:
Section:
Age:
Sex:

现在,让我们根据给定的要求放入一个学生记录 -

Now, let’s put one student record as per the given requirement −

Name: Zara Ali
Class: 6th
Section: J
Age: 13
Sex: F

第一个示例处理整数,第二个示例增加了两个小数,而第三个示例处理不同数据的混合。我们以如下方式表示 -

The first example dealt with whole numbers, the second example added two decimal numbers, whereas the third example is dealing with a mix of different data. Let’s put it as follows −

  1. Student name "Zara Ali" is a sequence of characters which is also called a string.

  2. Student class "6th" has been represented by a mix of whole number and a string of two characters. Such a mix is called alphanumeric.

  3. Student section has been represented by a single character which is 'J'.

  4. Student age has been represented by a whole number which is 13.

  5. Student sex has been represented by a single character which is 'F'.

通过这种方式,我们意识到在我们的日常生活中,我们会处理不同类型的数据,例如字符串、字符、整数和十进制数(浮点数)。

This way, we realized that in our day-to-day life, we deal with different types of data such as strings, characters, whole numbers (integers), and decimal numbers (floating point numbers).

同样,当我们编写计算机程序来处理不同类型的数据时,我们需要明确指定其类型;否则,计算机无法理解如何对该给定数据执行不同的操作。不同的编程语言使用不同的关键字来指定不同的数据类型。例如,C 和 Java 编程语言使用 int 来指定整数数据,而 char 指定字符数据类型。

Similarly, when we write a computer program to process different types of data, we need to specify its type clearly; otherwise the computer does not understand how different operations can be performed on that given data. Different programming languages use different keywords to specify different data types. For example, C and Java programming languages use int to specify integer data, whereas char specifies a character data type.

后续章节将向您展示如何在不同情况下使用不同的数据类型。现在,让我们检查 C、Java 和 Python 中可用重要的数据类型以及我们将用于指定这些数据类型的关键字。

Subsequent chapters will show you how to use different data types in different situations. For now, let’s check the important data types available in C, Java, and Python and the keywords we will use to specify those data types.

C and Java Data Types

C 和 Java 支持几乎相同的数据类型集,尽管 Java 支持额外的数据类型。现在,我们正在采用这两种编程语言支持的几种常见数据类型 -

C and Java support almost the same set of data types, though Java supports additional data types. For now, we are taking a few common data types supported by both the programming languages −

Type

Keyword

Value range which can be represented by this data type

Character

char

-128 to 127 or 0 to 255

Number

int

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

Small Number

short

-32,768 to 32,767

Long Number

long

-2,147,483,648 to 2,147,483,647

Decimal Number

float

1.2E-38 to 3.4E+38 till 6 decimal places

这些数据类型称为原始数据类型,您可以使用这些数据类型构建更复杂的数据类型,称为用户定义数据类型,例如字符串将是一系列字符。

These data types are called primitive data types and you can use these data types to build more complex data types, which are called user-defined data type, for example a string will be a sequence of characters.

Python Data Types

Python 有五种标准数据类型,但这种编程语言不会使用任何关键字来指定特定数据类型,而 Python 足够智能,可以自动识别给定的数据类型。

Python has five standard data types but this programming language does not make use of any keyword to specify a particular data type, rather Python is intelligent enough to understand a given data type automatically.

  1. Numbers

  2. String

  3. List

  4. Tuple

  5. Dictionary

在这里,Number 指定所有类型的数字,包括十进制数,string 表示长度为 1 个或多个字符的字符序列。现在,让我们继续这两种数据类型,并跳过 List、Tuple 和 Dictionary,它们是 Python 中的高级数据类型。

Here, Number specifies all types of numbers including decimal numbers and string represents a sequence of characters with a length of 1 or more characters. For now, let’s proceed with these two data types and skip List, Tuple, and Dictionary, which are advanced data types in Python.