Python Tutorials (2.2)--data types and variables

Source: Internet
Author: User

Like C + +, Java, Python also has two concepts: data type and variable.

Data type

Several basic data types in Python are integers (integer/int), floating-point numbers (float/float), Boolean values (Boolean/bool), and strings (STRING/STR).

Integer

An integer is the most basic type. The integer is represented in python with Int. Like 0, 1, 103, -4 these numbers are integers.

Integers can also be represented by binary (beginning with 0b or 0 b) and hexadecimal (beginning with 0x or 0X). For example 0B10 is 2,0x10 for 16.

There is no size limit for python integers.

Floating point number

Floating-point numbers (float) are different from integers, and they have decimal points. Floating-point numbers are represented in Python with float. Like 0.5, 1.23,-7.4, 2.0 These numbers are floating-point numbers. Note that 2.0 is mathematically an integer, but because it has decimal points, it is a floating-point number.

Floating-point numbers can also be represented by the E notation. For example, 7.2e9 means 7.2 times 10 for 9 squares (7200000000), -4E-7 for 4 by 10 to 7 (-0.0000004).

Python's floating point size range is approximately -1.79e308~1.79e308.

Boolean value

Boolean value (Boolean) The name comes from the mathematician George Boole. BOOL is used in Python to represent a Boolean value. A Boolean value can only be true or false. I'll talk about this later.

String

A string is a literal enclosed in quotation marks (single quotation marks, double quotes). In Python, the string is represented by Str. such as ' abc ', ' str ', ' Hello, world! ' And "A" are both strings. Note that "a" is a string, although it is only one character. In this tutorial, you use single quotes to enclose strings in uniform.

Variable

Variables are memory used to store numbers. Although Python has variables in common with C + + and Java languages, the details are different.

In Python, variables do not need to be declared, and they need to be assigned directly (assign) when used. For example, to use a variable to save the person's age:

>>> age = 17

A variable age is created directly here and has an int value of 17. The assignment is to set the value of a variable.

The initial type of the variable depends on the type of value that was assigned to it when it was created. Here 17 is an integer, and the type of age is int.

Now enter age and get 17:

>>> Age
17

The type of the python variable is variable. The following assignment causes the type of age to become float:

>>> age = 17.1

Enter age, get 17.1:

>>> Age 17.1

It can even become a Boolean (bool) or a string (str), which is, of course, out of the meaning:

>>> age = True>>> agetrue"Age">>> age  'age'

Constant

Unlike other programming languages, Python cannot define constants (constant). In Python, the variables that you define are modifiable.

However, there are times when we want to use a symbol to represent a constant, even if it can be modified. Constants are generally uppercase. For example:

PI = 3.141592653589793

Summary

1. The basic types of Python include int, float, bool, and Str.

2. The types of variables in Python are variable.

3. Constants cannot be defined in Python.

Python Tutorials (2.2)--data types and variables

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.