Python variables and data types

Source: Internet
Author: User
Tags stdin

Variable

What is a variable

The so-called variable, refers to the program in the process of the need to use the intermediate results, the variable is defined, will occupy a piece of memory open space address, through the memory space to ensure the uniqueness of the same time.

Print (ID. __doc__   is guaranteed to be unique among simultaneously existing objects. ( CPython uses the object's memory address.)

Naming rules for variables

1) A variable can be any combination of uppercase and lowercase letters, numbers, and underscores, and cannot begin with a number.

2) keywords in python cannot be declared as variable use.

3) Variable name description is stronger

>>> 1_level = 1File"<stdin>", Line 11_level= 1 ^syntaxerror:invalid Syntax>>> Level_1 = 1>>> _level1 = 1>>>Print= 10>>>Print(Level_1) Traceback (most recent): File"<stdin>", Line 1,inch<module>TypeError:'int'Object is  notCallable

Character encoding

ASCII (American Standard Information Interchange Code) character encoding for displaying modern English and Western European languages, up to 8 bits, or 1 bytes, up to 2**8-1 = 255 symbols.

About Chinese

The GB2312, which was designed to store Chinese in 1980, contains a total of 7,445 characters, including 6,763 characters and 682 other symbols.

1995 designed a 21,886 symbols can be included in the gbk1.0,2000 year GB18030 replace the GBK1.0 official national standards, can be included in 27848 of Chinese characters.

In order to solve the frequently occurring character incompatibility between different countries, the introduction of Unicode (Uniform Code, universal Code, single code), for each language of each character has a unified and unique binary encoding, each character occupies a uniform 2 bytes.

In order to solve Unicode in the storage of English, more than 1 bytes, continue to optimize the world's most popular UTF8 variable long character encoding, English 1 characters also accounted for 1 bytes, European language 1 characters accounted for 2 bytes, Chinese 1 characters accounted for 3 bytes.

The PYTHON2 interpreter uses the default (ASCII) character encoding when loading a. py bytecode file and does not support Chinese.

Cat hello.py # !/usr/bin/env python Print ' Hello, world. ' python hello.py   " hello.py ", line 2syntaxerror:non'\xe4' on for Details

Then, in Python2, you need to declare a Chinese-enabled character encoding when using Chinese.

Cat hello.py # !/usr/bin/env python # -*-coding:utf-8-*- Print ' Hello, world. ' python hello.py world, Hello

Python3 uses UTF-8 character encoding by default, and Chinese is supported by default.

Python 3.5.2 (default, DEC 2 2016, 17:47:43) [GCC4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on Darwintype" Help","Copyright","credits" or "License"  forMore information.>>>Print('Hello, world.') The world, Hello

Data type

Number type: Non-quoted number

int (integral type)

On a 32-bit system, the number of integers is 32 and the value range is from -2**31~2**31-1, i.e.: -2147483648~2147483647

On a system of 64, the number of digits of an integer is 64, and the range of values is from -2**63~2**63-1, i.e.: -9223372036854775808~9223372036854775807

L Ong (long plastic)

used in Python2, if an integer overflow occurs, Python automatically converts the integer value to the long shape value. is no longer used in Python3.

Float (float type)

The data representation of a number that is a specific subset of the rational number, which is approximately interpreted as a decimal and occupies 8 bytes.

Boolean value

True or False true false 1 or 0, the true representation condition is established.

>>> + >True>>> + + > +False

String type: The character that is enclosed in quotation marks

String to common functions:

Strip: Removing spaces on both sides of a string

Index: takes 1 characters from a string

Slice: Take more than one character in a string, the method of slicing is called Gu Tou regardless of tail.

Splicing: Using the + number, without splicing 1 times will open up a piece of memory space, string type and number type can not be spliced.

Formatted output: Using%, you can pass in multiple data types.

Python variables and data types

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.